create final nodes
This commit is contained in:
+40
-12
@@ -1,30 +1,58 @@
|
||||
import { END, START, StateGraph } from "@langchain/langgraph";
|
||||
import { MessagesState } from "./state";
|
||||
import { toolNode } from "./nodes/tool";
|
||||
import { createToolNode } from "./nodes/tool";
|
||||
import { createToolConditional } from "./conditionals/tool_end";
|
||||
import { normalizationSetup } from "./nodes/normalizationSetup";
|
||||
import { dummyNormalisationModel } from "./nodes/dummyNormalisationModel";
|
||||
import { dummyTriggerEventModel } from "./nodes/dummyTriggerEventModel";
|
||||
import { arithmeticToolsByName } from "./tools/arithmetic"
|
||||
import { createDummyModelNode } from "./nodes/dummyModel";
|
||||
import { verificationSetup } from "./nodes/verificationSetup";
|
||||
import { dummyRagasMetrics } from "./nodes/dummyRagasMetrics";
|
||||
import { produceRanking } from "./nodes/produceRanking";
|
||||
|
||||
const triggerEventToolNode = createToolNode(arithmeticToolsByName);
|
||||
const verificationToolNode = createToolNode(arithmeticToolsByName);
|
||||
|
||||
const dummyTriggerEventModel = createDummyModelNode("Trigger Events of");
|
||||
const dummyNormalisationModel = createDummyModelNode("Normalised");
|
||||
const dummyVerificationModel = createDummyModelNode("verification of");
|
||||
|
||||
const triggerEventToolConditional = createToolConditional("triggerEventToolNode", verificationSetup.name);
|
||||
const verificationToolConditional = createToolConditional("verificationToolNode", produceRanking.name);
|
||||
|
||||
|
||||
const triggerEventToolConditional = createToolConditional(toolNode.name, END)
|
||||
const agent = new StateGraph(MessagesState)
|
||||
|
||||
//NODES
|
||||
.addNode("toolNode", toolNode)
|
||||
|
||||
.addNode(normalizationSetup.name, normalizationSetup)
|
||||
.addNode(dummyNormalisationModel.name, dummyNormalisationModel)
|
||||
.addNode(dummyTriggerEventModel.name, dummyTriggerEventModel)
|
||||
.addNode("dummyNormalisationModel", dummyNormalisationModel)
|
||||
|
||||
.addNode("triggerEventToolNode", triggerEventToolNode)
|
||||
.addNode("dummyTriggerEventModel", dummyTriggerEventModel)
|
||||
|
||||
.addNode(verificationSetup.name, verificationSetup)
|
||||
.addNode("dummyVerificationModel", dummyVerificationModel)
|
||||
.addNode(dummyRagasMetrics.name, dummyRagasMetrics)
|
||||
.addNode("verificationToolNode", verificationToolNode)
|
||||
.addNode(produceRanking.name, produceRanking)
|
||||
|
||||
.addEdge(START, normalizationSetup.name)
|
||||
.addEdge(normalizationSetup.name, dummyNormalisationModel.name)
|
||||
.addEdge(dummyNormalisationModel.name, dummyTriggerEventModel.name)
|
||||
.addEdge(normalizationSetup.name, "dummyNormalisationModel")
|
||||
.addEdge("dummyNormalisationModel", "dummyTriggerEventModel")
|
||||
|
||||
// @ts-expect-error
|
||||
.addConditionalEdges(dummyTriggerEventModel.name, triggerEventToolConditional, [toolNode.name, END])
|
||||
.addConditionalEdges("dummyTriggerEventModel", triggerEventToolConditional, ["triggerEventToolNode", verificationSetup.name])
|
||||
.addEdge("triggerEventToolNode", "dummyTriggerEventModel")
|
||||
|
||||
.addEdge(toolNode.name, dummyTriggerEventModel.name)
|
||||
.addEdge(verificationSetup.name, "dummyVerificationModel")
|
||||
.addEdge(verificationSetup.name, dummyRagasMetrics.name)
|
||||
|
||||
// @ts-expect-error
|
||||
.addConditionalEdges("dummyVerificationModel", verificationToolConditional, ["verificationToolNode", produceRanking.name])
|
||||
.addEdge("verificationToolNode", "dummyVerificationModel")
|
||||
|
||||
.addEdge(dummyRagasMetrics.name, produceRanking.name)
|
||||
|
||||
.addEdge(dummyTriggerEventModel.name, END)
|
||||
.compile();
|
||||
|
||||
export {agent}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { GraphNode } from "@langchain/langgraph";
|
||||
import { MessagesState } from "../state";
|
||||
import { AIMessage } from "@langchain/core/messages";
|
||||
|
||||
export function createDummyModelNode(addition): GraphNode<typeof MessagesState> {
|
||||
return async (state) => {
|
||||
//TODO: call AI model with collected data
|
||||
|
||||
return {
|
||||
messages: [new AIMessage(addition + " : " + state.messages.at(-1)?.content)]
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { GraphNode } from "@langchain/langgraph";
|
||||
import { MessagesState } from "../state";
|
||||
import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
||||
|
||||
export const dummyNormalisationModel: GraphNode<typeof MessagesState> = async (state) => {
|
||||
//TODO: call AI model with collected data
|
||||
|
||||
return {
|
||||
messages: [ new AIMessage(state.messages.at(-1)?.content + " Processed")]
|
||||
};
|
||||
};
|
||||
@@ -1,11 +0,0 @@
|
||||
import { GraphNode } from "@langchain/langgraph";
|
||||
import { MessagesState } from "../state";
|
||||
import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
||||
|
||||
export const dummyTriggerEventModel: GraphNode<typeof MessagesState> = async (state) => {
|
||||
//TODO: call AI model with collected data
|
||||
|
||||
return {
|
||||
messages: [ new AIMessage("Trigger events of: " + state.messages.at(-1)?.content)]
|
||||
};
|
||||
};
|
||||
@@ -1,11 +0,0 @@
|
||||
import { GraphNode } from "@langchain/langgraph";
|
||||
import { MessagesState } from "../state";
|
||||
import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
||||
|
||||
export const dummyVerificationModel: GraphNode<typeof MessagesState> = async (state) => {
|
||||
//TODO: call AI model with collected data
|
||||
|
||||
return {
|
||||
messages: [ new AIMessage("Verified : " + state.messages.at(-1)?.content)]
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
import { GraphNode } from "@langchain/langgraph";
|
||||
import { MessagesState } from "../state";
|
||||
import { AIMessage, HumanMessage } from "@langchain/core/messages";
|
||||
|
||||
export const produceRanking: GraphNode<typeof MessagesState> = async (state) => {
|
||||
//TODO: produce ranking here
|
||||
|
||||
return { messages: [ new AIMessage(state.messages?.length.toString() ?? "0")] };
|
||||
};
|
||||
+18
-17
@@ -1,25 +1,26 @@
|
||||
import { AIMessage, ToolMessage } from "@langchain/core/messages";
|
||||
import { GraphNode } from "@langchain/langgraph";
|
||||
import { MessagesState } from "../state";
|
||||
import { arithmeticToolsByName } from "../tools/arithmetic";
|
||||
|
||||
export const toolNode: GraphNode<typeof MessagesState> = async (state) => {
|
||||
const lastMessage = state.messages.at(-1);
|
||||
export function createToolNode(tools): GraphNode<typeof MessagesState> {
|
||||
return async (state) => {
|
||||
const lastMessage = state.messages.at(-1);
|
||||
|
||||
//STARTTEMP
|
||||
return {messages: [new AIMessage("yeman")]}
|
||||
//ENDTEMP
|
||||
//STARTTEMP
|
||||
return {messages: [new AIMessage("yeman")]}
|
||||
//ENDTEMP
|
||||
|
||||
if (lastMessage == null || !AIMessage.isInstance(lastMessage)) {
|
||||
return { messages: [] };
|
||||
}
|
||||
if (lastMessage == null || !AIMessage.isInstance(lastMessage)) {
|
||||
return { messages: [] };
|
||||
}
|
||||
|
||||
const result: ToolMessage[] = [];
|
||||
for (const toolCall of lastMessage.tool_calls ?? []) {
|
||||
const tool = arithmeticToolsByName[toolCall.name];
|
||||
const observation = await tool.invoke(toolCall);
|
||||
result.push(observation);
|
||||
}
|
||||
const result: ToolMessage[] = [];
|
||||
for (const toolCall of (lastMessage as AIMessage).tool_calls ?? []) {
|
||||
const tool = tools[toolCall.name];
|
||||
const observation = await tool.invoke(toolCall);
|
||||
result.push(observation);
|
||||
}
|
||||
|
||||
return { messages: result };
|
||||
};
|
||||
return { messages: result };
|
||||
};
|
||||
}
|
||||
@@ -37,4 +37,4 @@ export const arithmeticToolsByName = {
|
||||
[divide.name]: divide,
|
||||
};
|
||||
|
||||
export const arithmeticTools = Object.values(arithmeticToolsByName);
|
||||
//const arithmeticTools = Object.values(arithmeticToolsByName);
|
||||
Reference in New Issue
Block a user