forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
50 lines (42 loc) · 1.93 KB
/
Copy pathagent.py
File metadata and controls
50 lines (42 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
This is the main entry point for the AI.
It defines the workflow graph and the entry point for the agent.
"""
# pylint: disable=line-too-long, unused-import
import json
from typing import cast
from langchain_core.messages import AIMessage, ToolMessage
from langgraph.graph import StateGraph, END
from langgraph.checkpoint.memory import MemorySaver
from copilotkit.demos.research_canvas.state import AgentState
from copilotkit.demos.research_canvas.download import download_node
from copilotkit.demos.research_canvas.chat import chat_node
from copilotkit.demos.research_canvas.search import search_node
from copilotkit.demos.research_canvas.delete import delete_node, perform_delete_node
# Define a new graph
workflow = StateGraph(AgentState)
workflow.add_node("download", download_node)
workflow.add_node("chat_node", chat_node)
workflow.add_node("search_node", search_node)
workflow.add_node("delete_node", delete_node)
workflow.add_node("perform_delete_node", perform_delete_node)
def route(state):
"""Route after the chat node."""
messages = state.get("messages", [])
if messages and isinstance(messages[-1], AIMessage):
ai_message = cast(AIMessage, messages[-1])
if ai_message.tool_calls and ai_message.tool_calls[0]["name"] == "Search":
return "search_node"
if ai_message.tool_calls and ai_message.tool_calls[0]["name"] == "DeleteResources":
return "delete_node"
if messages and isinstance(messages[-1], ToolMessage):
return "chat_node"
return END
memory = MemorySaver()
workflow.set_entry_point("download")
workflow.add_edge("download", "chat_node")
workflow.add_conditional_edges("chat_node", route, ["search_node", "chat_node", "delete_node", END])
workflow.add_edge("delete_node", "perform_delete_node")
workflow.add_edge("perform_delete_node", "chat_node")
workflow.add_edge("search_node", "download")
graph = workflow.compile(checkpointer=memory, interrupt_after=["delete_node"])