A programming framework for agentic AI
Rename `agnext.core` module to `agnext.base` (#416)
Jack Gerrits3d515a734b27
diff --git a/python/docs/src/cookbook/extracting-results-with-an-agent.md b/python/docs/src/cookbook/extracting-results-with-an-agent.mdindex efd7e7eb9..19549be74 100644--- a/python/docs/src/cookbook/extracting-results-with-an-agent.md+++ b/python/docs/src/cookbook/extracting-results-with-an-agent.md@@ -10,7 +10,7 @@ When considering how to extract results from a multi-agent system, you must alwa```pythonfrom agnext.application import SingleThreadedAgentRuntime-from agnext.core import AgentRuntime, AgentId, CancellationToken+from agnext.base import AgentRuntime, AgentId, CancellationTokenfrom agnext.components import ClosureAgentimport asynciodiff --git a/python/docs/src/cookbook/langgraph-agent.ipynb b/python/docs/src/cookbook/langgraph-agent.ipynbindex 5debab0c0..b46d29630 100644--- a/python/docs/src/cookbook/langgraph-agent.ipynb+++ b/python/docs/src/cookbook/langgraph-agent.ipynb@@ -1,300 +1,300 @@{- "cells": [- {- "cell_type": "markdown",- "metadata": {},- "source": [- "# Using LangGraph-Backed Agent\n",- "\n",- "This example demonstrates how to create an AI agent using LangGraph.\n",- "Based on the example in the LangGraph documentation:\n",- "https://langchain-ai.github.io/langgraph/.\n",- "\n",- "First install the dependencies:"- ]- },- {- "cell_type": "code",- "execution_count": 1,- "metadata": {- "vscode": {- "languageId": "shellscript"- }- },- "outputs": [],- "source": [- "# pip install langgraph langchain-openai azure-identity"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Let's import the modules."- ]- },- {- "cell_type": "code",- "execution_count": 2,- "metadata": {},- "outputs": [],- "source": [- "import os\n",- "from dataclasses import dataclass\n",- "from typing import Any, Callable, List, Literal\n",- "\n",- "from agnext.application import SingleThreadedAgentRuntime\n",- "from agnext.components import RoutedAgent, message_handler\n",- "from agnext.core import AgentId, MessageContext\n",- "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",- "from langchain_core.messages import HumanMessage, SystemMessage\n",- "from langchain_core.tools import tool # pyright: ignore\n",- "from langchain_openai import AzureChatOpenAI, ChatOpenAI\n",- "from langgraph.graph import END, MessagesState, StateGraph\n",- "from langgraph.prebuilt import ToolNode"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Define our message type that will be used to communicate with the agent."- ]- },- {- "cell_type": "code",- "execution_count": 3,- "metadata": {},- "outputs": [],- "source": [- "@dataclass\n",- "class Message:\n",- " content: str"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Define the tools the agent will use."- ]- },- {- "cell_type": "code",- "execution_count": 4,- "metadata": {},- "outputs": [],- "source": [- "@tool # pyright: ignore\n",- "def get_weather(location: str) -> str:\n",- " \"\"\"Call to surf the web.\"\"\"\n",- " # This is a placeholder, but don't tell the LLM that...\n",- " if \"sf\" in location.lower() or \"san francisco\" in location.lower():\n",- " return \"It's 60 degrees and foggy.\"\n",- " return \"It's 90 degrees and sunny.\""- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Define the agent using LangGraph's API."- ]- },- {- "cell_type": "code",- "execution_count": 11,- "metadata": {},- "outputs": [],- "source": [- "class LangGraphToolUseAgent(RoutedAgent):\n",- " def __init__(self, description: str, model: ChatOpenAI, tools: List[Callable[..., Any]]) -> None: # pyright: ignore\n",- " super().__init__(description)\n",- " self._model = model.bind_tools(tools) # pyright: ignore\n",- "\n",- " # Define the function that determines whether to continue or not\n",- " def should_continue(state: MessagesState) -> Literal[\"tools\", END]: # type: ignore\n",- " messages = state[\"messages\"]\n",- " last_message = messages[-1]\n",- " # If the LLM makes a tool call, then we route to the \"tools\" node\n",- " if last_message.tool_calls: # type: ignore\n",- " return \"tools\"\n",- " # Otherwise, we stop (reply to the user)\n",- " return END\n",- "\n",- " # Define the function that calls the model\n",- " async def call_model(state: MessagesState): # type: ignore\n",- " messages = state[\"messages\"]\n",- " response = await self._model.ainvoke(messages)\n",- " # We return a list, because this will get added to the existing list\n",- " return {\"messages\": [response]}\n",- "\n",- " tool_node = ToolNode(tools) # pyright: ignore\n",- "\n",- " # Define a new graph\n",- " self._workflow = StateGraph(MessagesState)\n",- "\n",- " # Define the two nodes we will cycle between\n",- " self._workflow.add_node(\"agent\", call_model) # pyright: ignore\n",- " self._workflow.add_node(\"tools\", tool_node) # pyright: ignore\n",- "\n",- " # Set the entrypoint as `agent`\n",- " # This means that this node is the first one called\n",- " self._workflow.set_entry_point(\"agent\")\n",- "\n",- " # We now add a conditional edge\n",- " self._workflow.add_conditional_edges(\n",- " # First, we define the start node. We use `agent`.\n",- " # This means these are the edges taken after the `agent` node is called.\n",- " \"agent\",\n",- " # Next, we pass in the function that will determine which node is called next.\n",- " should_continue, # type: ignore\n",- " )\n",- "\n",- " # We now add a normal edge from `tools` to `agent`.\n",- " # This means that after `tools` is called, `agent` node is called next.\n",- " self._workflow.add_edge(\"tools\", \"agent\")\n",- "\n",- " # Finally, we compile it!\n",- " # This compiles it into a LangChain Runnable,\n",- " # meaning you can use it as you would any other runnable.\n",- " # Note that we're (optionally) passing the memory when compiling the graph\n",- " self._app = self._workflow.compile()\n",- "\n",- " @message_handler\n",- " async def handle_user_message(self, message: Message, ctx: MessageContext) -> Message:\n",- " # Use the Runnable\n",- " final_state = await self._app.ainvoke(\n",- " {\n",- " \"messages\": [\n",- " SystemMessage(\n",- " content=\"You are a helpful AI assistant. You can use tools to help answer questions.\"\n",- " ),\n",- " HumanMessage(content=message.content),\n",- " ]\n",- " },\n",- " config={\"configurable\": {\"thread_id\": 42}},\n",- " )\n",- " response = Message(content=final_state[\"messages\"][-1].content)\n",- " return response"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Now let's test the agent. First we need to create an agent runtime and\n",- "register the agent, by providing the agent's name and a factory function\n",- "that will create the agent."- ]- },- {- "cell_type": "code",- "execution_count": 12,- "metadata": {},- "outputs": [],- "source": [- "runtime = SingleThreadedAgentRuntime()\n",- "await runtime.register(\n",- " \"langgraph_tool_use_agent\",\n",- " lambda: LangGraphToolUseAgent(\n",- " \"Tool use agent\",\n",- " ChatOpenAI(\n",- " model=\"gpt-4o\",\n",- " # api_key=os.getenv(\"OPENAI_API_KEY\"),\n",- " ),\n",- " # AzureChatOpenAI(\n",- " # azure_deployment=os.getenv(\"AZURE_OPENAI_DEPLOYMENT\"),\n",- " # azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",- " # api_version=os.getenv(\"AZURE_OPENAI_API_VERSION\"),\n",- " # # Using Azure Active Directory authentication.\n",- " # azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential()),\n",- " # # Using API key.\n",- " # # api_key=os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",- " # ),\n",- " [get_weather],\n",- " ),\n",- ")\n",- "agent = AgentId(\"langgraph_tool_use_agent\", key=\"default\")"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Start the agent runtime."- ]- },- {- "cell_type": "code",- "execution_count": 13,- "metadata": {},- "outputs": [],- "source": [- "runtime.start()"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Send a direct message to the agent, and print the response."- ]- },- {- "cell_type": "code",- "execution_count": 14,- "metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "The weather in San Francisco is currently 60 degrees and foggy.\n"- ]- }- ],- "source": [- "response = await runtime.send_message(Message(\"What's the weather in SF?\"), agent)\n",- "print(response.content)"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Stop the agent runtime."- ]- },- {- "cell_type": "code",- "execution_count": 15,- "metadata": {},- "outputs": [],- "source": [- "await runtime.stop()"- ]- }- ],- "metadata": {- "kernelspec": {- "display_name": "agnext",- "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.11.9"- }- },- "nbformat": 4,- "nbformat_minor": 2+ "cells": [+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "# Using LangGraph-Backed Agent\n",+ "\n",+ "This example demonstrates how to create an AI agent using LangGraph.\n",+ "Based on the example in the LangGraph documentation:\n",+ "https://langchain-ai.github.io/langgraph/.\n",+ "\n",+ "First install the dependencies:"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 1,+ "metadata": {+ "vscode": {+ "languageId": "shellscript"+ }+ },+ "outputs": [],+ "source": [+ "# pip install langgraph langchain-openai azure-identity"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Let's import the modules."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 2,+ "metadata": {},+ "outputs": [],+ "source": [+ "import os\n",+ "from dataclasses import dataclass\n",+ "from typing import Any, Callable, List, Literal\n",+ "\n",+ "from agnext.application import SingleThreadedAgentRuntime\n",+ "from agnext.base import AgentId, MessageContext\n",+ "from agnext.components import RoutedAgent, message_handler\n",+ "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",+ "from langchain_core.messages import HumanMessage, SystemMessage\n",+ "from langchain_core.tools import tool # pyright: ignore\n",+ "from langchain_openai import AzureChatOpenAI, ChatOpenAI\n",+ "from langgraph.graph import END, MessagesState, StateGraph\n",+ "from langgraph.prebuilt import ToolNode"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Define our message type that will be used to communicate with the agent."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 3,+ "metadata": {},+ "outputs": [],+ "source": [+ "@dataclass\n",+ "class Message:\n",+ " content: str"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Define the tools the agent will use."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 4,+ "metadata": {},+ "outputs": [],+ "source": [+ "@tool # pyright: ignore\n",+ "def get_weather(location: str) -> str:\n",+ " \"\"\"Call to surf the web.\"\"\"\n",+ " # This is a placeholder, but don't tell the LLM that...\n",+ " if \"sf\" in location.lower() or \"san francisco\" in location.lower():\n",+ " return \"It's 60 degrees and foggy.\"\n",+ " return \"It's 90 degrees and sunny.\""+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Define the agent using LangGraph's API."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 11,+ "metadata": {},+ "outputs": [],+ "source": [+ "class LangGraphToolUseAgent(RoutedAgent):\n",+ " def __init__(self, description: str, model: ChatOpenAI, tools: List[Callable[..., Any]]) -> None: # pyright: ignore\n",+ " super().__init__(description)\n",+ " self._model = model.bind_tools(tools) # pyright: ignore\n",+ "\n",+ " # Define the function that determines whether to continue or not\n",+ " def should_continue(state: MessagesState) -> Literal[\"tools\", END]: # type: ignore\n",+ " messages = state[\"messages\"]\n",+ " last_message = messages[-1]\n",+ " # If the LLM makes a tool call, then we route to the \"tools\" node\n",+ " if last_message.tool_calls: # type: ignore\n",+ " return \"tools\"\n",+ " # Otherwise, we stop (reply to the user)\n",+ " return END\n",+ "\n",+ " # Define the function that calls the model\n",+ " async def call_model(state: MessagesState): # type: ignore\n",+ " messages = state[\"messages\"]\n",+ " response = await self._model.ainvoke(messages)\n",+ " # We return a list, because this will get added to the existing list\n",+ " return {\"messages\": [response]}\n",+ "\n",+ " tool_node = ToolNode(tools) # pyright: ignore\n",+ "\n",+ " # Define a new graph\n",+ " self._workflow = StateGraph(MessagesState)\n",+ "\n",+ " # Define the two nodes we will cycle between\n",+ " self._workflow.add_node(\"agent\", call_model) # pyright: ignore\n",+ " self._workflow.add_node(\"tools\", tool_node) # pyright: ignore\n",+ "\n",+ " # Set the entrypoint as `agent`\n",+ " # This means that this node is the first one called\n",+ " self._workflow.set_entry_point(\"agent\")\n",+ "\n",+ " # We now add a conditional edge\n",+ " self._workflow.add_conditional_edges(\n",+ " # First, we define the start node. We use `agent`.\n",+ " # This means these are the edges taken after the `agent` node is called.\n",+ " \"agent\",\n",+ " # Next, we pass in the function that will determine which node is called next.\n",+ " should_continue, # type: ignore\n",+ " )\n",+ "\n",+ " # We now add a normal edge from `tools` to `agent`.\n",+ " # This means that after `tools` is called, `agent` node is called next.\n",+ " self._workflow.add_edge(\"tools\", \"agent\")\n",+ "\n",+ " # Finally, we compile it!\n",+ " # This compiles it into a LangChain Runnable,\n",+ " # meaning you can use it as you would any other runnable.\n",+ " # Note that we're (optionally) passing the memory when compiling the graph\n",+ " self._app = self._workflow.compile()\n",+ "\n",+ " @message_handler\n",+ " async def handle_user_message(self, message: Message, ctx: MessageContext) -> Message:\n",+ " # Use the Runnable\n",+ " final_state = await self._app.ainvoke(\n",+ " {\n",+ " \"messages\": [\n",+ " SystemMessage(\n",+ " content=\"You are a helpful AI assistant. You can use tools to help answer questions.\"\n",+ " ),\n",+ " HumanMessage(content=message.content),\n",+ " ]\n",+ " },\n",+ " config={\"configurable\": {\"thread_id\": 42}},\n",+ " )\n",+ " response = Message(content=final_state[\"messages\"][-1].content)\n",+ " return response"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Now let's test the agent. First we need to create an agent runtime and\n",+ "register the agent, by providing the agent's name and a factory function\n",+ "that will create the agent."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 12,+ "metadata": {},+ "outputs": [],+ "source": [+ "runtime = SingleThreadedAgentRuntime()\n",+ "await runtime.register(\n",+ " \"langgraph_tool_use_agent\",\n",+ " lambda: LangGraphToolUseAgent(\n",+ " \"Tool use agent\",\n",+ " ChatOpenAI(\n",+ " model=\"gpt-4o\",\n",+ " # api_key=os.getenv(\"OPENAI_API_KEY\"),\n",+ " ),\n",+ " # AzureChatOpenAI(\n",+ " # azure_deployment=os.getenv(\"AZURE_OPENAI_DEPLOYMENT\"),\n",+ " # azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",+ " # api_version=os.getenv(\"AZURE_OPENAI_API_VERSION\"),\n",+ " # # Using Azure Active Directory authentication.\n",+ " # azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential()),\n",+ " # # Using API key.\n",+ " # # api_key=os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",+ " # ),\n",+ " [get_weather],\n",+ " ),\n",+ ")\n",+ "agent = AgentId(\"langgraph_tool_use_agent\", key=\"default\")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Start the agent runtime."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 13,+ "metadata": {},+ "outputs": [],+ "source": [+ "runtime.start()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Send a direct message to the agent, and print the response."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 14,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "The weather in San Francisco is currently 60 degrees and foggy.\n"+ ]+ }+ ],+ "source": [+ "response = await runtime.send_message(Message(\"What's the weather in SF?\"), agent)\n",+ "print(response.content)"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Stop the agent runtime."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 15,+ "metadata": {},+ "outputs": [],+ "source": [+ "await runtime.stop()"+ ]+ }+ ],+ "metadata": {+ "kernelspec": {+ "display_name": "agnext",+ "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.11.9"+ }+ },+ "nbformat": 4,+ "nbformat_minor": 2}diff --git a/python/docs/src/cookbook/llamaindex-agent.ipynb b/python/docs/src/cookbook/llamaindex-agent.ipynbindex 2941f5fc6..177f30f3e 100644--- a/python/docs/src/cookbook/llamaindex-agent.ipynb+++ b/python/docs/src/cookbook/llamaindex-agent.ipynb@@ -1,533 +1,533 @@{- "cells": [- {- "cell_type": "markdown",- "metadata": {},- "source": [- "# Using LlamaIndex-Backed Agent\n",- "\n",- "This example demonstrates how to create an AI agent using LlamaIndex.\n",- "\n",- "First install the dependencies:"- ]- },- {- "cell_type": "code",- "execution_count": 3,- "metadata": {- "vscode": {- "languageId": "shellscript"- }- },- "outputs": [],- "source": [- "# pip install \"llama-index-readers-web\" \"llama-index-readers-wikipedia\" \"llama-index-tools-wikipedia\" \"llama-index-embeddings-azure-openai\" \"llama-index-llms-azure-openai\" \"llama-index\" \"azure-identity\""- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Let's import the modules."- ]- },- {- "cell_type": "code",- "execution_count": 8,- "metadata": {},- "outputs": [],- "source": [- "import asyncio\n",- "import os\n",- "from dataclasses import dataclass\n",- "from typing import List, Optional\n",- "\n",- "from agnext.application import SingleThreadedAgentRuntime\n",- "from agnext.components import RoutedAgent, message_handler\n",- "from agnext.core import AgentId, MessageContext\n",- "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",- "from llama_index.core import Settings\n",- "from llama_index.core.agent import ReActAgent\n",- "from llama_index.core.agent.runner.base import AgentRunner\n",- "from llama_index.core.base.llms.types import (\n",- " ChatMessage,\n",- " MessageRole,\n",- ")\n",- "from llama_index.core.chat_engine.types import AgentChatResponse\n",- "from llama_index.core.memory import ChatSummaryMemoryBuffer\n",- "from llama_index.core.memory.types import BaseMemory\n",- "from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding\n",- "from llama_index.embeddings.openai import OpenAIEmbedding\n",- "from llama_index.llms.azure_openai import AzureOpenAI\n",- "from llama_index.llms.openai import OpenAI\n",- "from llama_index.tools.wikipedia import WikipediaToolSpec"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Define our message type that will be used to communicate with the agent."- ]- },- {- "cell_type": "code",- "execution_count": 5,- "metadata": {},- "outputs": [],- "source": [- "@dataclass\n",- "class Resource:\n",- " content: str\n",- " node_id: str\n",- " score: Optional[float] = None\n",- "\n",- "\n",- "@dataclass\n",- "class Message:\n",- " content: str\n",- " sources: Optional[List[Resource]] = None"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Define the agent using LLamaIndex's API."- ]- },- {- "cell_type": "code",- "execution_count": 6,- "metadata": {},- "outputs": [],- "source": [- "class LlamaIndexAgent(RoutedAgent):\n",- " def __init__(self, description: str, llama_index_agent: AgentRunner, memory: BaseMemory | None = None) -> None:\n",- " super().__init__(description)\n",- "\n",- " self._llama_index_agent = llama_index_agent\n",- " self._memory = memory\n",- "\n",- " @message_handler\n",- " async def handle_user_message(self, message: Message, ctx: MessageContext) -> Message:\n",- " # retriever history messages from memory!\n",- " history_messages: List[ChatMessage] = []\n",- "\n",- " response: AgentChatResponse # pyright: ignore\n",- " if self._memory is not None:\n",- " history_messages = self._memory.get(input=message.content)\n",- "\n",- " response = await self._llama_index_agent.achat(message=message.content, history_messages=history_messages) # pyright: ignore\n",- " else:\n",- " response = await self._llama_index_agent.achat(message=message.content) # pyright: ignore\n",- "\n",- " if isinstance(response, AgentChatResponse):\n",- " if self._memory is not None:\n",- " self._memory.put(ChatMessage(role=MessageRole.USER, content=message.content))\n",- " self._memory.put(ChatMessage(role=MessageRole.ASSISTANT, content=response.response))\n",- "\n",- " assert isinstance(response.response, str)\n",- "\n",- " resources: List[Resource] = [\n",- " Resource(content=source_node.get_text(), score=source_node.score, node_id=source_node.id_)\n",- " for source_node in response.source_nodes\n",- " ]\n",- "\n",- " tools: List[Resource] = [\n",- " Resource(content=source.content, node_id=source.tool_name) for source in response.sources\n",- " ]\n",- "\n",- " resources.extend(tools)\n",- " return Message(content=response.response, sources=resources)\n",- " else:\n",- " return Message(content=\"I'm sorry, I don't have an answer for you.\")"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Setting up LlamaIndex."- ]- },- {- "cell_type": "code",- "execution_count": 10,- "metadata": {},- "outputs": [],- "source": [- "# llm = AzureOpenAI(\n",- "# deployment_name=os.getenv(\"AZURE_OPENAI_DEPLOYMENT\"),\n",- "# temperature=0.0,\n",- "# azure_ad_token_provider = get_bearer_token_provider(DefaultAzureCredential()),\n",- "# # api_key=os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",- "# azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",- "# api_version=os.getenv(\"AZURE_OPENAI_API_VERSION\"),\n",- "# )\n",- "llm = OpenAI(\n",- " model=\"gpt-4o\",\n",- " temperature=0.0,\n",- " api_key=os.getenv(\"OPENAI_API_KEY\"),\n",- ")\n",- "\n",- "# embed_model = AzureOpenAIEmbedding(\n",- "# deployment_name=os.getenv(\"AZURE_OPENAI_EMBEDDING_MODEL\"),\n",- "# temperature=0.0,\n",- "# azure_ad_token_provider = get_bearer_token_provider(DefaultAzureCredential()),\n",- "# api_key=os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",- "# azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",- "# api_version=os.getenv(\"AZURE_OPENAI_API_VERSION\"),\n",- "# )\n",- "embed_model = OpenAIEmbedding(\n",- " model=\"text-embedding-ada-002\",\n",- " api_key=os.getenv(\"OPENAI_API_KEY\"),\n",- ")\n",- "\n",- "Settings.llm = llm\n",- "Settings.embed_model = embed_model"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Create the tools."- ]- },- {- "cell_type": "code",- "execution_count": 11,- "metadata": {},- "outputs": [],- "source": [- "wiki_spec = WikipediaToolSpec()\n",- "wikipedia_tool = wiki_spec.to_tool_list()[1]"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Now let's test the agent. First we need to create an agent runtime and\n",- "register the agent, by providing the agent's name and a factory function\n",- "that will create the agent."- ]- },- {- "cell_type": "code",- "execution_count": 19,- "metadata": {},- "outputs": [],- "source": [- "runtime = SingleThreadedAgentRuntime()\n",- "await runtime.register(\n",- " \"chat_agent\",\n",- " lambda: LlamaIndexAgent(\n",- " description=\"Llama Index Agent\",\n",- " llama_index_agent=ReActAgent.from_tools(\n",- " tools=[wikipedia_tool],\n",- " llm=llm,\n",- " max_iterations=8,\n",- " memory=ChatSummaryMemoryBuffer(llm=llm, token_limit=16000),\n",- " verbose=True,\n",- " ),\n",- " ),\n",- ")\n",- "agent = AgentId(\"chat_agent\", \"default\")"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Start the agent runtime."- ]- },- {- "cell_type": "code",- "execution_count": 20,- "metadata": {},- "outputs": [],- "source": [- "runtime.start()"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Send a direct message to the agent, and print the response."- ]- },- {- "cell_type": "code",- "execution_count": 21,- "metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "> Running step 3cbf60cd-9827-4dfe-a3a9-eaff2bed9b75. Step input: What are the best movies from studio Ghibli?\n",- "\u001b[1;3;38;5;200mThought: The current language of the user is: English. I need to use a tool to help me answer the question.\n",- "Action: search_data\n",- "Action Input: {'query': 'best movies from Studio Ghibli'}\n",- "\u001b[0m\u001b[1;3;34mObservation: This is a list of works (films, television, shorts etc.) by the Japanese animation studio Studio Ghibli.\n",- "\n",- "\n",- "== Works ==\n",- "\n",- "\n",- "=== Feature films ===\n",- "\n",- "\n",- "=== Television ===\n",- "\n",- "\n",- "=== Short films ===\n",- "\n",- "These are short films, including those created for television, theatrical release, and the Ghibli Museum. Original video animation releases and music videos (theatrical and television) are also listed in this section.\n",- "\n",- "\n",- "=== Commercials ===\n",- "\n",- "\n",- "=== Video games ===\n",- "\n",- "\n",- "=== Stage productions ===\n",- "Princess Mononoke (2013)\n",- "Nausicaä of the Valley of the Wind (2019)\n",- "Spirited Away (2022)\n",- "My Neighbour Totoro (2022)\n",- "\n",- "\n",- "=== Other works ===\n",- "The works listed here consist of works that do not fall into the above categories. All of these films have been released on DVD or Blu-ray in Japan as part of the Ghibli Gakujutsu Library.\n",- "\n",- "\n",- "=== Exhibitions ===\n",- "A selection of layout designs for animated productions was exhibited in the Studio Ghibli Layout Designs: Understanding the Secrets of Takahata and Miyazaki Animation exhibition tour, which started in the Museum of Contemporary Art Tokyo (July 28, 2008 to September 28, 2008) and subsequently travelled to different museums throughout Japan and Asia, concluding its tour of Japan in the Fukuoka Asian Art Museum (October 12, 2013 to January 26, 2014) and its tour of Asia in the Hong Kong Heritage Museum (May 14, 2014 to August 31, 2014). Between October 4, 2014 and March 1, 2015 the layout designs were exhibited at Art Ludique in Paris. The exhibition catalogues contain annotated reproductions of the displayed artwork.\n",- "\n",- "\n",- "== Related works ==\n",- "These works were not created by Studio Ghibli, but were produced by a variety of studios and people who went on to form or join Studio Ghibli. This includes members of Topcraft that went on to create Studio Ghibli in 1985; works produced by Toei Animation, TMS Entertainment, Nippon Animation or other studios and featuring involvement by Hayao Miyazaki, Isao Takahata or other Ghibli staffers. The list also includes works created in cooperation with Studio Ghibli.\n",- "\n",- "\n",- "=== Pre-Ghibli ===\n",- "\n",- "\n",- "=== Cooperative works ===\n",- "\n",- "\n",- "=== Distributive works ===\n",- "These Western animated films (plus one Japanese film) have been distributed by Studio Ghibli, and now through their label, Ghibli Museum Library.\n",- "\n",- "\n",- "=== Contributive works ===\n",- "Studio Ghibli has made contributions to the following anime series and movies:\n",- "\n",- "\n",- "== Significant achievements ==\n",- "The highest-grossing film of 1989 in Japan: Kiki's Delivery Service\n",- "The highest-grossing film of 1991 in Japan: Only Yesterday\n",- "The highest-grossing film of 1992 in Japan: Porco Rosso\n",- "The highest-grossing film of 1994 in Japan: Pom Poko\n",- "The highest-grossing film of 1995 in Japan; the first Japanese film in Dolby Digital: Whisper of the Heart\n",- "The highest-grossing film of 2002 in Japan: Spirited Away\n",- "The highest-grossing film of 2008 in Japan: Ponyo\n",- "The highest-grossing Japanese film of 2010 in Japan: The Secret World of Arrietty\n",- "The highest-grossing film of 2013 in Japan: The Wind Rises\n",- "The first Studio Ghibli film to use computer graphics: Pom Poko\n",- "The first Miyazaki feature to use computer graphics, and the first Studio Ghibli film to use digital coloring; the first animated feature in Japan's history to gross more than 10 billion yen at the box office and the first animated film ever to win a National Academy Award for Best Picture of the Year: Princess Mononoke\n",- "The first Studio Ghibli film to be shot using a 100% digital process: My Neighbors the Yamadas\n",- "The first Miyazaki feature to be shot using a 100% digital process; the first film to gross $200 million worldwide before opening in North America; the film to finally overtake Titanic at the Japanese box office, becoming the top-grossing film in the history of Japanese cinema: Spirited Away\n",- "The first anime and traditionally animated winner of the Academy Award for Best Animated Feature: Spirited Away at the 75th Academy Awards. They would later win this award for a second time with The Boy and the Heron at the 96th Academy Awards, marking the second time a traditionally animated film won the award.\n",- "\n",- "\n",- "== Notes ==\n",- "\n",- "\n",- "== References ==\n",- "\u001b[0m> Running step 561e3dd3-d98b-4d37-b612-c99387182ee0. Step input: None\n",- "\u001b[1;3;38;5;200mThought: I can answer without using any more tools. I'll use the user's language to answer.\n",- "Answer: Studio Ghibli has produced many acclaimed films over the years. Some of the best and most popular movies from Studio Ghibli include:\n",- "\n",- "1. **Spirited Away (2001)** - Directed by Hayao Miyazaki, this film won the Academy Award for Best Animated Feature and is one of the highest-grossing films in Japanese history.\n",- "2. **My Neighbor Totoro (1988)** - Another classic by Hayao Miyazaki, this film is beloved for its heartwarming story and iconic characters.\n",- "3. **Princess Mononoke (1997)** - This epic fantasy film, also directed by Miyazaki, is known for its complex themes and stunning animation.\n",- "4. **Howl's Moving Castle (2004)** - Based on the novel by Diana Wynne Jones, this film features a magical story and beautiful animation.\n",- "5. **Kiki's Delivery Service (1989)** - A charming coming-of-age story about a young witch starting her own delivery service.\n",- "6. **Grave of the Fireflies (1988)** - Directed by Isao Takahata, this poignant film is a heartbreaking tale of two siblings struggling to survive during World War II.\n",- "7. **Ponyo (2008)** - A delightful and visually stunning film about a young fish-girl who wants to become human.\n",- "8. **The Wind Rises (2013)** - A more mature film by Miyazaki, focusing on the life of an aircraft designer during wartime Japan.\n",- "9. **The Secret World of Arrietty (2010)** - Based on Mary Norton's novel \"The Borrowers,\" this film tells the story of tiny people living secretly in a human house.\n",- "10. **Whisper of the Heart (1995)** - A touching story about a young girl discovering her passion for writing.\n",- "\n",- "These films are celebrated for their storytelling, animation quality, and emotional depth.\n",- "\u001b[0m"- ]- },- {- "name": "stdout",- "output_type": "stream",- "text": [- "Studio Ghibli has produced many acclaimed films over the years. Some of the best and most popular movies from Studio Ghibli include:\n",- "\n",- "1. **Spirited Away (2001)** - Directed by Hayao Miyazaki, this film won the Academy Award for Best Animated Feature and is one of the highest-grossing films in Japanese history.\n",- "2. **My Neighbor Totoro (1988)** - Another classic by Hayao Miyazaki, this film is beloved for its heartwarming story and iconic characters.\n",- "3. **Princess Mononoke (1997)** - This epic fantasy film, also directed by Miyazaki, is known for its complex themes and stunning animation.\n",- "4. **Howl's Moving Castle (2004)** - Based on the novel by Diana Wynne Jones, this film features a magical story and beautiful animation.\n",- "5. **Kiki's Delivery Service (1989)** - A charming coming-of-age story about a young witch starting her own delivery service.\n",- "6. **Grave of the Fireflies (1988)** - Directed by Isao Takahata, this poignant film is a heartbreaking tale of two siblings struggling to survive during World War II.\n",- "7. **Ponyo (2008)** - A delightful and visually stunning film about a young fish-girl who wants to become human.\n",- "8. **The Wind Rises (2013)** - A more mature film by Miyazaki, focusing on the life of an aircraft designer during wartime Japan.\n",- "9. **The Secret World of Arrietty (2010)** - Based on Mary Norton's novel \"The Borrowers,\" this film tells the story of tiny people living secretly in a human house.\n",- "10. **Whisper of the Heart (1995)** - A touching story about a young girl discovering her passion for writing.\n",- "\n",- "These films are celebrated for their storytelling, animation quality, and emotional depth.\n"- ]- }- ],- "source": [- "message = Message(content=\"What are the best movies from studio Ghibli?\")\n",- "response = await runtime.send_message(message, agent)\n",- "assert isinstance(response, Message)\n",- "print(response.content)"- ]- },- {- "cell_type": "code",- "execution_count": 22,- "metadata": {},- "outputs": [- {- "name": "stdout",- "output_type": "stream",- "text": [- "This is a list of works (films, television, shorts etc.) by the Japanese animation studio Studio Ghibli.\n",- "\n",- "\n",- "== Works ==\n",- "\n",- "\n",- "=== Feature films ===\n",- "\n",- "\n",- "=== Television ===\n",- "\n",- "\n",- "=== Short films ===\n",- "\n",- "These are short films, including those created for television, theatrical release, and the Ghibli Museum. Original video animation releases and music videos (theatrical and television) are also listed in this section.\n",- "\n",- "\n",- "=== Commercials ===\n",- "\n",- "\n",- "=== Video games ===\n",- "\n",- "\n",- "=== Stage productions ===\n",- "Princess Mononoke (2013)\n",- "Nausicaä of the Valley of the Wind (2019)\n",- "Spirited Away (2022)\n",- "My Neighbour Totoro (2022)\n",- "\n",- "\n",- "=== Other works ===\n",- "The works listed here consist of works that do not fall into the above categories. All of these films have been released on DVD or Blu-ray in Japan as part of the Ghibli Gakujutsu Library.\n",- "\n",- "\n",- "=== Exhibitions ===\n",- "A selection of layout designs for animated productions was exhibited in the Studio Ghibli Layout Designs: Understanding the Secrets of Takahata and Miyazaki Animation exhibition tour, which started in the Museum of Contemporary Art Tokyo (July 28, 2008 to September 28, 2008) and subsequently travelled to different museums throughout Japan and Asia, concluding its tour of Japan in the Fukuoka Asian Art Museum (October 12, 2013 to January 26, 2014) and its tour of Asia in the Hong Kong Heritage Museum (May 14, 2014 to August 31, 2014). Between October 4, 2014 and March 1, 2015 the layout designs were exhibited at Art Ludique in Paris. The exhibition catalogues contain annotated reproductions of the displayed artwork.\n",- "\n",- "\n",- "== Related works ==\n",- "These works were not created by Studio Ghibli, but were produced by a variety of studios and people who went on to form or join Studio Ghibli. This includes members of Topcraft that went on to create Studio Ghibli in 1985; works produced by Toei Animation, TMS Entertainment, Nippon Animation or other studios and featuring involvement by Hayao Miyazaki, Isao Takahata or other Ghibli staffers. The list also includes works created in cooperation with Studio Ghibli.\n",- "\n",- "\n",- "=== Pre-Ghibli ===\n",- "\n",- "\n",- "=== Cooperative works ===\n",- "\n",- "\n",- "=== Distributive works ===\n",- "These Western animated films (plus one Japanese film) have been distributed by Studio Ghibli, and now through their label, Ghibli Museum Library.\n",- "\n",- "\n",- "=== Contributive works ===\n",- "Studio Ghibli has made contributions to the following anime series and movies:\n",- "\n",- "\n",- "== Significant achievements ==\n",- "The highest-grossing film of 1989 in Japan: Kiki's Delivery Service\n",- "The highest-grossing film of 1991 in Japan: Only Yesterday\n",- "The highest-grossing film of 1992 in Japan: Porco Rosso\n",- "The highest-grossing film of 1994 in Japan: Pom Poko\n",- "The highest-grossing film of 1995 in Japan; the first Japanese film in Dolby Digital: Whisper of the Heart\n",- "The highest-grossing film of 2002 in Japan: Spirited Away\n",- "The highest-grossing film of 2008 in Japan: Ponyo\n",- "The highest-grossing Japanese film of 2010 in Japan: The Secret World of Arrietty\n",- "The highest-grossing film of 2013 in Japan: The Wind Rises\n",- "The first Studio Ghibli film to use computer graphics: Pom Poko\n",- "The first Miyazaki feature to use computer graphics, and the first Studio Ghibli film to use digital coloring; the first animated feature in Japan's history to gross more than 10 billion yen at the box office and the first animated film ever to win a National Academy Award for Best Picture of the Year: Princess Mononoke\n",- "The first Studio Ghibli film to be shot using a 100% digital process: My Neighbors the Yamadas\n",- "The first Miyazaki feature to be shot using a 100% digital process; the first film to gross $200 million worldwide before opening in North America; the film to finally overtake Titanic at the Japanese box office, becoming the top-grossing film in the history of Japanese cinema: Spirited Away\n",- "The first anime and traditionally animated winner of the Academy Award for Best Animated Feature: Spirited Away at the 75th Academy Awards. They would later win this award for a second time with The Boy and the Heron at the 96th Academy Awards, marking the second time a traditionally animated film won the award.\n",- "\n",- "\n",- "== Notes ==\n",- "\n",- "\n",- "== References ==\n"- ]- }- ],- "source": [- "if response.sources is not None:\n",- " for source in response.sources:\n",- " print(source.content)"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Stop the agent runtime."- ]- },- {- "cell_type": "code",- "execution_count": 23,- "metadata": {},- "outputs": [],- "source": [- "await runtime.stop()"- ]- }- ],- "metadata": {- "kernelspec": {- "display_name": "agnext",- "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.11.9"- }+ "cells": [+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "# Using LlamaIndex-Backed Agent\n",+ "\n",+ "This example demonstrates how to create an AI agent using LlamaIndex.\n",+ "\n",+ "First install the dependencies:"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 3,+ "metadata": {+ "vscode": {+ "languageId": "shellscript"+ }+ },+ "outputs": [],+ "source": [+ "# pip install \"llama-index-readers-web\" \"llama-index-readers-wikipedia\" \"llama-index-tools-wikipedia\" \"llama-index-embeddings-azure-openai\" \"llama-index-llms-azure-openai\" \"llama-index\" \"azure-identity\""+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Let's import the modules."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 8,+ "metadata": {},+ "outputs": [],+ "source": [+ "import asyncio\n",+ "import os\n",+ "from dataclasses import dataclass\n",+ "from typing import List, Optional\n",+ "\n",+ "from agnext.application import SingleThreadedAgentRuntime\n",+ "from agnext.base import AgentId, MessageContext\n",+ "from agnext.components import RoutedAgent, message_handler\n",+ "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",+ "from llama_index.core import Settings\n",+ "from llama_index.core.agent import ReActAgent\n",+ "from llama_index.core.agent.runner.base import AgentRunner\n",+ "from llama_index.core.base.llms.types import (\n",+ " ChatMessage,\n",+ " MessageRole,\n",+ ")\n",+ "from llama_index.core.chat_engine.types import AgentChatResponse\n",+ "from llama_index.core.memory import ChatSummaryMemoryBuffer\n",+ "from llama_index.core.memory.types import BaseMemory\n",+ "from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding\n",+ "from llama_index.embeddings.openai import OpenAIEmbedding\n",+ "from llama_index.llms.azure_openai import AzureOpenAI\n",+ "from llama_index.llms.openai import OpenAI\n",+ "from llama_index.tools.wikipedia import WikipediaToolSpec"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Define our message type that will be used to communicate with the agent."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 5,+ "metadata": {},+ "outputs": [],+ "source": [+ "@dataclass\n",+ "class Resource:\n",+ " content: str\n",+ " node_id: str\n",+ " score: Optional[float] = None\n",+ "\n",+ "\n",+ "@dataclass\n",+ "class Message:\n",+ " content: str\n",+ " sources: Optional[List[Resource]] = None"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Define the agent using LLamaIndex's API."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 6,+ "metadata": {},+ "outputs": [],+ "source": [+ "class LlamaIndexAgent(RoutedAgent):\n",+ " def __init__(self, description: str, llama_index_agent: AgentRunner, memory: BaseMemory | None = None) -> None:\n",+ " super().__init__(description)\n",+ "\n",+ " self._llama_index_agent = llama_index_agent\n",+ " self._memory = memory\n",+ "\n",+ " @message_handler\n",+ " async def handle_user_message(self, message: Message, ctx: MessageContext) -> Message:\n",+ " # retriever history messages from memory!\n",+ " history_messages: List[ChatMessage] = []\n",+ "\n",+ " response: AgentChatResponse # pyright: ignore\n",+ " if self._memory is not None:\n",+ " history_messages = self._memory.get(input=message.content)\n",+ "\n",+ " response = await self._llama_index_agent.achat(message=message.content, history_messages=history_messages) # pyright: ignore\n",+ " else:\n",+ " response = await self._llama_index_agent.achat(message=message.content) # pyright: ignore\n",+ "\n",+ " if isinstance(response, AgentChatResponse):\n",+ " if self._memory is not None:\n",+ " self._memory.put(ChatMessage(role=MessageRole.USER, content=message.content))\n",+ " self._memory.put(ChatMessage(role=MessageRole.ASSISTANT, content=response.response))\n",+ "\n",+ " assert isinstance(response.response, str)\n",+ "\n",+ " resources: List[Resource] = [\n",+ " Resource(content=source_node.get_text(), score=source_node.score, node_id=source_node.id_)\n",+ " for source_node in response.source_nodes\n",+ " ]\n",+ "\n",+ " tools: List[Resource] = [\n",+ " Resource(content=source.content, node_id=source.tool_name) for source in response.sources\n",+ " ]\n",+ "\n",+ " resources.extend(tools)\n",+ " return Message(content=response.response, sources=resources)\n",+ " else:\n",+ " return Message(content=\"I'm sorry, I don't have an answer for you.\")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Setting up LlamaIndex."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 10,+ "metadata": {},+ "outputs": [],+ "source": [+ "# llm = AzureOpenAI(\n",+ "# deployment_name=os.getenv(\"AZURE_OPENAI_DEPLOYMENT\"),\n",+ "# temperature=0.0,\n",+ "# azure_ad_token_provider = get_bearer_token_provider(DefaultAzureCredential()),\n",+ "# # api_key=os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",+ "# azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",+ "# api_version=os.getenv(\"AZURE_OPENAI_API_VERSION\"),\n",+ "# )\n",+ "llm = OpenAI(\n",+ " model=\"gpt-4o\",\n",+ " temperature=0.0,\n",+ " api_key=os.getenv(\"OPENAI_API_KEY\"),\n",+ ")\n",+ "\n",+ "# embed_model = AzureOpenAIEmbedding(\n",+ "# deployment_name=os.getenv(\"AZURE_OPENAI_EMBEDDING_MODEL\"),\n",+ "# temperature=0.0,\n",+ "# azure_ad_token_provider = get_bearer_token_provider(DefaultAzureCredential()),\n",+ "# api_key=os.getenv(\"AZURE_OPENAI_API_KEY\"),\n",+ "# azure_endpoint=os.getenv(\"AZURE_OPENAI_ENDPOINT\"),\n",+ "# api_version=os.getenv(\"AZURE_OPENAI_API_VERSION\"),\n",+ "# )\n",+ "embed_model = OpenAIEmbedding(\n",+ " model=\"text-embedding-ada-002\",\n",+ " api_key=os.getenv(\"OPENAI_API_KEY\"),\n",+ ")\n",+ "\n",+ "Settings.llm = llm\n",+ "Settings.embed_model = embed_model"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Create the tools."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 11,+ "metadata": {},+ "outputs": [],+ "source": [+ "wiki_spec = WikipediaToolSpec()\n",+ "wikipedia_tool = wiki_spec.to_tool_list()[1]"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Now let's test the agent. First we need to create an agent runtime and\n",+ "register the agent, by providing the agent's name and a factory function\n",+ "that will create the agent."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 19,+ "metadata": {},+ "outputs": [],+ "source": [+ "runtime = SingleThreadedAgentRuntime()\n",+ "await runtime.register(\n",+ " \"chat_agent\",\n",+ " lambda: LlamaIndexAgent(\n",+ " description=\"Llama Index Agent\",\n",+ " llama_index_agent=ReActAgent.from_tools(\n",+ " tools=[wikipedia_tool],\n",+ " llm=llm,\n",+ " max_iterations=8,\n",+ " memory=ChatSummaryMemoryBuffer(llm=llm, token_limit=16000),\n",+ " verbose=True,\n",+ " ),\n",+ " ),\n",+ ")\n",+ "agent = AgentId(\"chat_agent\", \"default\")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Start the agent runtime."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 20,+ "metadata": {},+ "outputs": [],+ "source": [+ "runtime.start()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Send a direct message to the agent, and print the response."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 21,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "> Running step 3cbf60cd-9827-4dfe-a3a9-eaff2bed9b75. Step input: What are the best movies from studio Ghibli?\n",+ "\u001b[1;3;38;5;200mThought: The current language of the user is: English. I need to use a tool to help me answer the question.\n",+ "Action: search_data\n",+ "Action Input: {'query': 'best movies from Studio Ghibli'}\n",+ "\u001b[0m\u001b[1;3;34mObservation: This is a list of works (films, television, shorts etc.) by the Japanese animation studio Studio Ghibli.\n",+ "\n",+ "\n",+ "== Works ==\n",+ "\n",+ "\n",+ "=== Feature films ===\n",+ "\n",+ "\n",+ "=== Television ===\n",+ "\n",+ "\n",+ "=== Short films ===\n",+ "\n",+ "These are short films, including those created for television, theatrical release, and the Ghibli Museum. Original video animation releases and music videos (theatrical and television) are also listed in this section.\n",+ "\n",+ "\n",+ "=== Commercials ===\n",+ "\n",+ "\n",+ "=== Video games ===\n",+ "\n",+ "\n",+ "=== Stage productions ===\n",+ "Princess Mononoke (2013)\n",+ "Nausicaä of the Valley of the Wind (2019)\n",+ "Spirited Away (2022)\n",+ "My Neighbour Totoro (2022)\n",+ "\n",+ "\n",+ "=== Other works ===\n",+ "The works listed here consist of works that do not fall into the above categories. All of these films have been released on DVD or Blu-ray in Japan as part of the Ghibli Gakujutsu Library.\n",+ "\n",+ "\n",+ "=== Exhibitions ===\n",+ "A selection of layout designs for animated productions was exhibited in the Studio Ghibli Layout Designs: Understanding the Secrets of Takahata and Miyazaki Animation exhibition tour, which started in the Museum of Contemporary Art Tokyo (July 28, 2008 to September 28, 2008) and subsequently travelled to different museums throughout Japan and Asia, concluding its tour of Japan in the Fukuoka Asian Art Museum (October 12, 2013 to January 26, 2014) and its tour of Asia in the Hong Kong Heritage Museum (May 14, 2014 to August 31, 2014). Between October 4, 2014 and March 1, 2015 the layout designs were exhibited at Art Ludique in Paris. The exhibition catalogues contain annotated reproductions of the displayed artwork.\n",+ "\n",+ "\n",+ "== Related works ==\n",+ "These works were not created by Studio Ghibli, but were produced by a variety of studios and people who went on to form or join Studio Ghibli. This includes members of Topcraft that went on to create Studio Ghibli in 1985; works produced by Toei Animation, TMS Entertainment, Nippon Animation or other studios and featuring involvement by Hayao Miyazaki, Isao Takahata or other Ghibli staffers. The list also includes works created in cooperation with Studio Ghibli.\n",+ "\n",+ "\n",+ "=== Pre-Ghibli ===\n",+ "\n",+ "\n",+ "=== Cooperative works ===\n",+ "\n",+ "\n",+ "=== Distributive works ===\n",+ "These Western animated films (plus one Japanese film) have been distributed by Studio Ghibli, and now through their label, Ghibli Museum Library.\n",+ "\n",+ "\n",+ "=== Contributive works ===\n",+ "Studio Ghibli has made contributions to the following anime series and movies:\n",+ "\n",+ "\n",+ "== Significant achievements ==\n",+ "The highest-grossing film of 1989 in Japan: Kiki's Delivery Service\n",+ "The highest-grossing film of 1991 in Japan: Only Yesterday\n",+ "The highest-grossing film of 1992 in Japan: Porco Rosso\n",+ "The highest-grossing film of 1994 in Japan: Pom Poko\n",+ "The highest-grossing film of 1995 in Japan; the first Japanese film in Dolby Digital: Whisper of the Heart\n",+ "The highest-grossing film of 2002 in Japan: Spirited Away\n",+ "The highest-grossing film of 2008 in Japan: Ponyo\n",+ "The highest-grossing Japanese film of 2010 in Japan: The Secret World of Arrietty\n",+ "The highest-grossing film of 2013 in Japan: The Wind Rises\n",+ "The first Studio Ghibli film to use computer graphics: Pom Poko\n",+ "The first Miyazaki feature to use computer graphics, and the first Studio Ghibli film to use digital coloring; the first animated feature in Japan's history to gross more than 10 billion yen at the box office and the first animated film ever to win a National Academy Award for Best Picture of the Year: Princess Mononoke\n",+ "The first Studio Ghibli film to be shot using a 100% digital process: My Neighbors the Yamadas\n",+ "The first Miyazaki feature to be shot using a 100% digital process; the first film to gross $200 million worldwide before opening in North America; the film to finally overtake Titanic at the Japanese box office, becoming the top-grossing film in the history of Japanese cinema: Spirited Away\n",+ "The first anime and traditionally animated winner of the Academy Award for Best Animated Feature: Spirited Away at the 75th Academy Awards. They would later win this award for a second time with The Boy and the Heron at the 96th Academy Awards, marking the second time a traditionally animated film won the award.\n",+ "\n",+ "\n",+ "== Notes ==\n",+ "\n",+ "\n",+ "== References ==\n",+ "\u001b[0m> Running step 561e3dd3-d98b-4d37-b612-c99387182ee0. Step input: None\n",+ "\u001b[1;3;38;5;200mThought: I can answer without using any more tools. I'll use the user's language to answer.\n",+ "Answer: Studio Ghibli has produced many acclaimed films over the years. Some of the best and most popular movies from Studio Ghibli include:\n",+ "\n",+ "1. **Spirited Away (2001)** - Directed by Hayao Miyazaki, this film won the Academy Award for Best Animated Feature and is one of the highest-grossing films in Japanese history.\n",+ "2. **My Neighbor Totoro (1988)** - Another classic by Hayao Miyazaki, this film is beloved for its heartwarming story and iconic characters.\n",+ "3. **Princess Mononoke (1997)** - This epic fantasy film, also directed by Miyazaki, is known for its complex themes and stunning animation.\n",+ "4. **Howl's Moving Castle (2004)** - Based on the novel by Diana Wynne Jones, this film features a magical story and beautiful animation.\n",+ "5. **Kiki's Delivery Service (1989)** - A charming coming-of-age story about a young witch starting her own delivery service.\n",+ "6. **Grave of the Fireflies (1988)** - Directed by Isao Takahata, this poignant film is a heartbreaking tale of two siblings struggling to survive during World War II.\n",+ "7. **Ponyo (2008)** - A delightful and visually stunning film about a young fish-girl who wants to become human.\n",+ "8. **The Wind Rises (2013)** - A more mature film by Miyazaki, focusing on the life of an aircraft designer during wartime Japan.\n",+ "9. **The Secret World of Arrietty (2010)** - Based on Mary Norton's novel \"The Borrowers,\" this film tells the story of tiny people living secretly in a human house.\n",+ "10. **Whisper of the Heart (1995)** - A touching story about a young girl discovering her passion for writing.\n",+ "\n",+ "These films are celebrated for their storytelling, animation quality, and emotional depth.\n",+ "\u001b[0m"+ ]},- "nbformat": 4,- "nbformat_minor": 2+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Studio Ghibli has produced many acclaimed films over the years. Some of the best and most popular movies from Studio Ghibli include:\n",+ "\n",+ "1. **Spirited Away (2001)** - Directed by Hayao Miyazaki, this film won the Academy Award for Best Animated Feature and is one of the highest-grossing films in Japanese history.\n",+ "2. **My Neighbor Totoro (1988)** - Another classic by Hayao Miyazaki, this film is beloved for its heartwarming story and iconic characters.\n",+ "3. **Princess Mononoke (1997)** - This epic fantasy film, also directed by Miyazaki, is known for its complex themes and stunning animation.\n",+ "4. **Howl's Moving Castle (2004)** - Based on the novel by Diana Wynne Jones, this film features a magical story and beautiful animation.\n",+ "5. **Kiki's Delivery Service (1989)** - A charming coming-of-age story about a young witch starting her own delivery service.\n",+ "6. **Grave of the Fireflies (1988)** - Directed by Isao Takahata, this poignant film is a heartbreaking tale of two siblings struggling to survive during World War II.\n",+ "7. **Ponyo (2008)** - A delightful and visually stunning film about a young fish-girl who wants to become human.\n",+ "8. **The Wind Rises (2013)** - A more mature film by Miyazaki, focusing on the life of an aircraft designer during wartime Japan.\n",+ "9. **The Secret World of Arrietty (2010)** - Based on Mary Norton's novel \"The Borrowers,\" this film tells the story of tiny people living secretly in a human house.\n",+ "10. **Whisper of the Heart (1995)** - A touching story about a young girl discovering her passion for writing.\n",+ "\n",+ "These films are celebrated for their storytelling, animation quality, and emotional depth.\n"+ ]+ }+ ],+ "source": [+ "message = Message(content=\"What are the best movies from studio Ghibli?\")\n",+ "response = await runtime.send_message(message, agent)\n",+ "assert isinstance(response, Message)\n",+ "print(response.content)"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 22,+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "This is a list of works (films, television, shorts etc.) by the Japanese animation studio Studio Ghibli.\n",+ "\n",+ "\n",+ "== Works ==\n",+ "\n",+ "\n",+ "=== Feature films ===\n",+ "\n",+ "\n",+ "=== Television ===\n",+ "\n",+ "\n",+ "=== Short films ===\n",+ "\n",+ "These are short films, including those created for television, theatrical release, and the Ghibli Museum. Original video animation releases and music videos (theatrical and television) are also listed in this section.\n",+ "\n",+ "\n",+ "=== Commercials ===\n",+ "\n",+ "\n",+ "=== Video games ===\n",+ "\n",+ "\n",+ "=== Stage productions ===\n",+ "Princess Mononoke (2013)\n",+ "Nausicaä of the Valley of the Wind (2019)\n",+ "Spirited Away (2022)\n",+ "My Neighbour Totoro (2022)\n",+ "\n",+ "\n",+ "=== Other works ===\n",+ "The works listed here consist of works that do not fall into the above categories. All of these films have been released on DVD or Blu-ray in Japan as part of the Ghibli Gakujutsu Library.\n",+ "\n",+ "\n",+ "=== Exhibitions ===\n",+ "A selection of layout designs for animated productions was exhibited in the Studio Ghibli Layout Designs: Understanding the Secrets of Takahata and Miyazaki Animation exhibition tour, which started in the Museum of Contemporary Art Tokyo (July 28, 2008 to September 28, 2008) and subsequently travelled to different museums throughout Japan and Asia, concluding its tour of Japan in the Fukuoka Asian Art Museum (October 12, 2013 to January 26, 2014) and its tour of Asia in the Hong Kong Heritage Museum (May 14, 2014 to August 31, 2014). Between October 4, 2014 and March 1, 2015 the layout designs were exhibited at Art Ludique in Paris. The exhibition catalogues contain annotated reproductions of the displayed artwork.\n",+ "\n",+ "\n",+ "== Related works ==\n",+ "These works were not created by Studio Ghibli, but were produced by a variety of studios and people who went on to form or join Studio Ghibli. This includes members of Topcraft that went on to create Studio Ghibli in 1985; works produced by Toei Animation, TMS Entertainment, Nippon Animation or other studios and featuring involvement by Hayao Miyazaki, Isao Takahata or other Ghibli staffers. The list also includes works created in cooperation with Studio Ghibli.\n",+ "\n",+ "\n",+ "=== Pre-Ghibli ===\n",+ "\n",+ "\n",+ "=== Cooperative works ===\n",+ "\n",+ "\n",+ "=== Distributive works ===\n",+ "These Western animated films (plus one Japanese film) have been distributed by Studio Ghibli, and now through their label, Ghibli Museum Library.\n",+ "\n",+ "\n",+ "=== Contributive works ===\n",+ "Studio Ghibli has made contributions to the following anime series and movies:\n",+ "\n",+ "\n",+ "== Significant achievements ==\n",+ "The highest-grossing film of 1989 in Japan: Kiki's Delivery Service\n",+ "The highest-grossing film of 1991 in Japan: Only Yesterday\n",+ "The highest-grossing film of 1992 in Japan: Porco Rosso\n",+ "The highest-grossing film of 1994 in Japan: Pom Poko\n",+ "The highest-grossing film of 1995 in Japan; the first Japanese film in Dolby Digital: Whisper of the Heart\n",+ "The highest-grossing film of 2002 in Japan: Spirited Away\n",+ "The highest-grossing film of 2008 in Japan: Ponyo\n",+ "The highest-grossing Japanese film of 2010 in Japan: The Secret World of Arrietty\n",+ "The highest-grossing film of 2013 in Japan: The Wind Rises\n",+ "The first Studio Ghibli film to use computer graphics: Pom Poko\n",+ "The first Miyazaki feature to use computer graphics, and the first Studio Ghibli film to use digital coloring; the first animated feature in Japan's history to gross more than 10 billion yen at the box office and the first animated film ever to win a National Academy Award for Best Picture of the Year: Princess Mononoke\n",+ "The first Studio Ghibli film to be shot using a 100% digital process: My Neighbors the Yamadas\n",+ "The first Miyazaki feature to be shot using a 100% digital process; the first film to gross $200 million worldwide before opening in North America; the film to finally overtake Titanic at the Japanese box office, becoming the top-grossing film in the history of Japanese cinema: Spirited Away\n",+ "The first anime and traditionally animated winner of the Academy Award for Best Animated Feature: Spirited Away at the 75th Academy Awards. They would later win this award for a second time with The Boy and the Heron at the 96th Academy Awards, marking the second time a traditionally animated film won the award.\n",+ "\n",+ "\n",+ "== Notes ==\n",+ "\n",+ "\n",+ "== References ==\n"+ ]+ }+ ],+ "source": [+ "if response.sources is not None:\n",+ " for source in response.sources:\n",+ " print(source.content)"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Stop the agent runtime."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 23,+ "metadata": {},+ "outputs": [],+ "source": [+ "await runtime.stop()"+ ]+ }+ ],+ "metadata": {+ "kernelspec": {+ "display_name": "agnext",+ "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.11.9"+ }+ },+ "nbformat": 4,+ "nbformat_minor": 2}diff --git a/python/docs/src/cookbook/openai-assistant-agent.ipynb b/python/docs/src/cookbook/openai-assistant-agent.ipynbindex 9a87ad746..6658aa98a 100644--- a/python/docs/src/cookbook/openai-assistant-agent.ipynb+++ b/python/docs/src/cookbook/openai-assistant-agent.ipynb@@ -1,842 +1,842 @@{- "cells": [- {- "cell_type": "markdown",- "metadata": {},- "source": [- "# OpenAI Assistant Agent\n",- "\n",- "[Open AI Assistant](https://platform.openai.com/docs/assistants/overview) \n",- "and [Azure OpenAI Assistant](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/assistant)\n",- "are server-side APIs for building\n",- "agents.\n",- "They can be used to build agents in AGNext. This cookbook demonstrates how to\n",- "to use OpenAI Assistant to create an agent that can run code and Q&A over document.\n",- "\n",- "## Message Protocol\n",- "\n",- "First, we need to specify the message protocol for the agent backed by \n",- "OpenAI Assistant. The message protocol defines the structure of messages\n",- "handled and published by the agent. \n",- "For illustration, we define a simple\n",- "message protocol of 4 message types: `Message`, `Reset`, `UploadForCodeInterpreter` and `UploadForFileSearch`."- ]- },- {- "cell_type": "code",- "execution_count": 1,- "metadata": {},- "outputs": [],- "source": [- "from dataclasses import dataclass\n",- "\n",- "\n",- "@dataclass\n",- "class TextMessage:\n",- " content: str\n",- " source: str\n",- "\n",- "\n",- "@dataclass\n",- "class Reset:\n",- " pass\n",- "\n",- "\n",- "@dataclass\n",- "class UploadForCodeInterpreter:\n",- " file_path: str\n",- "\n",- "\n",- "@dataclass\n",- "class UploadForFileSearch:\n",- " file_path: str\n",- " vector_store_id: str"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "The `TextMessage` message type is used to communicate with the agent. It has a\n",- "`content` field that contains the message content, and a `source` field\n",- "for the sender. The `Reset` message type is a control message that resets\n",- "the memory of the agent. It has no fields. This is useful when we need to\n",- "start a new conversation with the agent.\n",- "\n",- "The `UploadForCodeInterpreter` message type is used to upload data files\n",- "for the code interpreter and `UploadForFileSearch` message type is used to upload\n",- "documents for file search. Both message types have a `file_path` field that contains\n",- "the local path to the file to be uploaded.\n",- "\n",- "## Defining the Agent\n",- "\n",- "Next, we define the agent class.\n",- "The agent class constructor has the following arguments: `description`,\n",- "`client`, `assistant_id`, `thread_id`, and `assistant_event_handler_factory`.\n",- "The `client` argument is the OpenAI async client object, and the\n",- "`assistant_event_handler_factory` is for creating an assistant event handler\n",- "to handle OpenAI Assistant events.\n",- "This can be used to create streaming output from the assistant.\n",- "\n",- "The agent class has the following message handlers:\n",- "- `handle_message`: Handles the `TextMessage` message type, and sends back the\n",- " response from the assistant.\n",- "- `handle_reset`: Handles the `Reset` message type, and resets the memory\n",- " of the assistant agent.\n",- "- `handle_upload_for_code_interpreter`: Handles the `UploadForCodeInterpreter`\n",- " message type, and uploads the file to the code interpreter.\n",- "- `handle_upload_for_file_search`: Handles the `UploadForFileSearch`\n",- " message type, and uploads the document to the file search.\n",- "\n",- "\n",- "The memory of the assistant is stored inside a thread, which is kept in the\n",- "server side. The thread is referenced by the `thread_id` argument."- ]- },- {- "cell_type": "code",- "execution_count": 2,- "metadata": {},- "outputs": [],- "source": [- "import asyncio\n",- "import os\n",- "from typing import Any, Callable, List\n",- "\n",- "import aiofiles\n",- "from agnext.components import RoutedAgent, message_handler\n",- "from agnext.core import AgentId, MessageContext\n",- "from openai import AsyncAssistantEventHandler, AsyncClient\n",- "from openai.types.beta.thread import ToolResources, ToolResourcesFileSearch\n",- "\n",- "\n",- "class OpenAIAssistantAgent(RoutedAgent):\n",- " \"\"\"An agent implementation that uses the OpenAI Assistant API to generate\n",- " responses.\n",- "\n",- " Args:\n",- " description (str): The description of the agent.\n",- " client (openai.AsyncClient): The client to use for the OpenAI API.\n",- " assistant_id (str): The assistant ID to use for the OpenAI API.\n",- " thread_id (str): The thread ID to use for the OpenAI API.\n",- " assistant_event_handler_factory (Callable[[], AsyncAssistantEventHandler], optional):\n",- " A factory function to create an async assistant event handler. Defaults to None.\n",- " If provided, the agent will use the streaming mode with the event handler.\n",- " If not provided, the agent will use the blocking mode to generate responses.\n",- " \"\"\"\n",- "\n",- " def __init__(\n",- " self,\n",- " description: str,\n",- " client: AsyncClient,\n",- " assistant_id: str,\n",- " thread_id: str,\n",- " assistant_event_handler_factory: Callable[[], AsyncAssistantEventHandler],\n",- " ) -> None:\n",- " super().__init__(description)\n",- " self._client = client\n",- " self._assistant_id = assistant_id\n",- " self._thread_id = thread_id\n",- " self._assistant_event_handler_factory = assistant_event_handler_factory\n",- "\n",- " @message_handler\n",- " async def handle_message(self, message: TextMessage, ctx: MessageContext) -> TextMessage:\n",- " \"\"\"Handle a message. This method adds the message to the thread and publishes a response.\"\"\"\n",- " # Save the message to the thread.\n",- " await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(\n",- " self._client.beta.threads.messages.create(\n",- " thread_id=self._thread_id,\n",- " content=message.content,\n",- " role=\"user\",\n",- " metadata={\"sender\": message.source},\n",- " )\n",- " )\n",- " )\n",- " # Generate a response.\n",- " async with self._client.beta.threads.runs.stream(\n",- " thread_id=self._thread_id,\n",- " assistant_id=self._assistant_id,\n",- " event_handler=self._assistant_event_handler_factory(),\n",- " ) as stream:\n",- " await ctx.cancellation_token.link_future(asyncio.ensure_future(stream.until_done()))\n",- "\n",- " # Get the last message.\n",- " messages = await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(self._client.beta.threads.messages.list(self._thread_id, order=\"desc\", limit=1))\n",- " )\n",- " last_message_content = messages.data[0].content\n",- "\n",- " # Get the text content from the last message.\n",- " text_content = [content for content in last_message_content if content.type == \"text\"]\n",- " if not text_content:\n",- " raise ValueError(f\"Expected text content in the last message: {last_message_content}\")\n",- "\n",- " return TextMessage(content=text_content[0].text.value, source=self.metadata[\"type\"])\n",- "\n",- " @message_handler()\n",- " async def on_reset(self, message: Reset, ctx: MessageContext) -> None:\n",- " \"\"\"Handle a reset message. This method deletes all messages in the thread.\"\"\"\n",- " # Get all messages in this thread.\n",- " all_msgs: List[str] = []\n",- " while True:\n",- " if not all_msgs:\n",- " msgs = await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(self._client.beta.threads.messages.list(self._thread_id))\n",- " )\n",- " else:\n",- " msgs = await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(self._client.beta.threads.messages.list(self._thread_id, after=all_msgs[-1]))\n",- " )\n",- " for msg in msgs.data:\n",- " all_msgs.append(msg.id)\n",- " if not msgs.has_next_page():\n",- " break\n",- " # Delete all the messages.\n",- " for msg_id in all_msgs:\n",- " status = await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(\n",- " self._client.beta.threads.messages.delete(message_id=msg_id, thread_id=self._thread_id)\n",- " )\n",- " )\n",- " assert status.deleted is True\n",- "\n",- " @message_handler()\n",- " async def on_upload_for_code_interpreter(self, message: UploadForCodeInterpreter, ctx: MessageContext) -> None:\n",- " \"\"\"Handle an upload for code interpreter. This method uploads a file and updates the thread with the file.\"\"\"\n",- " # Get the file content.\n",- " async with aiofiles.open(message.file_path, mode=\"rb\") as f:\n",- " file_content = await ctx.cancellation_token.link_future(asyncio.ensure_future(f.read()))\n",- " file_name = os.path.basename(message.file_path)\n",- " # Upload the file.\n",- " file = await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(self._client.files.create(file=(file_name, file_content), purpose=\"assistants\"))\n",- " )\n",- " # Get existing file ids from tool resources.\n",- " thread = await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(self._client.beta.threads.retrieve(thread_id=self._thread_id))\n",- " )\n",- " tool_resources: ToolResources = thread.tool_resources if thread.tool_resources else ToolResources()\n",- " assert tool_resources.code_interpreter is not None\n",- " if tool_resources.code_interpreter.file_ids:\n",- " file_ids = tool_resources.code_interpreter.file_ids\n",- " else:\n",- " file_ids = [file.id]\n",- " # Update thread with new file.\n",- " await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(\n",- " self._client.beta.threads.update(\n",- " thread_id=self._thread_id,\n",- " tool_resources={\n",- " \"code_interpreter\": {\"file_ids\": file_ids},\n",- " },\n",- " )\n",- " )\n",- " )\n",- "\n",- " @message_handler()\n",- " async def on_upload_for_file_search(self, message: UploadForFileSearch, ctx: MessageContext) -> None:\n",- " \"\"\"Handle an upload for file search. This method uploads a file and updates the vector store.\"\"\"\n",- " # Get the file content.\n",- " async with aiofiles.open(message.file_path, mode=\"rb\") as file:\n",- " file_content = await ctx.cancellation_token.link_future(asyncio.ensure_future(file.read()))\n",- " file_name = os.path.basename(message.file_path)\n",- " # Upload the file.\n",- " await ctx.cancellation_token.link_future(\n",- " asyncio.ensure_future(\n",- " self._client.beta.vector_stores.file_batches.upload_and_poll(\n",- " vector_store_id=message.vector_store_id,\n",- " files=[(file_name, file_content)],\n",- " )\n",- " )\n",- " )"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "The agent class is a thin wrapper around the OpenAI Assistant API to implement\n",- "the message protocol. More features, such as multi-modal message handling,\n",- "can be added by extending the message protocol."- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Assistant Event Handler\n",- "\n",- "The assistant event handler provides call-backs for handling Assistant API\n",- "specific events. This is useful for handling streaming output from the assistant\n",- "and further user interface integration."- ]- },- {- "cell_type": "code",- "execution_count": 3,- "metadata": {},- "outputs": [],- "source": [- "from openai import AsyncAssistantEventHandler, AsyncClient\n",- "from openai.types.beta.threads import Message, Text, TextDelta\n",- "from openai.types.beta.threads.runs import RunStep, RunStepDelta\n",- "from typing_extensions import override\n",- "\n",- "\n",- "class EventHandler(AsyncAssistantEventHandler):\n",- " @override\n",- " async def on_text_delta(self, delta: TextDelta, snapshot: Text) -> None:\n",- " print(delta.value, end=\"\", flush=True)\n",- "\n",- " @override\n",- " async def on_run_step_created(self, run_step: RunStep) -> None:\n",- " details = run_step.step_details\n",- " if details.type == \"tool_calls\":\n",- " for tool in details.tool_calls:\n",- " if tool.type == \"code_interpreter\":\n",- " print(\"\\nGenerating code to interpret:\\n\\n```python\")\n",- "\n",- " @override\n",- " async def on_run_step_done(self, run_step: RunStep) -> None:\n",- " details = run_step.step_details\n",- " if details.type == \"tool_calls\":\n",- " for tool in details.tool_calls:\n",- " if tool.type == \"code_interpreter\":\n",- " print(\"\\n```\\nExecuting code...\")\n",- "\n",- " @override\n",- " async def on_run_step_delta(self, delta: RunStepDelta, snapshot: RunStep) -> None:\n",- " details = delta.step_details\n",- " if details is not None and details.type == \"tool_calls\":\n",- " for tool in details.tool_calls or []:\n",- " if tool.type == \"code_interpreter\" and tool.code_interpreter and tool.code_interpreter.input:\n",- " print(tool.code_interpreter.input, end=\"\", flush=True)\n",- "\n",- " @override\n",- " async def on_message_created(self, message: Message) -> None:\n",- " print(f\"{'-'*80}\\nAssistant:\\n\")\n",- "\n",- " @override\n",- " async def on_message_done(self, message: Message) -> None:\n",- " # print a citation to the file searched\n",- " if not message.content:\n",- " return\n",- " content = message.content[0]\n",- " if not content.type == \"text\":\n",- " return\n",- " text_content = content.text\n",- " annotations = text_content.annotations\n",- " citations: List[str] = []\n",- " for index, annotation in enumerate(annotations):\n",- " text_content.value = text_content.value.replace(annotation.text, f\"[{index}]\")\n",- " if file_citation := getattr(annotation, \"file_citation\", None):\n",- " client = AsyncClient()\n",- " cited_file = await client.files.retrieve(file_citation.file_id)\n",- " citations.append(f\"[{index}] {cited_file.filename}\")\n",- " if citations:\n",- " print(\"\\n\".join(citations))"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Using the Agent\n",- "\n",- "First we need to use the `openai` client to create the actual assistant,\n",- "thread, and vector store. Our AGNext agent will be using these."- ]- },- {- "cell_type": "code",- "execution_count": 4,- "metadata": {},- "outputs": [],- "source": [- "import openai\n",- "\n",- "# Create an assistant with code interpreter and file search tools.\n",- "oai_assistant = openai.beta.assistants.create(\n",- " model=\"gpt-4o-mini\",\n",- " description=\"An AI assistant that helps with everyday tasks.\",\n",- " instructions=\"Help the user with their task.\",\n",- " tools=[{\"type\": \"code_interpreter\"}, {\"type\": \"file_search\"}],\n",- ")\n",- "\n",- "# Create a vector store to be used for file search.\n",- "vector_store = openai.beta.vector_stores.create()\n",- "\n",- "# Create a thread which is used as the memory for the assistant.\n",- "thread = openai.beta.threads.create(\n",- " tool_resources={\"file_search\": {\"vector_store_ids\": [vector_store.id]}},\n",- ")"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Then, we create a runtime, and register an agent factory function for this \n",- "agent with the runtime."- ]- },- {- "cell_type": "code",- "execution_count": 5,- "metadata": {},- "outputs": [],- "source": [- "from agnext.application import SingleThreadedAgentRuntime\n",- "\n",- "runtime = SingleThreadedAgentRuntime()\n",- "await runtime.register(\n",- " \"assistant\",\n",- " lambda: OpenAIAssistantAgent(\n",- " description=\"OpenAI Assistant Agent\",\n",- " client=openai.AsyncClient(),\n",- " assistant_id=oai_assistant.id,\n",- " thread_id=thread.id,\n",- " assistant_event_handler_factory=lambda: EventHandler(),\n",- " ),\n",- ")\n",- "agent = AgentId(\"assistant\", \"default\")"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Let's turn on logging to see what's happening under the hood."- ]- },- {- "cell_type": "code",- "execution_count": 6,- "metadata": {},- "outputs": [],- "source": [- "import logging\n",- "\n",- "logging.basicConfig(level=logging.WARNING)\n",- "logging.getLogger(\"agnext\").setLevel(logging.DEBUG)"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Let's send a greeting message to the agent, and see the response streamed back."- ]- },- {- "cell_type": "code",- "execution_count": 7,- "metadata": {},- "outputs": [- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'Hello, how are you today!', 'source': 'user'}\n",- "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"- ]- },- {- "name": "stdout",- "output_type": "stream",- "text": [- "--------------------------------------------------------------------------------\n",- "Assistant:\n",- "\n",- "Hello! I'm here and ready to assist you. How can I help you today?"- ]- },- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': \"Hello! I'm here and ready to assist you. How can I help you today?\", 'source': 'assistant'}\n"- ]- }- ],- "source": [- "runtime.start()\n",- "await runtime.send_message(TextMessage(content=\"Hello, how are you today!\", source=\"user\"), agent)\n",- "await runtime.stop_when_idle()"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Assistant with Code Interpreter\n",- "\n",- "Let's ask some math question to the agent, and see it uses the code interpreter\n",- "to answer the question."- ]- },- {- "cell_type": "code",- "execution_count": 8,- "metadata": {},- "outputs": [- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'What is 1332322 x 123212?', 'source': 'user'}\n",- "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"- ]- },- {- "name": "stdout",- "output_type": "stream",- "text": [- "# Calculating the product of 1332322 and 123212\n",- "result = 1332322 * 123212\n",- "result\n",- "```\n",- "Executing code...\n",- "--------------------------------------------------------------------------------\n",- "Assistant:\n",- "\n",- "The product of 1,332,322 and 123,212 is 164,158,058,264."- ]- },- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': 'The product of 1,332,322 and 123,212 is 164,158,058,264.', 'source': 'assistant'}\n"- ]- }- ],- "source": [- "runtime.start()\n",- "await runtime.send_message(TextMessage(content=\"What is 1332322 x 123212?\", source=\"user\"), agent)\n",- "await runtime.stop_when_idle()"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Let's get some data from Seattle Open Data portal. We will be using the\n",- "[City of Seattle Wage Data](https://data.seattle.gov/City-Business/City-of-Seattle-Wage-Data/2khk-5ukd/).\n",- "Let's download it first."- ]- },- {- "cell_type": "code",- "execution_count": 9,- "metadata": {},- "outputs": [],- "source": [- "import requests\n",- "\n",- "response = requests.get(\"https://data.seattle.gov/resource/2khk-5ukd.csv\")\n",- "with open(\"seattle_city_wages.csv\", \"wb\") as file:\n",- " file.write(response.content)"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Let's send the file to the agent using an `UploadForCodeInterpreter` message."- ]- },- {- "cell_type": "code",- "execution_count": 10,- "metadata": {},- "outputs": [- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Sending message of type UploadForCodeInterpreter to assistant: {'file_path': 'seattle_city_wages.csv'}\n",- "INFO:agnext:Calling message handler for assistant:default with message type UploadForCodeInterpreter sent by Unknown\n",- "INFO:agnext:Resolving response with message type NoneType for recipient None from assistant: None\n"- ]- }- ],- "source": [- "runtime.start()\n",- "await runtime.send_message(UploadForCodeInterpreter(file_path=\"seattle_city_wages.csv\"), agent)\n",- "await runtime.stop_when_idle()"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "We can now ask some questions about the data to the agent."- ]- },- {- "cell_type": "code",- "execution_count": 12,- "metadata": {},- "outputs": [- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'Take a look at the uploaded CSV file.', 'source': 'user'}\n",- "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"- ]- },- {- "name": "stdout",- "output_type": "stream",- "text": [- "import pandas as pd\n",- "\n",- "# Load the uploaded CSV file to examine its contents\n",- "file_path = '/mnt/data/file-oEvRiyGyHc2jZViKyDqL8aoh'\n",- "csv_data = pd.read_csv(file_path)\n",- "\n",- "# Display the first few rows of the dataframe to understand its structure\n",- "csv_data.head()\n",- "```\n",- "Executing code...\n",- "--------------------------------------------------------------------------------\n",- "Assistant:\n",- "\n",- "The uploaded CSV file contains the following columns:\n",- "\n",- "1. **department**: The department in which the individual works.\n",- "2. **last_name**: The last name of the employee.\n",- "3. **first_name**: The first name of the employee.\n",- "4. **job_title**: The job title of the employee.\n",- "5. **hourly_rate**: The hourly rate for the employee's position.\n",- "\n",- "Here are the first few entries from the file:\n",- "\n",- "| department | last_name | first_name | job_title | hourly_rate |\n",- "|--------------------------------|-----------|------------|------------------------------------|-------------|\n",- "| Police Department | Aagard | Lori | Pol Capt-Precinct | 112.70 |\n",- "| Police Department | Aakervik | Dag | Pol Ofcr-Detective | 75.61 |\n",- "| Seattle City Light | Aaltonen | Evan | Pwrline Clear Tree Trimmer | 53.06 |\n",- "| Seattle Public Utilities | Aar | Abdimallik | Civil Engrng Spec,Sr | 64.43 |\n",- "| Seattle Dept of Transportation | Abad | Abigail | Admin Spec II-BU | 37.40 |\n",- "\n",- "If you need any specific analysis or information from this data, please let me know!"- ]- },- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': \"The uploaded CSV file contains the following columns:\\n\\n1. **department**: The department in which the individual works.\\n2. **last_name**: The last name of the employee.\\n3. **first_name**: The first name of the employee.\\n4. **job_title**: The job title of the employee.\\n5. **hourly_rate**: The hourly rate for the employee's position.\\n\\nHere are the first few entries from the file:\\n\\n| department | last_name | first_name | job_title | hourly_rate |\\n|--------------------------------|-----------|------------|------------------------------------|-------------|\\n| Police Department | Aagard | Lori | Pol Capt-Precinct | 112.70 |\\n| Police Department | Aakervik | Dag | Pol Ofcr-Detective | 75.61 |\\n| Seattle City Light | Aaltonen | Evan | Pwrline Clear Tree Trimmer | 53.06 |\\n| Seattle Public Utilities | Aar | Abdimallik | Civil Engrng Spec,Sr | 64.43 |\\n| Seattle Dept of Transportation | Abad | Abigail | Admin Spec II-BU | 37.40 |\\n\\nIf you need any specific analysis or information from this data, please let me know!\", 'source': 'assistant'}\n"- ]- }- ],- "source": [- "runtime.start()\n",- "await runtime.send_message(TextMessage(content=\"Take a look at the uploaded CSV file.\", source=\"user\"), agent)\n",- "await runtime.stop_when_idle()"- ]- },- {- "cell_type": "code",- "execution_count": 13,- "metadata": {},- "outputs": [- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'What are the top-10 salaries?', 'source': 'user'}\n",- "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"- ]- },- {- "name": "stdout",- "output_type": "stream",- "text": [- "# Sorting the data by hourly_rate in descending order and selecting the top 10 salaries\n",- "top_10_salaries = csv_data[['first_name', 'last_name', 'job_title', 'hourly_rate']].sort_values(by='hourly_rate', ascending=False).head(10)\n",- "top_10_salaries.reset_index(drop=True, inplace=True)\n",- "top_10_salaries\n",- "```\n",- "Executing code...\n",- "--------------------------------------------------------------------------------\n",- "Assistant:\n",- "\n",- "Here are the top 10 salaries based on the hourly rates from the CSV file:\n",- "\n",- "| First Name | Last Name | Job Title | Hourly Rate |\n",- "|------------|-----------|------------------------------------|-------------|\n",- "| Eric | Barden | Executive4 | 139.61 |\n",- "| Idris | Beauregard| Executive3 | 115.90 |\n",- "| Lori | Aagard | Pol Capt-Precinct | 112.70 |\n",- "| Krista | Bair | Pol Capt-Precinct | 108.74 |\n",- "| Amy | Bannister | Fire Chief, Dep Adm-80 Hrs | 104.07 |\n",- "| Ginger | Armbruster| Executive2 | 102.42 |\n",- "| William | Andersen | Executive2 | 102.42 |\n",- "| Valarie | Anderson | Executive2 | 102.42 |\n",- "| Paige | Alderete | Executive2 | 102.42 |\n",- "| Kathryn | Aisenberg | Executive2 | 100.65 |\n",- "\n",- "If you need any further details or analysis, let me know!"- ]- },- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': 'Here are the top 10 salaries based on the hourly rates from the CSV file:\\n\\n| First Name | Last Name | Job Title | Hourly Rate |\\n|------------|-----------|------------------------------------|-------------|\\n| Eric | Barden | Executive4 | 139.61 |\\n| Idris | Beauregard| Executive3 | 115.90 |\\n| Lori | Aagard | Pol Capt-Precinct | 112.70 |\\n| Krista | Bair | Pol Capt-Precinct | 108.74 |\\n| Amy | Bannister | Fire Chief, Dep Adm-80 Hrs | 104.07 |\\n| Ginger | Armbruster| Executive2 | 102.42 |\\n| William | Andersen | Executive2 | 102.42 |\\n| Valarie | Anderson | Executive2 | 102.42 |\\n| Paige | Alderete | Executive2 | 102.42 |\\n| Kathryn | Aisenberg | Executive2 | 100.65 |\\n\\nIf you need any further details or analysis, let me know!', 'source': 'assistant'}\n"- ]- }- ],- "source": [- "runtime.start()\n",- "await runtime.send_message(TextMessage(content=\"What are the top-10 salaries?\", source=\"user\"), agent)\n",- "await runtime.stop_when_idle()"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "## Assistant with File Search\n",- "\n",- "Let's try the Q&A over document feature. We first download Wikipedia page\n",- "on the Third Anglo-Afghan War."- ]- },- {- "cell_type": "code",- "execution_count": 14,- "metadata": {},- "outputs": [],- "source": [- "response = requests.get(\"https://en.wikipedia.org/wiki/Third_Anglo-Afghan_War\")\n",- "with open(\"third_anglo_afghan_war.html\", \"wb\") as file:\n",- " file.write(response.content)"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Send the file to the agent using an `UploadForFileSearch` message."- ]- },- {- "cell_type": "code",- "execution_count": 15,- "metadata": {},- "outputs": [- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Sending message of type UploadForFileSearch to assistant: {'file_path': 'third_anglo_afghan_war.html', 'vector_store_id': 'vs_h3xxPbJFnd1iZ9WdjsQwNdrp'}\n",- "INFO:agnext:Calling message handler for assistant:default with message type UploadForFileSearch sent by Unknown\n",- "INFO:agnext:Resolving response with message type NoneType for recipient None from assistant: None\n"- ]- }- ],- "source": [- "runtime.start()\n",- "await runtime.send_message(\n",- " UploadForFileSearch(file_path=\"third_anglo_afghan_war.html\", vector_store_id=vector_store.id), agent\n",- ")\n",- "await runtime.stop_when_idle()"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "Let's ask some questions about the document to the agent. Before asking,\n",- "we reset the agent memory to start a new conversation."- ]- },- {- "cell_type": "code",- "execution_count": 17,- "metadata": {},- "outputs": [- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Sending message of type Reset to assistant: {}\n",- "INFO:agnext:Calling message handler for assistant:default with message type Reset sent by Unknown\n",- "INFO:agnext:Resolving response with message type NoneType for recipient None from assistant: None\n",- "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'When and where was the treaty of Rawalpindi signed? Answer using the document provided.', 'source': 'user'}\n",- "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"- ]- },- {- "name": "stdout",- "output_type": "stream",- "text": [- "--------------------------------------------------------------------------------\n",- "Assistant:\n",- "\n",- "The Treaty of Rawalpindi was signed on **8 August 1919**. The location of the signing was in **Rawalpindi**, which is in present-day Pakistan【6:0†source】."- ]- },- {- "name": "stderr",- "output_type": "stream",- "text": [- "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': 'The Treaty of Rawalpindi was signed on **8 August 1919**. The location of the signing was in **Rawalpindi**, which is in present-day Pakistan【6:0†source】.', 'source': 'assistant'}\n"- ]- },- {- "name": "stdout",- "output_type": "stream",- "text": [- "[0] third_anglo_afghan_war.html\n"- ]- }- ],- "source": [- "runtime.start()\n",- "await runtime.send_message(Reset(), agent)\n",- "await runtime.send_message(\n",- " TextMessage(\n",- " content=\"When and where was the treaty of Rawalpindi signed? Answer using the document provided.\", source=\"user\"\n",- " ),\n",- " agent,\n",- ")\n",- "await runtime.stop_when_idle()"- ]- },- {- "cell_type": "markdown",- "metadata": {},- "source": [- "That's it! We have successfully built an agent backed by OpenAI Assistant."- ]- }- ],- "metadata": {- "kernelspec": {- "display_name": "agnext",- "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.4"- }+ "cells": [+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "# OpenAI Assistant Agent\n",+ "\n",+ "[Open AI Assistant](https://platform.openai.com/docs/assistants/overview) \n",+ "and [Azure OpenAI Assistant](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/assistant)\n",+ "are server-side APIs for building\n",+ "agents.\n",+ "They can be used to build agents in AGNext. This cookbook demonstrates how to\n",+ "to use OpenAI Assistant to create an agent that can run code and Q&A over document.\n",+ "\n",+ "## Message Protocol\n",+ "\n",+ "First, we need to specify the message protocol for the agent backed by \n",+ "OpenAI Assistant. The message protocol defines the structure of messages\n",+ "handled and published by the agent. \n",+ "For illustration, we define a simple\n",+ "message protocol of 4 message types: `Message`, `Reset`, `UploadForCodeInterpreter` and `UploadForFileSearch`."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 1,+ "metadata": {},+ "outputs": [],+ "source": [+ "from dataclasses import dataclass\n",+ "\n",+ "\n",+ "@dataclass\n",+ "class TextMessage:\n",+ " content: str\n",+ " source: str\n",+ "\n",+ "\n",+ "@dataclass\n",+ "class Reset:\n",+ " pass\n",+ "\n",+ "\n",+ "@dataclass\n",+ "class UploadForCodeInterpreter:\n",+ " file_path: str\n",+ "\n",+ "\n",+ "@dataclass\n",+ "class UploadForFileSearch:\n",+ " file_path: str\n",+ " vector_store_id: str"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "The `TextMessage` message type is used to communicate with the agent. It has a\n",+ "`content` field that contains the message content, and a `source` field\n",+ "for the sender. The `Reset` message type is a control message that resets\n",+ "the memory of the agent. It has no fields. This is useful when we need to\n",+ "start a new conversation with the agent.\n",+ "\n",+ "The `UploadForCodeInterpreter` message type is used to upload data files\n",+ "for the code interpreter and `UploadForFileSearch` message type is used to upload\n",+ "documents for file search. Both message types have a `file_path` field that contains\n",+ "the local path to the file to be uploaded.\n",+ "\n",+ "## Defining the Agent\n",+ "\n",+ "Next, we define the agent class.\n",+ "The agent class constructor has the following arguments: `description`,\n",+ "`client`, `assistant_id`, `thread_id`, and `assistant_event_handler_factory`.\n",+ "The `client` argument is the OpenAI async client object, and the\n",+ "`assistant_event_handler_factory` is for creating an assistant event handler\n",+ "to handle OpenAI Assistant events.\n",+ "This can be used to create streaming output from the assistant.\n",+ "\n",+ "The agent class has the following message handlers:\n",+ "- `handle_message`: Handles the `TextMessage` message type, and sends back the\n",+ " response from the assistant.\n",+ "- `handle_reset`: Handles the `Reset` message type, and resets the memory\n",+ " of the assistant agent.\n",+ "- `handle_upload_for_code_interpreter`: Handles the `UploadForCodeInterpreter`\n",+ " message type, and uploads the file to the code interpreter.\n",+ "- `handle_upload_for_file_search`: Handles the `UploadForFileSearch`\n",+ " message type, and uploads the document to the file search.\n",+ "\n",+ "\n",+ "The memory of the assistant is stored inside a thread, which is kept in the\n",+ "server side. The thread is referenced by the `thread_id` argument."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 2,+ "metadata": {},+ "outputs": [],+ "source": [+ "import asyncio\n",+ "import os\n",+ "from typing import Any, Callable, List\n",+ "\n",+ "import aiofiles\n",+ "from agnext.base import AgentId, MessageContext\n",+ "from agnext.components import RoutedAgent, message_handler\n",+ "from openai import AsyncAssistantEventHandler, AsyncClient\n",+ "from openai.types.beta.thread import ToolResources, ToolResourcesFileSearch\n",+ "\n",+ "\n",+ "class OpenAIAssistantAgent(RoutedAgent):\n",+ " \"\"\"An agent implementation that uses the OpenAI Assistant API to generate\n",+ " responses.\n",+ "\n",+ " Args:\n",+ " description (str): The description of the agent.\n",+ " client (openai.AsyncClient): The client to use for the OpenAI API.\n",+ " assistant_id (str): The assistant ID to use for the OpenAI API.\n",+ " thread_id (str): The thread ID to use for the OpenAI API.\n",+ " assistant_event_handler_factory (Callable[[], AsyncAssistantEventHandler], optional):\n",+ " A factory function to create an async assistant event handler. Defaults to None.\n",+ " If provided, the agent will use the streaming mode with the event handler.\n",+ " If not provided, the agent will use the blocking mode to generate responses.\n",+ " \"\"\"\n",+ "\n",+ " def __init__(\n",+ " self,\n",+ " description: str,\n",+ " client: AsyncClient,\n",+ " assistant_id: str,\n",+ " thread_id: str,\n",+ " assistant_event_handler_factory: Callable[[], AsyncAssistantEventHandler],\n",+ " ) -> None:\n",+ " super().__init__(description)\n",+ " self._client = client\n",+ " self._assistant_id = assistant_id\n",+ " self._thread_id = thread_id\n",+ " self._assistant_event_handler_factory = assistant_event_handler_factory\n",+ "\n",+ " @message_handler\n",+ " async def handle_message(self, message: TextMessage, ctx: MessageContext) -> TextMessage:\n",+ " \"\"\"Handle a message. This method adds the message to the thread and publishes a response.\"\"\"\n",+ " # Save the message to the thread.\n",+ " await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(\n",+ " self._client.beta.threads.messages.create(\n",+ " thread_id=self._thread_id,\n",+ " content=message.content,\n",+ " role=\"user\",\n",+ " metadata={\"sender\": message.source},\n",+ " )\n",+ " )\n",+ " )\n",+ " # Generate a response.\n",+ " async with self._client.beta.threads.runs.stream(\n",+ " thread_id=self._thread_id,\n",+ " assistant_id=self._assistant_id,\n",+ " event_handler=self._assistant_event_handler_factory(),\n",+ " ) as stream:\n",+ " await ctx.cancellation_token.link_future(asyncio.ensure_future(stream.until_done()))\n",+ "\n",+ " # Get the last message.\n",+ " messages = await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(self._client.beta.threads.messages.list(self._thread_id, order=\"desc\", limit=1))\n",+ " )\n",+ " last_message_content = messages.data[0].content\n",+ "\n",+ " # Get the text content from the last message.\n",+ " text_content = [content for content in last_message_content if content.type == \"text\"]\n",+ " if not text_content:\n",+ " raise ValueError(f\"Expected text content in the last message: {last_message_content}\")\n",+ "\n",+ " return TextMessage(content=text_content[0].text.value, source=self.metadata[\"type\"])\n",+ "\n",+ " @message_handler()\n",+ " async def on_reset(self, message: Reset, ctx: MessageContext) -> None:\n",+ " \"\"\"Handle a reset message. This method deletes all messages in the thread.\"\"\"\n",+ " # Get all messages in this thread.\n",+ " all_msgs: List[str] = []\n",+ " while True:\n",+ " if not all_msgs:\n",+ " msgs = await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(self._client.beta.threads.messages.list(self._thread_id))\n",+ " )\n",+ " else:\n",+ " msgs = await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(self._client.beta.threads.messages.list(self._thread_id, after=all_msgs[-1]))\n",+ " )\n",+ " for msg in msgs.data:\n",+ " all_msgs.append(msg.id)\n",+ " if not msgs.has_next_page():\n",+ " break\n",+ " # Delete all the messages.\n",+ " for msg_id in all_msgs:\n",+ " status = await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(\n",+ " self._client.beta.threads.messages.delete(message_id=msg_id, thread_id=self._thread_id)\n",+ " )\n",+ " )\n",+ " assert status.deleted is True\n",+ "\n",+ " @message_handler()\n",+ " async def on_upload_for_code_interpreter(self, message: UploadForCodeInterpreter, ctx: MessageContext) -> None:\n",+ " \"\"\"Handle an upload for code interpreter. This method uploads a file and updates the thread with the file.\"\"\"\n",+ " # Get the file content.\n",+ " async with aiofiles.open(message.file_path, mode=\"rb\") as f:\n",+ " file_content = await ctx.cancellation_token.link_future(asyncio.ensure_future(f.read()))\n",+ " file_name = os.path.basename(message.file_path)\n",+ " # Upload the file.\n",+ " file = await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(self._client.files.create(file=(file_name, file_content), purpose=\"assistants\"))\n",+ " )\n",+ " # Get existing file ids from tool resources.\n",+ " thread = await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(self._client.beta.threads.retrieve(thread_id=self._thread_id))\n",+ " )\n",+ " tool_resources: ToolResources = thread.tool_resources if thread.tool_resources else ToolResources()\n",+ " assert tool_resources.code_interpreter is not None\n",+ " if tool_resources.code_interpreter.file_ids:\n",+ " file_ids = tool_resources.code_interpreter.file_ids\n",+ " else:\n",+ " file_ids = [file.id]\n",+ " # Update thread with new file.\n",+ " await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(\n",+ " self._client.beta.threads.update(\n",+ " thread_id=self._thread_id,\n",+ " tool_resources={\n",+ " \"code_interpreter\": {\"file_ids\": file_ids},\n",+ " },\n",+ " )\n",+ " )\n",+ " )\n",+ "\n",+ " @message_handler()\n",+ " async def on_upload_for_file_search(self, message: UploadForFileSearch, ctx: MessageContext) -> None:\n",+ " \"\"\"Handle an upload for file search. This method uploads a file and updates the vector store.\"\"\"\n",+ " # Get the file content.\n",+ " async with aiofiles.open(message.file_path, mode=\"rb\") as file:\n",+ " file_content = await ctx.cancellation_token.link_future(asyncio.ensure_future(file.read()))\n",+ " file_name = os.path.basename(message.file_path)\n",+ " # Upload the file.\n",+ " await ctx.cancellation_token.link_future(\n",+ " asyncio.ensure_future(\n",+ " self._client.beta.vector_stores.file_batches.upload_and_poll(\n",+ " vector_store_id=message.vector_store_id,\n",+ " files=[(file_name, file_content)],\n",+ " )\n",+ " )\n",+ " )"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "The agent class is a thin wrapper around the OpenAI Assistant API to implement\n",+ "the message protocol. More features, such as multi-modal message handling,\n",+ "can be added by extending the message protocol."+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Assistant Event Handler\n",+ "\n",+ "The assistant event handler provides call-backs for handling Assistant API\n",+ "specific events. This is useful for handling streaming output from the assistant\n",+ "and further user interface integration."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 3,+ "metadata": {},+ "outputs": [],+ "source": [+ "from openai import AsyncAssistantEventHandler, AsyncClient\n",+ "from openai.types.beta.threads import Message, Text, TextDelta\n",+ "from openai.types.beta.threads.runs import RunStep, RunStepDelta\n",+ "from typing_extensions import override\n",+ "\n",+ "\n",+ "class EventHandler(AsyncAssistantEventHandler):\n",+ " @override\n",+ " async def on_text_delta(self, delta: TextDelta, snapshot: Text) -> None:\n",+ " print(delta.value, end=\"\", flush=True)\n",+ "\n",+ " @override\n",+ " async def on_run_step_created(self, run_step: RunStep) -> None:\n",+ " details = run_step.step_details\n",+ " if details.type == \"tool_calls\":\n",+ " for tool in details.tool_calls:\n",+ " if tool.type == \"code_interpreter\":\n",+ " print(\"\\nGenerating code to interpret:\\n\\n```python\")\n",+ "\n",+ " @override\n",+ " async def on_run_step_done(self, run_step: RunStep) -> None:\n",+ " details = run_step.step_details\n",+ " if details.type == \"tool_calls\":\n",+ " for tool in details.tool_calls:\n",+ " if tool.type == \"code_interpreter\":\n",+ " print(\"\\n```\\nExecuting code...\")\n",+ "\n",+ " @override\n",+ " async def on_run_step_delta(self, delta: RunStepDelta, snapshot: RunStep) -> None:\n",+ " details = delta.step_details\n",+ " if details is not None and details.type == \"tool_calls\":\n",+ " for tool in details.tool_calls or []:\n",+ " if tool.type == \"code_interpreter\" and tool.code_interpreter and tool.code_interpreter.input:\n",+ " print(tool.code_interpreter.input, end=\"\", flush=True)\n",+ "\n",+ " @override\n",+ " async def on_message_created(self, message: Message) -> None:\n",+ " print(f\"{'-'*80}\\nAssistant:\\n\")\n",+ "\n",+ " @override\n",+ " async def on_message_done(self, message: Message) -> None:\n",+ " # print a citation to the file searched\n",+ " if not message.content:\n",+ " return\n",+ " content = message.content[0]\n",+ " if not content.type == \"text\":\n",+ " return\n",+ " text_content = content.text\n",+ " annotations = text_content.annotations\n",+ " citations: List[str] = []\n",+ " for index, annotation in enumerate(annotations):\n",+ " text_content.value = text_content.value.replace(annotation.text, f\"[{index}]\")\n",+ " if file_citation := getattr(annotation, \"file_citation\", None):\n",+ " client = AsyncClient()\n",+ " cited_file = await client.files.retrieve(file_citation.file_id)\n",+ " citations.append(f\"[{index}] {cited_file.filename}\")\n",+ " if citations:\n",+ " print(\"\\n\".join(citations))"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Using the Agent\n",+ "\n",+ "First we need to use the `openai` client to create the actual assistant,\n",+ "thread, and vector store. Our AGNext agent will be using these."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 4,+ "metadata": {},+ "outputs": [],+ "source": [+ "import openai\n",+ "\n",+ "# Create an assistant with code interpreter and file search tools.\n",+ "oai_assistant = openai.beta.assistants.create(\n",+ " model=\"gpt-4o-mini\",\n",+ " description=\"An AI assistant that helps with everyday tasks.\",\n",+ " instructions=\"Help the user with their task.\",\n",+ " tools=[{\"type\": \"code_interpreter\"}, {\"type\": \"file_search\"}],\n",+ ")\n",+ "\n",+ "# Create a vector store to be used for file search.\n",+ "vector_store = openai.beta.vector_stores.create()\n",+ "\n",+ "# Create a thread which is used as the memory for the assistant.\n",+ "thread = openai.beta.threads.create(\n",+ " tool_resources={\"file_search\": {\"vector_store_ids\": [vector_store.id]}},\n",+ ")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Then, we create a runtime, and register an agent factory function for this \n",+ "agent with the runtime."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 5,+ "metadata": {},+ "outputs": [],+ "source": [+ "from agnext.application import SingleThreadedAgentRuntime\n",+ "\n",+ "runtime = SingleThreadedAgentRuntime()\n",+ "await runtime.register(\n",+ " \"assistant\",\n",+ " lambda: OpenAIAssistantAgent(\n",+ " description=\"OpenAI Assistant Agent\",\n",+ " client=openai.AsyncClient(),\n",+ " assistant_id=oai_assistant.id,\n",+ " thread_id=thread.id,\n",+ " assistant_event_handler_factory=lambda: EventHandler(),\n",+ " ),\n",+ ")\n",+ "agent = AgentId(\"assistant\", \"default\")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Let's turn on logging to see what's happening under the hood."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 6,+ "metadata": {},+ "outputs": [],+ "source": [+ "import logging\n",+ "\n",+ "logging.basicConfig(level=logging.WARNING)\n",+ "logging.getLogger(\"agnext\").setLevel(logging.DEBUG)"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Let's send a greeting message to the agent, and see the response streamed back."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 7,+ "metadata": {},+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'Hello, how are you today!', 'source': 'user'}\n",+ "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"+ ]},- "nbformat": 4,- "nbformat_minor": 2+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "--------------------------------------------------------------------------------\n",+ "Assistant:\n",+ "\n",+ "Hello! I'm here and ready to assist you. How can I help you today?"+ ]+ },+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': \"Hello! I'm here and ready to assist you. How can I help you today?\", 'source': 'assistant'}\n"+ ]+ }+ ],+ "source": [+ "runtime.start()\n",+ "await runtime.send_message(TextMessage(content=\"Hello, how are you today!\", source=\"user\"), agent)\n",+ "await runtime.stop_when_idle()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Assistant with Code Interpreter\n",+ "\n",+ "Let's ask some math question to the agent, and see it uses the code interpreter\n",+ "to answer the question."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 8,+ "metadata": {},+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'What is 1332322 x 123212?', 'source': 'user'}\n",+ "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"+ ]+ },+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "# Calculating the product of 1332322 and 123212\n",+ "result = 1332322 * 123212\n",+ "result\n",+ "```\n",+ "Executing code...\n",+ "--------------------------------------------------------------------------------\n",+ "Assistant:\n",+ "\n",+ "The product of 1,332,322 and 123,212 is 164,158,058,264."+ ]+ },+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': 'The product of 1,332,322 and 123,212 is 164,158,058,264.', 'source': 'assistant'}\n"+ ]+ }+ ],+ "source": [+ "runtime.start()\n",+ "await runtime.send_message(TextMessage(content=\"What is 1332322 x 123212?\", source=\"user\"), agent)\n",+ "await runtime.stop_when_idle()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Let's get some data from Seattle Open Data portal. We will be using the\n",+ "[City of Seattle Wage Data](https://data.seattle.gov/City-Business/City-of-Seattle-Wage-Data/2khk-5ukd/).\n",+ "Let's download it first."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 9,+ "metadata": {},+ "outputs": [],+ "source": [+ "import requests\n",+ "\n",+ "response = requests.get(\"https://data.seattle.gov/resource/2khk-5ukd.csv\")\n",+ "with open(\"seattle_city_wages.csv\", \"wb\") as file:\n",+ " file.write(response.content)"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Let's send the file to the agent using an `UploadForCodeInterpreter` message."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 10,+ "metadata": {},+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Sending message of type UploadForCodeInterpreter to assistant: {'file_path': 'seattle_city_wages.csv'}\n",+ "INFO:agnext:Calling message handler for assistant:default with message type UploadForCodeInterpreter sent by Unknown\n",+ "INFO:agnext:Resolving response with message type NoneType for recipient None from assistant: None\n"+ ]+ }+ ],+ "source": [+ "runtime.start()\n",+ "await runtime.send_message(UploadForCodeInterpreter(file_path=\"seattle_city_wages.csv\"), agent)\n",+ "await runtime.stop_when_idle()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "We can now ask some questions about the data to the agent."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 12,+ "metadata": {},+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'Take a look at the uploaded CSV file.', 'source': 'user'}\n",+ "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"+ ]+ },+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "import pandas as pd\n",+ "\n",+ "# Load the uploaded CSV file to examine its contents\n",+ "file_path = '/mnt/data/file-oEvRiyGyHc2jZViKyDqL8aoh'\n",+ "csv_data = pd.read_csv(file_path)\n",+ "\n",+ "# Display the first few rows of the dataframe to understand its structure\n",+ "csv_data.head()\n",+ "```\n",+ "Executing code...\n",+ "--------------------------------------------------------------------------------\n",+ "Assistant:\n",+ "\n",+ "The uploaded CSV file contains the following columns:\n",+ "\n",+ "1. **department**: The department in which the individual works.\n",+ "2. **last_name**: The last name of the employee.\n",+ "3. **first_name**: The first name of the employee.\n",+ "4. **job_title**: The job title of the employee.\n",+ "5. **hourly_rate**: The hourly rate for the employee's position.\n",+ "\n",+ "Here are the first few entries from the file:\n",+ "\n",+ "| department | last_name | first_name | job_title | hourly_rate |\n",+ "|--------------------------------|-----------|------------|------------------------------------|-------------|\n",+ "| Police Department | Aagard | Lori | Pol Capt-Precinct | 112.70 |\n",+ "| Police Department | Aakervik | Dag | Pol Ofcr-Detective | 75.61 |\n",+ "| Seattle City Light | Aaltonen | Evan | Pwrline Clear Tree Trimmer | 53.06 |\n",+ "| Seattle Public Utilities | Aar | Abdimallik | Civil Engrng Spec,Sr | 64.43 |\n",+ "| Seattle Dept of Transportation | Abad | Abigail | Admin Spec II-BU | 37.40 |\n",+ "\n",+ "If you need any specific analysis or information from this data, please let me know!"+ ]+ },+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': \"The uploaded CSV file contains the following columns:\\n\\n1. **department**: The department in which the individual works.\\n2. **last_name**: The last name of the employee.\\n3. **first_name**: The first name of the employee.\\n4. **job_title**: The job title of the employee.\\n5. **hourly_rate**: The hourly rate for the employee's position.\\n\\nHere are the first few entries from the file:\\n\\n| department | last_name | first_name | job_title | hourly_rate |\\n|--------------------------------|-----------|------------|------------------------------------|-------------|\\n| Police Department | Aagard | Lori | Pol Capt-Precinct | 112.70 |\\n| Police Department | Aakervik | Dag | Pol Ofcr-Detective | 75.61 |\\n| Seattle City Light | Aaltonen | Evan | Pwrline Clear Tree Trimmer | 53.06 |\\n| Seattle Public Utilities | Aar | Abdimallik | Civil Engrng Spec,Sr | 64.43 |\\n| Seattle Dept of Transportation | Abad | Abigail | Admin Spec II-BU | 37.40 |\\n\\nIf you need any specific analysis or information from this data, please let me know!\", 'source': 'assistant'}\n"+ ]+ }+ ],+ "source": [+ "runtime.start()\n",+ "await runtime.send_message(TextMessage(content=\"Take a look at the uploaded CSV file.\", source=\"user\"), agent)\n",+ "await runtime.stop_when_idle()"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 13,+ "metadata": {},+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'What are the top-10 salaries?', 'source': 'user'}\n",+ "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"+ ]+ },+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "# Sorting the data by hourly_rate in descending order and selecting the top 10 salaries\n",+ "top_10_salaries = csv_data[['first_name', 'last_name', 'job_title', 'hourly_rate']].sort_values(by='hourly_rate', ascending=False).head(10)\n",+ "top_10_salaries.reset_index(drop=True, inplace=True)\n",+ "top_10_salaries\n",+ "```\n",+ "Executing code...\n",+ "--------------------------------------------------------------------------------\n",+ "Assistant:\n",+ "\n",+ "Here are the top 10 salaries based on the hourly rates from the CSV file:\n",+ "\n",+ "| First Name | Last Name | Job Title | Hourly Rate |\n",+ "|------------|-----------|------------------------------------|-------------|\n",+ "| Eric | Barden | Executive4 | 139.61 |\n",+ "| Idris | Beauregard| Executive3 | 115.90 |\n",+ "| Lori | Aagard | Pol Capt-Precinct | 112.70 |\n",+ "| Krista | Bair | Pol Capt-Precinct | 108.74 |\n",+ "| Amy | Bannister | Fire Chief, Dep Adm-80 Hrs | 104.07 |\n",+ "| Ginger | Armbruster| Executive2 | 102.42 |\n",+ "| William | Andersen | Executive2 | 102.42 |\n",+ "| Valarie | Anderson | Executive2 | 102.42 |\n",+ "| Paige | Alderete | Executive2 | 102.42 |\n",+ "| Kathryn | Aisenberg | Executive2 | 100.65 |\n",+ "\n",+ "If you need any further details or analysis, let me know!"+ ]+ },+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': 'Here are the top 10 salaries based on the hourly rates from the CSV file:\\n\\n| First Name | Last Name | Job Title | Hourly Rate |\\n|------------|-----------|------------------------------------|-------------|\\n| Eric | Barden | Executive4 | 139.61 |\\n| Idris | Beauregard| Executive3 | 115.90 |\\n| Lori | Aagard | Pol Capt-Precinct | 112.70 |\\n| Krista | Bair | Pol Capt-Precinct | 108.74 |\\n| Amy | Bannister | Fire Chief, Dep Adm-80 Hrs | 104.07 |\\n| Ginger | Armbruster| Executive2 | 102.42 |\\n| William | Andersen | Executive2 | 102.42 |\\n| Valarie | Anderson | Executive2 | 102.42 |\\n| Paige | Alderete | Executive2 | 102.42 |\\n| Kathryn | Aisenberg | Executive2 | 100.65 |\\n\\nIf you need any further details or analysis, let me know!', 'source': 'assistant'}\n"+ ]+ }+ ],+ "source": [+ "runtime.start()\n",+ "await runtime.send_message(TextMessage(content=\"What are the top-10 salaries?\", source=\"user\"), agent)\n",+ "await runtime.stop_when_idle()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Assistant with File Search\n",+ "\n",+ "Let's try the Q&A over document feature. We first download Wikipedia page\n",+ "on the Third Anglo-Afghan War."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 14,+ "metadata": {},+ "outputs": [],+ "source": [+ "response = requests.get(\"https://en.wikipedia.org/wiki/Third_Anglo-Afghan_War\")\n",+ "with open(\"third_anglo_afghan_war.html\", \"wb\") as file:\n",+ " file.write(response.content)"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Send the file to the agent using an `UploadForFileSearch` message."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 15,+ "metadata": {},+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Sending message of type UploadForFileSearch to assistant: {'file_path': 'third_anglo_afghan_war.html', 'vector_store_id': 'vs_h3xxPbJFnd1iZ9WdjsQwNdrp'}\n",+ "INFO:agnext:Calling message handler for assistant:default with message type UploadForFileSearch sent by Unknown\n",+ "INFO:agnext:Resolving response with message type NoneType for recipient None from assistant: None\n"+ ]+ }+ ],+ "source": [+ "runtime.start()\n",+ "await runtime.send_message(\n",+ " UploadForFileSearch(file_path=\"third_anglo_afghan_war.html\", vector_store_id=vector_store.id), agent\n",+ ")\n",+ "await runtime.stop_when_idle()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "Let's ask some questions about the document to the agent. Before asking,\n",+ "we reset the agent memory to start a new conversation."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 17,+ "metadata": {},+ "outputs": [+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Sending message of type Reset to assistant: {}\n",+ "INFO:agnext:Calling message handler for assistant:default with message type Reset sent by Unknown\n",+ "INFO:agnext:Resolving response with message type NoneType for recipient None from assistant: None\n",+ "INFO:agnext:Sending message of type TextMessage to assistant: {'content': 'When and where was the treaty of Rawalpindi signed? Answer using the document provided.', 'source': 'user'}\n",+ "INFO:agnext:Calling message handler for assistant:default with message type TextMessage sent by Unknown\n"+ ]+ },+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "--------------------------------------------------------------------------------\n",+ "Assistant:\n",+ "\n",+ "The Treaty of Rawalpindi was signed on **8 August 1919**. The location of the signing was in **Rawalpindi**, which is in present-day Pakistan【6:0†source】."+ ]+ },+ {+ "name": "stderr",+ "output_type": "stream",+ "text": [+ "INFO:agnext:Resolving response with message type TextMessage for recipient None from assistant: {'content': 'The Treaty of Rawalpindi was signed on **8 August 1919**. The location of the signing was in **Rawalpindi**, which is in present-day Pakistan【6:0†source】.', 'source': 'assistant'}\n"+ ]+ },+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "[0] third_anglo_afghan_war.html\n"+ ]+ }+ ],+ "source": [+ "runtime.start()\n",+ "await runtime.send_message(Reset(), agent)\n",+ "await runtime.send_message(\n",+ " TextMessage(\n",+ " content=\"When and where was the treaty of Rawalpindi signed? Answer using the document provided.\", source=\"user\"\n",+ " ),\n",+ " agent,\n",+ ")\n",+ "await runtime.stop_when_idle()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "That's it! We have successfully built an agent backed by OpenAI Assistant."+ ]+ }+ ],+ "metadata": {+ "kernelspec": {+ "display_name": "agnext",+ "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.4"+ }+ },+ "nbformat": 4,+ "nbformat_minor": 2}diff --git a/python/docs/src/cookbook/termination-with-intervention.md b/python/docs/src/cookbook/termination-with-intervention.mdindex 10eb40066..2ed45ccbc 100644--- a/python/docs/src/cookbook/termination-with-intervention.md+++ b/python/docs/src/cookbook/termination-with-intervention.md@@ -13,8 +13,8 @@ from typing import Anyfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.components import RoutedAgent, message_handler-from agnext.core import AgentId, CancellationToken-from agnext.core.intervention import DefaultInterventionHandler+from agnext.base import AgentId, CancellationToken+from agnext.base.intervention import DefaultInterventionHandler```First, we define a dataclass that will be used to signal termination.diff --git a/python/docs/src/cookbook/type-routed-agent.md b/python/docs/src/cookbook/type-routed-agent.mdindex e3dbc0268..22d04608e 100644--- a/python/docs/src/cookbook/type-routed-agent.md+++ b/python/docs/src/cookbook/type-routed-agent.md@@ -9,7 +9,7 @@ The decorator {py:func}`agnext.components.message_handler` should be added to fu- The function must have exactly 3 arguments.- `self`- `message`: The message to be handled, this must be type hinted with the message type that it is intended to handle.- - `cancellation_token`: A {py:class}`agnext.core.CancellationToken` object+ - `cancellation_token`: A {py:class}`agnext.base.CancellationToken` object- The function must be type hinted with what message types it can return.```{tip}@@ -26,7 +26,7 @@ One important thing to point out is that when an agent is constructed it must befrom dataclasses import dataclassfrom typing import List, Unionfrom agnext.components import RoutedAgent, message_handler, Image-from agnext.core import AgentRuntime, CancellationToken+from agnext.base import AgentRuntime, CancellationToken@dataclassclass TextMessage:diff --git a/python/docs/src/core-concepts/architecture.md b/python/docs/src/core-concepts/architecture.mdindex 5de22b85d..513e174ce 100644--- a/python/docs/src/core-concepts/architecture.md+++ b/python/docs/src/core-concepts/architecture.md@@ -30,7 +30,7 @@ memory stores, and more.The API consists of the following layers:-- {py:mod}`agnext.core`+- {py:mod}`agnext.base`- {py:mod}`agnext.application`- {py:mod}`agnext.components`@@ -38,7 +38,7 @@ The following diagram shows the relationship between the layers.-The {py:mod}`agnext.core` layer defines the+The {py:mod}`agnext.base` layer defines thecore interfaces and base classes for agents, messages, and runtime.This layer is the foundation of the framework and is used by the other layers.diff --git a/python/docs/src/getting-started/agent-and-agent-runtime.ipynb b/python/docs/src/getting-started/agent-and-agent-runtime.ipynbindex 662635d0b..118c043ab 100644--- a/python/docs/src/getting-started/agent-and-agent-runtime.ipynb+++ b/python/docs/src/getting-started/agent-and-agent-runtime.ipynb@@ -16,10 +16,10 @@"cell_type": "markdown","metadata": {},"source": [- "An agent in AGNext is an entity defined by the base class {py:class}`agnext.core.BaseAgent`.\n",- "It has a unique identifier of the type {py:class}`agnext.core.AgentId`,\n",- "a metadata dictionary of the type {py:class}`agnext.core.AgentMetadata`,\n",- "and method for handling messages {py:meth}`agnext.core.BaseAgent.on_message`.\n",+ "An agent in AGNext is an entity defined by the base class {py:class}`agnext.base.BaseAgent`.\n",+ "It has a unique identifier of the type {py:class}`agnext.base.AgentId`,\n",+ "a metadata dictionary of the type {py:class}`agnext.base.AgentMetadata`,\n",+ "and method for handling messages {py:meth}`agnext.base.BaseAgent.on_message`.\n","\n","An agent runtime is the execution environment for agents in AGNext.\n","Similar to the runtime environment of a programming language,\n",@@ -41,9 +41,9 @@"source": ["## Implementing an Agent\n","\n",- "To implement an agent, developer must subclass the {py:class}`~agnext.core.BaseAgent` class,\n",- "declare the message types it can handle in the {py:attr}`~agnext.core.AgentMetadata.subscriptions` metadata,\n",- "and implement the {py:meth}`~agnext.core.BaseAgent.on_message` method.\n",+ "To implement an agent, developer must subclass the {py:class}`~agnext.base.BaseAgent` class,\n",+ "declare the message types it can handle in the {py:attr}`~agnext.base.AgentMetadata.subscriptions` metadata,\n",+ "and implement the {py:meth}`~agnext.base.BaseAgent.on_message` method.\n","This method is invoked when the agent receives a message. For example,\n","the following agent handles a simple message type and simply prints message it receives:"]@@ -56,7 +56,7 @@"source": ["from dataclasses import dataclass\n","\n",- "from agnext.core import AgentId, BaseAgent, MessageContext\n",+ "from agnext.base import AgentId, BaseAgent, MessageContext\n","\n","\n","@dataclass\n",@@ -88,7 +88,7 @@"## Registering Agent Type\n","\n","To make agents available to the runtime, developers can use the\n",- "{py:meth}`~agnext.core.AgentRuntime.register` method.\n",+ "{py:meth}`~agnext.base.AgentRuntime.register` method.\n","The process of registration associates an agent type and a factory function\n","that creates an instance of the agent type.\n","The factory function is used to allow automatic creation of agent instances \n",@@ -129,7 +129,7 @@"metadata": {},"source": ["Once an agent type is registered, we can send a direct message to an agent instance\n",- "using an {py:class}`~agnext.core.AgentId`.\n",+ "using an {py:class}`~agnext.base.AgentId`.\n","The runtime will create the instance the first time it delivers a\n","message to this instance."]@@ -159,7 +159,7 @@"metadata": {},"source": ["```{note}\n",- "Because the runtime manages the lifecycle of agents, an {py:class}`~agnext.core.AgentId`\n",+ "Because the runtime manages the lifecycle of agents, an {py:class}`~agnext.base.AgentId`\n","is only used to communicate with the agent or retrieve its metadata (e.g., description).\n","```"]diff --git a/python/docs/src/getting-started/group-chat.ipynb b/python/docs/src/getting-started/group-chat.ipynbindex dd2208c2a..ed84c9db1 100644--- a/python/docs/src/getting-started/group-chat.ipynb+++ b/python/docs/src/getting-started/group-chat.ipynb@@ -85,9 +85,9 @@"source": ["from typing import List\n","\n",+ "from agnext.base import MessageContext\n","from agnext.components import DefaultTopicId, RoutedAgent, message_handler\n","from agnext.components.models import AssistantMessage, ChatCompletionClient, LLMMessage, SystemMessage, UserMessage\n",- "from agnext.core import MessageContext\n","\n","\n","class WriterAgent(RoutedAgent):\n",@@ -216,7 +216,7 @@"metadata": {},"outputs": [],"source": [- "from agnext.core import AgentId\n",+ "from agnext.base import AgentId\n","\n","\n","class GroupChatManager(RoutedAgent):\n",@@ -311,9 +311,9 @@],"source": ["from agnext.application import SingleThreadedAgentRuntime\n",+ "from agnext.base import AgentId\n","from agnext.components import DefaultSubscription\n","from agnext.components.models import OpenAIChatCompletionClient\n",- "from agnext.core import AgentId\n","\n","runtime = SingleThreadedAgentRuntime()\n","\n",diff --git a/python/docs/src/getting-started/message-and-communication.ipynb b/python/docs/src/getting-started/message-and-communication.ipynbindex ae673173e..3a83773b6 100644--- a/python/docs/src/getting-started/message-and-communication.ipynb+++ b/python/docs/src/getting-started/message-and-communication.ipynb@@ -62,14 +62,14 @@"### Message Handlers\n","\n","When an agent receives a message the runtime will invoke the agent's message handler\n",- "({py:meth}`~agnext.core.Agent.on_message`) which should implement the agents message handling logic.\n",+ "({py:meth}`~agnext.base.Agent.on_message`) which should implement the agents message handling logic.\n","If this message cannot be handled by the agent, the agent should raise a\n",- "{py:class}`~agnext.core.exceptions.CantHandleException`.\n",+ "{py:class}`~agnext.base.exceptions.CantHandleException`.\n","\n","For convenience, the {py:class}`~agnext.components.RoutedAgent` base class\n","provides the {py:meth}`~agnext.components.message_handler` decorator\n","for associating message types with message handlers,\n",- "so developers do not need to implement the {py:meth}`~agnext.core.Agent.on_message` method.\n",+ "so developers do not need to implement the {py:meth}`~agnext.base.Agent.on_message` method.\n","\n","For example, the following type-routed agent responds to `TextMessage` and `ImageMessage`\n","using different message handlers:"@@ -82,8 +82,8 @@"outputs": [],"source": ["from agnext.application import SingleThreadedAgentRuntime\n",+ "from agnext.base import AgentId, MessageContext\n","from agnext.components import RoutedAgent, message_handler\n",- "from agnext.core import AgentId, MessageContext\n","\n","\n","class MyAgent(RoutedAgent):\n",@@ -166,8 +166,8 @@"\n","Let's first look at direct messaging.\n","To send a direct message to another agent, within a message handler use\n",- "the {py:meth}`agnext.core.BaseAgent.send_message` method,\n",- "from the runtime use the {py:meth}`agnext.core.AgentRuntime.send_message` method.\n",+ "the {py:meth}`agnext.base.BaseAgent.send_message` method,\n",+ "from the runtime use the {py:meth}`agnext.base.AgentRuntime.send_message` method.\n","Awaiting calls to these methods will return the return value of the\n","receiving agent's message handler.\n","When the receiving agent's handler returns `None`, `None` will be returned.\n",@@ -196,8 +196,8 @@"from dataclasses import dataclass\n","\n","from agnext.application import SingleThreadedAgentRuntime\n",+ "from agnext.base import MessageContext\n","from agnext.components import RoutedAgent, message_handler\n",- "from agnext.core import MessageContext\n","\n","\n","@dataclass\n",@@ -299,8 +299,8 @@"### Publishing to Topics\n","\n","To publish a message from an agent's handler,\n",- "use the {py:meth}`agnext.core.BaseAgent.publish_message` method and specify\n",- "a {py:class}`agnext.core.TopicId`.\n",+ "use the {py:meth}`agnext.base.BaseAgent.publish_message` method and specify\n",+ "a {py:class}`agnext.base.TopicId`.\n","This call must still be awaited to allow the runtime to schedule delivery of \n","the message to all subscribers,\n","but it will always return `None`.\n",@@ -319,8 +319,8 @@"outputs": [],"source": ["from agnext.application import SingleThreadedAgentRuntime\n",+ "from agnext.base import MessageContext, TopicId\n","from agnext.components import RoutedAgent, message_handler\n",- "from agnext.core import MessageContext, TopicId\n","\n","\n","class BroadcastingAgent(RoutedAgent):\n",@@ -417,7 +417,7 @@"### Default Topic and Subscriptions\n","\n","In the above example, we used\n",- "{py:class}`~agnext.core.TopicId` and {py:class}`~agnext.components.TypeSubscription`\n",+ "{py:class}`~agnext.base.TopicId` and {py:class}`~agnext.components.TypeSubscription`\n","to specify the topic and subscriptions respectively.\n","This is the appropriate way for many scenarios.\n","However, when there is a single scope of publishing, that is, \n",diff --git a/python/docs/src/getting-started/model-clients.ipynb b/python/docs/src/getting-started/model-clients.ipynbindex cb1d0349e..da82443f1 100644--- a/python/docs/src/getting-started/model-clients.ipynb+++ b/python/docs/src/getting-started/model-clients.ipynb@@ -227,9 +227,9 @@"from dataclasses import dataclass\n","\n","from agnext.application import SingleThreadedAgentRuntime\n",+ "from agnext.base import MessageContext\n","from agnext.components import RoutedAgent, message_handler\n","from agnext.components.models import ChatCompletionClient, OpenAIChatCompletionClient, SystemMessage, UserMessage\n",- "from agnext.core import MessageContext\n","\n","\n","@dataclass\n",@@ -265,7 +265,7 @@"It then returns the response to the user, following the direct communication model.\n","\n","```{note}\n",- "The `cancellation_token` of the type {py:class}`agnext.core.CancellationToken` is used to cancel\n",+ "The `cancellation_token` of the type {py:class}`agnext.base.CancellationToken` is used to cancel\n","asynchronous operations. It is linked to async calls inside the message handlers\n","and can be used by the caller to cancel the handlers.\n","```"@@ -318,7 +318,7 @@],"source": ["# Create the runtime and register the agent.\n",- "from agnext.core import AgentId\n",+ "from agnext.base import AgentId\n","\n","runtime = SingleThreadedAgentRuntime()\n","await runtime.register(\n",diff --git a/python/docs/src/getting-started/quickstart.ipynb b/python/docs/src/getting-started/quickstart.ipynbindex 6dff5e86b..a7e2a5cf6 100644--- a/python/docs/src/getting-started/quickstart.ipynb+++ b/python/docs/src/getting-started/quickstart.ipynb@@ -26,10 +26,10 @@"from dataclasses import dataclass\n","from typing import List\n","\n",+ "from agnext.base import MessageContext\n","from agnext.components import DefaultTopicId, RoutedAgent, message_handler\n","from agnext.components.code_executor import CodeExecutor, extract_markdown_code_blocks\n","from agnext.components.models import AssistantMessage, ChatCompletionClient, LLMMessage, SystemMessage, UserMessage\n",- "from agnext.core import MessageContext\n","\n","\n","@dataclass\n",diff --git a/python/docs/src/getting-started/reflection.ipynb b/python/docs/src/getting-started/reflection.ipynbindex 3a783fac0..b292f9ef7 100644--- a/python/docs/src/getting-started/reflection.ipynb+++ b/python/docs/src/getting-started/reflection.ipynb@@ -100,6 +100,7 @@"import uuid\n","from typing import Dict, List, Union\n","\n",+ "from agnext.base import MessageContext, TopicId\n","from agnext.components import RoutedAgent, message_handler\n","from agnext.components.models import (\n"," AssistantMessage,\n",@@ -107,8 +108,7 @@" LLMMessage,\n"," SystemMessage,\n"," UserMessage,\n",- ")\n",- "from agnext.core import MessageContext, TopicId"+ ")"]},{diff --git a/python/docs/src/getting-started/tools.ipynb b/python/docs/src/getting-started/tools.ipynbindex a24d26b1c..3d7381639 100644--- a/python/docs/src/getting-started/tools.ipynb+++ b/python/docs/src/getting-started/tools.ipynb@@ -43,9 +43,9 @@}],"source": [+ "from agnext.base import CancellationToken\n","from agnext.components.code_executor import LocalCommandLineCodeExecutor\n","from agnext.components.tools import PythonCodeExecutionTool\n",- "from agnext.core import CancellationToken\n","\n","# Create the tool.\n","code_executor = LocalCommandLineCodeExecutor()\n",@@ -100,8 +100,8 @@"source": ["import random\n","\n",+ "from agnext.base import CancellationToken\n","from agnext.components.tools import FunctionTool\n",- "from agnext.core import CancellationToken\n","from typing_extensions import Annotated\n","\n","\n",@@ -143,6 +143,7 @@"from typing import List\n","\n","from agnext.application import SingleThreadedAgentRuntime\n",+ "from agnext.base import AgentId, AgentInstantiationContext, MessageContext\n","from agnext.components import RoutedAgent, message_handler\n","from agnext.components.models import (\n"," ChatCompletionClient,\n",@@ -153,7 +154,6 @@")\n","from agnext.components.tool_agent import ToolAgent, tool_agent_caller_loop\n","from agnext.components.tools import FunctionTool, Tool, ToolSchema\n",- "from agnext.core import AgentId, AgentInstantiationContext, MessageContext\n","\n","\n","@dataclass\n",diff --git a/python/docs/src/index.rst b/python/docs/src/index.rstindex 41ffecd87..11ad69db3 100644--- a/python/docs/src/index.rst+++ b/python/docs/src/index.rst@@ -3,9 +3,9 @@ AGNextAGNext is a framework for building multi-agent applications with AI agents.-At a high level, it provides a framework for inter-agent communication and a+At a high level, it provides a framework for inter-agent communication and asuite of independent components for building and managing agents.-You can implement agents in+You can implement agents indifferent programming languages and deploy them on different machines across organizational boundaries.You can also implement agents using other agent frameworks and run them in AGNext.@@ -66,7 +66,7 @@ from `Agent and Multi-Agent Application <core-concepts/agent-and-multi-agent-appreference/agnext.componentsreference/agnext.application- reference/agnext.core+ reference/agnext.base.. toctree:::caption: Otherdiff --git a/python/samples/byoa/langgraph_agent.py b/python/samples/byoa/langgraph_agent.pyindex 55be01218..f07b1f76d 100644--- a/python/samples/byoa/langgraph_agent.py+++ b/python/samples/byoa/langgraph_agent.py@@ -9,8 +9,8 @@ from dataclasses import dataclassfrom typing import Any, Callable, List, Literalfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentId, MessageContextfrom agnext.components import RoutedAgent, message_handler-from agnext.core import AgentId, MessageContextfrom langchain_core.messages import HumanMessage, SystemMessagefrom langchain_core.tools import tool # pyright: ignorefrom langchain_openai import ChatOpenAIdiff --git a/python/samples/byoa/llamaindex_agent.py b/python/samples/byoa/llamaindex_agent.pyindex ab52e8475..0d98fe64f 100644--- a/python/samples/byoa/llamaindex_agent.py+++ b/python/samples/byoa/llamaindex_agent.py@@ -8,8 +8,8 @@ from dataclasses import dataclassfrom typing import List, Optionalfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentId, MessageContextfrom agnext.components import RoutedAgent, message_handler-from agnext.core import AgentId, MessageContextfrom llama_index.core import Settingsfrom llama_index.core.agent import ReActAgentfrom llama_index.core.agent.runner.base import AgentRunnerdiff --git a/python/samples/common/agents/_chat_completion_agent.py b/python/samples/common/agents/_chat_completion_agent.pyindex c71589dd5..b7ffb36a4 100644--- a/python/samples/common/agents/_chat_completion_agent.py+++ b/python/samples/common/agents/_chat_completion_agent.py@@ -2,6 +2,7 @@ import asyncioimport jsonfrom typing import Any, Coroutine, Dict, List, Mapping, Sequence, Tuple+from agnext.base import AgentId, CancellationToken, MessageContextfrom agnext.components import (DefaultTopicId,FunctionCall,@@ -16,7 +17,6 @@ from agnext.components.models import (SystemMessage,)from agnext.components.tools import Tool-from agnext.core import AgentId, CancellationToken, MessageContextfrom ..types import (FunctionCallMessage,diff --git a/python/samples/common/agents/_image_generation_agent.py b/python/samples/common/agents/_image_generation_agent.pyindex 8b0f105a1..648c14258 100644--- a/python/samples/common/agents/_image_generation_agent.py+++ b/python/samples/common/agents/_image_generation_agent.py@@ -1,6 +1,7 @@from typing import Literalimport openai+from agnext.base import CancellationToken, MessageContextfrom agnext.components import (DefaultTopicId,Image,@@ -8,7 +9,6 @@ from agnext.components import (message_handler,)from agnext.components.memory import ChatMemory-from agnext.core import CancellationToken, MessageContextfrom ..types import (Message,diff --git a/python/samples/common/agents/_oai_assistant.py b/python/samples/common/agents/_oai_assistant.pyindex 2a1d72d0f..689275ca3 100644--- a/python/samples/common/agents/_oai_assistant.py+++ b/python/samples/common/agents/_oai_assistant.py@@ -1,11 +1,11 @@from typing import Any, Callable, List, Mappingimport openai-from agnext.components import DefaultTopicId, RoutedAgent, message_handler-from agnext.core import (+from agnext.base import (CancellationToken,MessageContext, # type: ignore)+from agnext.components import DefaultTopicId, RoutedAgent, message_handlerfrom openai import AsyncAssistantEventHandlerfrom openai.types import ResponseFormatJSONObject, ResponseFormatTextdiff --git a/python/samples/common/agents/_user_proxy.py b/python/samples/common/agents/_user_proxy.pyindex 442955050..1bf6ea23f 100644--- a/python/samples/common/agents/_user_proxy.py+++ b/python/samples/common/agents/_user_proxy.py@@ -1,7 +1,7 @@import asyncio+from agnext.base import MessageContextfrom agnext.components import DefaultTopicId, RoutedAgent, message_handler-from agnext.core import MessageContextfrom ..types import PublishNow, TextMessagediff --git a/python/samples/common/patterns/_group_chat_manager.py b/python/samples/common/patterns/_group_chat_manager.pyindex cb8581d5d..62c5d0b07 100644--- a/python/samples/common/patterns/_group_chat_manager.py+++ b/python/samples/common/patterns/_group_chat_manager.py@@ -1,10 +1,10 @@import loggingfrom typing import Any, Callable, List, Mapping+from agnext.base import AgentId, AgentProxy, MessageContextfrom agnext.components import RoutedAgent, message_handlerfrom agnext.components.memory import ChatMemoryfrom agnext.components.models import ChatCompletionClient-from agnext.core import AgentId, AgentProxy, MessageContextfrom ..types import (Message,diff --git a/python/samples/common/patterns/_group_chat_utils.py b/python/samples/common/patterns/_group_chat_utils.pyindex 980365b8a..c61d7ff26 100644--- a/python/samples/common/patterns/_group_chat_utils.py+++ b/python/samples/common/patterns/_group_chat_utils.py@@ -3,9 +3,9 @@import refrom typing import Dict, List+from agnext.base import AgentProxyfrom agnext.components.memory import ChatMemoryfrom agnext.components.models import ChatCompletionClient, SystemMessage-from agnext.core import AgentProxyfrom ..types import Message, TextMessagediff --git a/python/samples/common/patterns/_orchestrator_chat.py b/python/samples/common/patterns/_orchestrator_chat.pyindex 974b20384..3275f0358 100644--- a/python/samples/common/patterns/_orchestrator_chat.py+++ b/python/samples/common/patterns/_orchestrator_chat.py@@ -1,8 +1,8 @@import jsonfrom typing import Any, Sequence, Tuple+from agnext.base import AgentId, AgentRuntime, MessageContextfrom agnext.components import RoutedAgent, message_handler-from agnext.core import AgentId, AgentRuntime, MessageContextfrom ..types import Reset, RespondNow, ResponseFormat, TextMessagediff --git a/python/samples/core/inner_outer_direct.py b/python/samples/core/inner_outer_direct.pyindex e926c829f..d87d73372 100644--- a/python/samples/core/inner_outer_direct.py+++ b/python/samples/core/inner_outer_direct.py@@ -11,8 +11,8 @@ import loggingfrom dataclasses import dataclassfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentId, AgentInstantiationContext, MessageContextfrom agnext.components import RoutedAgent, message_handler-from agnext.core import AgentId, AgentInstantiationContext, MessageContext@dataclassdiff --git a/python/samples/core/one_agent_direct.py b/python/samples/core/one_agent_direct.pyindex 6055c64e0..4b31febaa 100644--- a/python/samples/core/one_agent_direct.py+++ b/python/samples/core/one_agent_direct.py@@ -11,17 +11,17 @@ import sysfrom dataclasses import dataclassfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentIdfrom agnext.components import RoutedAgent, message_handlerfrom agnext.components.models import (ChatCompletionClient,SystemMessage,UserMessage,)-from agnext.core import AgentIdsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.utils import get_chat_completion_client_from_envsdiff --git a/python/samples/core/two_agents_pub_sub.py b/python/samples/core/two_agents_pub_sub.pyindex c33a67be9..fe3134f72 100644--- a/python/samples/core/two_agents_pub_sub.py+++ b/python/samples/core/two_agents_pub_sub.py@@ -17,6 +17,7 @@ from dataclasses import dataclassfrom typing import Listfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentIdfrom agnext.components import DefaultSubscription, DefaultTopicId, RoutedAgent, message_handlerfrom agnext.components.models import (AssistantMessage,@@ -25,11 +26,10 @@ from agnext.components.models import (SystemMessage,UserMessage,)-from agnext.core import AgentIdsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.utils import get_chat_completion_client_from_envsdiff --git a/python/samples/demos/assistant.py b/python/samples/demos/assistant.pyindex 471bcd018..29918bef5 100644--- a/python/samples/demos/assistant.py+++ b/python/samples/demos/assistant.py@@ -12,8 +12,8 @@ from typing import Listimport aiofilesimport openaifrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentId, AgentRuntime, MessageContextfrom agnext.components import DefaultTopicId, RoutedAgent, message_handler-from agnext.core import AgentId, AgentRuntime, MessageContextfrom openai import AsyncAssistantEventHandlerfrom openai.types.beta.thread import ToolResourcesfrom openai.types.beta.threads import Message, Text, TextDelta@@ -22,7 +22,7 @@ from typing_extensions import overridesys.path.append(os.path.join(os.path.dirname(__file__), ".."))-from agnext.core import AgentInstantiationContext+from agnext.base import AgentInstantiationContextfrom common.agents import OpenAIAssistantAgentfrom common.memory import BufferedChatMemoryfrom common.patterns._group_chat_manager import GroupChatManagerdiff --git a/python/samples/demos/chat_room.py b/python/samples/demos/chat_room.pyindex ad7ea1364..5a9ddbe45 100644--- a/python/samples/demos/chat_room.py+++ b/python/samples/demos/chat_room.py@@ -6,15 +6,15 @@ import osimport sysfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentId, AgentInstantiationContext, AgentProxy, AgentRuntimefrom agnext.components import DefaultSubscription, DefaultTopicId, RoutedAgent, message_handlerfrom agnext.components.memory import ChatMemoryfrom agnext.components.models import ChatCompletionClient, SystemMessage-from agnext.core import AgentId, AgentInstantiationContext, AgentProxy, AgentRuntimesys.path.append(os.path.abspath(os.path.dirname(__file__)))sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.memory import BufferedChatMemoryfrom common.types import Message, TextMessagefrom common.utils import convert_messages_to_llm_messages, get_chat_completion_client_from_envsdiff --git a/python/samples/demos/chess_game.py b/python/samples/demos/chess_game.pyindex 604258402..566b096de 100644--- a/python/samples/demos/chess_game.py+++ b/python/samples/demos/chess_game.py@@ -10,16 +10,16 @@ import sysfrom typing import Annotated, Literalfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentInstantiationContext, AgentRuntimefrom agnext.components import DefaultSubscription, DefaultTopicIdfrom agnext.components.models import SystemMessagefrom agnext.components.tools import FunctionTool-from agnext.core import AgentInstantiationContext, AgentRuntimefrom chess import BLACK, SQUARE_NAMES, WHITE, Board, Movefrom chess import piece_name as get_piece_namesys.path.append(os.path.join(os.path.dirname(__file__), ".."))-from agnext.core import AgentId+from agnext.base import AgentIdfrom common.agents._chat_completion_agent import ChatCompletionAgentfrom common.memory import BufferedChatMemoryfrom common.patterns._group_chat_manager import GroupChatManagerdiff --git a/python/samples/demos/illustrator_critics.py b/python/samples/demos/illustrator_critics.pyindex 44286465f..af49daa53 100644--- a/python/samples/demos/illustrator_critics.py+++ b/python/samples/demos/illustrator_critics.py@@ -6,13 +6,13 @@ import sysimport openaifrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentInstantiationContext, AgentRuntimefrom agnext.components.models import SystemMessage-from agnext.core import AgentInstantiationContext, AgentRuntimesys.path.append(os.path.abspath(os.path.dirname(__file__)))sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import AgentId, AgentProxy+from agnext.base import AgentId, AgentProxyfrom common.agents import ChatCompletionAgent, ImageGenerationAgentfrom common.memory import BufferedChatMemoryfrom common.patterns._group_chat_manager import GroupChatManagerdiff --git a/python/samples/demos/software_consultancy.py b/python/samples/demos/software_consultancy.pyindex 499a20b33..d3fcb7985 100644--- a/python/samples/demos/software_consultancy.py+++ b/python/samples/demos/software_consultancy.py@@ -17,9 +17,9 @@ import aiofilesimport aiohttpimport openaifrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentInstantiationContext, AgentRuntimefrom agnext.components.models import SystemMessagefrom agnext.components.tools import FunctionTool-from agnext.core import AgentInstantiationContext, AgentRuntimefrom markdownify import markdownify # type: ignorefrom tqdm import tqdmfrom typing_extensions import Annotated@@ -27,7 +27,7 @@ from typing_extensions import Annotatedsys.path.append(os.path.abspath(os.path.dirname(__file__)))sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import AgentId+from agnext.base import AgentIdfrom common.agents import ChatCompletionAgentfrom common.memory import HeadAndTailChatMemoryfrom common.patterns._group_chat_manager import GroupChatManagerdiff --git a/python/samples/demos/utils.py b/python/samples/demos/utils.pyindex 6fadc8979..a5a7ab285 100644--- a/python/samples/demos/utils.py+++ b/python/samples/demos/utils.py@@ -4,8 +4,8 @@ import randomimport sysfrom asyncio import Future+from agnext.base import AgentRuntime, CancellationTokenfrom agnext.components import DefaultTopicId, Image, RoutedAgent, message_handler-from agnext.core import AgentRuntime, CancellationTokenfrom textual.app import App, ComposeResultfrom textual.containers import ScrollableContainerfrom textual.widgets import Button, Footer, Header, Input, Markdown, Staticdiff --git a/python/samples/marketing-agents/app.py b/python/samples/marketing-agents/app.pyindex 878913aee..18adbf38f 100644--- a/python/samples/marketing-agents/app.py+++ b/python/samples/marketing-agents/app.py@@ -1,8 +1,8 @@import os+from agnext.base import AgentRuntimefrom agnext.components import DefaultSubscriptionfrom agnext.components.models import AzureOpenAIChatCompletionClient-from agnext.core import AgentRuntimefrom auditor import AuditAgentfrom graphic_designer import GraphicDesignerAgentfrom openai import AsyncAzureOpenAIdiff --git a/python/samples/marketing-agents/auditor.py b/python/samples/marketing-agents/auditor.pyindex 3a9ff9cd9..088a89218 100644--- a/python/samples/marketing-agents/auditor.py+++ b/python/samples/marketing-agents/auditor.py@@ -1,7 +1,7 @@+from agnext.base import MessageContextfrom agnext.components import DefaultTopicId, RoutedAgent, message_handlerfrom agnext.components.models import ChatCompletionClientfrom agnext.components.models._types import SystemMessage-from agnext.core import MessageContextfrom messages import AuditorAlert, AuditTextauditor_prompt = """You are an Auditor in a Marketing teamdiff --git a/python/samples/marketing-agents/graphic_designer.py b/python/samples/marketing-agents/graphic_designer.pyindex cdbb52ed6..203d34afb 100644--- a/python/samples/marketing-agents/graphic_designer.py+++ b/python/samples/marketing-agents/graphic_designer.py@@ -2,12 +2,12 @@ import loggingfrom typing import Literalimport openai+from agnext.base import MessageContextfrom agnext.components import (DefaultTopicId,RoutedAgent,message_handler,)-from agnext.core import MessageContextfrom messages import ArticleCreated, GraphicDesignCreateddiff --git a/python/samples/marketing-agents/test_usage.py b/python/samples/marketing-agents/test_usage.pyindex 648b2f6d7..70806c36f 100644--- a/python/samples/marketing-agents/test_usage.py+++ b/python/samples/marketing-agents/test_usage.py@@ -2,8 +2,8 @@ import asyncioimport osfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import MessageContextfrom agnext.components import DefaultTopicId, Image, RoutedAgent, message_handler-from agnext.core import MessageContextfrom app import build_appfrom dotenv import load_dotenvfrom messages import ArticleCreated, AuditorAlert, AuditText, GraphicDesignCreateddiff --git a/python/samples/marketing-agents/worker.py b/python/samples/marketing-agents/worker.pyindex 0f09a3b20..c8b44989e 100644--- a/python/samples/marketing-agents/worker.py+++ b/python/samples/marketing-agents/worker.py@@ -2,7 +2,7 @@ import asyncioimport loggingfrom agnext.application import WorkerAgentRuntime-from agnext.core import MESSAGE_TYPE_REGISTRY+from agnext.base import MESSAGE_TYPE_REGISTRYfrom app import build_appfrom dotenv import load_dotenvfrom messages import ArticleCreated, AuditorAlert, AuditText, GraphicDesignCreateddiff --git a/python/samples/patterns/coder_executor.py b/python/samples/patterns/coder_executor.pyindex fac6f66dc..44ae4c057 100644--- a/python/samples/patterns/coder_executor.py+++ b/python/samples/patterns/coder_executor.py@@ -33,7 +33,7 @@ from agnext.components.models import (sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.utils import get_chat_completion_client_from_envsdiff --git a/python/samples/patterns/coder_reviewer.py b/python/samples/patterns/coder_reviewer.pyindex 988dcf384..4516368d3 100644--- a/python/samples/patterns/coder_reviewer.py+++ b/python/samples/patterns/coder_reviewer.py@@ -32,7 +32,7 @@ from agnext.components.models import (sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.utils import get_chat_completion_client_from_envsdiff --git a/python/samples/patterns/group_chat.py b/python/samples/patterns/group_chat.pyindex 83726afbb..d84aa7508 100644--- a/python/samples/patterns/group_chat.py+++ b/python/samples/patterns/group_chat.py@@ -18,6 +18,7 @@ from dataclasses import dataclassfrom typing import Listfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentId, AgentInstantiationContextfrom agnext.components import DefaultTopicId, RoutedAgent, message_handlerfrom agnext.components.models import (AssistantMessage,@@ -26,11 +27,10 @@ from agnext.components.models import (SystemMessage,UserMessage,)-from agnext.core import AgentId, AgentInstantiationContextsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.utils import get_chat_completion_client_from_envsdiff --git a/python/samples/patterns/mixture_of_agents.py b/python/samples/patterns/mixture_of_agents.pyindex 953ae5fb5..f208e82ff 100644--- a/python/samples/patterns/mixture_of_agents.py+++ b/python/samples/patterns/mixture_of_agents.py@@ -15,9 +15,9 @@ from dataclasses import dataclassfrom typing import Dict, Listfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import MessageContextfrom agnext.components import DefaultSubscription, DefaultTopicId, RoutedAgent, message_handlerfrom agnext.components.models import ChatCompletionClient, SystemMessage, UserMessage-from agnext.core import MessageContextsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))diff --git a/python/samples/patterns/multi_agent_debate.py b/python/samples/patterns/multi_agent_debate.pyindex c1d9bac84..a5c5f523a 100644--- a/python/samples/patterns/multi_agent_debate.py+++ b/python/samples/patterns/multi_agent_debate.py@@ -51,7 +51,7 @@ from agnext.components.models import (sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.utils import get_chat_completion_client_from_envslogger = logging.getLogger(__name__)diff --git a/python/samples/tool-use/coding_direct.py b/python/samples/tool-use/coding_direct.pyindex 8aa73b862..a2b76d215 100644--- a/python/samples/tool-use/coding_direct.py+++ b/python/samples/tool-use/coding_direct.py@@ -17,6 +17,7 @@ from dataclasses import dataclassfrom typing import Listfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentId, AgentInstantiationContextfrom agnext.components import FunctionCall, RoutedAgent, message_handlerfrom agnext.components.code_executor import LocalCommandLineCodeExecutorfrom agnext.components.models import (@@ -30,11 +31,10 @@ from agnext.components.models import ()from agnext.components.tool_agent import ToolAgent, ToolExceptionfrom agnext.components.tools import PythonCodeExecutionTool, Tool, ToolSchema-from agnext.core import AgentId, AgentInstantiationContextsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.utils import get_chat_completion_client_from_envsdiff --git a/python/samples/tool-use/coding_direct_with_intercept.py b/python/samples/tool-use/coding_direct_with_intercept.pyindex 64475ca56..c4341084a 100644--- a/python/samples/tool-use/coding_direct_with_intercept.py+++ b/python/samples/tool-use/coding_direct_with_intercept.py@@ -11,13 +11,13 @@ import sysfrom typing import Any, Listfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentId, AgentInstantiationContext+from agnext.base.intervention import DefaultInterventionHandler, DropMessagefrom agnext.components import FunctionCallfrom agnext.components.code_executor import LocalCommandLineCodeExecutorfrom agnext.components.models import SystemMessagefrom agnext.components.tool_agent import ToolAgent, ToolExceptionfrom agnext.components.tools import PythonCodeExecutionTool, Tool-from agnext.core import AgentId, AgentInstantiationContext-from agnext.core.intervention import DefaultInterventionHandler, DropMessagesys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))diff --git a/python/samples/tool-use/coding_pub_sub.py b/python/samples/tool-use/coding_pub_sub.pyindex aa1c0f793..810d8f69f 100644--- a/python/samples/tool-use/coding_pub_sub.py+++ b/python/samples/tool-use/coding_pub_sub.py@@ -35,7 +35,7 @@ from agnext.components.tools import PythonCodeExecutionTool, Toolsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import MessageContext+from agnext.base import MessageContextfrom common.utils import get_chat_completion_client_from_envsdiff --git a/python/samples/tool-use/custom_tool_direct.py b/python/samples/tool-use/custom_tool_direct.pyindex 9b975a6ab..0cc882c38 100644--- a/python/samples/tool-use/custom_tool_direct.py+++ b/python/samples/tool-use/custom_tool_direct.py@@ -10,18 +10,18 @@ import sysfrom typing import Listfrom agnext.application import SingleThreadedAgentRuntime+from agnext.base import AgentInstantiationContextfrom agnext.components.models import (SystemMessage,)from agnext.components.tool_agent import ToolAgentfrom agnext.components.tools import FunctionTool, Tool-from agnext.core import AgentInstantiationContextfrom typing_extensions import Annotatedsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__))))sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))-from agnext.core import AgentId+from agnext.base import AgentIdfrom coding_direct import Message, ToolUseAgentfrom common.utils import get_chat_completion_client_from_envsdiff --git a/python/samples/worker/run_worker_pub_sub.py b/python/samples/worker/run_worker_pub_sub.pyindex b12f21d4e..455715c4a 100644--- a/python/samples/worker/run_worker_pub_sub.py+++ b/python/samples/worker/run_worker_pub_sub.py@@ -4,8 +4,8 @@ from dataclasses import dataclassfrom typing import Any, NoReturnfrom agnext.application import WorkerAgentRuntime+from agnext.base import MESSAGE_TYPE_REGISTRY, MessageContextfrom agnext.components import DefaultSubscription, DefaultTopicId, RoutedAgent, message_handler-from agnext.core import MESSAGE_TYPE_REGISTRY, MessageContext@dataclassdiff --git a/python/samples/worker/run_worker_rpc.py b/python/samples/worker/run_worker_rpc.pyindex c371a6f4c..d349878ad 100644--- a/python/samples/worker/run_worker_rpc.py+++ b/python/samples/worker/run_worker_rpc.py@@ -4,8 +4,8 @@ from dataclasses import dataclassfrom typing import Any, NoReturnfrom agnext.application import WorkerAgentRuntime+from agnext.base import MESSAGE_TYPE_REGISTRY, AgentId, AgentInstantiationContext, MessageContextfrom agnext.components import DefaultSubscription, DefaultTopicId, RoutedAgent, message_handler-from agnext.core import MESSAGE_TYPE_REGISTRY, AgentId, AgentInstantiationContext, MessageContext@dataclassdiff --git a/python/src/agnext/application/_helpers.py b/python/src/agnext/application/_helpers.pyindex 3432965da..6c7fae0e2 100644--- a/python/src/agnext/application/_helpers.py+++ b/python/src/agnext/application/_helpers.py@@ -1,11 +1,11 @@from collections import defaultdictfrom typing import Awaitable, Callable, DefaultDict, List, Set-from ..core._agent import Agent-from ..core._agent_id import AgentId-from ..core._agent_type import AgentType-from ..core._subscription import Subscription-from ..core._topic import TopicId+from ..base._agent import Agent+from ..base._agent_id import AgentId+from ..base._agent_type import AgentType+from ..base._subscription import Subscription+from ..base._topic import TopicIdasync def get_impl(diff --git a/python/src/agnext/application/_host_runtime_servicer.py b/python/src/agnext/application/_host_runtime_servicer.pyindex 9c167810d..46e8547bc 100644--- a/python/src/agnext/application/_host_runtime_servicer.py+++ b/python/src/agnext/application/_host_runtime_servicer.py@@ -6,8 +6,8 @@ from typing import Any, Dict, Setimport grpc+from ..base import TopicIdfrom ..components import TypeSubscription-from ..core import TopicIdfrom ._helpers import SubscriptionManagerfrom .protos import agent_worker_pb2, agent_worker_pb2_grpcdiff --git a/python/src/agnext/application/_single_threaded_agent_runtime.py b/python/src/agnext/application/_single_threaded_agent_runtime.pyindex f493e8dfd..f1cb27237 100644--- a/python/src/agnext/application/_single_threaded_agent_runtime.py+++ b/python/src/agnext/application/_single_threaded_agent_runtime.py@@ -11,7 +11,7 @@ from dataclasses import dataclassfrom enum import Enumfrom typing import Any, Awaitable, Callable, Dict, List, Mapping, ParamSpec, Set, Type, TypeVar, cast-from ..core import (+from ..base import (Agent,AgentId,AgentInstantiationContext,@@ -25,8 +25,8 @@ from ..core import (SubscriptionInstantiationContext,TopicId,)-from ..core.exceptions import MessageDroppedException-from ..core.intervention import DropMessage, InterventionHandler+from ..base.exceptions import MessageDroppedException+from ..base.intervention import DropMessage, InterventionHandlerfrom ._helpers import SubscriptionManager, get_impllogger = logging.getLogger("agnext")diff --git a/python/src/agnext/application/_worker_runtime.py b/python/src/agnext/application/_worker_runtime.pyindex e4004e1be..c5c28466f 100644--- a/python/src/agnext/application/_worker_runtime.py+++ b/python/src/agnext/application/_worker_runtime.py@@ -28,8 +28,7 @@ import grpcfrom grpc.aio import StreamStreamCallfrom typing_extensions import Self-from ..components import TypeSubscription-from ..core import (+from ..base import (MESSAGE_TYPE_REGISTRY,Agent,AgentId,@@ -44,6 +43,7 @@ from ..core import (SubscriptionInstantiationContext,TopicId,)+from ..components import TypeSubscriptionfrom ._helpers import SubscriptionManager, get_implfrom .protos import agent_worker_pb2, agent_worker_pb2_grpcdiff --git a/python/src/agnext/application/logging/events.py b/python/src/agnext/application/logging/events.pyindex f3fc71151..4d33ba3c0 100644--- a/python/src/agnext/application/logging/events.py+++ b/python/src/agnext/application/logging/events.py@@ -2,7 +2,7 @@ import jsonfrom enum import Enumfrom typing import Any, cast-from agnext.core import AgentId+from agnext.base import AgentIdclass LLMCallEvent:diff --git a/python/src/agnext/core/__init__.py b/python/src/agnext/base/__init__.pysimilarity index 95%rename from python/src/agnext/core/__init__.pyrename to python/src/agnext/base/__init__.pyindex 9e1d76c2a..a84d49b11 100644--- a/python/src/agnext/core/__init__.py+++ b/python/src/agnext/base/__init__.py@@ -1,5 +1,5 @@"""-The :mod:`agnext.core` module provides the foundational generic interfaces upon which all else is built. This module must not depend on any other module.+The :mod:`agnext.base` module provides the foundational generic interfaces upon which all else is built. This module must not depend on any other module."""from ._agent import Agentdiff --git a/python/src/agnext/core/_agent.py b/python/src/agnext/base/_agent.pysimilarity index 100%rename from python/src/agnext/core/_agent.pyrename to python/src/agnext/base/_agent.pydiff --git a/python/src/agnext/core/_agent_id.py b/python/src/agnext/base/_agent_id.pysimilarity index 100%rename from python/src/agnext/core/_agent_id.pyrename to python/src/agnext/base/_agent_id.pydiff --git a/python/src/agnext/core/_agent_instantiation.py b/python/src/agnext/base/_agent_instantiation.pysimilarity index 100%rename from python/src/agnext/core/_agent_instantiation.pyrename to python/src/agnext/base/_agent_instantiation.pydiff --git a/python/src/agnext/core/_agent_metadata.py b/python/src/agnext/base/_agent_metadata.pysimilarity index 100%rename from python/src/agnext/core/_agent_metadata.pyrename to python/src/agnext/base/_agent_metadata.pydiff --git a/python/src/agnext/core/_agent_props.py b/python/src/agnext/base/_agent_props.pysimilarity index 100%rename from python/src/agnext/core/_agent_props.pyrename to python/src/agnext/base/_agent_props.pydiff --git a/python/src/agnext/core/_agent_proxy.py b/python/src/agnext/base/_agent_proxy.pysimilarity index 100%rename from python/src/agnext/core/_agent_proxy.pyrename to python/src/agnext/base/_agent_proxy.pydiff --git a/python/src/agnext/core/_agent_runtime.py b/python/src/agnext/base/_agent_runtime.pysimilarity index 99%rename from python/src/agnext/core/_agent_runtime.pyrename to python/src/agnext/base/_agent_runtime.pyindex 7821db8f1..d9734e1fb 100644--- a/python/src/agnext/core/_agent_runtime.py+++ b/python/src/agnext/base/_agent_runtime.py@@ -79,7 +79,7 @@ class AgentRuntime(Protocol):Args:type (str): The type of agent this factory creates. It is not the same as agent class name. The `type` parameter is used to differentiate between different factory functions rather than agent classes.- agent_factory (Callable[[], T]): The factory that creates the agent, where T is a concrete Agent type. Inside the factory, use `agnext.core.AgentInstantiationContext` to access variables like the current runtime and agent ID.+ agent_factory (Callable[[], T]): The factory that creates the agent, where T is a concrete Agent type. Inside the factory, use `agnext.base.AgentInstantiationContext` to access variables like the current runtime and agent ID.subscriptions (Callable[[], list[Subscription]] | list[Subscription] | None, optional): The subscriptions that the agent should be subscribed to. Defaults to None.diff --git a/python/src/agnext/core/_agent_type.py b/python/src/agnext/base/_agent_type.pysimilarity index 100%rename from python/src/agnext/core/_agent_type.pyrename to python/src/agnext/base/_agent_type.pydiff --git a/python/src/agnext/core/_base_agent.py b/python/src/agnext/base/_base_agent.pysimilarity index 97%rename from python/src/agnext/core/_base_agent.pyrename to python/src/agnext/base/_base_agent.pyindex b5239faf9..7dec8d5d3 100644--- a/python/src/agnext/core/_base_agent.py+++ b/python/src/agnext/base/_base_agent.py@@ -55,7 +55,7 @@ class BaseAgent(ABC, Agent):*,cancellation_token: CancellationToken | None = None,) -> Any:- """See :py:meth:`agnext.core.AgentRuntime.send_message` for more information."""+ """See :py:meth:`agnext.base.AgentRuntime.send_message` for more information."""if cancellation_token is None:cancellation_token = CancellationToken()diff --git a/python/src/agnext/core/_cancellation_token.py b/python/src/agnext/base/_cancellation_token.pysimilarity index 100%rename from python/src/agnext/core/_cancellation_token.pyrename to python/src/agnext/base/_cancellation_token.pydiff --git a/python/src/agnext/core/_message_context.py b/python/src/agnext/base/_message_context.pysimilarity index 100%rename from python/src/agnext/core/_message_context.pyrename to python/src/agnext/base/_message_context.pydiff --git a/python/src/agnext/core/_message_handler_context.py b/python/src/agnext/base/_message_handler_context.pysimilarity index 100%rename from python/src/agnext/core/_message_handler_context.pyrename to python/src/agnext/base/_message_handler_context.pydiff --git a/python/src/agnext/core/_serialization.py b/python/src/agnext/base/_serialization.pysimilarity index 100%rename from python/src/agnext/core/_serialization.pyrename to python/src/agnext/base/_serialization.pydiff --git a/python/src/agnext/core/_subscription.py b/python/src/agnext/base/_subscription.pysimilarity index 98%rename from python/src/agnext/core/_subscription.pyrename to python/src/agnext/base/_subscription.pyindex d606fc176..9595a9795 100644--- a/python/src/agnext/core/_subscription.py+++ b/python/src/agnext/base/_subscription.py@@ -1,6 +1,6 @@from typing import Protocol, runtime_checkable-from agnext.core import AgentId+from agnext.base import AgentIdfrom ._topic import TopicIddiff --git a/python/src/agnext/core/_subscription_context.py b/python/src/agnext/base/_subscription_context.pysimilarity index 96%rename from python/src/agnext/core/_subscription_context.pyrename to python/src/agnext/base/_subscription_context.pyindex 7e8c64515..0b560c685 100644--- a/python/src/agnext/core/_subscription_context.py+++ b/python/src/agnext/base/_subscription_context.py@@ -2,7 +2,7 @@ from contextlib import contextmanagerfrom contextvars import ContextVarfrom typing import Any, ClassVar, Generator-from agnext.core._agent_type import AgentType+from agnext.base._agent_type import AgentTypeclass SubscriptionInstantiationContext:diff --git a/python/src/agnext/core/_topic.py b/python/src/agnext/base/_topic.pysimilarity index 100%rename from python/src/agnext/core/_topic.pyrename to python/src/agnext/base/_topic.pydiff --git a/python/src/agnext/core/exceptions.py b/python/src/agnext/base/exceptions.pysimilarity index 100%rename from python/src/agnext/core/exceptions.pyrename to python/src/agnext/base/exceptions.pydiff --git a/python/src/agnext/core/intervention.py b/python/src/agnext/base/intervention.pysimilarity index 97%rename from python/src/agnext/core/intervention.pyrename to python/src/agnext/base/intervention.pyindex afa837085..5e1677c9d 100644--- a/python/src/agnext/core/intervention.py+++ b/python/src/agnext/base/intervention.py@@ -1,6 +1,6 @@from typing import Any, Awaitable, Callable, Protocol, final-from agnext.core import AgentId+from agnext.base import AgentId__all__ = ["DropMessage",diff --git a/python/src/agnext/components/_closure_agent.py b/python/src/agnext/components/_closure_agent.pyindex f048db669..73400b5e1 100644--- a/python/src/agnext/components/_closure_agent.py+++ b/python/src/agnext/components/_closure_agent.py@@ -1,15 +1,15 @@import inspectfrom typing import Any, Awaitable, Callable, Mapping, Sequence, TypeVar, get_type_hints-from agnext.core import MessageContext--from ..core._agent import Agent-from ..core._agent_id import AgentId-from ..core._agent_instantiation import AgentInstantiationContext-from ..core._agent_metadata import AgentMetadata-from ..core._agent_runtime import AgentRuntime-from ..core._serialization import MESSAGE_TYPE_REGISTRY-from ..core.exceptions import CantHandleException+from agnext.base import MessageContext++from ..base._agent import Agent+from ..base._agent_id import AgentId+from ..base._agent_instantiation import AgentInstantiationContext+from ..base._agent_metadata import AgentMetadata+from ..base._agent_runtime import AgentRuntime+from ..base._serialization import MESSAGE_TYPE_REGISTRY+from ..base.exceptions import CantHandleExceptionfrom ._type_helpers import get_typesT = TypeVar("T")diff --git a/python/src/agnext/components/_default_subscription.py b/python/src/agnext/components/_default_subscription.pyindex c7606186d..f85585077 100644--- a/python/src/agnext/components/_default_subscription.py+++ b/python/src/agnext/components/_default_subscription.py@@ -1,6 +1,6 @@-from agnext.core.exceptions import CantHandleException+from agnext.base.exceptions import CantHandleException-from ..core import SubscriptionInstantiationContext+from ..base import SubscriptionInstantiationContextfrom ._type_subscription import TypeSubscriptiondiff --git a/python/src/agnext/components/_default_topic.py b/python/src/agnext/components/_default_topic.pyindex 6e58bf947..9c8e0c5ca 100644--- a/python/src/agnext/components/_default_topic.py+++ b/python/src/agnext/components/_default_topic.py@@ -1,4 +1,4 @@-from ..core import MessageHandlerContext, TopicId+from ..base import MessageHandlerContext, TopicIdclass DefaultTopicId(TopicId):diff --git a/python/src/agnext/components/_routed_agent.py b/python/src/agnext/components/_routed_agent.pyindex a3fc984e7..c45aa408a 100644--- a/python/src/agnext/components/_routed_agent.py+++ b/python/src/agnext/components/_routed_agent.py@@ -17,8 +17,8 @@ from typing import (runtime_checkable,)-from ..core import MESSAGE_TYPE_REGISTRY, BaseAgent, MessageContext-from ..core.exceptions import CantHandleException+from ..base import MESSAGE_TYPE_REGISTRY, BaseAgent, MessageContext+from ..base.exceptions import CantHandleExceptionfrom ._type_helpers import AnyType, get_typeslogger = logging.getLogger("agnext")diff --git a/python/src/agnext/components/_type_subscription.py b/python/src/agnext/components/_type_subscription.pyindex 28e8c2a46..de619f6f2 100644--- a/python/src/agnext/components/_type_subscription.py+++ b/python/src/agnext/components/_type_subscription.py@@ -1,8 +1,8 @@import uuid-from agnext.core.exceptions import CantHandleException+from agnext.base.exceptions import CantHandleException-from ..core import AgentId, Subscription, TopicId+from ..base import AgentId, Subscription, TopicIdclass TypeSubscription(Subscription):diff --git a/python/src/agnext/components/code_executor/_base.py b/python/src/agnext/components/code_executor/_base.pyindex 8e440cef6..851b94fde 100644--- a/python/src/agnext/components/code_executor/_base.py+++ b/python/src/agnext/components/code_executor/_base.py@@ -6,7 +6,7 @@ from __future__ import annotationsfrom dataclasses import dataclassfrom typing import List, Protocol, runtime_checkable-from agnext.core import CancellationToken+from agnext.base import CancellationToken@dataclassdiff --git a/python/src/agnext/components/code_executor/_impl/azure_container_code_executor.py b/python/src/agnext/components/code_executor/_impl/azure_container_code_executor.pyindex 9ace177de..d84fbe01a 100644--- a/python/src/agnext/components/code_executor/_impl/azure_container_code_executor.py+++ b/python/src/agnext/components/code_executor/_impl/azure_container_code_executor.py@@ -16,7 +16,7 @@ from azure.core.credentials import AccessToken# from azure.mgmt.appcontainers import ContainerAppsAPIClientfrom typing_extensions import ParamSpec-from ....core import CancellationToken+from ....base import CancellationTokenfrom .._base import CodeBlock, CodeExecutor, CodeResultfrom .._func_with_reqs import (FunctionWithRequirements,diff --git a/python/src/agnext/components/code_executor/_impl/local_commandline_code_executor.py b/python/src/agnext/components/code_executor/_impl/local_commandline_code_executor.pyindex 686835df2..60ddf3c0e 100644--- a/python/src/agnext/components/code_executor/_impl/local_commandline_code_executor.py+++ b/python/src/agnext/components/code_executor/_impl/local_commandline_code_executor.py@@ -12,7 +12,7 @@ from typing import Any, Callable, ClassVar, List, Sequence, Unionfrom typing_extensions import ParamSpec-from ....core import CancellationToken+from ....base import CancellationTokenfrom .._base import CodeBlock, CodeExecutorfrom .._func_with_reqs import (FunctionWithRequirements,diff --git a/python/src/agnext/components/models/_model_client.py b/python/src/agnext/components/models/_model_client.pyindex 36696ae0b..532bb2ea1 100644--- a/python/src/agnext/components/models/_model_client.py+++ b/python/src/agnext/components/models/_model_client.py@@ -11,7 +11,7 @@ from typing_extensions import (Union,)-from ...core import CancellationToken+from ...base import CancellationTokenfrom ..tools import Tool, ToolSchemafrom ._types import CreateResult, LLMMessage, RequestUsagediff --git a/python/src/agnext/components/models/_openai_client.py b/python/src/agnext/components/models/_openai_client.pyindex a00d79154..7e81a4ff4 100644--- a/python/src/agnext/components/models/_openai_client.py+++ b/python/src/agnext/components/models/_openai_client.py@@ -37,7 +37,7 @@ from typing_extensions import Unpackfrom ...application.logging import EVENT_LOGGER_NAME, TRACE_LOGGER_NAMEfrom ...application.logging.events import LLMCallEvent-from ...core import CancellationToken+from ...base import CancellationTokenfrom .. import (FunctionCall,Image,diff --git a/python/src/agnext/components/tool_agent/_caller_loop.py b/python/src/agnext/components/tool_agent/_caller_loop.pyindex ab50e6e86..ea2922f23 100644--- a/python/src/agnext/components/tool_agent/_caller_loop.py+++ b/python/src/agnext/components/tool_agent/_caller_loop.py@@ -1,8 +1,8 @@import asynciofrom typing import List+from ...base import AgentId, AgentRuntime, BaseAgent, CancellationTokenfrom ...components import FunctionCall-from ...core import AgentId, AgentRuntime, BaseAgent, CancellationTokenfrom ..models import (AssistantMessage,ChatCompletionClient,diff --git a/python/src/agnext/components/tool_agent/_tool_agent.py b/python/src/agnext/components/tool_agent/_tool_agent.pyindex b3fa92537..8e1c2993e 100644--- a/python/src/agnext/components/tool_agent/_tool_agent.py+++ b/python/src/agnext/components/tool_agent/_tool_agent.py@@ -2,7 +2,7 @@ import jsonfrom dataclasses import dataclassfrom typing import List-from ...core import MessageContext+from ...base import MessageContextfrom .. import FunctionCall, RoutedAgent, message_handlerfrom ..models import FunctionExecutionResultfrom ..tools import Tooldiff --git a/python/src/agnext/components/tools/_base.py b/python/src/agnext/components/tools/_base.pyindex 2dfaefff7..abbd30025 100644--- a/python/src/agnext/components/tools/_base.py+++ b/python/src/agnext/components/tools/_base.py@@ -6,7 +6,7 @@ from typing import Any, Dict, Generic, Mapping, Protocol, Type, TypedDict, TypeVfrom pydantic import BaseModelfrom typing_extensions import NotRequired-from ...core import CancellationToken+from ...base import CancellationTokenfrom .._function_utils import normalize_annotated_typeT = TypeVar("T", bound=BaseModel, contravariant=True)diff --git a/python/src/agnext/components/tools/_code_execution.py b/python/src/agnext/components/tools/_code_execution.pyindex 40a9f6fb6..48dbe729a 100644--- a/python/src/agnext/components/tools/_code_execution.py+++ b/python/src/agnext/components/tools/_code_execution.py@@ -1,6 +1,6 @@from pydantic import BaseModel, Field, model_serializer-from ...core import CancellationToken+from ...base import CancellationTokenfrom ..code_executor import CodeBlock, CodeExecutorfrom ._base import BaseTooldiff --git a/python/src/agnext/components/tools/_function_tool.py b/python/src/agnext/components/tools/_function_tool.pyindex f0536b9bb..131cea748 100644--- a/python/src/agnext/components/tools/_function_tool.py+++ b/python/src/agnext/components/tools/_function_tool.py@@ -4,7 +4,7 @@ from typing import Any, Callablefrom pydantic import BaseModel-from ...core import CancellationToken+from ...base import CancellationTokenfrom .._function_utils import (args_base_model_from_signature,get_typed_signature,diff --git a/python/teams/team-one/examples/example.py b/python/teams/team-one/examples/example.pyindex 1fb9aa869..cf524a0a7 100644--- a/python/teams/team-one/examples/example.py+++ b/python/teams/team-one/examples/example.py@@ -3,8 +3,8 @@ import loggingfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import AgentId, AgentProxyfrom agnext.components import DefaultSubscription-from agnext.core import AgentId, AgentProxyfrom team_one.agents.coder import Coder, Executorfrom team_one.agents.orchestrator import LedgerOrchestratorfrom team_one.agents.user_proxy import UserProxydiff --git a/python/teams/team-one/examples/example_coder.py b/python/teams/team-one/examples/example_coder.pyindex a4143a063..5964d5686 100644--- a/python/teams/team-one/examples/example_coder.py+++ b/python/teams/team-one/examples/example_coder.py@@ -3,8 +3,8 @@ import loggingfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import AgentId, AgentProxyfrom agnext.components import DefaultSubscription-from agnext.core import AgentId, AgentProxyfrom team_one.agents.coder import Coder, Executorfrom team_one.agents.orchestrator import RoundRobinOrchestratorfrom team_one.agents.user_proxy import UserProxydiff --git a/python/teams/team-one/examples/example_file_surfer.py b/python/teams/team-one/examples/example_file_surfer.pyindex fe6d7f273..9a1db1533 100644--- a/python/teams/team-one/examples/example_file_surfer.py+++ b/python/teams/team-one/examples/example_file_surfer.py@@ -3,8 +3,8 @@ import loggingfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import AgentId, AgentProxyfrom agnext.components import DefaultSubscription-from agnext.core import AgentId, AgentProxyfrom team_one.agents.file_surfer import FileSurferfrom team_one.agents.orchestrator import RoundRobinOrchestratorfrom team_one.agents.user_proxy import UserProxydiff --git a/python/teams/team-one/examples/example_reflexagents.py b/python/teams/team-one/examples/example_reflexagents.pyindex aa95b44c5..dc6df5ca2 100644--- a/python/teams/team-one/examples/example_reflexagents.py+++ b/python/teams/team-one/examples/example_reflexagents.py@@ -3,9 +3,9 @@ import loggingfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import AgentId, AgentProxyfrom agnext.components import DefaultSubscription, DefaultTopicIdfrom agnext.components.models import UserMessage-from agnext.core import AgentId, AgentProxyfrom team_one.agents.orchestrator import RoundRobinOrchestratorfrom team_one.agents.reflex_agents import ReflexAgentfrom team_one.messages import BroadcastMessagediff --git a/python/teams/team-one/examples/example_userproxy.py b/python/teams/team-one/examples/example_userproxy.pyindex e29cfdb61..4ca54f5ec 100644--- a/python/teams/team-one/examples/example_userproxy.py+++ b/python/teams/team-one/examples/example_userproxy.py@@ -3,10 +3,10 @@ import loggingfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import AgentId, AgentProxy# from typing import Any, Dict, List, Tuple, Unionfrom agnext.components import DefaultSubscription-from agnext.core import AgentId, AgentProxyfrom team_one.agents.coder import Coderfrom team_one.agents.orchestrator import RoundRobinOrchestratorfrom team_one.agents.user_proxy import UserProxydiff --git a/python/teams/team-one/examples/example_websurfer.py b/python/teams/team-one/examples/example_websurfer.pyindex c149cfec3..38920057a 100644--- a/python/teams/team-one/examples/example_websurfer.py+++ b/python/teams/team-one/examples/example_websurfer.py@@ -4,8 +4,8 @@ import osfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import AgentId, AgentProxyfrom agnext.components import DefaultSubscription-from agnext.core import AgentId, AgentProxyfrom team_one.agents.multimodal_web_surfer import MultimodalWebSurferfrom team_one.agents.orchestrator import RoundRobinOrchestratorfrom team_one.agents.user_proxy import UserProxydiff --git a/python/teams/team-one/src/team_one/agents/base_agent.py b/python/teams/team-one/src/team_one/agents/base_agent.pyindex 4d30a1cb5..2f0c6ce0f 100644--- a/python/teams/team-one/src/team_one/agents/base_agent.py+++ b/python/teams/team-one/src/team_one/agents/base_agent.py@@ -3,8 +3,8 @@ import loggingfrom typing import Anyfrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import MessageContextfrom agnext.components import RoutedAgent, message_handler-from agnext.core import MessageContextfrom team_one.messages import (AgentEvent,diff --git a/python/teams/team-one/src/team_one/agents/base_orchestrator.py b/python/teams/team-one/src/team_one/agents/base_orchestrator.pyindex c039bfa79..9c7250ca0 100644--- a/python/teams/team-one/src/team_one/agents/base_orchestrator.py+++ b/python/teams/team-one/src/team_one/agents/base_orchestrator.py@@ -3,8 +3,8 @@ import timefrom typing import List, Optionalfrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import AgentProxy, CancellationToken, MessageContextfrom agnext.components.models import AssistantMessage, LLMMessage, UserMessage-from agnext.core import AgentProxy, CancellationToken, MessageContextfrom ..messages import BroadcastMessage, OrchestrationEvent, RequestReplyMessage, ResetMessagefrom ..utils import message_content_to_strdiff --git a/python/teams/team-one/src/team_one/agents/base_worker.py b/python/teams/team-one/src/team_one/agents/base_worker.pyindex 6f8b68884..a7ba690bb 100644--- a/python/teams/team-one/src/team_one/agents/base_worker.py+++ b/python/teams/team-one/src/team_one/agents/base_worker.py@@ -1,11 +1,11 @@from typing import List, Tuple+from agnext.base import CancellationToken, MessageContext, TopicIdfrom agnext.components.models import (AssistantMessage,LLMMessage,UserMessage,)-from agnext.core import CancellationToken, MessageContext, TopicIdfrom team_one.messages import (BroadcastMessage,diff --git a/python/teams/team-one/src/team_one/agents/coder.py b/python/teams/team-one/src/team_one/agents/coder.pyindex 0b5f79766..f8fe7878e 100644--- a/python/teams/team-one/src/team_one/agents/coder.py+++ b/python/teams/team-one/src/team_one/agents/coder.py@@ -1,13 +1,13 @@import refrom typing import List, Optional, Tuple, Union+from agnext.base import CancellationTokenfrom agnext.components.code_executor import CodeBlock, CodeExecutor, LocalCommandLineCodeExecutorfrom agnext.components.models import (ChatCompletionClient,SystemMessage,UserMessage,)-from agnext.core import CancellationTokenfrom ..messages import UserContentfrom ..utils import message_content_to_strdiff --git a/python/teams/team-one/src/team_one/agents/file_surfer/file_surfer.py b/python/teams/team-one/src/team_one/agents/file_surfer/file_surfer.pyindex bd85cf7a6..7e7031821 100644--- a/python/teams/team-one/src/team_one/agents/file_surfer/file_surfer.py+++ b/python/teams/team-one/src/team_one/agents/file_surfer/file_surfer.py@@ -2,13 +2,13 @@ import jsonimport timefrom typing import List, Optional, Tuple+from agnext.base import CancellationTokenfrom agnext.components import FunctionCallfrom agnext.components.models import (ChatCompletionClient,SystemMessage,UserMessage,)-from agnext.core import CancellationTokenfrom ...markdown_browser import RequestsMarkdownBrowserfrom ..base_worker import BaseWorkerdiff --git a/python/teams/team-one/src/team_one/agents/multimodal_web_surfer/multimodal_web_surfer.py b/python/teams/team-one/src/team_one/agents/multimodal_web_surfer/multimodal_web_surfer.pyindex 293e50f7f..024749a21 100644--- a/python/teams/team-one/src/team_one/agents/multimodal_web_surfer/multimodal_web_surfer.py+++ b/python/teams/team-one/src/team_one/agents/multimodal_web_surfer/multimodal_web_surfer.py@@ -12,6 +12,7 @@ from urllib.parse import quote_plus # parse_qs, quote, unquote, urlparse, urlunimport aiofilesfrom agnext.application.logging import EVENT_LOGGER_NAME+from agnext.base import CancellationTokenfrom agnext.components import FunctionCallfrom agnext.components import Image as AGImagefrom agnext.components.models import (@@ -21,7 +22,6 @@ from agnext.components.models import (SystemMessage,UserMessage,)-from agnext.core import CancellationTokenfrom PIL import Imagefrom playwright._impl._errors import Error as PlaywrightErrorfrom playwright._impl._errors import TimeoutErrordiff --git a/python/teams/team-one/src/team_one/agents/orchestrator.py b/python/teams/team-one/src/team_one/agents/orchestrator.pyindex f8f08bacb..78ae57e9f 100644--- a/python/teams/team-one/src/team_one/agents/orchestrator.py+++ b/python/teams/team-one/src/team_one/agents/orchestrator.py@@ -1,8 +1,8 @@import jsonfrom typing import Any, Dict, List, Optional+from agnext.base import AgentProxy, MessageContext, TopicIdfrom agnext.components.models import AssistantMessage, ChatCompletionClient, LLMMessage, SystemMessage, UserMessage-from agnext.core import AgentProxy, MessageContext, TopicIdfrom ..messages import BroadcastMessage, OrchestrationEvent, ResetMessagefrom .base_orchestrator import BaseOrchestrator, loggerdiff --git a/python/teams/team-one/src/team_one/agents/reflex_agents.py b/python/teams/team-one/src/team_one/agents/reflex_agents.pyindex 31d50bde7..3fa642e2a 100644--- a/python/teams/team-one/src/team_one/agents/reflex_agents.py+++ b/python/teams/team-one/src/team_one/agents/reflex_agents.py@@ -1,6 +1,6 @@+from agnext.base import MessageContext, TopicIdfrom agnext.components import RoutedAgent, message_handlerfrom agnext.components.models import UserMessage-from agnext.core import MessageContext, TopicIdfrom ..messages import BroadcastMessage, RequestReplyMessagediff --git a/python/teams/team-one/src/team_one/agents/user_proxy.py b/python/teams/team-one/src/team_one/agents/user_proxy.pyindex 6d12f90c8..f5585f5d2 100755--- a/python/teams/team-one/src/team_one/agents/user_proxy.py+++ b/python/teams/team-one/src/team_one/agents/user_proxy.py@@ -1,7 +1,7 @@import asynciofrom typing import Tuple-from agnext.core import CancellationToken+from agnext.base import CancellationTokenfrom ..messages import UserContentfrom .base_worker import BaseWorkerdiff --git a/python/teams/team-one/tests/headless_web_surfer/test_web_surfer.py b/python/teams/team-one/tests/headless_web_surfer/test_web_surfer.pyindex 855c7cf53..083a49e86 100644--- a/python/teams/team-one/tests/headless_web_surfer/test_web_surfer.py+++ b/python/teams/team-one/tests/headless_web_surfer/test_web_surfer.py@@ -8,8 +8,8 @@ from math import ceilimport asyncioimport pytest-from agnext.core import AgentId-from agnext.core import AgentProxy+from agnext.base import AgentId+from agnext.base import AgentProxypytest_plugins = ('pytest_asyncio',)from json import dumpsdiff --git a/python/tests/execution/test_azure_container_code_executor.py b/python/tests/execution/test_azure_container_code_executor.pyindex b5e102362..a9e6d0c9c 100644--- a/python/tests/execution/test_azure_container_code_executor.py+++ b/python/tests/execution/test_azure_container_code_executor.py@@ -10,7 +10,7 @@ from anyio import open_fileimport pytestfrom azure.identity import DefaultAzureCredentialfrom agnext.components.code_executor import CodeBlock, AzureContainerCodeExecutor-from agnext.core import CancellationToken+from agnext.base import CancellationTokenUNIX_SHELLS = ["bash", "sh", "shell"]WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"]@@ -109,7 +109,7 @@ async def test_upload_files() -> None:test_file_2 = "test2"test_file_2_contents = "test file 2"cancellation_token = CancellationToken()-+with tempfile.TemporaryDirectory() as temp_dir:executor = AzureContainerCodeExecutor(pool_management_endpoint=POOL_ENDPOINT, credential=DefaultAzureCredential(), work_dir=temp_dir)@@ -148,7 +148,7 @@ async def test_download_files() -> None:test_file_2 = "test2"test_file_2_contents = "azure test file 2"cancellation_token = CancellationToken()-+with tempfile.TemporaryDirectory() as temp_dir:executor = AzureContainerCodeExecutor(pool_management_endpoint=POOL_ENDPOINT, credential=DefaultAzureCredential(), work_dir=temp_dir)diff --git a/python/tests/execution/test_commandline_code_executor.py b/python/tests/execution/test_commandline_code_executor.pyindex 5fbe0a8bd..12e49d705 100644--- a/python/tests/execution/test_commandline_code_executor.py+++ b/python/tests/execution/test_commandline_code_executor.py@@ -8,7 +8,7 @@ from pathlib import Pathimport pytestfrom agnext.components.code_executor import CodeBlock, LocalCommandLineCodeExecutor-from agnext.core import CancellationToken+from agnext.base import CancellationTokenUNIX_SHELLS = ["bash", "sh", "shell"]WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"]diff --git a/python/tests/execution/test_user_defined_functions.py b/python/tests/execution/test_user_defined_functions.pyindex 655cd9ed3..648497707 100644--- a/python/tests/execution/test_user_defined_functions.py+++ b/python/tests/execution/test_user_defined_functions.py@@ -15,7 +15,7 @@ from agnext.components.code_executor import (AzureContainerCodeExecutor,with_requirements,)-from agnext.core import CancellationToken+from agnext.base import CancellationTokenENVIRON_KEY_AZURE_POOL_ENDPOINT = "AZURE_POOL_ENDPOINT"@@ -255,11 +255,11 @@ def add_two_numbers(a: int, b: int) -> int:return a + b''')-+assert_str = '''def add_two_numbers(a: int, b: int) -> int:"""Add two numbers together."""'''-+with tempfile.TemporaryDirectory() as temp_dir:executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[func])diff --git a/python/tests/test_base_agent.py b/python/tests/test_base_agent.pyindex d05db1cff..cbbf1d43a 100644--- a/python/tests/test_base_agent.py+++ b/python/tests/test_base_agent.py@@ -1,6 +1,6 @@import pytestfrom pytest_mock import MockerFixture-from agnext.core import AgentRuntime, AgentInstantiationContext, AgentId+from agnext.base import AgentRuntime, AgentInstantiationContext, AgentIdfrom test_utils import NoopAgentdiff --git a/python/tests/test_cancellation.py b/python/tests/test_cancellation.pyindex 6dc915fde..eb8d47fcb 100644--- a/python/tests/test_cancellation.py+++ b/python/tests/test_cancellation.py@@ -4,9 +4,9 @@ from dataclasses import dataclassimport pytestfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.components import RoutedAgent, message_handler-from agnext.core import AgentId, CancellationToken-from agnext.core import MessageContext-from agnext.core import AgentInstantiationContext+from agnext.base import AgentId, CancellationToken+from agnext.base import MessageContext+from agnext.base import AgentInstantiationContext@dataclassdiff --git a/python/tests/test_closure_agent.py b/python/tests/test_closure_agent.pyindex 2492bc4a3..a3514a527 100644--- a/python/tests/test_closure_agent.py+++ b/python/tests/test_closure_agent.py@@ -6,15 +6,15 @@ import pytestfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.components._type_subscription import TypeSubscription-from agnext.core import AgentRuntime, AgentId+from agnext.base import AgentRuntime, AgentIdfrom agnext.components import ClosureAgentimport asyncio-from agnext.core import MessageContext-from agnext.core import TopicId+from agnext.base import MessageContext+from agnext.base import TopicId@dataclassclass Message:diff --git a/python/tests/test_intervention.py b/python/tests/test_intervention.pyindex f859cc18d..c7ddca613 100644--- a/python/tests/test_intervention.py+++ b/python/tests/test_intervention.py@@ -1,8 +1,8 @@import pytestfrom agnext.application import SingleThreadedAgentRuntime-from agnext.core import AgentId-from agnext.core.exceptions import MessageDroppedException-from agnext.core.intervention import DefaultInterventionHandler, DropMessage+from agnext.base import AgentId+from agnext.base.exceptions import MessageDroppedException+from agnext.base.intervention import DefaultInterventionHandler, DropMessagefrom test_utils import LoopbackAgent, MessageTypediff --git a/python/tests/test_model_client.py b/python/tests/test_model_client.pyindex 47f36ac13..5eafd9722 100644--- a/python/tests/test_model_client.py+++ b/python/tests/test_model_client.py@@ -16,7 +16,7 @@ from agnext.components.models import ()from agnext.components.models._model_info import resolve_modelfrom agnext.components.tools import FunctionTool-from agnext.core import CancellationToken+from agnext.base import CancellationTokenfrom openai.resources.chat.completions import AsyncCompletionsfrom openai.types.chat.chat_completion import ChatCompletion, Choicefrom openai.types.chat.chat_completion_chunk import ChatCompletionChunk, ChoiceDeltadiff --git a/python/tests/test_routed_agent.py b/python/tests/test_routed_agent.pyindex 4d45932e3..51af708dc 100644--- a/python/tests/test_routed_agent.py+++ b/python/tests/test_routed_agent.py@@ -4,7 +4,7 @@ import loggingfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.components import TypeSubscription-from agnext.core import TopicId+from agnext.base import TopicIdfrom test_utils import LoopbackAgentdiff --git a/python/tests/test_runtime.py b/python/tests/test_runtime.pyindex 8cde2a8f7..0af1e8cc1 100644--- a/python/tests/test_runtime.py+++ b/python/tests/test_runtime.py@@ -2,10 +2,10 @@ import asyncioimport pytestfrom agnext.application import SingleThreadedAgentRuntimefrom agnext.components import TypeSubscription, DefaultTopicId, DefaultSubscription-from agnext.core import AgentId, AgentInstantiationContext-from agnext.core import TopicId-from agnext.core import Subscription-from agnext.core import SubscriptionInstantiationContext+from agnext.base import AgentId, AgentInstantiationContext+from agnext.base import TopicId+from agnext.base import Subscription+from agnext.base import SubscriptionInstantiationContextfrom test_utils import CascadingAgent, CascadingMessageType, LoopbackAgent, MessageType, NoopAgentdiff --git a/python/tests/test_serialization.py b/python/tests/test_serialization.pyindex dacd272bc..5b292fffa 100644--- a/python/tests/test_serialization.py+++ b/python/tests/test_serialization.py@@ -5,7 +5,7 @@ from dataclasses import dataclassimport pytest-from agnext.core import Serialization+from agnext.base import Serializationclass PydanticMessage(BaseModel):message: strdiff --git a/python/tests/test_state.py b/python/tests/test_state.pyindex 33fc8d67a..c16906e24 100644--- a/python/tests/test_state.py+++ b/python/tests/test_state.py@@ -2,8 +2,8 @@ from typing import Any, Mappingimport pytestfrom agnext.application import SingleThreadedAgentRuntime-from agnext.core import BaseAgent, MessageContext-from agnext.core import AgentId+from agnext.base import BaseAgent, MessageContext+from agnext.base import AgentIdclass StatefulAgent(BaseAgent):diff --git a/python/tests/test_subscription.py b/python/tests/test_subscription.pyindex 849d2f008..5c0eb5248 100644--- a/python/tests/test_subscription.py+++ b/python/tests/test_subscription.py@@ -1,9 +1,9 @@from agnext.components import TypeSubscription-from agnext.core import TopicId, AgentId+from agnext.base import TopicId, AgentIdimport pytest-from agnext.core.exceptions import CantHandleException+from agnext.base.exceptions import CantHandleExceptiondef test_type_subscription_match() -> None:sub = TypeSubscription(topic_type="t1", agent_type="a1")diff --git a/python/tests/test_tool_agent.py b/python/tests/test_tool_agent.pyindex 7f7cc5089..76dcafe2a 100644--- a/python/tests/test_tool_agent.py+++ b/python/tests/test_tool_agent.py@@ -20,7 +20,7 @@ from agnext.components.tool_agent import (tool_agent_caller_loop,)from agnext.components.tools import FunctionTool, Tool-from agnext.core import CancellationToken, AgentId+from agnext.base import CancellationToken, AgentIdfrom agnext.components.models import (AssistantMessage,FunctionExecutionResult,@@ -162,10 +162,10 @@ async def test_caller_loop(monkeypatch: pytest.MonkeyPatch) -> None:agent = AgentId("tool_agent", "default")runtime.start()messages = await tool_agent_caller_loop(- runtime,- agent,- client,- [UserMessage(content="Hello", source="user")],+ runtime,+ agent,+ client,+ [UserMessage(content="Hello", source="user")],tool_schema=tools)assert len(messages) == 3diff --git a/python/tests/test_tools.py b/python/tests/test_tools.pyindex 18f554c7f..2d8081d3f 100644--- a/python/tests/test_tools.py+++ b/python/tests/test_tools.py@@ -6,7 +6,7 @@ import pytestfrom agnext.components._function_utils import get_typed_signaturefrom agnext.components.tools import BaseTool, FunctionToolfrom agnext.components.models._openai_client import convert_tools-from agnext.core import CancellationToken+from agnext.base import CancellationTokenfrom pydantic import BaseModel, Field, model_serializerfrom pydantic_core import PydanticUndefineddiff --git a/python/tests/test_types.py b/python/tests/test_types.pyindex 8665c0aa5..098f2556d 100644--- a/python/tests/test_types.py+++ b/python/tests/test_types.py@@ -3,7 +3,7 @@ from typing import Any, Optional, Unionfrom agnext.components._routed_agent import message_handlerfrom agnext.components._type_helpers import AnyType, get_types-from agnext.core import MessageContext+from agnext.base import MessageContextdef test_get_types() -> None:diff --git a/python/tests/test_utils/__init__.py b/python/tests/test_utils/__init__.pyindex acec46e8c..1ebb09eff 100644--- a/python/tests/test_utils/__init__.py+++ b/python/tests/test_utils/__init__.py@@ -3,8 +3,8 @@ from typing import Anyfrom agnext.components import RoutedAgent, message_handlerfrom agnext.components import DefaultTopicId-from agnext.core import BaseAgent-from agnext.core import MessageContext+from agnext.base import BaseAgent+from agnext.base import MessageContext@dataclass