Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions examples/tutorials/00_sync/000_hello_acp/dev.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "36834357",
"metadata": {},
"outputs": [],
"source": [
"from agentex import Agentex\n",
"\n",
"client = Agentex(base_url=\"http://localhost:5003\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d1c309d6",
"metadata": {},
"outputs": [],
"source": [
"AGENT_NAME = \"s000-hello-acp\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9f6e6ef0",
"metadata": {},
"outputs": [],
"source": [
"# # (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",
"\n",
"# import uuid\n",
"\n",
"# TASK_ID = str(uuid.uuid4())[:8]\n",
"\n",
"# rpc_response = client.agents.rpc_by_name(\n",
"# agent_name=AGENT_NAME,\n",
"# method=\"task/create\",\n",
"# params={\n",
"# \"name\": f\"{TASK_ID}-task\",\n",
"# \"params\": {}\n",
"# }\n",
"# )\n",
"\n",
"# task = rpc_response.result\n",
"# print(task)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b03b0d37",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello! I've received your message. Here's a generic response, but in future tutorials we'll see how you can get me to intelligently respond to your message. This is what I heard you say: Hello what can you do?\n"
]
}
],
"source": [
"# Test non streaming response\n",
"from typing import List, cast\n",
"from agentex.types import TaskMessage, TextContent\n",
"\n",
"# The response is expected to be a list of TaskMessage objects, which is a union of the following types:\n",
"# - TextContent: A message with just text content \n",
"# - DataContent: A message with JSON-serializable data content\n",
"# - ToolRequestContent: A message with a tool request, which contains a JSON-serializable request to call a tool\n",
"# - ToolResponseContent: A message with a tool response, which contains response object from a tool call in its content\n",
"\n",
"# 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",
"\n",
"rpc_response = client.agents.rpc_by_name(\n",
" agent_name=AGENT_NAME,\n",
" method=\"message/send\",\n",
" params={\n",
" \"content\": {\"type\": \"text\", \"author\": \"user\", \"content\": \"Hello what can you do?\"},\n",
" \"stream\": False\n",
" }\n",
")\n",
"\n",
"# # Extract and print just the text content from the response\n",
"# # The response is expected to be a dict with a \"result\" key containing a list of message dicts\n",
"if rpc_response and rpc_response.result:\n",
"\n",
" # We know that the result of the message/send when stream is set to False will be a list of TaskMessage objects\n",
" task_message_list = cast(List[TaskMessage], rpc_response.result)\n",
" for task_message in rpc_response.result:\n",
" if isinstance(task_message, TaskMessage):\n",
" content = task_message.content\n",
" if isinstance(content, TextContent):\n",
" text = content.content\n",
" print(text)\n",
" else:\n",
" print(f\"Found non-text {type(task_message)} object in response.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "79688331",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello! I've received your message. Here's a generic response, but in future tutorials we'll see how you can get me to intelligently respond to your message. This is what I heard you say: Hello what can you do?\n"
]
}
],
"source": [
"# Test streaming response\n",
"import json\n",
"from agentex.types import AgentRpcResponse\n",
"from agentex.types.agent_rpc_result import StreamTaskMessageDelta, StreamTaskMessageFull\n",
"from agentex.types.text_delta import TextDelta\n",
"from agentex.types.task_message_update import TaskMessageUpdate\n",
"\n",
"\n",
"# The result object of message/send will be a TaskMessageUpdate which is a union of the following types:\n",
"# - StreamTaskMessageStart: \n",
"# - An indicator that a streaming message was started, doesn't contain any useful content\n",
"# - StreamTaskMessageDelta: \n",
"# - A delta of a streaming message, contains the text delta to aggregate\n",
"# - StreamTaskMessageDone: \n",
"# - An indicator that a streaming message was done, doesn't contain any useful content\n",
"# - StreamTaskMessageFull: \n",
"# - A non-streaming message, there is nothing to aggregate, since this contains the full message, not deltas\n",
"\n",
"# Whenn processing StreamTaskMessageDelta, if you are expecting more than TextDeltas, such as DataDelta, ToolRequestDelta, or ToolResponseDelta, you can process them as well\n",
"# Whenn processing StreamTaskMessageFull, if you are expecting more than TextContent, such as DataContent, ToolRequestContent, or ToolResponseContent, you can process them as well\n",
"\n",
"with client.agents.with_streaming_response.rpc_by_name(\n",
" agent_name=AGENT_NAME,\n",
" method=\"message/send\",\n",
" params={\n",
" \"content\": {\"type\": \"text\", \"author\": \"user\", \"content\": \"Hello what can you do?\"},\n",
" \"stream\": True\n",
" }\n",
") as response:\n",
" for agent_rpc_response_str in response.iter_text():\n",
" chunk_rpc_response = AgentRpcResponse.model_validate(json.loads(agent_rpc_response_str))\n",
" # We know that the result of the message/send when stream is set to True will be a TaskMessageUpdate\n",
" task_message_update = cast(TaskMessageUpdate, chunk_rpc_response.result)\n",
"\n",
" # Print oly the text deltas as they arrive or any full messages\n",
" if isinstance(task_message_update, StreamTaskMessageDelta):\n",
" delta = task_message_update.delta\n",
" if isinstance(delta, TextDelta):\n",
" print(delta.text_delta, end=\"\", flush=True)\n",
" else:\n",
" print(f\"Found non-text {type(task_message)} object in streaming message.\")\n",
" elif isinstance(task_message_update, StreamTaskMessageFull):\n",
" content = task_message_update.content\n",
" if isinstance(content, TextContent):\n",
" print(content.content)\n",
" else:\n",
" print(f\"Found non-text {type(task_message)} object in full message.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "568673bf",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading