diff --git a/notebook/tools_pydantic_ai_tools_integration.ipynb b/notebook/tools_pydantic_ai_tools_integration.ipynb index 1b44cfe53e..edf2170135 100644 --- a/notebook/tools_pydantic_ai_tools_integration.ipynb +++ b/notebook/tools_pydantic_ai_tools_integration.ipynb @@ -24,7 +24,16 @@ { "cell_type": "markdown", "metadata": {}, - "source": [] + "source": [ + "## Imports\n", + "\n", + "Import necessary modules and tools.\n", + "- `BaseModel`: Used to define data structures for tool inputs and outputs.\n", + "- `RunContext`: Provides context during the execution of tools.\n", + "- `PydanticAITool`: Represents a tool in the PydanticAI framework.\n", + "- `AssistantAgent` and `UserProxyAgent`: Agents that facilitate communication in the AG2 framework.\n", + "- `PydanticAIInteroperability`: A bridge for integrating PydanticAI tools with the AG2 framework." + ] }, { "cell_type": "code", @@ -73,6 +82,22 @@ ")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tool Integration\n", + "\n", + "Integrate the PydanticAI tool with AG2.\n", + "\n", + "- Define a `Player` model using `BaseModel` to structure the input data.\n", + "- Use `RunContext` to securely inject dependencies (like the `Player` instance) into the tool function without exposing them to the LLM.\n", + "- Implement `get_player` to define the tool's functionality, accessing `ctx.deps` for injected data.\n", + "- Convert the tool to an AG2-compatible format with `PydanticAIInteroperability` and register it for execution and LLM communication.\n", + "- Convert the PydanticAI tool into an AG2-compatible format using `convert_tool`.\n", + "- Register the tool for both execution and communication with the LLM by associating it with the `user_proxy` and `chatbot`." + ] + }, { "cell_type": "code", "execution_count": 3, @@ -96,6 +121,7 @@ "pydantic_ai_interop = PydanticAIInteroperability()\n", "pydantic_ai_tool = PydanticAITool(get_player, takes_ctx=True)\n", "\n", + "# player will be injected as a dependency\n", "player = Player(name=\"Luka\", age=25)\n", "ag2_tool = pydantic_ai_interop.convert_tool(tool=pydantic_ai_tool, deps=player)\n", "\n", @@ -103,6 +129,17 @@ "ag2_tool.register_for_llm(chatbot)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Initiate a conversation between the `UserProxyAgent` and the `AssistantAgent`.\n", + "\n", + "- Use the `initiate_chat` method to send a message from the `user_proxy` to the `chatbot`.\n", + "- In this example, the user requests the chatbot to retrieve player information, providing \"goal keeper\" as additional context.\n", + "- The `Player` instance is securely injected into the tool using `RunContext`, ensuring the chatbot can retrieve and use this data during the interaction." + ] + }, { "cell_type": "code", "execution_count": null,