update openai samples to use agents (#2012)

This commit is contained in:
Giles Odigwe
2025-11-12 14:58:41 -08:00
committed by GitHub
Unverified
parent 406a8560c6
commit 4bffe1ebc8
4 changed files with 40 additions and 47 deletions
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft. All rights reserved.
import os
import asyncio
import os
from agent_framework import ChatAgent, MCPStreamableHTTPTool
from agent_framework.azure import AzureOpenAIResponsesClient
@@ -18,13 +18,14 @@ servers.
# --- Below code uses Microsoft Learn MCP server over Streamable HTTP ---
# --- Users can set these environment variables, or just edit the values below to their desired local MCP server
MCP_NAME = os.environ.get("MCP_NAME", "Microsoft Learn MCP") # example name
MCP_URL = os.environ.get("MCP_URL", "https://learn.microsoft.com/api/mcp") # example endpoint
MCP_URL = os.environ.get("MCP_URL", "https://learn.microsoft.com/api/mcp") # example endpoint
# Environment variables for Azure OpenAI Responses authentication
# AZURE_OPENAI_ENDPOINT="<your-azure openai-endpoint>"
# AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME="<your-deployment-name>"
# AZURE_OPENAI_API_VERSION="<your-api-version>" # e.g. "2025-03-01-preview"
async def main():
"""Example showing local MCP tools for a Azure OpenAI Responses Agent."""
# AuthN: use Azure CLI
@@ -38,16 +39,13 @@ async def main():
agent: ChatAgent = responses_client.create_agent(
name="DocsAgent",
instructions=(
"You are a helpful assistant that can help with Microsoft documentation questions."
),
instructions=("You are a helpful assistant that can help with Microsoft documentation questions."),
)
# Connect to the MCP server (Streamable HTTP)
async with MCPStreamableHTTPTool(
name=MCP_NAME,
url=MCP_URL,
) as mcp_tool:
# First query — expect the agent to use the MCP tool if it helps
q1 = "How to create an Azure storage account using az cli?"
@@ -2,7 +2,7 @@
import asyncio
from agent_framework import HostedWebSearchTool
from agent_framework import ChatAgent, HostedWebSearchTool
from agent_framework.openai import OpenAIChatClient
"""
@@ -14,34 +14,32 @@ for real-time information retrieval and current data access.
async def main() -> None:
client = OpenAIChatClient(model_id="gpt-4o-search-preview")
message = "What is the current weather? Do not ask for my current location."
# Test that the client will use the web search tool with location
# Test that the agent will use the web search tool with location
additional_properties = {
"user_location": {
"country": "US",
"city": "Seattle",
}
}
agent = ChatAgent(
chat_client=OpenAIChatClient(model_id="gpt-4o-search-preview"),
instructions="You are a helpful assistant that can search the web for current information.",
tools=[HostedWebSearchTool(additional_properties=additional_properties)],
)
message = "What is the current weather? Do not ask for my current location."
stream = False
print(f"User: {message}")
if stream:
print("Assistant: ", end="")
async for chunk in client.get_streaming_response(
message,
tools=[HostedWebSearchTool(additional_properties=additional_properties)],
tool_choice="auto",
):
async for chunk in agent.run_stream(message):
if chunk.text:
print(chunk.text, end="")
print("")
else:
response = await client.get_response(
message,
tools=[HostedWebSearchTool(additional_properties=additional_properties)],
tool_choice="auto",
)
response = await agent.run(message)
print(f"Assistant: {response}")
@@ -2,7 +2,7 @@
import asyncio
from agent_framework import HostedFileSearchTool, HostedVectorStoreContent
from agent_framework import ChatAgent, HostedFileSearchTool, HostedVectorStoreContent
from agent_framework.openai import OpenAIResponsesClient
"""
@@ -46,22 +46,21 @@ async def main() -> None:
stream = False
print(f"User: {message}")
file_id, vector_store = await create_vector_store(client)
agent = ChatAgent(
chat_client=client,
instructions="You are a helpful assistant that can search through files to find information.",
tools=[HostedFileSearchTool(inputs=vector_store)],
)
if stream:
print("Assistant: ", end="")
async for chunk in client.get_streaming_response(
message,
tools=[HostedFileSearchTool(inputs=vector_store)],
tool_choice="auto",
):
async for chunk in agent.run_stream(message):
if chunk.text:
print(chunk.text, end="")
print("")
else:
response = await client.get_response(
message,
tools=[HostedFileSearchTool(inputs=vector_store)],
tool_choice="auto",
)
response = await agent.run(message)
print(f"Assistant: {response}")
await delete_vector_store(client, file_id, vector_store.vector_store_id)
@@ -2,7 +2,7 @@
import asyncio
from agent_framework import HostedWebSearchTool
from agent_framework import ChatAgent, HostedWebSearchTool
from agent_framework.openai import OpenAIResponsesClient
"""
@@ -14,34 +14,32 @@ for direct real-time information retrieval and current data access.
async def main() -> None:
client = OpenAIResponsesClient()
message = "What is the current weather? Do not ask for my current location."
# Test that the client will use the web search tool with location
# Test that the agent will use the web search tool with location
additional_properties = {
"user_location": {
"country": "US",
"city": "Seattle",
}
}
agent = ChatAgent(
chat_client=OpenAIResponsesClient(),
instructions="You are a helpful assistant that can search the web for current information.",
tools=[HostedWebSearchTool(additional_properties=additional_properties)],
)
message = "What is the current weather? Do not ask for my current location."
stream = False
print(f"User: {message}")
if stream:
print("Assistant: ", end="")
async for chunk in client.get_streaming_response(
message,
tools=[HostedWebSearchTool(additional_properties=additional_properties)],
tool_choice="auto",
):
async for chunk in agent.run_stream(message):
if chunk.text:
print(chunk.text, end="")
print("")
else:
response = await client.get_response(
message,
tools=[HostedWebSearchTool(additional_properties=additional_properties)],
tool_choice="auto",
)
response = await agent.run(message)
print(f"Assistant: {response}")