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
48 lines (39 loc) · 1.97 KB
/
Copy pathagent.py
File metadata and controls
48 lines (39 loc) · 1.97 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
from typing import Annotated
from llama_index.core.workflow import Context
from llama_index.llms.openai import OpenAI
from llama_index.protocols.ag_ui.events import StateSnapshotWorkflowEvent
from llama_index.protocols.ag_ui.router import get_ag_ui_workflow_router
# This tool has a client-side version that is actually called to change the background
# These tools just need a response string to make it look like they are executing
def change_theme_color(
theme_color: Annotated[str, "The hex color value. i.e. '#123456''"],
) -> str:
"""Change the background color of the chat. Can be any hex color value."""
return f"Changing background to {theme_color}"
# This is another client-side tool that is actually called to add a proverb to the list
# These tools just need a response string to make it look like they are executing
async def add_proverb(
proverb: Annotated[str, "The proverb to add. Make it witty, short and concise."],
) -> str:
"""Add a proverb to the list of proverbs."""
return f"Added proverb: {proverb}"
# This is a backend tool that executes code on the backend server
# For now this is a dummy implementation, but it could very well call a weather API
async def get_weather(
location: Annotated[str, "The location to get the weather for."],
) -> str:
"""Get the weather for a given location."""
return f"The weather in {location} is sunny and 70 degrees."
agentic_chat_router = get_ag_ui_workflow_router(
llm=OpenAI(model="gpt-4.1"),
# Tools that are executed in the frontend client
frontend_tools=[change_theme_color, add_proverb],
# Tools that are executed in the backend server
backend_tools=[get_weather],
system_prompt="You are a helpful assistant that can add proverbs to a list, get the weather for a given location, and change the background color of the chat/app background.",
initial_state={
"proverbs": [
"CopilotKit may be new, but its the best thing since sliced bread.",
],
},
)