Skip to content

Commit 66d8c4e

Browse files
committed
add sync notebook to the j2
1 parent 9e129ce commit 66d8c4e

File tree

5 files changed

+421
-198
lines changed

5 files changed

+421
-198
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 9,
6+
"id": "36834357",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from agentex import Agentex\n",
11+
"\n",
12+
"client = Agentex(base_url=\"http://localhost:5003\")"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 10,
18+
"id": "d1c309d6",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"AGENT_NAME = \"s020-streaming\""
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 11,
28+
"id": "9f6e6ef0",
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"# # (Optional) Create a new task. If you don't create a new task, each message will be sent to a new task. The server will create the task for you.\n",
33+
"\n",
34+
"# import uuid\n",
35+
"\n",
36+
"# TASK_ID = str(uuid.uuid4())[:8]\n",
37+
"\n",
38+
"# rpc_response = client.agents.rpc_by_name(\n",
39+
"# agent_name=AGENT_NAME,\n",
40+
"# method=\"task/create\",\n",
41+
"# params={\n",
42+
"# \"name\": f\"{TASK_ID}-task\",\n",
43+
"# \"params\": {}\n",
44+
"# }\n",
45+
"# )\n",
46+
"\n",
47+
"# task = rpc_response.result\n",
48+
"# print(task)"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 12,
54+
"id": "b03b0d37",
55+
"metadata": {},
56+
"outputs": [
57+
{
58+
"name": "stdout",
59+
"output_type": "stream",
60+
"text": [
61+
"Hello! I can assist you with a variety of tasks, including:\n",
62+
"\n",
63+
"1. Answering questions across a wide range of topics, such as science, history, technology, and more.\n",
64+
"2. Providing explanations and summaries of complex concepts.\n",
65+
"3. Offering writing assistance, including grammar and style suggestions.\n",
66+
"4. Helping with language translation and learning.\n",
67+
"5. Providing recommendations for books, movies, or other media.\n",
68+
"6. Engaging in general conversation or brainstorming ideas.\n",
69+
"\n",
70+
"Feel free to ask me anything specific you'd like help with!\n"
71+
]
72+
}
73+
],
74+
"source": [
75+
"# Test non streaming response\n",
76+
"from typing import List, cast\n",
77+
"from agentex.types import TaskMessage, TextContent\n",
78+
"\n",
79+
"# The response is expected to be a list of TaskMessage objects, which is a union of the following types:\n",
80+
"# - TextContent: A message with just text content \n",
81+
"# - DataContent: A message with JSON-serializable data content\n",
82+
"# - ToolRequestContent: A message with a tool request, which contains a JSON-serializable request to call a tool\n",
83+
"# - ToolResponseContent: A message with a tool response, which contains response object from a tool call in its content\n",
84+
"\n",
85+
"# When processing the message/send response, if you are expecting more than TextContent, such as DataContent, ToolRequestContent, or ToolResponseContent, you can process them as well\n",
86+
"\n",
87+
"rpc_response = client.agents.rpc_by_name(\n",
88+
" agent_name=AGENT_NAME,\n",
89+
" method=\"message/send\",\n",
90+
" params={\n",
91+
" \"content\": {\"type\": \"text\", \"author\": \"user\", \"content\": \"Hello what can you do?\"},\n",
92+
" \"stream\": False\n",
93+
" }\n",
94+
")\n",
95+
"\n",
96+
"# # Extract and print just the text content from the response\n",
97+
"# # The response is expected to be a dict with a \"result\" key containing a list of message dicts\n",
98+
"if rpc_response and rpc_response.result:\n",
99+
"\n",
100+
" # We know that the result of the message/send when stream is set to False will be a list of TaskMessage objects\n",
101+
" task_message_list = cast(List[TaskMessage], rpc_response.result)\n",
102+
" for task_message in rpc_response.result:\n",
103+
" if isinstance(task_message, TaskMessage):\n",
104+
" content = task_message.content\n",
105+
" if isinstance(content, TextContent):\n",
106+
" text = content.content\n",
107+
" print(text)\n",
108+
" else:\n",
109+
" print(f\"Found non-text {type(task_message)} object in response.\")\n"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 13,
115+
"id": "79688331",
116+
"metadata": {},
117+
"outputs": [
118+
{
119+
"name": "stdout",
120+
"output_type": "stream",
121+
"text": [
122+
"Hello! I can assist you with a variety of tasks, including:\n",
123+
"\n",
124+
"1. **Answering Questions**: I can provide information and answer questions on a wide range of topics, from general knowledge to specific subjects.\n",
125+
"2. **Providing Explanations**: If you need clarification on concepts or topics, I can explain them in detail.\n",
126+
"3. **Assisting with Writing**: I can help you draft emails, essays, reports, and other written documents.\n",
127+
"4. **Generating Ideas**: If you're looking for creative ideas or brainstorming, I can help with that too.\n",
128+
"5. **Learning and Study Help**: I can provide educational support, including summaries, study techniques, and practice questions.\n",
129+
"6. **Language Assistance**: If you need help with language learning or translation, I can assist with that as well.\n",
130+
"7. **Problem-Solving**: I can help you think through problems or challenges you might be facing.\n",
131+
"\n",
132+
"Feel free to ask me anything, and I'll do my best to help you!"
133+
]
134+
}
135+
],
136+
"source": [
137+
"# Test streaming response\n",
138+
"import json\n",
139+
"from agentex.types import AgentRpcResponse\n",
140+
"from agentex.types.agent_rpc_result import StreamTaskMessageDelta, StreamTaskMessageFull\n",
141+
"from agentex.types.text_delta import TextDelta\n",
142+
"from agentex.types.task_message_update import TaskMessageUpdate\n",
143+
"\n",
144+
"\n",
145+
"# The result object of message/send will be a TaskMessageUpdate which is a union of the following types:\n",
146+
"# - StreamTaskMessageStart: \n",
147+
"# - An indicator that a streaming message was started, doesn't contain any useful content\n",
148+
"# - StreamTaskMessageDelta: \n",
149+
"# - A delta of a streaming message, contains the text delta to aggregate\n",
150+
"# - StreamTaskMessageDone: \n",
151+
"# - An indicator that a streaming message was done, doesn't contain any useful content\n",
152+
"# - StreamTaskMessageFull: \n",
153+
"# - A non-streaming message, there is nothing to aggregate, since this contains the full message, not deltas\n",
154+
"\n",
155+
"# Whenn processing StreamTaskMessageDelta, if you are expecting more than TextDeltas, such as DataDelta, ToolRequestDelta, or ToolResponseDelta, you can process them as well\n",
156+
"# Whenn processing StreamTaskMessageFull, if you are expecting more than TextContent, such as DataContent, ToolRequestContent, or ToolResponseContent, you can process them as well\n",
157+
"\n",
158+
"with client.agents.with_streaming_response.rpc_by_name(\n",
159+
" agent_name=AGENT_NAME,\n",
160+
" method=\"message/send\",\n",
161+
" params={\n",
162+
" \"content\": {\"type\": \"text\", \"author\": \"user\", \"content\": \"Hello what can you do?\"},\n",
163+
" \"stream\": True\n",
164+
" }\n",
165+
") as response:\n",
166+
" for agent_rpc_response_str in response.iter_text():\n",
167+
" chunk_rpc_response = AgentRpcResponse.model_validate(json.loads(agent_rpc_response_str))\n",
168+
" # We know that the result of the message/send when stream is set to True will be a TaskMessageUpdate\n",
169+
" task_message_update = cast(TaskMessageUpdate, chunk_rpc_response.result)\n",
170+
"\n",
171+
" # Print oly the text deltas as they arrive or any full messages\n",
172+
" if isinstance(task_message_update, StreamTaskMessageDelta):\n",
173+
" delta = task_message_update.delta\n",
174+
" if isinstance(delta, TextDelta):\n",
175+
" print(delta.text_delta, end=\"\", flush=True)\n",
176+
" else:\n",
177+
" print(f\"Found non-text {type(task_message)} object in streaming message.\")\n",
178+
" elif isinstance(task_message_update, StreamTaskMessageFull):\n",
179+
" content = task_message_update.content\n",
180+
" if isinstance(content, TextContent):\n",
181+
" print(content.content)\n",
182+
" else:\n",
183+
" print(f\"Found non-text {type(task_message)} object in full message.\")\n"
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": null,
189+
"id": "4ffb663c",
190+
"metadata": {},
191+
"outputs": [],
192+
"source": []
193+
}
194+
],
195+
"metadata": {
196+
"kernelspec": {
197+
"display_name": ".venv",
198+
"language": "python",
199+
"name": "python3"
200+
},
201+
"language_info": {
202+
"codemirror_mode": {
203+
"name": "ipython",
204+
"version": 3
205+
},
206+
"file_extension": ".py",
207+
"mimetype": "text/x-python",
208+
"name": "python",
209+
"nbconvert_exporter": "python",
210+
"pygments_lexer": "ipython3",
211+
"version": "3.12.9"
212+
}
213+
},
214+
"nbformat": 4,
215+
"nbformat_minor": 5
216+
}

0 commit comments

Comments
 (0)