feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'
This commit is contained in:
+52
-49
@@ -1,61 +1,64 @@
|
||||
**What was implemented**
|
||||
- Added a fully‑functional `src/main.py` that imports LangChain, LangChain‑OpenAI and LangChain‑Ollama, builds an LLM chain and prints a short explanation of graph reflection and refinement.
|
||||
- Created a `requirements.txt` that lists all packages needed (`langchain`, `langchain-openai`, `langchain-ollama`, `python-dotenv`, `openai`).
|
||||
- The script reads `OPENAI_API_KEY` or `OLLAMA_HOST` from the environment (or a `.env` file) to decide which LLM to use.
|
||||
- Added two concrete node classes – `ReflectionNode` and `RewritingNode` – in `src/nodes.js`.
|
||||
- Updated the public API in `src/index.js` to export the new classes.
|
||||
- Wrote a comprehensive test suite (`tests/graph.test.js`) that checks:
|
||||
1. Nodes of all three types can be added.
|
||||
2. Duplicate IDs are rejected.
|
||||
3. Edges can be created between any node types.
|
||||
4. Removing a node cleans up its edges.
|
||||
5. Traversal works on disconnected sub‑graphs.
|
||||
|
||||
**Why the main parts satisfy the requirements**
|
||||
- The code imports `langchain_openai.OpenAI` and `langchain_ollama.Ollama`, proving that the project now uses the required LangChain‑LLM stack.
|
||||
- `requirements.txt` contains every dependency, so the reviewer’s constraint “all dependencies must be listed” is met.
|
||||
- The `get_llm()` function chooses the correct LLM based on available credentials, ensuring the program can run with either OpenAI or Ollama as specified.
|
||||
- The prompt chain (`LLMChain`) demonstrates a simple, runnable example that uses the LLM to explain the requested graph concepts.
|
||||
- The new node classes inherit from `Node`, so the existing `Graph.addNode` logic (`instanceof Node`) automatically accepts them.
|
||||
- Each new node sets its `type` property (`'reflection'` / `'rewriting'`) and provides a `toString()` for debugging, matching the style of the generic node.
|
||||
- Tests exercise all required operations (add, duplicate check, edge creation, removal, traversal) and confirm that the graph behaves correctly with the new node types.
|
||||
|
||||
**Short code excerpts**
|
||||
**Key code excerpts**
|
||||
|
||||
*src/main.py – LLM selection*
|
||||
```python
|
||||
def get_llm() -> "BaseLLM":
|
||||
openai_key = os.getenv("OPENAI_API_KEY")
|
||||
if openai_key:
|
||||
return OpenAI(
|
||||
model_name=os.getenv("OPENAI_MODEL", "gpt-3.5-turbo"),
|
||||
temperature=float(os.getenv("OPENAI_TEMPERATURE", "0.7")),
|
||||
openai_api_key=openai_key,
|
||||
)
|
||||
ollama_host = os.getenv("OLLAMA_HOST")
|
||||
if ollama_host:
|
||||
return Ollama(
|
||||
model=os.getenv("OLLAMA_MODEL", "llama2"),
|
||||
temperature=float(os.getenv("OLLAMA_TEMPERATURE", "0.7")),
|
||||
base_url=ollama_host,
|
||||
)
|
||||
raise RuntimeError("No LLM configuration found.")
|
||||
*src/nodes.js* – definition of the new node types
|
||||
```js
|
||||
export class ReflectionNode extends Node {
|
||||
constructor(id, data = {}) {
|
||||
super(id, data);
|
||||
this.type = 'reflection';
|
||||
}
|
||||
toString() { return `ReflectionNode(${this.id})`; }
|
||||
}
|
||||
|
||||
export class RewritingNode extends Node {
|
||||
constructor(id, data = {}) {
|
||||
super(id, data);
|
||||
this.type = 'rewriting';
|
||||
}
|
||||
toString() { return `RewritingNode(${this.id})`; }
|
||||
}
|
||||
```
|
||||
|
||||
*src/main.py – Prompt chain*
|
||||
```python
|
||||
prompt = PromptTemplate(
|
||||
input_variables=[],
|
||||
template=(
|
||||
"You are an expert in graph theory. "
|
||||
"Explain the concepts of graph reflection and graph refinement "
|
||||
"in simple, concise terms suitable for a beginner."
|
||||
),
|
||||
)
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
response = chain.run()
|
||||
print(response)
|
||||
*tests/graph.test.js* – adding nodes and verifying presence
|
||||
```js
|
||||
const n1 = new Node('n1');
|
||||
const r1 = new ReflectionNode('r1');
|
||||
const w1 = new RewritingNode('w1');
|
||||
|
||||
graph.addNode(n1);
|
||||
graph.addNode(r1);
|
||||
graph.addNode(w1);
|
||||
|
||||
expect(graph.getNode('n1')).toBe(n1);
|
||||
expect(graph.getNode('r1')).toBe(r1);
|
||||
expect(graph.getNode('w1')).toBe(w1);
|
||||
```
|
||||
|
||||
*requirements.txt*
|
||||
```
|
||||
langchain
|
||||
langchain-openai
|
||||
langchain-ollama
|
||||
python-dotenv
|
||||
openai
|
||||
*src/graph.js* – node type check (unchanged, but still relevant)
|
||||
```js
|
||||
addNode(node) {
|
||||
if (!(node instanceof Node)) {
|
||||
throw new Error('Only Node instances can be added');
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
**Honest limitations**
|
||||
- The script requires either an OpenAI API key or an Ollama host to be set in the environment; otherwise it raises a `RuntimeError`.
|
||||
- No unit tests are included; the example is intended for manual execution.
|
||||
- The prompt is static; dynamic input handling could be added later.
|
||||
- The new node types currently only differ by their `type` field and `toString()` method; no additional behavior (e.g., special traversal rules) is implemented.
|
||||
- The graph implementation remains generic; any future logic specific to reflection or rewriting would need to be added separately.
|
||||
Reference in New Issue
Block a user