mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [BREAKING] Python: Provider-leading client design & OpenAI package extraction (#4818)
* Python: Provider-leading client design & OpenAI package extraction Major refactoring of the Python Agent Framework client architecture: - Extract OpenAI clients into new `agent-framework-openai` package - Core package no longer depends on openai, azure-identity, azure-ai-projects - Rename clients for discoverability: OpenAIResponsesClient → OpenAIChatClient, OpenAIChatClient → OpenAIChatCompletionClient - Unify `model_id`/`deployment_name`/`model_deployment_name` → `model` param - New FoundryChatClient for Azure AI Foundry Responses API - New FoundryAgent/FoundryAgentClient for connecting to pre-configured Foundry agents - Remove OpenAIBase/OpenAIConfigMixin from non-deprecated client MRO - Deprecate AzureOpenAI* clients, AzureAIClient, OpenAIAssistantsClient - Reorganize samples: azure_openai+azure_ai+azure_ai_agent → azure/ - ADR-0020: Provider-Leading Client Design Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: missing Agent imports in samples, .model_id → .model in foundry_local sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: CI failures — mypy errors, coverage targets, sample imports - azure-ai mypy: add type ignores for TypedDict total=, model arg, forward ref - Coverage: replace core.azure/openai targets with openai package target - project_provider: add type annotation for opts dict Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: populate openai .pyi stub, fix broken README links, coverage targets Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fixes * updated observabilitty * reset azure init.pyi * fix errors * updated adr number * fix foundry local * fixed not renamed docstrings and comments, and added deprecated markers to old classes * fix tests and pyprojects * fix test vars * updated function tests * update durable * updated test setup for functions * Fix Foundry auth in workflow samples Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Stabilize Python integration workflows Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update hosting samples for Foundry Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Trigger full CI rerun Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Trigger CI rerun again Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * trigger rerun * trigger rerun * fix for litellm * undo durabletask changes * Move Foundry APIs into foundry namespace Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix Foundry pyproject formatting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Split provider samples by Foundry surface Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Restore hosting sample requirements Also fix the Foundry Local sample link after the provider sample move. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * updated tests * udpated foundry integration tests * removed dist from azurefunctions tests * Use separate Foundry clients for concurrent agents Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix client setup in azfunc and durable * disabled two tests * updated setup for some function and durable tests * improved azure openai setup with new clients * ignore deprecated * fixes * skip 11 * remove openai assistants int tests --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
4b533608b6
commit
5e056b672e
@@ -44,7 +44,7 @@ async def non_streaming_example() -> None:
|
||||
# Create a new assistant via the provider
|
||||
agent = await provider.create_agent(
|
||||
name="WeatherAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
@@ -69,7 +69,7 @@ async def streaming_example() -> None:
|
||||
# Create a new assistant via the provider
|
||||
agent = await provider.create_agent(
|
||||
name="WeatherAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
@@ -5,7 +5,7 @@ import os
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import tool
|
||||
from agent_framework import Agent, tool
|
||||
from agent_framework.openai import OpenAIAssistantProvider
|
||||
from dotenv import load_dotenv
|
||||
from openai import AsyncOpenAI
|
||||
@@ -46,7 +46,7 @@ async def create_agent_example() -> None:
|
||||
):
|
||||
agent = await provider.create_agent(
|
||||
name="WeatherAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful weather assistant.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
@@ -69,7 +69,7 @@ async def get_agent_example() -> None:
|
||||
):
|
||||
# Create an assistant directly with SDK (simulating pre-existing assistant)
|
||||
sdk_assistant = await client.beta.assistants.create(
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
name="ExistingAssistant",
|
||||
instructions="You always respond with 'Hello!'",
|
||||
)
|
||||
@@ -86,7 +86,7 @@ async def get_agent_example() -> None:
|
||||
|
||||
|
||||
async def as_agent_example() -> None:
|
||||
"""Wrap an SDK Assistant object using provider.as_agent()."""
|
||||
"""Wrap an SDK Assistant object using Agent(client=provider, ...)."""
|
||||
print("\n--- as_agent() ---")
|
||||
|
||||
async with (
|
||||
@@ -95,14 +95,14 @@ async def as_agent_example() -> None:
|
||||
):
|
||||
# Create assistant using SDK
|
||||
sdk_assistant = await client.beta.assistants.create(
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
name="WrappedAssistant",
|
||||
instructions="You respond with poetry.",
|
||||
)
|
||||
|
||||
try:
|
||||
# Wrap synchronously (no HTTP call)
|
||||
agent = provider.as_agent(sdk_assistant)
|
||||
agent = Agent(client=provider, agent=sdk_assistant)
|
||||
print(f"Wrapped: {agent.name} (ID: {agent.id})")
|
||||
|
||||
result = await agent.run("Tell me about the sunset.")
|
||||
@@ -121,14 +121,14 @@ async def multiple_agents_example() -> None:
|
||||
):
|
||||
weather_agent = await provider.create_agent(
|
||||
name="WeatherSpecialist",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a weather specialist.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
greeter_agent = await provider.create_agent(
|
||||
name="GreeterAgent",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a friendly greeter.",
|
||||
)
|
||||
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ async def main() -> None:
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="CodeHelper",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful assistant that can write and execute Python code to solve problems.",
|
||||
tools=[chat_client.get_code_interpreter_tool()],
|
||||
)
|
||||
|
||||
+6
-5
@@ -5,7 +5,7 @@ import os
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import tool
|
||||
from agent_framework import Agent, tool
|
||||
from agent_framework.openai import OpenAIAssistantProvider
|
||||
from dotenv import load_dotenv
|
||||
from openai import AsyncOpenAI
|
||||
@@ -43,7 +43,7 @@ async def example_get_agent_by_id() -> None:
|
||||
|
||||
# Create an assistant via SDK (simulating an existing assistant)
|
||||
created_assistant = await client.beta.assistants.create(
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
name="WeatherAssistant",
|
||||
tools=[
|
||||
{
|
||||
@@ -86,7 +86,7 @@ async def example_as_agent_wrap_sdk_object() -> None:
|
||||
|
||||
# Create and fetch an assistant via SDK
|
||||
created_assistant = await client.beta.assistants.create(
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
name="SimpleAssistant",
|
||||
instructions="You are a friendly assistant.",
|
||||
)
|
||||
@@ -94,8 +94,9 @@ async def example_as_agent_wrap_sdk_object() -> None:
|
||||
|
||||
try:
|
||||
# Use as_agent() to wrap the SDK object
|
||||
agent = provider.as_agent(
|
||||
created_assistant,
|
||||
agent = Agent(
|
||||
client=provider,
|
||||
agent=created_assistant,
|
||||
instructions="You are an extremely helpful assistant. Be enthusiastic!",
|
||||
)
|
||||
|
||||
|
||||
+1
-1
@@ -43,7 +43,7 @@ async def main() -> None:
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="WeatherAssistant",
|
||||
model=os.environ["OPENAI_CHAT_MODEL_ID"],
|
||||
model=os.environ["OPENAI_MODEL"],
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
@@ -50,7 +50,7 @@ async def main() -> None:
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="SearchAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful assistant that searches files in a knowledge base.",
|
||||
tools=[chat_client.get_file_search_tool()],
|
||||
)
|
||||
|
||||
@@ -53,7 +53,7 @@ async def tools_on_agent_level() -> None:
|
||||
# The agent can use these tools for any query during its lifetime
|
||||
agent = await provider.create_agent(
|
||||
name="InfoAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful assistant that can provide weather and time information.",
|
||||
tools=[get_weather, get_time], # Tools defined at agent creation
|
||||
)
|
||||
@@ -90,7 +90,7 @@ async def tools_on_run_level() -> None:
|
||||
# Agent created with base tools, additional tools can be passed at run time
|
||||
agent = await provider.create_agent(
|
||||
name="FlexibleAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful assistant.",
|
||||
tools=[get_weather], # Base tool
|
||||
)
|
||||
@@ -127,7 +127,7 @@ async def mixed_tools_example() -> None:
|
||||
# Agent created with some base tools
|
||||
agent = await provider.create_agent(
|
||||
name="ComprehensiveAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a comprehensive assistant that can help with various information requests.",
|
||||
tools=[get_weather], # Base tool available for all queries
|
||||
)
|
||||
|
||||
@@ -50,7 +50,7 @@ async def main() -> None:
|
||||
# Create agent with default response_format (WeatherInfo)
|
||||
agent = await provider.create_agent(
|
||||
name="StructuredReporter",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="Return structured JSON based on the requested format.",
|
||||
default_options={"response_format": WeatherInfo},
|
||||
)
|
||||
|
||||
@@ -43,7 +43,7 @@ async def example_with_automatic_session_creation() -> None:
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="WeatherAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
@@ -75,7 +75,7 @@ async def example_with_session_persistence() -> None:
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="WeatherAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
@@ -120,7 +120,7 @@ async def example_with_existing_session_id() -> None:
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="WeatherAssistant",
|
||||
model=os.environ.get("OPENAI_CHAT_MODEL_ID", "gpt-4"),
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-4"),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@ import asyncio
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import tool
|
||||
from agent_framework import Agent, tool
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -35,7 +35,8 @@ async def non_streaming_example() -> None:
|
||||
"""Example of non-streaming response (get the complete result at once)."""
|
||||
print("=== Non-streaming Response Example ===")
|
||||
|
||||
agent = OpenAIChatClient().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIChatClient(),
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
@@ -51,7 +52,8 @@ async def streaming_example() -> None:
|
||||
"""Example of streaming response (get results as they are generated)."""
|
||||
print("=== Streaming Response Example ===")
|
||||
|
||||
agent = OpenAIChatClient().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIChatClient(),
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
|
||||
+7
-4
@@ -5,7 +5,7 @@ import os
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import tool
|
||||
from agent_framework import Agent, tool
|
||||
from agent_framework.openai import OpenAIChatClient
|
||||
from dotenv import load_dotenv
|
||||
from pydantic import Field
|
||||
@@ -36,10 +36,13 @@ def get_weather(
|
||||
async def main() -> None:
|
||||
print("=== OpenAI Chat Client with Explicit Settings ===")
|
||||
|
||||
agent = OpenAIChatClient(
|
||||
model_id=os.environ["OPENAI_CHAT_MODEL_ID"],
|
||||
_client = OpenAIChatClient(
|
||||
model=os.environ["OPENAI_MODEL"],
|
||||
api_key=os.environ["OPENAI_API_KEY"],
|
||||
).as_agent(
|
||||
)
|
||||
|
||||
agent = Agent(
|
||||
client=_client,
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
@@ -59,7 +59,8 @@ async def mcp_tools_on_agent_level() -> None:
|
||||
# Tools are provided when creating the agent
|
||||
# The agent can use these tools for any query during its lifetime
|
||||
# The agent will connect to the MCP server through its context manager.
|
||||
async with OpenAIChatClient().as_agent(
|
||||
async with Agent(
|
||||
client=OpenAIChatClient(),
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
|
||||
tools=MCPStreamableHTTPTool( # Tools defined at agent creation
|
||||
|
||||
+5
-2
@@ -3,6 +3,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from agent_framework import Agent
|
||||
from agent_framework.openai import OpenAIChatClient, OpenAIChatOptions
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -36,7 +37,8 @@ runtime_schema = {
|
||||
async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIChatClient[OpenAIChatOptions]().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIChatClient[OpenAIChatOptions](),
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
@@ -69,7 +71,8 @@ async def non_streaming_example() -> None:
|
||||
async def streaming_example() -> None:
|
||||
print("=== Streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIChatClient().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIChatClient(),
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ for real-time information retrieval and current data access.
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
client = OpenAIChatClient(model_id="gpt-4o-search-preview")
|
||||
client = OpenAIChatClient(model="gpt-4o-search-preview")
|
||||
|
||||
# Create web search tool with location context
|
||||
web_search_tool = client.get_web_search_tool(
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import Content
|
||||
from agent_framework import Agent, Content
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -21,7 +21,8 @@ async def main():
|
||||
print("=== OpenAI Responses Agent with Image Analysis ===")
|
||||
|
||||
# 1. Create an OpenAI Responses agent with vision capabilities
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIResponsesClient(),
|
||||
name="VisionAgent",
|
||||
instructions="You are a image analysist, you get a image and need to respond with what you see in the picture.",
|
||||
)
|
||||
|
||||
+3
-2
@@ -6,7 +6,7 @@ import tempfile
|
||||
import urllib.request as urllib_request
|
||||
from pathlib import Path
|
||||
|
||||
from agent_framework import Content
|
||||
from agent_framework import Agent, Content
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -61,7 +61,8 @@ async def main() -> None:
|
||||
|
||||
# Create an agent with customized image generation options
|
||||
client = OpenAIResponsesClient()
|
||||
agent = client.as_agent(
|
||||
agent = Agent(
|
||||
client=client,
|
||||
instructions="You are a helpful AI that can generate images.",
|
||||
tools=[
|
||||
client.get_image_generation_tool(
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import Agent
|
||||
from agent_framework.openai import OpenAIResponsesClient, OpenAIResponsesOptions
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -23,7 +24,8 @@ In this case they are here: https://platform.openai.com/docs/api-reference/respo
|
||||
"""
|
||||
|
||||
|
||||
agent = OpenAIResponsesClient[OpenAIResponsesOptions](model_id="gpt-5").as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIResponsesClient[OpenAIResponsesOptions](model_id="gpt-5"),
|
||||
name="MathHelper",
|
||||
instructions="You are a personal math tutor. When asked a math question, "
|
||||
"reason over how best to approach the problem and share your thought process.",
|
||||
|
||||
+3
-16
@@ -6,23 +6,19 @@ import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import anyio
|
||||
from agent_framework import Content
|
||||
from agent_framework import Agent, Content
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
"""OpenAI Responses Client Streaming Image Generation Example
|
||||
|
||||
Demonstrates streaming partial image generation using OpenAI's image generation tool.
|
||||
Shows progressive image rendering with partial images for improved user experience.
|
||||
|
||||
Note: The number of partial images received depends on generation speed:
|
||||
- High quality/complex images: More partials (generation takes longer)
|
||||
- Low quality/simple images: Fewer partials (generation completes quickly)
|
||||
- You may receive fewer partial images than requested if generation is fast
|
||||
|
||||
Important: The final partial image IS the complete, full-quality image. Each partial
|
||||
represents a progressive refinement, with the last one being the finished result.
|
||||
"""
|
||||
@@ -35,7 +31,6 @@ async def save_image_from_data_uri(data_uri: str, filename: str) -> None:
|
||||
# Extract base64 data
|
||||
base64_data = data_uri.split(",", 1)[1]
|
||||
image_bytes = base64.b64decode(base64_data)
|
||||
|
||||
# Save to file
|
||||
await anyio.Path(filename).write_bytes(image_bytes)
|
||||
print(f" Saved: {filename} ({len(image_bytes) / 1024:.1f} KB)")
|
||||
@@ -46,10 +41,10 @@ async def save_image_from_data_uri(data_uri: str, filename: str) -> None:
|
||||
async def main():
|
||||
"""Demonstrate streaming image generation with partial images."""
|
||||
print("=== OpenAI Streaming Image Generation Example ===\n")
|
||||
|
||||
# Create agent with streaming image generation enabled
|
||||
client = OpenAIResponsesClient()
|
||||
agent = client.as_agent(
|
||||
agent = Agent(
|
||||
client=client,
|
||||
instructions="You are a helpful agent that can generate images.",
|
||||
tools=[
|
||||
client.get_image_generation_tool(
|
||||
@@ -59,18 +54,14 @@ async def main():
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
query = "Draw a beautiful sunset over a calm ocean with sailboats"
|
||||
print(f" User: {query}")
|
||||
print()
|
||||
|
||||
# Track partial images
|
||||
image_count = 0
|
||||
|
||||
# Use temp directory for output
|
||||
output_dir = Path(tempfile.gettempdir()) / "generated_images"
|
||||
output_dir.mkdir(exist_ok=True)
|
||||
|
||||
print(" Streaming response:")
|
||||
async for update in agent.run(query, stream=True):
|
||||
for content in update.contents:
|
||||
@@ -81,18 +72,14 @@ async def main():
|
||||
image_output: Content = content.outputs
|
||||
if image_output.type == "data" and image_output.additional_properties.get("is_partial_image"):
|
||||
print(f" Image {image_count} received")
|
||||
|
||||
# Extract file extension from media_type (e.g., "image/png" -> "png")
|
||||
extension = "png" # Default fallback
|
||||
if image_output.media_type and "/" in image_output.media_type:
|
||||
extension = image_output.media_type.split("/")[-1]
|
||||
|
||||
# Save images with correct extension
|
||||
filename = output_dir / f"image{image_count}.{extension}"
|
||||
await save_image_from_data_uri(image_output.uri, str(filename))
|
||||
|
||||
image_count += 1
|
||||
|
||||
# Summary
|
||||
print("\n Summary:")
|
||||
print(f" Images received: {image_count}")
|
||||
|
||||
+5
-3
@@ -3,7 +3,7 @@
|
||||
import asyncio
|
||||
from collections.abc import Awaitable, Callable
|
||||
|
||||
from agent_framework import FunctionInvocationContext
|
||||
from agent_framework import Agent, FunctionInvocationContext
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -40,7 +40,8 @@ async def main() -> None:
|
||||
client = OpenAIResponsesClient()
|
||||
|
||||
# Create a specialized writer agent
|
||||
writer = client.as_agent(
|
||||
writer = Agent(
|
||||
client=client,
|
||||
name="WriterAgent",
|
||||
instructions="You are a creative writer. Write short, engaging content.",
|
||||
)
|
||||
@@ -54,7 +55,8 @@ async def main() -> None:
|
||||
)
|
||||
|
||||
# Create coordinator agent with writer as a tool
|
||||
coordinator = client.as_agent(
|
||||
coordinator = Agent(
|
||||
client=client,
|
||||
name="CoordinatorAgent",
|
||||
instructions="You coordinate with specialized agents. Delegate writing tasks to the creative_writer tool.",
|
||||
tools=[writer_tool],
|
||||
|
||||
+7
-4
@@ -5,7 +5,7 @@ import os
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import tool
|
||||
from agent_framework import Agent, tool
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
from dotenv import load_dotenv
|
||||
from pydantic import Field
|
||||
@@ -36,10 +36,13 @@ def get_weather(
|
||||
async def main() -> None:
|
||||
print("=== OpenAI Responses Client with Explicit Settings ===")
|
||||
|
||||
agent = OpenAIResponsesClient(
|
||||
model_id=os.environ["OPENAI_RESPONSES_MODEL_ID"],
|
||||
_client = OpenAIResponsesClient(
|
||||
model=os.environ["OPENAI_MODEL"],
|
||||
api_key=os.environ["OPENAI_API_KEY"],
|
||||
).as_agent(
|
||||
)
|
||||
|
||||
agent = Agent(
|
||||
client=_client,
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
+5
-2
@@ -3,6 +3,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from agent_framework import Agent
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -36,7 +37,8 @@ runtime_schema = {
|
||||
async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIResponsesClient(),
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
@@ -69,7 +71,8 @@ async def non_streaming_example() -> None:
|
||||
async def streaming_example() -> None:
|
||||
print("=== Streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIResponsesClient(),
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
|
||||
+5
-3
@@ -2,7 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import AgentResponse
|
||||
from agent_framework import Agent, AgentResponse
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
from dotenv import load_dotenv
|
||||
from pydantic import BaseModel
|
||||
@@ -29,7 +29,8 @@ async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming example ===")
|
||||
|
||||
# Create an OpenAI Responses agent
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIResponsesClient(),
|
||||
name="CityAgent",
|
||||
instructions="You are a helpful agent that describes cities in a structured format.",
|
||||
)
|
||||
@@ -54,7 +55,8 @@ async def streaming_example() -> None:
|
||||
print("=== Streaming example ===")
|
||||
|
||||
# Create an OpenAI Responses agent
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
agent = Agent(
|
||||
client=OpenAIResponsesClient(),
|
||||
name="CityAgent",
|
||||
instructions="You are a helpful agent that describes cities in a structured format.",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user