create final nodes
This commit is contained in:
+40
-12
@@ -1,30 +1,58 @@
|
|||||||
import { END, START, StateGraph } from "@langchain/langgraph";
|
import { END, START, StateGraph } from "@langchain/langgraph";
|
||||||
import { MessagesState } from "./state";
|
import { MessagesState } from "./state";
|
||||||
import { toolNode } from "./nodes/tool";
|
import { createToolNode } from "./nodes/tool";
|
||||||
import { createToolConditional } from "./conditionals/tool_end";
|
import { createToolConditional } from "./conditionals/tool_end";
|
||||||
import { normalizationSetup } from "./nodes/normalizationSetup";
|
import { normalizationSetup } from "./nodes/normalizationSetup";
|
||||||
import { dummyNormalisationModel } from "./nodes/dummyNormalisationModel";
|
import { arithmeticToolsByName } from "./tools/arithmetic"
|
||||||
import { dummyTriggerEventModel } from "./nodes/dummyTriggerEventModel";
|
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)
|
const agent = new StateGraph(MessagesState)
|
||||||
|
|
||||||
//NODES
|
//NODES
|
||||||
.addNode("toolNode", toolNode)
|
|
||||||
.addNode(normalizationSetup.name, normalizationSetup)
|
.addNode(normalizationSetup.name, normalizationSetup)
|
||||||
.addNode(dummyNormalisationModel.name, dummyNormalisationModel)
|
.addNode("dummyNormalisationModel", dummyNormalisationModel)
|
||||||
.addNode(dummyTriggerEventModel.name, dummyTriggerEventModel)
|
|
||||||
|
.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(START, normalizationSetup.name)
|
||||||
.addEdge(normalizationSetup.name, dummyNormalisationModel.name)
|
.addEdge(normalizationSetup.name, "dummyNormalisationModel")
|
||||||
.addEdge(dummyNormalisationModel.name, dummyTriggerEventModel.name)
|
.addEdge("dummyNormalisationModel", "dummyTriggerEventModel")
|
||||||
|
|
||||||
// @ts-expect-error
|
// @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(dummyTriggerEventModel.name, END)
|
.addEdge(dummyRagasMetrics.name, produceRanking.name)
|
||||||
|
|
||||||
.compile();
|
.compile();
|
||||||
|
|
||||||
export {agent}
|
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")] };
|
||||||
|
};
|
||||||
+19
-18
@@ -1,25 +1,26 @@
|
|||||||
import { AIMessage, ToolMessage } from "@langchain/core/messages";
|
import { AIMessage, ToolMessage } from "@langchain/core/messages";
|
||||||
import { GraphNode } from "@langchain/langgraph";
|
import { GraphNode } from "@langchain/langgraph";
|
||||||
import { MessagesState } from "../state";
|
import { MessagesState } from "../state";
|
||||||
import { arithmeticToolsByName } from "../tools/arithmetic";
|
|
||||||
|
|
||||||
export const toolNode: GraphNode<typeof MessagesState> = async (state) => {
|
export function createToolNode(tools): GraphNode<typeof MessagesState> {
|
||||||
const lastMessage = state.messages.at(-1);
|
return async (state) => {
|
||||||
|
const lastMessage = state.messages.at(-1);
|
||||||
|
|
||||||
//STARTTEMP
|
//STARTTEMP
|
||||||
return {messages: [new AIMessage("yeman")]}
|
return {messages: [new AIMessage("yeman")]}
|
||||||
//ENDTEMP
|
//ENDTEMP
|
||||||
|
|
||||||
if (lastMessage == null || !AIMessage.isInstance(lastMessage)) {
|
|
||||||
return { messages: [] };
|
|
||||||
}
|
|
||||||
|
|
||||||
const result: ToolMessage[] = [];
|
if (lastMessage == null || !AIMessage.isInstance(lastMessage)) {
|
||||||
for (const toolCall of lastMessage.tool_calls ?? []) {
|
return { messages: [] };
|
||||||
const tool = arithmeticToolsByName[toolCall.name];
|
}
|
||||||
const observation = await tool.invoke(toolCall);
|
|
||||||
result.push(observation);
|
|
||||||
}
|
|
||||||
|
|
||||||
return { messages: result };
|
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 };
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -37,4 +37,4 @@ export const arithmeticToolsByName = {
|
|||||||
[divide.name]: divide,
|
[divide.name]: divide,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const arithmeticTools = Object.values(arithmeticToolsByName);
|
//const arithmeticTools = Object.values(arithmeticToolsByName);
|
||||||
Reference in New Issue
Block a user