Python: Added fixes and more examples for OpenAI and Azure chat client agents (#232)

* Added non-streaming and streaming examples

* Updated resource management

* Added examples with thread management

* Added function tools examples

* Small rename

* Added code interpreter example

* Updated example

* Addressed PR feedback

* Added more OpenAI and Azure chat completion examples

* File renaming

* Small fix in tests

* Small revert

* More renaming

* Small fix
This commit is contained in:
Dmytro Struk
2025-07-24 08:31:05 -07:00
committed by GitHub
Unverified
parent 14efb626ab
commit a72245287f
14 changed files with 694 additions and 89 deletions
@@ -47,7 +47,7 @@ async def streaming_example() -> None:
) as agent:
query = "What's the weather like in Portland?"
print(f"User: {query}")
print("Assistant: ", end="", flush=True)
print("Agent: ", end="", flush=True)
async for chunk in agent.run_stream(query):
if chunk.text:
print(chunk.text, end="", flush=True)
@@ -55,7 +55,7 @@ async def streaming_example() -> None:
async def main() -> None:
print("=== Basic Foundry Chat Client Example ===")
print("=== Basic Foundry Chat Client Agent Example ===")
await non_streaming_example()
await streaming_example()
@@ -42,7 +42,7 @@ async def main() -> None:
) as agent:
query = "What is current datetime?"
print(f"User: {query}")
print("Assistant: ", end="", flush=True)
print("Agent: ", end="", flush=True)
generated_code = ""
async for chunk in agent.run_stream(query):
if chunk.text:
@@ -39,19 +39,19 @@ async def tools_on_agent_level() -> None:
query1 = "What's the weather like in New York?"
print(f"User: {query1}")
result1 = await agent.run(query1)
print(f"Assistant: {result1}\n")
print(f"Agent: {result1}\n")
# Second query - agent can use time tool
query2 = "What's the current UTC time?"
print(f"User: {query2}")
result2 = await agent.run(query2)
print(f"Assistant: {result2}\n")
print(f"Agent: {result2}\n")
# Third query - agent can use both tools if needed
query3 = "What's the weather in London and what's the current UTC time?"
print(f"User: {query3}")
result3 = await agent.run(query3)
print(f"Assistant: {result3}\n")
print(f"Agent: {result3}\n")
async def tools_on_run_level() -> None:
@@ -68,19 +68,19 @@ async def tools_on_run_level() -> None:
query1 = "What's the weather like in Seattle?"
print(f"User: {query1}")
result1 = await agent.run(query1, tools=[get_weather]) # Tool passed to run method
print(f"Assistant: {result1}\n")
print(f"Agent: {result1}\n")
# Second query with time tool
query2 = "What's the current UTC time?"
print(f"User: {query2}")
result2 = await agent.run(query2, tools=[get_time]) # Different tool for this query
print(f"Assistant: {result2}\n")
print(f"Agent: {result2}\n")
# Third query with multiple tools
query3 = "What's the weather in Chicago and what's the current UTC time?"
print(f"User: {query3}")
result3 = await agent.run(query3, tools=[get_weather, get_time]) # Multiple tools
print(f"Assistant: {result3}\n")
print(f"Agent: {result3}\n")
async def mixed_tools_example() -> None:
@@ -102,7 +102,7 @@ async def mixed_tools_example() -> None:
query,
tools=[get_time], # Additional tools for this specific query
)
print(f"Assistant: {result}\n")
print(f"Agent: {result}\n")
async def main() -> None:
@@ -30,13 +30,13 @@ async def example_with_automatic_thread_creation() -> None:
query1 = "What's the weather like in Seattle?"
print(f"User: {query1}")
result1 = await agent.run(query1)
print(f"Assistant: {result1.text}")
print(f"Agent: {result1.text}")
# Second conversation - still no thread provided, will create another new thread
query2 = "What was the last city I asked about?"
print(f"\nUser: {query2}")
result2 = await agent.run(query2)
print(f"Assistant: {result2.text}")
print(f"Agent: {result2.text}")
print("Note: Each call creates a separate thread, so the agent doesn't remember previous context.\n")
@@ -47,7 +47,7 @@ async def example_with_thread_persistence() -> None:
async with ChatClientAgent(
chat_client=FoundryChatClient(),
instructions="You are a helpful weather agent. Remember previous cities asked about.",
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent:
# Create a new thread that will be reused
@@ -57,19 +57,19 @@ async def example_with_thread_persistence() -> None:
query1 = "What's the weather like in Tokyo?"
print(f"User: {query1}")
result1 = await agent.run(query1, thread=thread)
print(f"Assistant: {result1.text}")
print(f"Agent: {result1.text}")
# Second conversation using the same thread - maintains context
query2 = "How about comparing it to London?"
print(f"\nUser: {query2}")
result2 = await agent.run(query2, thread=thread)
print(f"Assistant: {result2.text}")
print(f"Agent: {result2.text}")
# Third conversation - agent should remember both previous cities
query3 = "Which of the cities I asked about has better weather?"
print(f"\nUser: {query3}")
result3 = await agent.run(query3, thread=thread)
print(f"Assistant: {result3.text}")
print(f"Agent: {result3.text}")
print("Note: The agent remembers context from previous messages in the same thread.\n")
@@ -91,7 +91,7 @@ async def example_with_existing_thread_id() -> None:
query1 = "What's the weather in Paris?"
print(f"User: {query1}")
result1 = await agent.run(query1, thread=thread)
print(f"Assistant: {result1.text}")
print(f"Agent: {result1.text}")
# The thread ID is set after the first response
existing_thread_id = thread.id
@@ -112,7 +112,7 @@ async def example_with_existing_thread_id() -> None:
query2 = "What was the last city I asked about?"
print(f"User: {query2}")
result2 = await agent.run(query2, thread=thread)
print(f"Assistant: {result2.text}")
print(f"Agent: {result2.text}")
print("Note: The agent continues the conversation from the previous thread.\n")