-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Python: feat: enable dynamic tool loading by passing tools list in kwargs [progressive exposure] #3398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Python: feat: enable dynamic tool loading by passing tools list in kwargs [progressive exposure] #3398
Changes from 1 commit
554445d
e90fff2
5400f59
e265208
d5f19b7
f12e48f
b101121
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,135 @@ | ||||||
| # Copyright (c) Microsoft. All rights reserved. | ||||||
|
|
||||||
| """ | ||||||
| Dynamic Tool Loading Example | ||||||
| This sample demonstrates how tools can dynamically add new tools during execution, | ||||||
| which become immediately available for the same agent run. This is useful when: | ||||||
| - A tool needs to load additional capabilities based on context | ||||||
| - Tools need to be registered based on the result of a previous tool call | ||||||
| - Lazy loading of tools is needed for performance | ||||||
| The key is using **kwargs to receive the tools list from the framework. | ||||||
| """ | ||||||
suneetnangia marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| import asyncio | ||||||
| import logging | ||||||
| import os | ||||||
| from typing import Annotated, Any | ||||||
|
|
||||||
| from dotenv import load_dotenv | ||||||
|
|
||||||
| from agent_framework import ChatAgent, ai_function | ||||||
| from agent_framework.azure import AzureOpenAIChatClient | ||||||
| from azure.identity import AzureCliCredential | ||||||
|
|
||||||
| load_dotenv() | ||||||
|
|
||||||
| logging.basicConfig( | ||||||
| level=os.getenv("LOG_LEVEL", "INFO").upper(), | ||||||
| format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | ||||||
| force=True, | ||||||
| ) | ||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
| @ai_function | ||||||
| def load_math_tools( | ||||||
| operation: Annotated[str, "The math operation category to load (e.g., 'advanced')"], | ||||||
| **kwargs: Any, | ||||||
| ) -> str: | ||||||
| """Load additional math tools dynamically based on the requested category. | ||||||
| This tool demonstrates dynamic tool loading - it can add new tools to the | ||||||
| agent during execution, making them available for immediate use. | ||||||
| """ | ||||||
| # Access tools list directly | ||||||
| tools_list = kwargs.get("tools") | ||||||
|
|
||||||
| if not tools_list: | ||||||
|
||||||
| if not tools_list: | |
| if tools_list is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if the suggestion is valid in this instance.
Since we are checking in the tool itself, framework should never pass is an empty i.e. there should be at least 1 tool?
Uh oh!
There was an error while loading. Please reload this page.