|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": 2, |
6 | | - "id": "36834357", |
| 5 | + "execution_count": null, |
| 6 | + "id": "0", |
7 | 7 | "metadata": {}, |
8 | 8 | "outputs": [], |
9 | 9 | "source": [ |
|
14 | 14 | }, |
15 | 15 | { |
16 | 16 | "cell_type": "code", |
17 | | - "execution_count": 3, |
18 | | - "id": "d1c309d6", |
| 17 | + "execution_count": null, |
| 18 | + "id": "1", |
19 | 19 | "metadata": {}, |
20 | 20 | "outputs": [], |
21 | 21 | "source": [ |
|
24 | 24 | }, |
25 | 25 | { |
26 | 26 | "cell_type": "code", |
27 | | - "execution_count": 4, |
28 | | - "id": "9f6e6ef0", |
| 27 | + "execution_count": null, |
| 28 | + "id": "2", |
29 | 29 | "metadata": {}, |
30 | 30 | "outputs": [], |
31 | 31 | "source": [ |
|
50 | 50 | }, |
51 | 51 | { |
52 | 52 | "cell_type": "code", |
53 | | - "execution_count": 5, |
54 | | - "id": "b03b0d37", |
| 53 | + "execution_count": null, |
| 54 | + "id": "3", |
55 | 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 on a wide range of topics, including science, history, technology, and more.\n", |
64 | | - "2. Providing explanations or summaries of complex concepts.\n", |
65 | | - "3. Offering writing assistance, such as proofreading, editing, or generating ideas for essays and articles.\n", |
66 | | - "4. Helping with problem-solving in areas like math, coding, or logic puzzles.\n", |
67 | | - "5. Engaging in conversation to provide companionship or entertainment.\n", |
68 | | - "\n", |
69 | | - "If there's something specific you need help with, feel free to ask!\n" |
70 | | - ] |
71 | | - } |
72 | | - ], |
| 56 | + "outputs": [], |
73 | 57 | "source": [ |
74 | 58 | "# Test non streaming response\n", |
75 | | - "from typing import List, cast\n", |
76 | | - "from agentex.types import TaskMessage, TextContent\n", |
| 59 | + "from agentex.types import TextContent\n", |
77 | 60 | "\n", |
78 | 61 | "# The response is expected to be a list of TaskMessage objects, which is a union of the following types:\n", |
79 | 62 | "# - TextContent: A message with just text content \n", |
|
83 | 66 | "\n", |
84 | 67 | "# 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", |
85 | 68 | "\n", |
86 | | - "rpc_response = client.agents.rpc_by_name(\n", |
| 69 | + "rpc_response = client.agents.send_message(\n", |
87 | 70 | " agent_name=AGENT_NAME,\n", |
88 | | - " method=\"message/send\",\n", |
89 | 71 | " params={\n", |
90 | 72 | " \"content\": {\"type\": \"text\", \"author\": \"user\", \"content\": \"Hello what can you do?\"},\n", |
91 | 73 | " \"stream\": False\n", |
92 | 74 | " }\n", |
93 | 75 | ")\n", |
94 | 76 | "\n", |
95 | | - "# # Extract and print just the text content from the response\n", |
96 | | - "# # The response is expected to be a dict with a \"result\" key containing a list of message dicts\n", |
97 | | - "if rpc_response and rpc_response.result:\n", |
| 77 | + "if not rpc_response or not rpc_response.result:\n", |
| 78 | + " raise ValueError(\"No result in response\")\n", |
98 | 79 | "\n", |
99 | | - " # We know that the result of the message/send when stream is set to False will be a list of TaskMessage objects\n", |
100 | | - " task_message_list = cast(List[TaskMessage], rpc_response.result)\n", |
101 | | - " for task_message in rpc_response.result:\n", |
102 | | - " if isinstance(task_message, TaskMessage):\n", |
103 | | - " content = task_message.content\n", |
104 | | - " if isinstance(content, TextContent):\n", |
105 | | - " text = content.content\n", |
106 | | - " print(text)\n", |
107 | | - " else:\n", |
108 | | - " print(f\"Found non-text {type(task_message)} object in response.\")\n" |
| 80 | + "# Extract and print just the text content from the response\n", |
| 81 | + "for task_message in rpc_response.result:\n", |
| 82 | + " content = task_message.content\n", |
| 83 | + " if isinstance(content, TextContent):\n", |
| 84 | + " text = content.content\n", |
| 85 | + " print(text)\n" |
109 | 86 | ] |
110 | 87 | }, |
111 | 88 | { |
112 | 89 | "cell_type": "code", |
113 | | - "execution_count": 6, |
114 | | - "id": "79688331", |
| 90 | + "execution_count": null, |
| 91 | + "id": "4", |
115 | 92 | "metadata": {}, |
116 | | - "outputs": [ |
117 | | - { |
118 | | - "name": "stdout", |
119 | | - "output_type": "stream", |
120 | | - "text": [ |
121 | | - "Hello! I can assist you with a wide range of tasks, including:\n", |
122 | | - "\n", |
123 | | - "1. **Answering questions**: I can provide information on a variety of topics, including history, science, technology, and more.\n", |
124 | | - "2. **Learning and education**: I can help explain concepts, provide summaries, and assist with studying.\n", |
125 | | - "3. **Writing assistance**: I can help you draft, edit, or brainstorm ideas for essays, reports, and creative writing.\n", |
126 | | - "4. **Programming help**: I can assist with coding questions, debugging, and providing explanations of programming concepts.\n", |
127 | | - "5. **Language translation**: I can translate text between several languages.\n", |
128 | | - "6. **General advice**: I can offer suggestions on topics like time management, study techniques, and more.\n", |
129 | | - "\n", |
130 | | - "Feel free to ask me anything specific you need help with!\n" |
131 | | - ] |
132 | | - } |
133 | | - ], |
| 93 | + "outputs": [], |
134 | 94 | "source": [ |
135 | 95 | "# Test streaming response\n", |
136 | | - "import json\n", |
137 | | - "from agentex.types import AgentRpcResponse\n", |
138 | | - "from agentex.types.agent_rpc_result import StreamTaskMessageDelta, StreamTaskMessageFull\n", |
| 96 | + "from agentex.types.task_message_update import StreamTaskMessageDelta, StreamTaskMessageFull\n", |
139 | 97 | "from agentex.types.text_delta import TextDelta\n", |
140 | | - "from agentex.types.task_message_update import TaskMessageUpdate\n", |
141 | 98 | "\n", |
142 | 99 | "\n", |
143 | 100 | "# The result object of message/send will be a TaskMessageUpdate which is a union of the following types:\n", |
|
153 | 110 | "# Whenn processing StreamTaskMessageDelta, if you are expecting more than TextDeltas, such as DataDelta, ToolRequestDelta, or ToolResponseDelta, you can process them as well\n", |
154 | 111 | "# Whenn processing StreamTaskMessageFull, if you are expecting more than TextContent, such as DataContent, ToolRequestContent, or ToolResponseContent, you can process them as well\n", |
155 | 112 | "\n", |
156 | | - "with client.agents.with_streaming_response.rpc_by_name(\n", |
| 113 | + "for agent_rpc_response_chunk in client.agents.send_message_stream(\n", |
157 | 114 | " agent_name=AGENT_NAME,\n", |
158 | | - " method=\"message/send\",\n", |
159 | 115 | " params={\n", |
160 | 116 | " \"content\": {\"type\": \"text\", \"author\": \"user\", \"content\": \"Hello what can you do?\"},\n", |
161 | 117 | " \"stream\": True\n", |
162 | 118 | " }\n", |
163 | | - ") as response:\n", |
164 | | - " for agent_rpc_response_str in response.iter_text():\n", |
165 | | - " chunk_rpc_response = AgentRpcResponse.model_validate(json.loads(agent_rpc_response_str))\n", |
166 | | - " # We know that the result of the message/send when stream is set to True will be a TaskMessageUpdate\n", |
167 | | - " task_message_update = cast(TaskMessageUpdate, chunk_rpc_response.result)\n", |
168 | | - "\n", |
169 | | - " # Print oly the text deltas as they arrive or any full messages\n", |
170 | | - " if isinstance(task_message_update, StreamTaskMessageDelta):\n", |
171 | | - " delta = task_message_update.delta\n", |
172 | | - " if isinstance(delta, TextDelta):\n", |
173 | | - " print(delta.text_delta, end=\"\", flush=True)\n", |
174 | | - " else:\n", |
175 | | - " print(f\"Found non-text {type(task_message)} object in streaming message.\")\n", |
176 | | - " elif isinstance(task_message_update, StreamTaskMessageFull):\n", |
177 | | - " content = task_message_update.content\n", |
178 | | - " if isinstance(content, TextContent):\n", |
179 | | - " print(content.content)\n", |
180 | | - " else:\n", |
181 | | - " print(f\"Found non-text {type(task_message)} object in full message.\")\n" |
| 119 | + "):\n", |
| 120 | + " # We know that the result of the message/send when stream is set to True will be a TaskMessageUpdate\n", |
| 121 | + " task_message_update = agent_rpc_response_chunk.result\n", |
| 122 | + " # Print oly the text deltas as they arrive or any full messages\n", |
| 123 | + " if isinstance(task_message_update, StreamTaskMessageDelta):\n", |
| 124 | + " delta = task_message_update.delta\n", |
| 125 | + " if isinstance(delta, TextDelta):\n", |
| 126 | + " print(delta.text_delta, end=\"\", flush=True)\n", |
| 127 | + " else:\n", |
| 128 | + " print(f\"Found non-text {type(task_message)} object in streaming message.\")\n", |
| 129 | + " elif isinstance(task_message_update, StreamTaskMessageFull):\n", |
| 130 | + " content = task_message_update.content\n", |
| 131 | + " if isinstance(content, TextContent):\n", |
| 132 | + " print(content.content)\n", |
| 133 | + " else:\n", |
| 134 | + " print(f\"Found non-text {type(task_message)} object in full message.\")\n" |
182 | 135 | ] |
183 | 136 | }, |
184 | 137 | { |
185 | 138 | "cell_type": "code", |
186 | 139 | "execution_count": null, |
187 | | - "id": "42689ee4", |
| 140 | + "id": "5", |
188 | 141 | "metadata": {}, |
189 | 142 | "outputs": [], |
190 | 143 | "source": [] |
|
0 commit comments