How to build a lightweight AI-powered code review system using LangChain, DeepSeek, and Flask.
Code reviews are a cornerstone of software quality, but what if you could supercharge your review process with AI?
In this post, I'll walk you through building a lightweight AI-powered code review system using:
- LangChain for prompting
- DeepSeek (or any large-code-model) for deep analysis
- Flask for exposing it as an API
💻 The Code Setup:
We need to import our deepseek model r1, before we do that, the model weights should be downloaded and present on your computer so that it would be easier to import the model locally. To do that we need to install ollama first on our local machine:
1- You can find the link here:
https://ollama.com/download/windows
Step 1: Install Ollama
- Go to the official Ollama website and download the installer for your platform (Windows in your case).
- Run the installer. It sets up everything, including GPU support (if compatible drivers are installed).
Step 2: Open Terminal / Command Prompt
Once installed, open Command Prompt or Windows Terminal.
Step 3: Pull & Run the DeepSeek R1 Model
Use the following command to download and start the model:
ollama run deepseek-r1:1.5b
- Pull the DeepSeek R1 (1.5B) model
- Set up a local instance
- Start an interactive chat
You'll now see a blinking prompt where you can begin chatting with the model.
Now we need to import the deepseek-r1 model in our python jupyter notebook:
llm = OllamaLLM(model="deepseek-r1:1.5b")
But for this you must have the below dependencies installed:
from langchain.chains import SequentialChain, LLMChain
from langchain.prompts import PromptTemplate
from langchain_ollama.llms import OllamaLLM
Now we are going to build the prompts for light review and another for deep inspection.
from langchain import PromptTemplate
code_review_prompt = PromptTemplate(
input_variables=["code_snippet"],
template="""
You are an AI system designed for code review.
- Analyze the code for bugs, performance issues, and logical errors.
- Flag potential risks and suggest if DeepSeek review is necessary.
Code to Review:
{code_snippet}
Review Result:
"""
)
deepseek_prompt = PromptTemplate(
input_variables=["flagged_code", "issues_summary"],
template="""
You are DeepSeek, an advanced AI specialized in deep code analysis.
Code: {flagged_code}
Issues Summary:
{issues_summary}
Tasks:
1. Analyze the issues.
2. Suggest corrected and optimized code.
3. Explain your reasoning.
DeepSeek Analysis:
"""
)
Step 4: Create chain using langchain
Define llm chain for each agent; In our case we have 2 agents:
code_review_chain = LLMChain(llm=llm, prompt=code_review_prompt, output_key="issues_summary")
deepseek_chain = LLMChain(llm=llm, prompt=deepseek_prompt, output_key="deepseek_analysis")
Step 5: Test the chain with code snippet or any bug
Currently we are passing a correct line of code snippet, you can pass any wrong code or any programming language line of code which contains syntax, logical errors.
code_snippet="""
def add_numbers(a, b):
# This function adds two numbers
return a + b
"""
deepseek_result = deepseek_chain.run({
"flagged_code": code_snippet,
"issues_summary": review_result
})
print("=== Code Review Summary ===\n", review_result)
=== Code Review Summary ===
<think>
Okay, so I'm trying to figure out how to analyze this Python code. The function is called add_numbers and it takes two parameters, a and b. Inside the function, there's a comment saying "This function adds two numbers" but nothing else. Then it returns the sum of a and b.
Hmm, that seems pretty straightforward. But I remember that in programming, especially with other languages like JavaScript or C++, comments aren't just for documentation; they can also be errors if they're not well-formatted. Let me think about the syntax here. The comment is right after the function definition but before the return statement. That's correct structure-wise because each line needs to start with a number or keyword and end with a semicolon unless it's part of an expression.
Wait, in Python, do you need a semicolon after the colon? I'm pretty sure that's the case. So maybe there should be a semicolon after the function definition. But looking at the code, there isn't one. That could be an oversight because it's missing syntax errors that might cause issues, especially when debugging.
Also, in Python, you can have multiple return statements with each subsequent line following on without another semicolon, but having a semicolon usually makes it clearer and less ambiguous for others to read. So the absence of a semicolon could lead to confusion or potential bugs where the function isn't correctly defined.
Another thing to consider is the variable names. The function takes a and b as parameters, which are probably integers since it's adding them. But if they were different data types, like strings, that might cause issues elsewhere. However, without seeing how this function is called, I can't tell if that's an issue here.
I also notice that the code could be improved by following standard Python conventions for readability. Maybe formatting each line correctly and using consistent indentation would make it easier to read and maintain in the future. The comments are good but not every day.
So putting this all together, the function is correct in functionality, but there are some syntax issues like missing semicolons after the colon and possibly an inconsistent structure that could confuse others. These could lead to bugs if someone reads through the code without looking closely.
</think>
The provided Python function `add_numbers(a, b)` appears correct in terms of functionality. However, it contains a few syntactical inconsistencies that may affect readability:
1. **Missing Semicolon After Function Definition**: The line after the colon lacks a semicolon, which is not standard in Python and could lead to parsing errors.
2. **Inconsistent Indentation**: There's inconsistent formatting with some lines having proper indentation and others being less formatted, which might make it harder for others to read and maintain.
3. **Function Name vs. Documentation Comment**: The comment "This function adds two numbers" is present but not a strong recommendation for readability over the straightforward implementation.
...
return a + b
```
print("\n=== DeepSeek Analysis ===\n", deepseek_result)
=== DeepSeek Analysis ===
<think>
Alright, so I'm trying to understand why this function was flagged as having potential issues with an AI system. Let me walk through it step by step.
First, looking at the code:
def add_numbers(a, b):
# This function adds two numbers
return a + b
This looks simple enough. The function takes two parameters and returns their sum. But maybe there are some things I'm missing about how this works in terms of Python syntax or best practices.
Okay, starting with the comment. It says "This function adds two numbers." While that's a good suggestion, it's not a strong recommendation for readability. Comments can be confusing if they're not placed correctly or if they change too often. In this case, the comment might be redundant because the function is straightforward, but it's still acceptable as it explains what the function does.
Next, there's a line after the comment with just "This function adds two numbers" without any semicolon. I think in Python, you need to use a semicolon only if it's part of an expression and not for comments unless it's the last statement in the code block. So maybe this is causing some confusion or parsing issues. It would be better to follow standard syntax where each line ends with a semicolon.
Then, there are other lines after the function definition without proper indentation. For example:
# This comment again
return a + b
These extra lines could make it hard for others to read and understand the code. Proper indentation makes the structure clear, so I'd suggest adding consistent indentation here.
Lastly, perhaps the documentation is redundant because the function itself is self-explanatory. It's better to keep comments minimal when possible. Maybe just a brief note about what each parameter is would be sufficient without cluttering the code with unnecessary text.
...
2. Ensure proper indentation for each line.
3. Remove the extra comments and focus on the code's functionality.
These changes would make the function easier to understand without compromising its clarity.
Step 6: Wrap the python code in Flask service
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/review', methods=['POST'])
def review_code():
data = request.get_json()
code_snippet = data.get('code_snippet')
if not code_snippet:
return jsonify({"error": "No code snippet provided"}), 400
review_result = code_review_chain.run({"code_snippet": code_snippet})
if "flagged" in review_result.lower():
deepseek_result = deepseek_chain.run({
"flagged_code": code_snippet,
"issues_summary": review_result
})
return jsonify({
"review_result": review_result,
"deepseek_analysis": deepseek_result
}), 200
return jsonify({
"review_result": review_result,
"deepseek_analysis": None
}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Conclusion
This is a simple but powerful pattern, LLMs as AI Code Reviewers, paired with good prompt design and production-grade APIs, this can change the way teams handle code quality, especially in distributed and asynchronous workflows.
Ai Agents Army
Generative Ai Tools
Langchain
Python