mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
7c2dae8855
* Fix sample bugs: incorrect API params, wrong client types, and invalid options - typed_options.py: Fix AnthropicClient model->model_id, wrap raw strings in Message objects for get_response(), fix reasoning_effort->reasoning dict, fix budget_tokens minimum (1024), use OpenAIChatClient not FoundryChatClient, remove unused import - client_reasoning.py: Fix deprecated model_id to model param - client_with_hosted_mcp.py: Remove invalid store=True kwarg from Agent.run() - code_defined_skill.py: Fix precision kwarg to use function_invocation_kwargs - Various other samples: Fix deprecated API usage and incorrect params Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address PR review comments - client_with_hosted_mcp.py: Fix remaining store=True kwarg on line 68 to use options dict - client_with_session.py: Change store=True to store=False to match in-memory persistence demo intent - typed_options.py: Remove non-existent import and model key from docstring example Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * new sample fixes --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
163 lines
6.2 KiB
Python
163 lines
6.2 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import asyncio
|
|
from random import randrange
|
|
from typing import TYPE_CHECKING, Annotated, Any
|
|
|
|
from agent_framework import Agent, AgentResponse, Message, tool
|
|
from agent_framework.openai import OpenAIChatClient
|
|
from dotenv import load_dotenv
|
|
|
|
if TYPE_CHECKING:
|
|
from agent_framework import SupportsAgentRun
|
|
|
|
"""
|
|
Demonstration of a tool with approvals.
|
|
|
|
This sample demonstrates using AI functions with user approval workflows.
|
|
It shows how to handle function call approvals without using threads.
|
|
"""
|
|
|
|
# Load environment variables from .env file
|
|
load_dotenv()
|
|
|
|
conditions = ["sunny", "cloudy", "raining", "snowing", "clear"]
|
|
|
|
|
|
# NOTE: approval_mode="never_require" is for sample brevity. Use "always_require" in production;
|
|
# see samples/02-agents/tools/function_tool_with_approval.py
|
|
# and samples/02-agents/tools/function_tool_with_approval_and_sessions.py.
|
|
@tool(approval_mode="never_require")
|
|
def get_weather(location: Annotated[str, "The city and state, e.g. San Francisco, CA"]) -> str:
|
|
"""Get the current weather for a given location."""
|
|
# Simulate weather data
|
|
return f"The weather in {location} is {conditions[randrange(0, len(conditions))]} and {randrange(-10, 30)}°C."
|
|
|
|
|
|
# Define a simple weather tool that requires approval
|
|
@tool(approval_mode="always_require")
|
|
def get_weather_detail(location: Annotated[str, "The city and state, e.g. San Francisco, CA"]) -> str:
|
|
"""Get the current weather for a given location."""
|
|
# Simulate weather data
|
|
return (
|
|
f"The weather in {location} is {conditions[randrange(0, len(conditions))]} and {randrange(-10, 30)}°C, "
|
|
"with a humidity of 88%. "
|
|
f"Tomorrow will be {conditions[randrange(0, len(conditions))]} with a high of {randrange(-10, 30)}°C."
|
|
)
|
|
|
|
|
|
async def handle_approvals(query: str, agent: "SupportsAgentRun") -> AgentResponse:
|
|
"""Handle function call approvals.
|
|
|
|
When we don't have a thread, we need to ensure we include the original query,
|
|
the approval request, and the approval response in each iteration.
|
|
"""
|
|
result = await agent.run(query)
|
|
while len(result.user_input_requests) > 0:
|
|
# Start with the original query
|
|
new_inputs: list[Any] = [query]
|
|
|
|
for user_input_needed in result.user_input_requests:
|
|
print(
|
|
f"\nUser Input Request for function from {agent.name}:"
|
|
f"\n Function: {user_input_needed.function_call.name}"
|
|
f"\n Arguments: {user_input_needed.function_call.arguments}"
|
|
)
|
|
|
|
# Add the assistant message with the approval request
|
|
new_inputs.append(Message("assistant", [user_input_needed]))
|
|
|
|
# Get user approval
|
|
user_approval = await asyncio.to_thread(input, "\nApprove function call? (y/n): ")
|
|
|
|
# Add the user's approval response
|
|
new_inputs.append(
|
|
Message("user", [user_input_needed.to_function_approval_response(user_approval.lower() == "y")])
|
|
)
|
|
|
|
# Run again with all the context
|
|
result = await agent.run(new_inputs)
|
|
|
|
return result
|
|
|
|
|
|
async def handle_approvals_streaming(query: str, agent: "SupportsAgentRun") -> None:
|
|
"""Handle function call approvals with streaming responses.
|
|
|
|
When we don't have a thread, we need to ensure we include the original query,
|
|
the approval request, and the approval response in each iteration.
|
|
"""
|
|
current_input: str | list[Any] = query
|
|
has_user_input_requests = True
|
|
while has_user_input_requests:
|
|
has_user_input_requests = False
|
|
user_input_requests: list[Any] = []
|
|
|
|
# Stream the response
|
|
async for chunk in agent.run(current_input, stream=True):
|
|
if chunk.text:
|
|
print(chunk.text, end="", flush=True)
|
|
|
|
# Collect user input requests from the stream
|
|
if chunk.user_input_requests:
|
|
user_input_requests.extend(chunk.user_input_requests)
|
|
|
|
if user_input_requests:
|
|
has_user_input_requests = True
|
|
# Start with the original query
|
|
new_inputs: list[Any] = [query]
|
|
|
|
for user_input_needed in user_input_requests:
|
|
print(
|
|
f"\n\nUser Input Request for function from {agent.name}:"
|
|
f"\n Function: {user_input_needed.function_call.name}"
|
|
f"\n Arguments: {user_input_needed.function_call.arguments}"
|
|
)
|
|
|
|
# Add the assistant message with the approval request
|
|
new_inputs.append(Message("assistant", [user_input_needed]))
|
|
|
|
# Get user approval
|
|
user_approval = await asyncio.to_thread(input, "\nApprove function call? (y/n): ")
|
|
|
|
# Add the user's approval response
|
|
new_inputs.append(
|
|
Message("user", [user_input_needed.to_function_approval_response(user_approval.lower() == "y")])
|
|
)
|
|
|
|
# Update input with all the context for next iteration
|
|
current_input = new_inputs
|
|
|
|
|
|
async def run_weather_agent_with_approval(stream: bool) -> None:
|
|
"""Example showing AI function with approval requirement."""
|
|
print(f"\n=== Weather Agent with Approval Required ({'Streaming' if stream else 'Non-Streaming'}) ===\n")
|
|
|
|
async with Agent(
|
|
client=OpenAIChatClient(),
|
|
name="WeatherAgent",
|
|
instructions=("You are a helpful weather assistant. Use the get_weather tool to provide weather information."),
|
|
tools=[get_weather, get_weather_detail],
|
|
) as agent:
|
|
query = "Can you give me an update of the weather in LA and Portland and detailed weather for Seattle?"
|
|
print(f"User: {query}")
|
|
|
|
if stream:
|
|
print(f"\n{agent.name}: ", end="", flush=True)
|
|
await handle_approvals_streaming(query, agent)
|
|
print()
|
|
else:
|
|
result = await handle_approvals(query, agent)
|
|
print(f"\n{agent.name}: {result}\n")
|
|
|
|
|
|
async def main() -> None:
|
|
print("=== Demonstration of a tool with approvals ===\n")
|
|
|
|
await run_weather_agent_with_approval(stream=False)
|
|
await run_weather_agent_with_approval(stream=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|