Python: added create_agent and removed need for Field in annotations (#380)

* added create_agent

* add minimal sample

* allow multiple annotations

* improved docstring
This commit is contained in:
Eduard van Valkenburg
2025-08-08 19:55:20 +02:00
committed by GitHub
Unverified
parent 757c48e367
commit 8f27e63df6
5 changed files with 86 additions and 16 deletions
@@ -4,7 +4,6 @@ import asyncio
from random import randint
from typing import Annotated
from agent_framework import ChatClientAgent
from agent_framework.foundry import FoundryChatClient
from pydantic import Field
@@ -23,8 +22,8 @@ async def non_streaming_example() -> None:
# Since no Agent ID is provided, the agent will be automatically created
# and deleted after getting a response
async with ChatClientAgent(
chat_client=FoundryChatClient(),
async with FoundryChatClient().create_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent:
@@ -40,8 +39,8 @@ async def streaming_example() -> None:
# Since no Agent ID is provided, the agent will be automatically created
# and deleted after getting a response
async with ChatClientAgent(
chat_client=FoundryChatClient(),
async with FoundryChatClient().create_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent:
@@ -4,13 +4,11 @@ import asyncio
from random import randint
from typing import Annotated
from agent_framework import ChatClientAgent
from agent_framework.openai import OpenAIChatClient
from pydantic import Field
def get_weather(
location: Annotated[str, Field(description="The location to get the weather for.")],
location: Annotated[str, "The location to get the weather for."],
) -> str:
"""Get the weather for a given location."""
conditions = ["sunny", "cloudy", "rainy", "stormy"]
@@ -21,8 +19,8 @@ async def non_streaming_example() -> None:
"""Example of non-streaming response (get the complete result at once)."""
print("=== Non-streaming Response Example ===")
agent = ChatClientAgent(
chat_client=OpenAIChatClient(),
agent = OpenAIChatClient().create_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
)
@@ -37,8 +35,8 @@ async def streaming_example() -> None:
"""Example of streaming response (get results as they are generated)."""
print("=== Streaming Response Example ===")
agent = ChatClientAgent(
chat_client=OpenAIChatClient(),
agent = OpenAIChatClient().create_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
)
@@ -0,0 +1,21 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from random import randint
from typing import Annotated
from agent_framework.openai import OpenAIChatClient
def get_weather(
location: Annotated[str, "The location to get the weather for."],
) -> str:
"""Get the weather for a given location."""
conditions = ["sunny", "cloudy", "rainy", "stormy"]
return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."
agent = OpenAIChatClient().create_agent(
name="WeatherAgent", instructions="You are a helpful weather agent.", tools=get_weather
)
print(asyncio.run(agent.run("What's the weather like in Seattle?")))