feat: solution for 'Экзамен: Самокорректирующийся агент'
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
}
|
||||
+12
-61
@@ -1,66 +1,17 @@
|
||||
import ReflectionNode from './nodes/reflectionNode.js';
|
||||
import RewriteNode from './nodes/rewriteNode.js';
|
||||
import { getResponse } from './agent.js';
|
||||
|
||||
/**
|
||||
* Simple directed graph implementation that supports reflection and rewrite nodes.
|
||||
* Entry point for the self‑correcting agent demo.
|
||||
*/
|
||||
class Graph {
|
||||
constructor() {
|
||||
/** @type {Object.<string, Object>} */
|
||||
this.nodes = {};
|
||||
/** @type {Array<{from: string, to: string}>} */
|
||||
this.edges = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a node to the graph.
|
||||
* @param {Object} node - Node instance (must have id and type).
|
||||
*/
|
||||
addNode(node) {
|
||||
if (!node || !node.id) {
|
||||
throw new Error('Node must have an id.');
|
||||
}
|
||||
this.nodes[node.id] = node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a directed edge from one node to another.
|
||||
* @param {string} fromId - Source node id.
|
||||
* @param {string} toId - Destination node id.
|
||||
*/
|
||||
addEdge(fromId, toId) {
|
||||
if (!this.nodes[fromId] || !this.nodes[toId]) {
|
||||
throw new Error('Both nodes must exist before adding an edge.');
|
||||
}
|
||||
this.edges.push({ from: fromId, to: toId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the graph in topological order.
|
||||
* @returns {Object.<string, *>} Mapping of node ids to their output values.
|
||||
*/
|
||||
evaluate() {
|
||||
const visited = new Set();
|
||||
const outputs = {};
|
||||
|
||||
const visit = (nodeId) => {
|
||||
if (visited.has(nodeId)) return;
|
||||
visited.add(nodeId);
|
||||
|
||||
// Find all incoming edges to this node
|
||||
const incoming = this.edges.filter((e) => e.to === nodeId);
|
||||
const inputValues = incoming.map((e) => outputs[e.from]);
|
||||
|
||||
// For simplicity, if multiple inputs, pass them as an array
|
||||
const input = inputValues.length === 1 ? inputValues[0] : inputValues;
|
||||
|
||||
const node = this.nodes[nodeId];
|
||||
outputs[nodeId] = node.process(input);
|
||||
};
|
||||
|
||||
Object.keys(this.nodes).forEach(visit);
|
||||
return outputs;
|
||||
}
|
||||
export async function main() {
|
||||
const prompt = 'Hello, world! What is the capital of France?';
|
||||
const answer = await getResponse(prompt);
|
||||
console.log('LLM response:', answer);
|
||||
}
|
||||
|
||||
export { Graph, ReflectionNode, RewriteNode };
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
main().catch((err) => {
|
||||
console.error('Error:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user