mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [BREAKING] Replace Hosted*Tool classes with tool methods (#3634)
* Replace Hosted*Tool classes with client static factory methods * fixed failing test * mypy fix * mypy fix 2 * declarative mypy fix * addressed comments * ToolProtocol removal * fixed test * agents mypy fix * fix failing tests * mypy fix * addressed comments * fixed tests * addressed comments + added factory method overrides for azureai v2 client * mypy fix * added kwargs to azureai tool methods * fixed in test * _sessions fix * test fix
This commit is contained in:
committed by
GitHub
Unverified
parent
d249473a6d
commit
7a88af0aef
+12
-6
@@ -39,18 +39,24 @@ async def run_semantic_kernel() -> None:
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from agent_framework.azure import AzureAIAgentClient, HostedCodeInterpreterTool
|
||||
from agent_framework.azure import AzureAIAgentClient, AzureAIAgentsProvider
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
|
||||
async with (
|
||||
AzureCliCredential() as credential,
|
||||
AzureAIAgentClient(credential=credential).as_agent(
|
||||
AzureAIAgentsProvider(credential=credential) as provider,
|
||||
):
|
||||
# Create a client to access hosted tool factory methods
|
||||
client = AzureAIAgentClient(agents_client=provider._agents_client)
|
||||
code_interpreter_tool = client.get_code_interpreter_tool()
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="Analyst",
|
||||
instructions="Use the code interpreter for numeric work.",
|
||||
tools=[HostedCodeInterpreterTool()],
|
||||
) as agent,
|
||||
):
|
||||
# HostedCodeInterpreterTool mirrors the built-in Azure AI capability.
|
||||
tools=[code_interpreter_tool],
|
||||
)
|
||||
|
||||
# Code interpreter tool mirrors the built-in Azure AI capability.
|
||||
reply = await agent.run(
|
||||
"Use Python to compute 42 ** 2 and explain the result.",
|
||||
tool_choice="auto",
|
||||
|
||||
+5
-2
@@ -37,16 +37,19 @@ async def run_semantic_kernel() -> None:
|
||||
|
||||
|
||||
async def run_agent_framework() -> None:
|
||||
from agent_framework import HostedCodeInterpreterTool
|
||||
from agent_framework.openai import OpenAIAssistantsClient
|
||||
|
||||
assistants_client = OpenAIAssistantsClient()
|
||||
|
||||
# Create code interpreter tool using static method
|
||||
code_interpreter_tool = OpenAIAssistantsClient.get_code_interpreter_tool()
|
||||
|
||||
# AF exposes the same tool configuration via create_agent.
|
||||
async with assistants_client.as_agent(
|
||||
name="CodeRunner",
|
||||
instructions="Use the code interpreter when calculations are required.",
|
||||
model="gpt-4.1",
|
||||
tools=[HostedCodeInterpreterTool()],
|
||||
tools=[code_interpreter_tool],
|
||||
) as assistant_agent:
|
||||
response = await assistant_agent.run(
|
||||
"Use Python to calculate the mean of [41, 42, 45] and explain the steps.",
|
||||
|
||||
@@ -19,8 +19,8 @@ from agent_framework import (
|
||||
Message,
|
||||
WorkflowEvent,
|
||||
)
|
||||
from agent_framework.orchestrations import HandoffBuilder, HandoffUserInputRequest
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework.orchestrations import HandoffBuilder, HandoffUserInputRequest
|
||||
from azure.identity import AzureCliCredential
|
||||
from semantic_kernel.agents import Agent, ChatCompletionAgent, HandoffOrchestration, OrchestrationHandoffs
|
||||
from semantic_kernel.agents.runtime import InProcessRuntime
|
||||
|
||||
@@ -15,7 +15,7 @@ import asyncio
|
||||
from collections.abc import Sequence
|
||||
from typing import cast
|
||||
|
||||
from agent_framework import Agent, HostedCodeInterpreterTool
|
||||
from agent_framework import Agent
|
||||
from agent_framework.openai import OpenAIChatClient, OpenAIResponsesClient
|
||||
from agent_framework.orchestrations import MagenticBuilder
|
||||
from semantic_kernel.agents import (
|
||||
@@ -138,12 +138,16 @@ async def run_agent_framework_example(prompt: str) -> str | None:
|
||||
client=OpenAIChatClient(ai_model_id="gpt-4o-search-preview"),
|
||||
)
|
||||
|
||||
# Create code interpreter tool using instance method
|
||||
coder_client = OpenAIResponsesClient()
|
||||
code_interpreter_tool = coder_client.get_code_interpreter_tool()
|
||||
|
||||
coder = Agent(
|
||||
name="CoderAgent",
|
||||
description="A helpful assistant that writes and executes code to process and analyze data.",
|
||||
instructions="You solve questions using code. Please provide detailed analysis and computation process.",
|
||||
client=OpenAIResponsesClient(),
|
||||
tools=HostedCodeInterpreterTool(),
|
||||
client=coder_client,
|
||||
tools=code_interpreter_tool,
|
||||
)
|
||||
|
||||
# Create a manager agent for orchestration
|
||||
|
||||
@@ -20,7 +20,7 @@ from typing import TYPE_CHECKING, ClassVar, cast
|
||||
######################################################################
|
||||
# region Agent Framework imports
|
||||
######################################################################
|
||||
from agent_framework import Executor, WorkflowBuilder, WorkflowContext, handler
|
||||
from agent_framework import Executor, WorkflowBuilder, WorkflowContext, handler
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
######################################################################
|
||||
|
||||
@@ -26,7 +26,6 @@ from agent_framework import (
|
||||
WorkflowBuilder,
|
||||
WorkflowContext,
|
||||
WorkflowExecutor,
|
||||
|
||||
handler,
|
||||
)
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
Reference in New Issue
Block a user