mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fixed AutoGen migration and tool samples (#4027)
* Fixed ollama_chat_client sample * Fixed ollama_chat_multimodal sample * Fixed function_tool_with_approval_and_sessions sample * Updated function_tool_with_session_injection sample * Small clean-up * Update 01_round_robin_group_chat.py * Update 02_selector_group_chat.py * Update 03_swarm.py * Update 03_assistant_agent_thread_and_stream.py * Update 04_agent_as_tool.py * Resolved comments
This commit is contained in:
committed by
GitHub
Unverified
parent
f9f630829a
commit
f087b864fb
@@ -3,7 +3,7 @@
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
|
||||
from agent_framework import tool
|
||||
from agent_framework import Message, tool
|
||||
from agent_framework.ollama import OllamaChatClient
|
||||
|
||||
"""
|
||||
@@ -29,16 +29,17 @@ def get_time():
|
||||
async def main() -> None:
|
||||
client = OllamaChatClient()
|
||||
message = "What time is it? Use a tool call"
|
||||
messages = [Message(role="user", text=message)]
|
||||
stream = False
|
||||
print(f"User: {message}")
|
||||
if stream:
|
||||
print("Assistant: ", end="")
|
||||
async for chunk in client.get_response(message, tools=get_time, stream=True):
|
||||
async for chunk in client.get_response(messages, tools=get_time, stream=True):
|
||||
if str(chunk):
|
||||
print(str(chunk), end="")
|
||||
print("")
|
||||
else:
|
||||
response = await client.get_response(message, tools=get_time)
|
||||
response = await client.get_response(messages, tools=get_time)
|
||||
print(f"Assistant: {response}")
|
||||
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ async def test_image() -> None:
|
||||
],
|
||||
)
|
||||
|
||||
response = await client.get_response(message)
|
||||
response = await client.get_response([message])
|
||||
print(f"Image Response: {response}")
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import Agent, Message, tool
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from azure.identity import AzureCliCredential
|
||||
|
||||
"""
|
||||
Tool Approvals with Sessions
|
||||
@@ -29,7 +30,7 @@ async def approval_example() -> None:
|
||||
print("=== Tool Approval with Session ===\n")
|
||||
|
||||
agent = Agent(
|
||||
client=AzureOpenAIChatClient(),
|
||||
client=AzureOpenAIChatClient(credential=AzureCliCredential()),
|
||||
name="CalendarAgent",
|
||||
instructions="You are a helpful calendar assistant.",
|
||||
tools=[add_to_calendar],
|
||||
@@ -65,7 +66,7 @@ async def rejection_example() -> None:
|
||||
print("=== Tool Rejection with Session ===\n")
|
||||
|
||||
agent = Agent(
|
||||
client=AzureOpenAIChatClient(),
|
||||
client=AzureOpenAIChatClient(credential=AzureCliCredential()),
|
||||
name="CalendarAgent",
|
||||
instructions="You are a helpful calendar assistant.",
|
||||
tools=[add_to_calendar],
|
||||
|
||||
@@ -67,8 +67,6 @@ Expected Output:
|
||||
============================================================
|
||||
Step 1: Call divide(10, 0) - tool raises exception
|
||||
Tool failed with error: division by zero
|
||||
[2025-10-31 15:39:53 - /Users/edvan/Work/agent-framework/python/packages/core/agent_framework/_tools.py:718 - ERROR]
|
||||
Function failed. Error: division by zero
|
||||
Response: Division by zero is undefined in standard arithmetic. There is no finite value for 10 ÷ 0.
|
||||
|
||||
If you want alternatives:
|
||||
@@ -80,9 +78,6 @@ If you want alternatives:
|
||||
Would you like me to show a safe division snippet in a specific language, or compute something else?
|
||||
============================================================
|
||||
Step 2: Call divide(100, 0) - will refuse to execute due to max_invocations
|
||||
[2025-10-31 15:40:09 - /Users/edvan/Work/agent-framework/python/packages/core/agent_framework/_tools.py:718 - ERROR]
|
||||
Function failed. Error: Function 'safe_divide' has reached its maximum exception limit, you tried to use this
|
||||
tool too many times and it kept failing.
|
||||
Response: Division by zero is undefined in standard arithmetic, so 100 ÷ 0 has no finite value.
|
||||
|
||||
If you’re coding and want safe handling, here are quick patterns in a few languages:
|
||||
|
||||
@@ -58,9 +58,6 @@ Step 1: Call unicorn_function
|
||||
Response: Five unicorns summoned: 🦄🦄🦄🦄🦄✨
|
||||
============================================================
|
||||
Step 2: Call unicorn_function again - will refuse to execute due to max_invocations
|
||||
[2025-10-31 15:54:40 - /Users/edvan/Work/agent-framework/python/packages/core/agent_framework/_tools.py:718 - ERROR]
|
||||
Function failed. Error: Function 'unicorn_function' has reached its maximum invocation limit,
|
||||
you can no longer use this tool.
|
||||
Response: The unicorn function has reached its maximum invocation limit. I can’t call it again right now.
|
||||
|
||||
Here are 10 unicorns manually: 🦄 🦄 🦄 🦄 🦄 🦄 🦄 🦄 🦄 🦄
|
||||
|
||||
@@ -43,8 +43,10 @@ async def main() -> None:
|
||||
session = agent.create_session()
|
||||
|
||||
# Run the agent with the session
|
||||
print(f"Agent: {await agent.run('What is the weather in London?', session=session)}")
|
||||
print(f"Agent: {await agent.run('What is the weather in Amsterdam?', session=session)}")
|
||||
# Pass session via additional_function_arguments so tools can access it via **kwargs
|
||||
opts = {"additional_function_arguments": {"session": session}}
|
||||
print(f"Agent: {await agent.run('What is the weather in London?', session=session, options=opts)}")
|
||||
print(f"Agent: {await agent.run('What is the weather in Amsterdam?', session=session, options=opts)}")
|
||||
print(f"Agent: {await agent.run('What cities did I ask about?', session=session)}")
|
||||
|
||||
|
||||
|
||||
@@ -79,8 +79,6 @@ If you want a numeric surrogate, you can use a small nonzero denominator, e.g.,
|
||||
see more on limits or handle it with a tiny epsilon?
|
||||
============================================================
|
||||
Step 2: Call set safe to False and call again
|
||||
[2025-10-31 16:17:44 - /Users/edvan/Work/agent-framework/python/packages/core/agent_framework/_tools.py:718 - ERROR]
|
||||
Function failed. Error: division by zero
|
||||
Response: Division by zero is undefined in standard arithmetic. There is no number y such that 0 × y = 10.
|
||||
|
||||
If you’re looking at limits:
|
||||
|
||||
Reference in New Issue
Block a user