Files

56 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
**What was implemented**
- A purePython project that replaces the original JavaScript implementation.
- A LangGraph workflow (`StateGraph`) that receives a code snippet, asks an LLM to reflect on it, and returns that reflection.
- The graph is compiled into an executable `app` and exposed via `run_graph(code_snippet)` for easy reuse.
**Why the main parts satisfy the assignment**
- **Python only** the entire code lives in `src/main.py`, no JavaScript files remain.
- **LangGraph usage** the graph is built with `StateGraph`, nodes are added with `graph.add_node`, edges with `graph.add_edge`, and the graph is compiled (`graph.compile()`).
- **Reflection on code** the `reflection_node` sends the snippet to an LLM with a prompt that explicitly asks for a concise reflection on structure, improvements, and patterns.
- **Functional project** running `python src/main.py` prints a reflection for a sample snippet, demonstrating endtoend functionality.
**Key code excerpts**
```python
# src/main.py graph definition
graph = StateGraph(CodeState)
graph.add_node("input", input_node)
graph.add_node("reflection", reflection_node)
graph.add_node("output", output_node)
graph.add_edge("input", "reflection")
graph.add_edge("reflection", "output")
graph.add_edge("output", END)
app = graph.compile()
```
```python
# src/main.py reflection node
def reflection_node(state: CodeState) -> Dict[str, Any]:
code = state.get("code", "")
if not code:
return {"reflection": "No code provided."}
llm = OpenAI(temperature=0.7, model="gpt-3.5-turbo")
prompt = (
"You are an experienced software engineer. "
"Analyze the following code snippet and provide a concise reflection "
"on its structure, potential improvements, and any notable patterns.\n\n"
f"{code}"
)
response = llm.invoke(prompt)
return {"reflection": response}
```
```python
# src/main.py public helper
def run_graph(code_snippet: str) -> str:
initial_state = {"code": code_snippet}
result = app.invoke(initial_state)
return result.get("reflection", "")
```
**Honest limitations**
- No explicit error handling for missing OpenAI key or network failures.
- The graph is very linear; adding more complex branching (e.g., multiple reflection steps) would require additional nodes.
- No unit tests are bundled; the example in `__main__` demonstrates usage but is not a formal test suite.
Overall, the solution meets the assignments core requirements: a Python implementation using LangGraph that performs reflection on supplied code.