You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dynamically create graph from config
I want to be able to dynamically generate graphs from configs:
export const markdownFormatterAgentSpec: AgentSpec = {
id: "markdown-formatter-agent-id-1",
name: "Markdown Formatter Agent",
description: "An agent that formats text using Markdown.",
coreInstructions: markdownFormatterAgentCoreInstructions,
model: {
name: "gpt-3.5-turbo",
temperature: 0.2, // Optional: Adjust temperature if needed
},
subAgents: [], // No subagents in this case, can be populated if needed
tools: [], // No tools for the markdown formatter agent
};
and from the config, I generate the nodes:
function generateNodes(
agents: Record<string, ReturnType<typeof createReactAgent>>
): Record<string, AgentNodeFunction> {
const nodes: Record<string, AgentNodeFunction> = {};
for (const [id, agent] of Object.entries(agents)) {
nodes[id] = async (state, config) => {
try {
const result = await agent.invoke(state, config);
// Process the messages to create a new state
const newMessages = result.messages.map(
(msg: any) =>
new HumanMessage({
content: msg.content,
name: id,
})
);
return {
...state,
messages: [...state.messages, ...newMessages],
};
} catch (error: any) {
// Handle and log errors
return {
...state,
messages: [
...state.messages,
new SystemMessage(`Error in ${id}: ${error.message}`),
],
};
}
};
}
return nodes;
}
now I have a problem with creating the graph:
function generateGraph(
agents: Record<string, ReturnType<typeof createReactAgent>>,
rootAgentId: string
) {
const nodes = generateNodes(agents);
const graph = new StateGraph(AgentState);
// Add nodes to the graph
for (const [id, node] of Object.entries(nodes)) {
graph.addNode(id, node);
}
// Dynamically define edges based on hierarchy
function addEdges(agentSpec: AgentSpec) {
if (agentSpec.subAgents && agentSpec.subAgents.length > 0) {
for (const subAgent of agentSpec.subAgents) {
const agentId = agentSpec.id;
const subAgentId = subAgent.id;
graph.addEdge(agentId, subAgent.id); // Connect parent to sub-agent
addEdges(subAgent); // Recursively add edges for sub-agents
}
}
}
// Add edges starting from the root agent
if (agents[rootAgentId]) {
addEdges(agents[rootAgentId]);
} else {
throw new Error(
`Root agent with ID ${rootAgentId} is not defined in agents map.`
);
}
// Connect START to the root agent
graph.addEdge(START, rootAgentId);
// Connect all agents back to END
for (const id of Object.keys(nodes)) {
graph.addEdge(id, END);
}
return graph.compile();
}
In theory this should work but the problem is addEdge doesn't work.
When you call addNode("id", node)
It makes id as an option for addEdge, because its a literal value, but when the id is coming from a variable, it doesn't work:
Argument of type 'string' is not assignable to parameter of type '"__start__" | "__start__"[]'.ts(2345)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Dynamically create graph from config
I want to be able to dynamically generate graphs from configs:
and from the config, I generate the nodes:
now I have a problem with creating the graph:
In theory this should work but the problem is
addEdge
doesn't work.When you call
addNode("id", node)
It makes id as an option for addEdge, because its a literal value, but when the id is coming from a variable, it doesn't work:
How can I be able to dynamically create a graph?
Beta Was this translation helpful? Give feedback.
All reactions