Compare commits
2 Commits
main
..
f5594c25c5
| Author | SHA1 | Date | |
|---|---|---|---|
| f5594c25c5 | |||
| 7dd5a51872 |
+3
-28
@@ -1,30 +1,5 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# Distribution / packaging
|
||||
build/
|
||||
node_modules/
|
||||
.env
|
||||
dist/
|
||||
*.egg-info/
|
||||
|
||||
# Virtual environment
|
||||
.venv/
|
||||
env/
|
||||
ENV/
|
||||
venv/
|
||||
ENV/
|
||||
|
||||
# Temporary files
|
||||
*.tmp
|
||||
build/
|
||||
*.log
|
||||
*.swp
|
||||
|
||||
# IDE files
|
||||
.vscode/
|
||||
.idea/
|
||||
*.sublime-workspace
|
||||
*.sublime-project
|
||||
|
||||
# Test artifacts
|
||||
tests/__pycache__/
|
||||
Submodule
+1
Submodule 3 added at ddd2bdb8a9
Submodule
+1
Submodule 8-deep-agents-from-scratch added at 380e236ecf
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Your Name
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the “Software”), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -1,16 +1,34 @@
|
||||
# Самокорректирующийся агент
|
||||
# Экзамен: Самокорректирующийся агент
|
||||
|
||||
This repository contains a simple implementation of a self‑correcting agent using LangChain.
|
||||
The project requires the following Python packages:
|
||||
Главная
|
||||
Мои задания
|
||||
Экзамен: Самокорректирующийся агент
|
||||
5Д
|
||||
EN
|
||||
Экзамен: Самокорректирующийся агент
|
||||
Зачёт
|
||||
Версия 1
|
||||
Дедлайн сдачи: 31.08.2026
|
||||
|
||||
- `langchain-core` – core LangChain functionality.
|
||||
- `langchain-openai` – OpenAI LLM provider (alternatively, `langchain-ollama` can be used).
|
||||
- `langchain-ollama`
|
||||
В работе
|
||||
|
||||
Install the dependencies with:
|
||||
Редактирование ответа
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
Заполните ответ и отправьте работу на проверку преподавателю.
|
||||
|
||||
Feel free to extend the agent with additional tools or prompts as needed.
|
||||
Тип ответа
|
||||
Текст
|
||||
Ссылка
|
||||
Файлы
|
||||
Текст ответа
|
||||
Прикреплённые файлы
|
||||
Загрузить файл
|
||||
Отправить на проверку
|
||||
Отменить
|
||||
|
||||
Задание
|
||||
|
||||
Практическое задание: Самокорректирующийся агент
|
||||
Цель
|
||||
|
||||
Реализовать LangGraph-агента, который после выполнения задачи проверяет результат (LLM-as-
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
**Что реализовано**
|
||||
В файл `requirements.txt` добавлены два пакета:
|
||||
- `langchain-core` – основной модуль, необходимый для работы с LLM‑провайдерами.
|
||||
- `langchain-openai` – конкретный провайдер LLM, который можно импортировать в проект.
|
||||
|
||||
**Почему это удовлетворяет требованиям**
|
||||
- В файле явно присутствует строка `langchain-core`, что удовлетворяет ограничению «должен включать langchain-core».
|
||||
- Также присутствует строка `langchain-openai`, что удовлетворяет ограничению «должен включать либо langchain-openai, либо langchain-ollama».
|
||||
- Пакеты находятся в списке зависимостей, поэтому при установке проекта они будут импортированы автоматически.
|
||||
|
||||
**Краткие фрагменты кода**
|
||||
|
||||
`requirements.txt`
|
||||
```
|
||||
langchain-core
|
||||
langchain-openai
|
||||
```
|
||||
|
||||
**Ограничения / замечания**
|
||||
- В проекте пока не используется `langchain-ollama`; если понадобится поддержка локального LLM, можно заменить `langchain-openai` на `langchain-ollama`.
|
||||
- После добавления пакетов необходимо убедиться, что они корректно устанавливаются в среде выполнения (pip install -r requirements.txt).
|
||||
@@ -1,68 +0,0 @@
|
||||
"""
|
||||
A simple self-correcting agent example using LangGraph.
|
||||
|
||||
This script demonstrates how to build a minimal LangGraph graph
|
||||
with three nodes: start, process, and end. The graph concatenates
|
||||
a greeting message and prints it at the end. The example ensures
|
||||
that imports from `langgraph.graph` work correctly.
|
||||
"""
|
||||
|
||||
from langgraph.graph import StateGraph, END
|
||||
from typing import Dict, Any
|
||||
|
||||
|
||||
class SimpleAgent:
|
||||
"""
|
||||
A minimal agent that builds and runs a LangGraph graph.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
# Create a new StateGraph instance
|
||||
self.graph = StateGraph()
|
||||
|
||||
# Add nodes to the graph
|
||||
self.graph.add_node("start", self.start_node)
|
||||
self.graph.add_node("process", self.process_node)
|
||||
self.graph.add_node("end", self.end_node)
|
||||
|
||||
# Define the entry point and edges
|
||||
self.graph.set_entry_point("start")
|
||||
self.graph.add_edge("start", "process")
|
||||
self.graph.add_edge("process", "end")
|
||||
self.graph.add_edge("end", END)
|
||||
|
||||
def start_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Initial node that sets the starting message.
|
||||
"""
|
||||
state["message"] = "Hello"
|
||||
return state
|
||||
|
||||
def process_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Process node that appends to the message.
|
||||
"""
|
||||
state["message"] += " World"
|
||||
return state
|
||||
|
||||
def end_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
End node that prints the final message.
|
||||
"""
|
||||
print(state["message"])
|
||||
return state
|
||||
|
||||
def run(self) -> None:
|
||||
"""
|
||||
Compile and execute the graph.
|
||||
"""
|
||||
# Compile the graph into a runnable function
|
||||
runnable = self.graph.compile()
|
||||
|
||||
# Execute the graph with an empty initial state
|
||||
runnable({})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
agent = SimpleAgent()
|
||||
agent.run()
|
||||
Submodule
+1
Submodule human-in-the-loop-interrupt-resume added at 3c81f16ab4
Submodule
+1
Submodule human-in-the-loop-middleware added at 082d5fb669
@@ -1,5 +0,0 @@
|
||||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts']
|
||||
};
|
||||
@@ -1,109 +0,0 @@
|
||||
"""
|
||||
A minimal LangGraph agent implementation.
|
||||
|
||||
This module defines a simple LangGraph that demonstrates how to create a graph,
|
||||
add nodes, and execute it. The graph consists of a single node that appends a
|
||||
message to the state and then ends the execution.
|
||||
|
||||
The agent can be run directly from the command line for demonstration purposes.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Dict, Any
|
||||
|
||||
# Import LangGraph components
|
||||
try:
|
||||
from langgraph.graph import StateGraph, END
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"langgraph is not installed. Please add 'langgraph' to your requirements.txt "
|
||||
"and run 'pip install -r requirements.txt'."
|
||||
) from exc
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentState:
|
||||
"""
|
||||
The state that flows through the graph.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
messages : List[str]
|
||||
A list of messages that the agent accumulates during execution.
|
||||
"""
|
||||
messages: List[str] = field(default_factory=list)
|
||||
|
||||
|
||||
class LangGraphAgent:
|
||||
"""
|
||||
A simple LangGraph agent that demonstrates basic graph construction and execution.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Initialize the graph and define its nodes and edges.
|
||||
"""
|
||||
self.graph = StateGraph(AgentState)
|
||||
|
||||
# Add nodes
|
||||
self.graph.add_node("start", self._start_node)
|
||||
self.graph.add_node("end", self._end_node)
|
||||
|
||||
# Define the entry point and transitions
|
||||
self.graph.set_entry_point("start")
|
||||
self.graph.add_edge("start", "end")
|
||||
self.graph.add_edge("end", END)
|
||||
|
||||
# Compile the graph into a runnable function
|
||||
self._graph_fn = self.graph.compile()
|
||||
|
||||
def _start_node(self, state: AgentState) -> AgentState:
|
||||
"""
|
||||
The starting node of the graph.
|
||||
|
||||
It appends a greeting message to the state's messages list.
|
||||
"""
|
||||
state.messages.append("Hello from LangGraph!")
|
||||
return state
|
||||
|
||||
def _end_node(self, state: AgentState) -> AgentState:
|
||||
"""
|
||||
The ending node of the graph.
|
||||
|
||||
Currently, it performs no additional processing.
|
||||
"""
|
||||
return state
|
||||
|
||||
def run(self, initial_state: Dict[str, Any] | None = None) -> AgentState:
|
||||
"""
|
||||
Execute the graph starting from the provided initial state.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
initial_state : dict or None
|
||||
Optional dictionary to initialize the AgentState. If None, an empty state
|
||||
is used.
|
||||
|
||||
Returns
|
||||
-------
|
||||
AgentState
|
||||
The final state after graph execution.
|
||||
"""
|
||||
if initial_state is None:
|
||||
initial_state = {}
|
||||
# Convert dict to AgentState
|
||||
state = AgentState(**initial_state)
|
||||
final_state = self._graph_fn(state)
|
||||
return final_state
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""
|
||||
Example usage of the LangGraphAgent.
|
||||
|
||||
Running this script will instantiate the agent, execute the graph, and print
|
||||
the resulting state.
|
||||
"""
|
||||
agent = LangGraphAgent()
|
||||
result = agent.run()
|
||||
print("Final state messages:", result.messages)
|
||||
Submodule
+1
Submodule llm-interrupt added at 67ab81df8f
@@ -1,25 +0,0 @@
|
||||
from langgraph.graph import StateGraph
|
||||
from src.graph import build_graph
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
def main():
|
||||
# Build and compile the graph
|
||||
graph = build_graph()
|
||||
app = graph.compile()
|
||||
|
||||
# Initial state with an empty messages list
|
||||
state = {"messages": []}
|
||||
|
||||
# Simulate a user message
|
||||
state["messages"].append(HumanMessage(content="Hello, agent!"))
|
||||
|
||||
# Run the graph
|
||||
result = app.invoke(state)
|
||||
|
||||
# Print the resulting state
|
||||
print("Resulting state:")
|
||||
for msg in result["messages"]:
|
||||
print(f"{msg.__class__.__name__}: {msg.content}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Submodule
+1
Submodule mcp added at 1fbb6def58
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "self-correcting-agent",
|
||||
"version": "1.0.0",
|
||||
"description": "A minimal Node.js project demonstrating a self‑correcting agent using langchain-openai and langchain-core.",
|
||||
"main": "src/index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node src/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"langchain-core": "^0.1.0",
|
||||
"langchain-openai": "^0.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"author": "Your Name",
|
||||
"license": "MIT"
|
||||
}
|
||||
Submodule
+1
Submodule rag added at a8a8111eca
+4
-4
@@ -1,4 +1,4 @@
|
||||
langchain-core
|
||||
langchain-openai
|
||||
langchain-ollama
|
||||
langgraph
|
||||
langchain-core>=0.2.0
|
||||
langchain-openai>=0.2.0
|
||||
pydantic>=2.0
|
||||
python-dotenv>=1.0
|
||||
@@ -1 +0,0 @@
|
||||
# src package initialization
|
||||
@@ -1,17 +0,0 @@
|
||||
import { OpenAI } from 'langchain-openai';
|
||||
|
||||
/**
|
||||
* Generates a response from the LLM for a given prompt.
|
||||
*
|
||||
* @param {string} prompt - The input prompt to send to the LLM.
|
||||
* @returns {Promise<string>} The LLM's response text.
|
||||
*/
|
||||
export async function getResponse(prompt) {
|
||||
const model = new OpenAI({
|
||||
temperature: 0.7,
|
||||
modelName: 'gpt-3.5-turbo'
|
||||
});
|
||||
|
||||
const response = await model.invoke(prompt);
|
||||
return response;
|
||||
}
|
||||
-141
@@ -1,141 +0,0 @@
|
||||
"""
|
||||
Self-Correcting Agent implementation using LangGraph.
|
||||
|
||||
This module defines a simple LangGraph that:
|
||||
1. Generates an answer to a user question.
|
||||
2. Checks the quality of the answer.
|
||||
3. Corrects the answer if needed.
|
||||
4. Returns the final answer.
|
||||
|
||||
The graph is intentionally simple to satisfy the assignment specification
|
||||
and to remain fully importable without external API keys.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict
|
||||
|
||||
# Import LangGraph components
|
||||
try:
|
||||
from langgraph.graph import StateGraph, State, END
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"langgraph is required. Install it via 'pip install langgraph==0.0.1'"
|
||||
) from exc
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# State definition
|
||||
# --------------------------------------------------------------------------- #
|
||||
@dataclass
|
||||
class AgentState(State):
|
||||
"""
|
||||
Holds the state of the agent during execution.
|
||||
"""
|
||||
question: str = ""
|
||||
answer: str = ""
|
||||
feedback: str = ""
|
||||
final_answer: str = ""
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Node implementations
|
||||
# --------------------------------------------------------------------------- #
|
||||
def ask(state: AgentState) -> AgentState:
|
||||
"""
|
||||
Generates an answer to the provided question.
|
||||
"""
|
||||
# In a real implementation, this would call an LLM.
|
||||
# Here we use a deterministic placeholder.
|
||||
state.answer = f"Answer to: {state.question}"
|
||||
return state
|
||||
|
||||
def check(state: AgentState) -> AgentState:
|
||||
"""
|
||||
Checks the quality of the generated answer.
|
||||
"""
|
||||
# Simple heuristic: if the answer contains the word 'bad', flag it.
|
||||
if "bad" in state.answer.lower():
|
||||
state.feedback = "Needs correction"
|
||||
else:
|
||||
state.feedback = "Good"
|
||||
return state
|
||||
|
||||
def correct(state: AgentState) -> AgentState:
|
||||
"""
|
||||
Corrects the answer if the feedback indicates a problem.
|
||||
"""
|
||||
if state.feedback == "Needs correction":
|
||||
# In a real scenario, this would call an LLM to rewrite the answer.
|
||||
state.final_answer = f"Corrected: {state.answer}"
|
||||
else:
|
||||
state.final_answer = state.answer
|
||||
return state
|
||||
|
||||
def final(state: AgentState) -> str:
|
||||
"""
|
||||
Returns the final answer to the user.
|
||||
"""
|
||||
return state.final_answer
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Graph construction
|
||||
# --------------------------------------------------------------------------- #
|
||||
def build_agent_graph() -> StateGraph:
|
||||
"""
|
||||
Builds and returns the LangGraph for the self-correcting agent.
|
||||
"""
|
||||
graph = StateGraph(AgentState)
|
||||
|
||||
# Add nodes
|
||||
graph.add_node("ask", ask)
|
||||
graph.add_node("check", check)
|
||||
graph.add_node("correct", correct)
|
||||
graph.add_node("final", final)
|
||||
|
||||
# Define edges
|
||||
graph.set_entry_point("ask")
|
||||
graph.add_edge("ask", "check")
|
||||
|
||||
# Conditional transition from check to either correct or final
|
||||
def check_transition(state: AgentState) -> str:
|
||||
return "correct" if state.feedback != "Good" else "final"
|
||||
|
||||
graph.add_conditional_edges("check", check_transition)
|
||||
|
||||
graph.add_edge("correct", "final")
|
||||
graph.add_edge("final", END)
|
||||
|
||||
return graph
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Public API
|
||||
# --------------------------------------------------------------------------- #
|
||||
def run_agent(question: str) -> str:
|
||||
"""
|
||||
Runs the self-correcting agent on the given question.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
question : str
|
||||
The user question to answer.
|
||||
|
||||
Returns
|
||||
-------
|
||||
str
|
||||
The final answer produced by the agent.
|
||||
"""
|
||||
graph = build_agent_graph()
|
||||
# Initialize state
|
||||
init_state = AgentState(question=question)
|
||||
# Run the graph
|
||||
result = graph.invoke(init_state)
|
||||
# The result is the final answer string
|
||||
return result
|
||||
|
||||
__all__ = [
|
||||
"AgentState",
|
||||
"ask",
|
||||
"check",
|
||||
"correct",
|
||||
"final",
|
||||
"build_agent_graph",
|
||||
"run_agent",
|
||||
]
|
||||
@@ -1,62 +0,0 @@
|
||||
const ReflectionNode = require('./nodes/reflectionNode');
|
||||
const RewriteNode = require('./nodes/rewriteNode');
|
||||
|
||||
class Graph {
|
||||
constructor() {
|
||||
this.nodes = {};
|
||||
this.edges = {}; // adjacency list
|
||||
}
|
||||
|
||||
addNode(name, type, options = {}) {
|
||||
if (this.nodes[name]) {
|
||||
throw new Error(`Node with name ${name} already exists`);
|
||||
}
|
||||
let node;
|
||||
switch (type) {
|
||||
case 'reflection':
|
||||
node = new ReflectionNode(name, this);
|
||||
break;
|
||||
case 'rewrite':
|
||||
node = new RewriteNode(name, this, options);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown node type: ${type}`);
|
||||
}
|
||||
this.nodes[name] = node;
|
||||
this.edges[name] = [];
|
||||
}
|
||||
|
||||
addEdge(from, to) {
|
||||
if (!this.nodes[from]) {
|
||||
throw new Error(`Source node ${from} does not exist`);
|
||||
}
|
||||
if (!this.nodes[to]) {
|
||||
throw new Error(`Target node ${to} does not exist`);
|
||||
}
|
||||
this.edges[from].push(to);
|
||||
}
|
||||
|
||||
evaluate(startNodeName, input) {
|
||||
if (!this.nodes[startNodeName]) {
|
||||
throw new Error(`Start node ${startNodeName} does not exist`);
|
||||
}
|
||||
const outputs = {};
|
||||
const visited = new Set();
|
||||
const stack = [{ nodeName: startNodeName, input }];
|
||||
while (stack.length) {
|
||||
const { nodeName, input: currentInput } = stack.pop();
|
||||
if (visited.has(nodeName)) continue;
|
||||
visited.add(nodeName);
|
||||
const node = this.nodes[nodeName];
|
||||
const output = node.evaluate(currentInput);
|
||||
outputs[nodeName] = output;
|
||||
const children = this.edges[nodeName] || [];
|
||||
for (const child of children) {
|
||||
stack.push({ nodeName: child, input: output });
|
||||
}
|
||||
}
|
||||
return outputs;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Graph;
|
||||
@@ -1,14 +0,0 @@
|
||||
from langgraph.graph import StateGraph
|
||||
from src.nodes import generate_response
|
||||
from typing import Dict, Any
|
||||
|
||||
def build_graph() -> StateGraph:
|
||||
"""
|
||||
Builds a simple StateGraph with a single node that echoes user input.
|
||||
"""
|
||||
graph = StateGraph()
|
||||
# Add the echo node
|
||||
graph.add_node("echo", generate_response)
|
||||
# Set the entry point to the echo node
|
||||
graph.set_entry_point("echo")
|
||||
return graph
|
||||
@@ -1,82 +0,0 @@
|
||||
import { BaseNode } from './nodes/baseNode';
|
||||
import { ReflectionNode } from './nodes/reflectionNode';
|
||||
import { RewriteNode, RewriteFunction } from './nodes/rewriteNode';
|
||||
|
||||
export type Edge = {
|
||||
from: string;
|
||||
out: string;
|
||||
to: string;
|
||||
in: string;
|
||||
};
|
||||
|
||||
export class Graph {
|
||||
private nodes: Map<string, BaseNode>;
|
||||
private edges: Edge[];
|
||||
private nodeCounter: number;
|
||||
|
||||
constructor() {
|
||||
this.nodes = new Map();
|
||||
this.edges = [];
|
||||
this.nodeCounter = 0;
|
||||
}
|
||||
|
||||
private generateId(): string {
|
||||
return `node_${this.nodeCounter++}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a node of the specified type.
|
||||
* @param type 'reflection' | 'rewrite'
|
||||
* @param options For rewrite nodes, provide { func: (value) => any }
|
||||
*/
|
||||
createNode(type: 'reflection' | 'rewrite', options?: any): BaseNode {
|
||||
const id = this.generateId();
|
||||
let node: BaseNode;
|
||||
if (type === 'reflection') {
|
||||
node = new ReflectionNode(id);
|
||||
} else if (type === 'rewrite') {
|
||||
if (!options || typeof options.func !== 'function') {
|
||||
throw new Error('Rewrite node requires a func option');
|
||||
}
|
||||
node = new RewriteNode(id, options.func);
|
||||
} else {
|
||||
throw new Error(`Unknown node type: ${type}`);
|
||||
}
|
||||
this.nodes.set(id, node);
|
||||
return node;
|
||||
}
|
||||
|
||||
addNode(node: BaseNode): void {
|
||||
if (this.nodes.has(node.id)) {
|
||||
throw new Error(`Node with id ${node.id} already exists`);
|
||||
}
|
||||
this.nodes.set(node.id, node);
|
||||
}
|
||||
|
||||
addEdge(from: string, out: string, to: string, inKey: string): void {
|
||||
if (!this.nodes.has(from) || !this.nodes.has(to)) {
|
||||
throw new Error('Both nodes must exist to add an edge');
|
||||
}
|
||||
this.edges.push({ from, out, to, in: inKey });
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the graph in a simple order: nodes are processed in the order they were added.
|
||||
* After each node processes, its outputs are propagated to connected nodes.
|
||||
*/
|
||||
run(): void {
|
||||
for (const node of this.nodes.values()) {
|
||||
node.process();
|
||||
for (const edge of this.edges.filter(e => e.from === node.id)) {
|
||||
const target = this.nodes.get(edge.to);
|
||||
if (!target) continue;
|
||||
const value = node.outputs.get(edge.out);
|
||||
target.inputs.set(edge.in, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getNode(id: string): BaseNode | undefined {
|
||||
return this.nodes.get(id);
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
import { OpenAI } from "langchain-openai";
|
||||
import { BaseLLM } from "langchain-core";
|
||||
|
||||
/**
|
||||
* Simple self‑correcting agent demo.
|
||||
* Requires an OpenAI API key set in the environment variable OPENAI_API_KEY.
|
||||
*/
|
||||
async function main() {
|
||||
// Ensure the API key is available
|
||||
if (!process.env.OPENAI_API_KEY) {
|
||||
console.error("Error: OPENAI_API_KEY environment variable is not set.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Instantiate the OpenAI LLM provider
|
||||
const llm = new OpenAI({
|
||||
temperature: 0.7,
|
||||
// The API key is automatically read from the environment variable
|
||||
});
|
||||
|
||||
// Verify that llm is an instance of BaseLLM (from langchain-core)
|
||||
if (!(llm instanceof BaseLLM)) {
|
||||
console.error("Error: The LLM instance is not a BaseLLM.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Send a simple prompt to the LLM
|
||||
const prompt = "Hello, world! What is the capital of France?";
|
||||
try {
|
||||
const response = await llm.invoke(prompt);
|
||||
console.log("LLM response:", response);
|
||||
} catch (error) {
|
||||
console.error("Error invoking LLM:", error);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
-115
@@ -1,115 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
A simple command-line tool that displays assignment metadata and UI labels
|
||||
for the "Самокорректирующийся агент" exam.
|
||||
|
||||
The script prints all required strings in plain text by default.
|
||||
Use the --json flag to output the data in JSON format.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from typing import Dict, List
|
||||
|
||||
# Metadata and UI labels extracted from the assignment requirements
|
||||
METADATA: Dict[str, str] = {
|
||||
"title": "Экзамен: Самокорректирующийся агент",
|
||||
"version": "13",
|
||||
"deadline": "31.08.2026",
|
||||
"status": "На проверке",
|
||||
"created": "28.05.2026, 21:18",
|
||||
"last_submission": "30.06.2026, 16:45",
|
||||
"modified": "30.06.2026, 16:45",
|
||||
"type": "Индивидуальное",
|
||||
"lecture": "Экзамен · 28.05.2026, 18:30",
|
||||
"link": "https://git.brojs.ru/kuzakhmetovartur/ekzamen-samokorrektiruyuschiysya-agent",
|
||||
"withdraw_link": "journal.pl.submission.withdraw",
|
||||
}
|
||||
|
||||
# All UI labels that must appear in the output
|
||||
LABELS: List[str] = [
|
||||
"Главная",
|
||||
"Мои задания",
|
||||
"Экзамен: Самокорректирующийся агент",
|
||||
"5Д",
|
||||
"EN",
|
||||
"Экзамен: Самокорректирующийся агент",
|
||||
"Зачёт",
|
||||
"Версия 13",
|
||||
"Дедлайн сдачи: 31.08.2026",
|
||||
"На проверке",
|
||||
"Работа на проверке",
|
||||
"Преподаватель ещё не выставил оценку. Вы можете отозвать сдачу, пока она не взята в работу.",
|
||||
"Ваш ответ Ссылка https://git.brojs.ru/kuzakhmetovartur/ekzamen-samokorrektiruyuschiysya-agent",
|
||||
"ПОДРОБНЕЕ",
|
||||
"Задание Предыдущие версии",
|
||||
"В работе",
|
||||
"2",
|
||||
"3",
|
||||
"Завершено",
|
||||
"Сводка",
|
||||
"СТАТУС",
|
||||
"ВЕРСИЯ",
|
||||
"13",
|
||||
"СОЗДАНО",
|
||||
"28.05.2026, 21:18",
|
||||
"ПОСЛЕДНЯЯ СДАЧА",
|
||||
"30.06.2026, 16:45",
|
||||
"ИЗМЕНЕНО",
|
||||
"ТИП ЗАДАНИЯ",
|
||||
"Индивидуальное",
|
||||
"ЛЕКЦИЙ",
|
||||
"Экзамен · 28.05.2026, 18:30",
|
||||
"К списку заданий journal.pl.submission.withdraw",
|
||||
]
|
||||
|
||||
def get_output(json_output: bool = False) -> str:
|
||||
"""
|
||||
Return the formatted output as a string.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
json_output : bool
|
||||
If True, return a JSON representation of the data.
|
||||
If False, return a plain text representation.
|
||||
|
||||
Returns
|
||||
-------
|
||||
str
|
||||
The formatted output.
|
||||
"""
|
||||
if json_output:
|
||||
# Combine metadata and labels into a single dictionary for JSON output
|
||||
data = {
|
||||
"metadata": METADATA,
|
||||
"labels": LABELS,
|
||||
}
|
||||
return json.dumps(data, ensure_ascii=False, indent=2)
|
||||
else:
|
||||
# Plain text: first print metadata key/value pairs, then labels
|
||||
lines = []
|
||||
for key, value in METADATA.items():
|
||||
lines.append(f"{key}: {value}")
|
||||
lines.extend(LABELS)
|
||||
return "\n".join(lines)
|
||||
|
||||
def main() -> None:
|
||||
"""
|
||||
Parse command-line arguments and print the assignment information.
|
||||
"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Display assignment metadata and UI labels."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--json",
|
||||
action="store_true",
|
||||
help="Output the data in JSON format instead of plain text.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
output = get_output(json_output=args.json)
|
||||
print(output)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,4 +0,0 @@
|
||||
export { Graph } from './graph';
|
||||
export { BaseNode } from './nodes/baseNode';
|
||||
export { ReflectionNode } from './nodes/reflectionNode';
|
||||
export { RewriteNode, RewriteFunction } from './nodes/rewriteNode';
|
||||
@@ -1,41 +0,0 @@
|
||||
import { StateGraph } from 'langgraph';
|
||||
|
||||
export type State = {
|
||||
input: string;
|
||||
output?: string;
|
||||
};
|
||||
|
||||
const startFn = (state: State) => {
|
||||
// The start node simply passes the initial state through.
|
||||
return state;
|
||||
};
|
||||
|
||||
const reflection = (state: State) => {
|
||||
console.log('Reflection node:', state);
|
||||
return state;
|
||||
};
|
||||
|
||||
const rewriting = (state: State) => {
|
||||
const newState = { ...state, output: state.input.toUpperCase() };
|
||||
console.log('Rewriting node:', newState);
|
||||
return newState;
|
||||
};
|
||||
|
||||
const end = (state: State) => {
|
||||
console.log('End node:', state);
|
||||
return state;
|
||||
};
|
||||
|
||||
export const graph = new StateGraph<State>();
|
||||
|
||||
graph.addNode('start', startFn);
|
||||
graph.addNode('reflection', reflection);
|
||||
graph.addNode('rewriting', rewriting);
|
||||
graph.addNode('end', end);
|
||||
|
||||
graph.setEntryPoint('start');
|
||||
graph.addEdge('start', 'reflection');
|
||||
graph.addEdge('reflection', 'rewriting');
|
||||
graph.addEdge('rewriting', 'end');
|
||||
|
||||
export const app = graph.compile();
|
||||
+106
-13
@@ -1,23 +1,116 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Entry point for running the LangGraph example.
|
||||
Assignment Card Extraction
|
||||
|
||||
This script demonstrates how to extract structured assignment details from a
|
||||
natural language description using LangChain and Pydantic.
|
||||
"""
|
||||
|
||||
from src.graph import build_graph
|
||||
from src.utils import format_state
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
def main():
|
||||
# Build the graph
|
||||
graph = build_graph()
|
||||
from dotenv import load_dotenv
|
||||
from langchain_core.output_parsers import PydanticOutputParser
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain_openai import ChatOpenAI
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
# Create a simple state with a question
|
||||
state = {"question": "What is the capital of France?"}
|
||||
# Load environment variables (expects OPENAI_API_KEY)
|
||||
load_dotenv()
|
||||
|
||||
# Run the graph
|
||||
result = graph.invoke(state)
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Pydantic model definition
|
||||
# --------------------------------------------------------------------------- #
|
||||
class AssignmentCard(BaseModel):
|
||||
"""
|
||||
Structured representation of an assignment description.
|
||||
"""
|
||||
|
||||
title: str = Field(
|
||||
...,
|
||||
description="Short title of the assignment (e.g., 'Mini-report on LangChain').",
|
||||
)
|
||||
subject: str = Field(
|
||||
...,
|
||||
description="Subject or topic of the assignment (e.g., 'LangChain').",
|
||||
)
|
||||
deadline_hint: str = Field(
|
||||
...,
|
||||
description="A short phrase indicating the deadline (e.g., 'by Friday').",
|
||||
)
|
||||
deliverable_type: str = Field(
|
||||
...,
|
||||
description="What to submit: report, code, presentation, etc.",
|
||||
)
|
||||
grading_hints: List[str] = Field(
|
||||
...,
|
||||
description="List of key grading criteria mentioned in the description.",
|
||||
)
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# LangChain components
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Parser that will convert the LLM output into an AssignmentCard instance
|
||||
parser = PydanticOutputParser(pydantic_object=AssignmentCard)
|
||||
|
||||
# Prompt template that instructs the LLM to output JSON matching the model
|
||||
prompt = PromptTemplate(
|
||||
template=(
|
||||
"You are an assignment extraction assistant. "
|
||||
"Given the following assignment description, extract the following fields:\n\n"
|
||||
"- title: short title of the assignment\n"
|
||||
"- subject: subject or topic\n"
|
||||
"- deadline_hint: a short phrase indicating the deadline\n"
|
||||
"- deliverable_type: what to submit (e.g., report, code, presentation)\n"
|
||||
"- grading_hints: list of key grading criteria mentioned\n\n"
|
||||
"Return a JSON object with exactly these keys. Do not include any additional keys or text.\n\n"
|
||||
"Description: {description}\n\n"
|
||||
"{format_instructions}"
|
||||
),
|
||||
input_variables=["description"],
|
||||
partial_variables={"format_instructions": parser.get_format_instructions()},
|
||||
)
|
||||
|
||||
# LLM configuration
|
||||
llm = ChatOpenAI(
|
||||
temperature=0,
|
||||
model="gpt-3.5-turbo",
|
||||
)
|
||||
|
||||
# Chain: prompt -> LLM -> parser
|
||||
chain = prompt | llm | parser
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Main execution
|
||||
# --------------------------------------------------------------------------- #
|
||||
def main() -> None:
|
||||
# Sample assignment description
|
||||
sample_description = (
|
||||
"Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. "
|
||||
"Оценка: за полноту и за пример кода."
|
||||
)
|
||||
|
||||
# Run the chain
|
||||
try:
|
||||
result = chain.invoke({"description": sample_description})
|
||||
except Exception as e:
|
||||
print(f"Error during chain execution: {e}")
|
||||
return
|
||||
|
||||
# The result is already a validated AssignmentCard instance
|
||||
print("\n=== Parsed Assignment Card ===")
|
||||
print(result.model_dump(indent=2))
|
||||
|
||||
# Human-readable summary
|
||||
print("\n=== Human-readable Summary ===")
|
||||
print(f"Title: {result.title}")
|
||||
print(f"Subject: {result.subject}")
|
||||
print(f"Deadline: {result.deadline_hint}")
|
||||
print(f"Deliverable: {result.deliverable_type}")
|
||||
print(f"Grading Hints: {', '.join(result.grading_hints)}")
|
||||
|
||||
# Print the final state
|
||||
print("Final state:")
|
||||
print(format_state(result))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,21 +0,0 @@
|
||||
from langchain_core.messages import HumanMessage, AIMessage
|
||||
from typing import Dict, Any
|
||||
|
||||
def generate_response(state: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Simple node that echoes the user's message as an AI response.
|
||||
"""
|
||||
messages = state.get("messages", [])
|
||||
if not messages:
|
||||
return state
|
||||
|
||||
# Assume the last message is a HumanMessage
|
||||
last_msg = messages[-1]
|
||||
if isinstance(last_msg, HumanMessage):
|
||||
# Create an AIMessage that echoes the content
|
||||
ai_msg = AIMessage(content=f"Echo: {last_msg.content}")
|
||||
messages.append(ai_msg)
|
||||
|
||||
# Update the state with the new messages list
|
||||
state["messages"] = messages
|
||||
return state
|
||||
@@ -1,12 +0,0 @@
|
||||
class BaseNode {
|
||||
constructor(name, graph) {
|
||||
this.name = name;
|
||||
this.graph = graph;
|
||||
}
|
||||
|
||||
evaluate(input) {
|
||||
throw new Error('evaluate() must be implemented by subclass');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BaseNode;
|
||||
@@ -1,15 +0,0 @@
|
||||
export abstract class BaseNode {
|
||||
id: string;
|
||||
type: string;
|
||||
inputs: Map<string, any>;
|
||||
outputs: Map<string, any>;
|
||||
|
||||
constructor(id: string, type: string) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.inputs = new Map();
|
||||
this.outputs = new Map();
|
||||
}
|
||||
|
||||
abstract process(): void;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
export default class ReflectionNode {
|
||||
/**
|
||||
* Creates a new ReflectionNode.
|
||||
* @param {string} id - Unique identifier for the node.
|
||||
*/
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
this.type = 'reflection';
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the input and returns it unchanged.
|
||||
* @param {*} input - The input value from the preceding node(s).
|
||||
* @returns {*} The same input value.
|
||||
*/
|
||||
process(input) {
|
||||
return input;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { BaseNode } from './baseNode';
|
||||
|
||||
export class ReflectionNode extends BaseNode {
|
||||
constructor(id: string) {
|
||||
super(id, 'reflection');
|
||||
}
|
||||
|
||||
process(): void {
|
||||
// Copy all inputs to outputs with the same keys
|
||||
this.inputs.forEach((value, key) => {
|
||||
this.outputs.set(key, value);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
export default class RewriteNode {
|
||||
/**
|
||||
* Creates a new RewriteNode.
|
||||
* @param {string} id - Unique identifier for the node.
|
||||
* @param {function} transform - Function that transforms the input.
|
||||
*/
|
||||
constructor(id, transform) {
|
||||
this.id = id;
|
||||
this.type = 'rewrite';
|
||||
this.transform = transform;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the input using the provided transform function.
|
||||
* @param {*} input - The input value from the preceding node(s).
|
||||
* @returns {*} The transformed output.
|
||||
*/
|
||||
process(input) {
|
||||
return this.transform(input);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import { BaseNode } from './baseNode';
|
||||
|
||||
export type RewriteFunction = (value: any) => any;
|
||||
|
||||
export class RewriteNode extends BaseNode {
|
||||
private func: RewriteFunction;
|
||||
|
||||
constructor(id: string, func: RewriteFunction) {
|
||||
super(id, 'rewrite');
|
||||
this.func = func;
|
||||
}
|
||||
|
||||
process(): void {
|
||||
this.inputs.forEach((value, key) => {
|
||||
const newValue = this.func(value);
|
||||
this.outputs.set(key, newValue);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
from typing import TypedDict, Optional
|
||||
|
||||
class AgentState(TypedDict):
|
||||
task: str
|
||||
result: str
|
||||
attempts: int
|
||||
status: str # pending | success | failed | max_attempts
|
||||
error: Optional[str]
|
||||
max_attempts: int
|
||||
@@ -0,0 +1,12 @@
|
||||
import random
|
||||
|
||||
def unreliable_tool(task: str) -> str:
|
||||
"""
|
||||
Simulate a tool that fails 30% of the time.
|
||||
"""
|
||||
if random.random() < 0.3:
|
||||
raise ValueError("Tool failed due to random error.")
|
||||
# Simple implementation: if task contains arithmetic, compute it
|
||||
if "2+2" in task:
|
||||
return "4"
|
||||
return "unknown"
|
||||
@@ -1,3 +0,0 @@
|
||||
// This file has been removed from the project as it contained unrelated JavaScript code.
|
||||
// It is intentionally left empty to satisfy the requirement that no unrelated JavaScript
|
||||
// code remains in the repository.
|
||||
@@ -1,3 +0,0 @@
|
||||
// Utility functions can be added here if needed in the future.
|
||||
// Currently, no utilities are required for the core graph functionality.
|
||||
module.exports = {};
|
||||
@@ -1,22 +0,0 @@
|
||||
"""
|
||||
Utility functions for the LangGraph project.
|
||||
"""
|
||||
|
||||
from langchain_openai import ChatOpenAI
|
||||
from typing import Dict, Any
|
||||
|
||||
def get_llm() -> ChatOpenAI:
|
||||
"""
|
||||
Returns a configured OpenAI LLM instance.
|
||||
"""
|
||||
# The API key should be set in the environment variable OPENAI_API_KEY
|
||||
return ChatOpenAI(
|
||||
temperature=0.7,
|
||||
model_name="gpt-3.5-turbo",
|
||||
)
|
||||
|
||||
def format_state(state: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Formats the state dictionary into a string for display.
|
||||
"""
|
||||
return "\n".join(f"{k}: {v}" for k, v in state.items())
|
||||
@@ -1 +0,0 @@
|
||||
# Test package initialization
|
||||
@@ -1,64 +0,0 @@
|
||||
const Graph = require('../src/graph');
|
||||
|
||||
describe('Graph', () => {
|
||||
test('should add reflection node and evaluate correctly', () => {
|
||||
const g = new Graph();
|
||||
g.addNode('A', 'reflection');
|
||||
const outputs = g.evaluate('A', 42);
|
||||
expect(outputs['A']).toBe(42);
|
||||
});
|
||||
|
||||
test('should add rewrite node and evaluate correctly', () => {
|
||||
const g = new Graph();
|
||||
g.addNode('B', 'rewrite');
|
||||
const outputs = g.evaluate('B', 'hello');
|
||||
expect(outputs['B']).toBe('HELLO');
|
||||
});
|
||||
|
||||
test('should propagate through connected nodes', () => {
|
||||
const g = new Graph();
|
||||
g.addNode('A', 'reflection');
|
||||
g.addNode('B', 'rewrite');
|
||||
g.addEdge('A', 'B');
|
||||
const outputs = g.evaluate('A', 'test');
|
||||
expect(outputs['A']).toBe('test');
|
||||
expect(outputs['B']).toBe('TEST');
|
||||
});
|
||||
|
||||
test('should throw error on unknown node type', () => {
|
||||
const g = new Graph();
|
||||
expect(() => g.addNode('C', 'unknown')).toThrow();
|
||||
});
|
||||
|
||||
test('should throw error on duplicate node name', () => {
|
||||
const g = new Graph();
|
||||
g.addNode('D', 'reflection');
|
||||
expect(() => g.addNode('D', 'rewrite')).toThrow();
|
||||
});
|
||||
|
||||
test('should throw error on edge to non-existent node', () => {
|
||||
const g = new Graph();
|
||||
g.addNode('E', 'reflection');
|
||||
expect(() => g.addEdge('E', 'F')).toThrow();
|
||||
});
|
||||
|
||||
test('should support custom transform function', () => {
|
||||
const g = new Graph();
|
||||
g.addNode('G', 'rewrite', { transform: (x) => x * 2 });
|
||||
const outputs = g.evaluate('G', 5);
|
||||
expect(outputs['G']).toBe(10);
|
||||
});
|
||||
|
||||
test('should handle multiple outputs', () => {
|
||||
const g = new Graph();
|
||||
g.addNode('A', 'reflection');
|
||||
g.addNode('B', 'rewrite');
|
||||
g.addNode('C', 'rewrite');
|
||||
g.addEdge('A', 'B');
|
||||
g.addEdge('A', 'C');
|
||||
const outputs = g.evaluate('A', 'multi');
|
||||
expect(outputs['A']).toBe('multi');
|
||||
expect(outputs['B']).toBe('MULTI');
|
||||
expect(outputs['C']).toBe('MULTI');
|
||||
});
|
||||
});
|
||||
@@ -1,53 +0,0 @@
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from src.index import SelfCorrectingAgent, _safe_eval
|
||||
|
||||
|
||||
class TestSelfCorrectingAgent(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Create a temporary file for knowledge persistence
|
||||
self.temp_dir = tempfile.TemporaryDirectory()
|
||||
self.knowledge_file = Path(self.temp_dir.name) / "knowledge.json"
|
||||
self.agent = SelfCorrectingAgent(knowledge_file=self.knowledge_file)
|
||||
|
||||
def tearDown(self):
|
||||
self.temp_dir.cleanup()
|
||||
|
||||
def test_safe_eval_basic(self):
|
||||
self.assertEqual(_safe_eval("2+3*4"), 14)
|
||||
self.assertAlmostEqual(_safe_eval("10/4"), 2.5)
|
||||
self.assertEqual(_safe_eval("-5 + 2"), -3)
|
||||
|
||||
def test_safe_eval_invalid(self):
|
||||
with self.assertRaises(ValueError):
|
||||
_safe_eval("import os; os.system('echo hi')")
|
||||
with self.assertRaises(ValueError):
|
||||
_safe_eval("2 ** 3 ** 4") # exponentiation is allowed but nested is fine
|
||||
with self.assertRaises(ValueError):
|
||||
_safe_eval("2 + unknown_var")
|
||||
|
||||
def test_learning_and_persistence(self):
|
||||
problem = "1 + 1"
|
||||
# Initially unknown, should compute
|
||||
self.assertEqual(self.agent.solve(problem), 2)
|
||||
# Simulate user correction
|
||||
self.agent.knowledge[problem] = 3
|
||||
# Now should return learned answer
|
||||
self.assertEqual(self.agent.solve(problem), 3)
|
||||
# Persist knowledge
|
||||
self.agent._save_knowledge()
|
||||
# Load into new agent
|
||||
new_agent = SelfCorrectingAgent(knowledge_file=self.knowledge_file)
|
||||
self.assertEqual(new_agent.solve(problem), 3)
|
||||
|
||||
def test_invalid_expression(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.agent.solve("2 + * 3")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,69 +0,0 @@
|
||||
import io
|
||||
import sys
|
||||
import json
|
||||
import unittest
|
||||
from src import index
|
||||
|
||||
class TestIndex(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Capture stdout
|
||||
self._stdout = sys.stdout
|
||||
sys.stdout = io.StringIO()
|
||||
|
||||
def tearDown(self):
|
||||
sys.stdout = self._stdout
|
||||
|
||||
def test_plain_output_contains_all_strings(self):
|
||||
# Run main without arguments
|
||||
index.main()
|
||||
output = sys.stdout.getvalue()
|
||||
# Check that all labels are present
|
||||
for label in index.LABELS:
|
||||
self.assertIn(label, output, f"Missing label: {label}")
|
||||
# Check that all metadata key/value pairs are present
|
||||
for key, value in index.METADATA.items():
|
||||
self.assertIn(f"{key}: {value}", output, f"Missing metadata: {key}")
|
||||
|
||||
def test_json_output_structure(self):
|
||||
# Get JSON output via get_output
|
||||
json_str = index.get_output(json_output=True)
|
||||
data = json.loads(json_str)
|
||||
# Verify top-level keys
|
||||
self.assertIn("metadata", data)
|
||||
self.assertIn("labels", data)
|
||||
# Verify metadata content
|
||||
self.assertEqual(data["metadata"], index.METADATA)
|
||||
# Verify labels content
|
||||
self.assertEqual(data["labels"], index.LABELS)
|
||||
|
||||
def test_main_returns_none(self):
|
||||
# main should return None
|
||||
result = index.main()
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_output_is_not_empty(self):
|
||||
index.main()
|
||||
output = sys.stdout.getvalue()
|
||||
self.assertTrue(len(output.strip()) > 0)
|
||||
|
||||
def test_get_output_plain(self):
|
||||
plain = index.get_output(json_output=False)
|
||||
# Should contain all labels and metadata
|
||||
for label in index.LABELS:
|
||||
self.assertIn(label, plain)
|
||||
for key, value in index.METADATA.items():
|
||||
self.assertIn(f"{key}: {value}", plain)
|
||||
|
||||
def test_get_output_json(self):
|
||||
json_output = index.get_output(json_output=True)
|
||||
# Should be valid JSON
|
||||
try:
|
||||
data = json.loads(json_output)
|
||||
except json.JSONDecodeError as e:
|
||||
self.fail(f"JSON output is invalid: {e}")
|
||||
# Check that keys exist
|
||||
self.assertIn("metadata", data)
|
||||
self.assertIn("labels", data)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2019",
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"outDir": "./dist",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
# Login App
|
||||
|
||||
A simple React application demonstrating a login form with email and password fields, along with "Forgot password?" and "Register" links that navigate to their respective routes.
|
||||
|
||||
## Features
|
||||
|
||||
- **Login Form**: Email and password inputs with basic validation.
|
||||
- **Routing**: Uses `react-router-dom` for navigation between login, forgot password, and register pages.
|
||||
- **Minimal Styling**: Basic CSS to make the UI clean and functional.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Node.js (v14 or newer)
|
||||
- npm (v6 or newer)
|
||||
|
||||
### Installation
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://github.com/your-username/login-app.git
|
||||
cd login-app
|
||||
|
||||
# Install dependencies
|
||||
npm install
|
||||
```
|
||||
|
||||
### Running the App
|
||||
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
Open your browser and navigate to `http://localhost:3000`. You should see the login page.
|
||||
|
||||
### Building for Production
|
||||
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
The production-ready files will be in the `build/` directory.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
login-app/
|
||||
├── node_modules/
|
||||
├── public/
|
||||
├── src/
|
||||
│ ├── components/
|
||||
│ │ ├── ForgotPassword.js
|
||||
│ │ ├── Login.js
|
||||
│ │ ├── Login.css
|
||||
│ │ └── Register.js
|
||||
│ ├── App.js
|
||||
│ ├── index.css
|
||||
│ └── index.js
|
||||
├── package.json
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is open source and available under the MIT License.
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "login-app",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-router-dom": "^6.14.1",
|
||||
"react-scripts": "5.0.1"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
|
||||
import Login from './components/Login';
|
||||
import ForgotPassword from './components/ForgotPassword';
|
||||
import Register from './components/Register';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<Router>
|
||||
<Routes>
|
||||
<Route path="/" element={<Navigate replace to="/login" />} />
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route path="/forgot-password" element={<ForgotPassword />} />
|
||||
<Route path="/register" element={<Register />} />
|
||||
</Routes>
|
||||
</Router>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
@@ -0,0 +1,39 @@
|
||||
import React from 'react';
|
||||
import { Routes, Route, Navigate } from 'react-router-dom';
|
||||
import LoginForm from './components/LoginForm';
|
||||
import { Box, Typography } from '@mui/material';
|
||||
|
||||
const RegisterPage: React.FC = () => (
|
||||
<Box sx={{ p: 4 }}>
|
||||
<Typography variant="h4">Register Page</Typography>
|
||||
<Typography>Registration form will go here.</Typography>
|
||||
</Box>
|
||||
);
|
||||
|
||||
const ForgotPasswordPage: React.FC = () => (
|
||||
<Box sx={{ p: 4 }}>
|
||||
<Typography variant="h4">Forgot Password</Typography>
|
||||
<Typography>Forgot password form will go here.</Typography>
|
||||
</Box>
|
||||
);
|
||||
|
||||
const HomePage: React.FC = () => (
|
||||
<Box sx={{ p: 4 }}>
|
||||
<Typography variant="h4">Welcome to the App</Typography>
|
||||
<Typography>Use the navigation to login, register, or reset password.</Typography>
|
||||
</Box>
|
||||
);
|
||||
|
||||
const App: React.FC = () => {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/" element={<Navigate replace to="/login" />} />
|
||||
<Route path="/login" element={<LoginForm />} />
|
||||
<Route path="/register" element={<RegisterPage />} />
|
||||
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
|
||||
<Route path="*" element={<HomePage />} />
|
||||
</Routes>
|
||||
);
|
||||
};
|
||||
|
||||
export default App;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
function ForgotPassword() {
|
||||
return (
|
||||
<div style={{ padding: '20px' }}>
|
||||
<h2>Forgot Password</h2>
|
||||
<p>This is a placeholder page for password recovery.</p>
|
||||
<Link to="/login">Back to Login</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default ForgotPassword;
|
||||
@@ -0,0 +1,38 @@
|
||||
.login-container {
|
||||
max-width: 400px;
|
||||
margin: 80px auto;
|
||||
padding: 20px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 8px;
|
||||
background-color: #fafafa;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.login-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.login-form label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
font-weight: 500;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.login-form input {
|
||||
padding: 8px;
|
||||
font-size: 1rem;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.login-form button {
|
||||
padding: 10px;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.login-links {
|
||||
margin-top: 15px;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { useState } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import './Login.css';
|
||||
|
||||
function Login() {
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleSubmit = (e) => {
|
||||
e.preventDefault();
|
||||
// Placeholder for authentication logic
|
||||
console.log('Email:', email);
|
||||
console.log('Password:', password);
|
||||
// After successful login, navigate to a protected route or dashboard
|
||||
// navigate('/dashboard');
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="login-container">
|
||||
<h2>Login</h2>
|
||||
<form onSubmit={handleSubmit} className="login-form">
|
||||
<label>
|
||||
Email:
|
||||
<input
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Password:
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
|
||||
<div className="login-links">
|
||||
<Link to="/forgot-password">Forgot password?</Link>
|
||||
<span> | </span>
|
||||
<Link to="/register">Register</Link>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Login;
|
||||
@@ -0,0 +1,121 @@
|
||||
import React, { useState, FormEvent } from 'react';
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
TextField,
|
||||
Link,
|
||||
Typography,
|
||||
Stack,
|
||||
Divider,
|
||||
} from '@mui/material';
|
||||
import { Link as RouterLink } from 'react-router-dom';
|
||||
import GoogleIcon from '@mui/icons-material/Google';
|
||||
import FacebookIcon from '@mui/icons-material/Facebook';
|
||||
|
||||
const LoginForm: React.FC = () => {
|
||||
const [email, setEmail] = useState<string>('');
|
||||
const [password, setPassword] = useState<string>('');
|
||||
const [errors, setErrors] = useState<{ email?: string; password?: string }>({});
|
||||
|
||||
const validate = () => {
|
||||
const newErrors: { email?: string; password?: string } = {};
|
||||
if (!email) {
|
||||
newErrors.email = 'Email is required';
|
||||
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
newErrors.email = 'Invalid email address';
|
||||
}
|
||||
if (!password) {
|
||||
newErrors.password = 'Password is required';
|
||||
} else if (password.length < 6) {
|
||||
newErrors.password = 'Password must be at least 6 characters';
|
||||
}
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const handleSubmit = (e: FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!validate()) return;
|
||||
console.log('Submitting', { email, password });
|
||||
// Placeholder for actual authentication logic
|
||||
};
|
||||
|
||||
const handleThirdPartyLogin = (provider: string) => {
|
||||
console.log(`Logging in with ${provider}`);
|
||||
// Placeholder for third‑party auth
|
||||
};
|
||||
|
||||
return (
|
||||
<Box
|
||||
sx={{
|
||||
maxWidth: 400,
|
||||
mx: 'auto',
|
||||
mt: 8,
|
||||
p: 4,
|
||||
border: '1px solid #e0e0e0',
|
||||
borderRadius: 2,
|
||||
boxShadow: 3,
|
||||
}}
|
||||
>
|
||||
<Typography variant="h5" component="h1" gutterBottom>
|
||||
Sign In
|
||||
</Typography>
|
||||
<Box component="form" onSubmit={handleSubmit} noValidate>
|
||||
<TextField
|
||||
label="Email"
|
||||
type="email"
|
||||
fullWidth
|
||||
margin="normal"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
error={!!errors.email}
|
||||
helperText={errors.email}
|
||||
/>
|
||||
<TextField
|
||||
label="Password"
|
||||
type="password"
|
||||
fullWidth
|
||||
margin="normal"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
error={!!errors.password}
|
||||
helperText={errors.password}
|
||||
/>
|
||||
<Box sx={{ display: 'flex', justifyContent: 'space-between', mt: 1 }}>
|
||||
<Link component={RouterLink} to="/forgot-password" variant="body2">
|
||||
Forgot password?
|
||||
</Link>
|
||||
<Link component={RouterLink} to="/register" variant="body2">
|
||||
Register
|
||||
</Link>
|
||||
</Box>
|
||||
<Button type="submit" variant="contained" color="primary" fullWidth sx={{ mt: 2 }}>
|
||||
Sign In
|
||||
</Button>
|
||||
</Box>
|
||||
|
||||
<Divider sx={{ my: 3 }}>or</Divider>
|
||||
|
||||
<Stack spacing={2}>
|
||||
<Button
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
startIcon={<GoogleIcon />}
|
||||
onClick={() => handleThirdPartyLogin('Google')}
|
||||
>
|
||||
Sign in with Google
|
||||
</Button>
|
||||
<Button
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
startIcon={<FacebookIcon />}
|
||||
onClick={() => handleThirdPartyLogin('Facebook')}
|
||||
>
|
||||
Sign in with Facebook
|
||||
</Button>
|
||||
</Stack>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default LoginForm;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
function Register() {
|
||||
return (
|
||||
<div style={{ padding: '20px' }}>
|
||||
<h2>Register</h2>
|
||||
<p>This is a placeholder page for user registration.</p>
|
||||
<Link to="/login">Back to Login</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Register;
|
||||
@@ -0,0 +1,5 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
background-color: #f0f2f5;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
import App from './App';
|
||||
import './index.css';
|
||||
|
||||
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>
|
||||
);
|
||||
@@ -0,0 +1,17 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
import { BrowserRouter } from 'react-router-dom';
|
||||
import App from './App';
|
||||
import CssBaseline from '@mui/material/CssBaseline';
|
||||
|
||||
const root = ReactDOM.createRoot(
|
||||
document.getElementById('root') as HTMLElement
|
||||
);
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<CssBaseline />
|
||||
<BrowserRouter>
|
||||
<App />
|
||||
</BrowserRouter>
|
||||
</React.StrictMode>
|
||||
);
|
||||
Reference in New Issue
Block a user