Skip to content

Commit

Permalink
Add tools_pydantic_ai_tools_integration.ipynb tutorial notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
rjambrecic committed Dec 18, 2024
1 parent 89e1ac9 commit d0eab73
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion notebook/tools_pydantic_ai_tools_integration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -96,13 +121,25 @@
"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",
"ag2_tool.register_for_execution(user_proxy)\n",
"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,
Expand Down

0 comments on commit d0eab73

Please sign in to comment.