forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_agent.py
More file actions
89 lines (73 loc) · 2.25 KB
/
Copy pathemail_agent.py
File metadata and controls
89 lines (73 loc) · 2.25 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Test Joker Agent"""
from typing import Any, cast
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, END
from langgraph.graph import MessagesState
from langgraph.checkpoint.memory import MemorySaver
from langchain_core.runnables import RunnableConfig
from langchain_core.messages import SystemMessage, ToolMessage
from copilotkit.langchain import copilotkit_customize_config, copilotkit_exit
class EmailAgentState(MessagesState):
"""Email Agent State"""
email: str
async def email_node(state: EmailAgentState, config: RunnableConfig):
"""
Make a joke.
"""
config = copilotkit_customize_config(
config,
emit_messages=True,
emit_intermediate_state=[
{
"state_key": "email",
"tool": "write_email",
"tool_argument": "the_email"
},
]
)
system_message = "You write emails."
email_tool = {
'name': 'write_email',
'description': """Write an email.""",
'parameters': {
'type': 'object',
'properties': {
'the_email': {
'description': """The email""",
'type': 'string',
}
},
'required': ['the_email']
}
}
email_model = ChatOpenAI(model="gpt-4o").bind_tools(
[email_tool],
parallel_tool_calls=False,
tool_choice="write_email"
)
response = await email_model.ainvoke([
*state["messages"],
SystemMessage(
content=system_message
)
], config)
tool_calls = getattr(response, "tool_calls")
email = tool_calls[0]["args"]["the_email"]
await copilotkit_exit(config)
return {
"messages": [
response,
ToolMessage(
name=tool_calls[0]["name"],
content=email,
tool_call_id=tool_calls[0]["id"]
)
],
"email": email,
}
workflow = StateGraph(EmailAgentState)
workflow.add_node("email_node", cast(Any, email_node))
workflow.set_entry_point("email_node")
workflow.add_edge("email_node", END)
memory = MemorySaver()
email_graph = workflow.compile(checkpointer=memory)