Python: Small fixes in examples (#540)

* Fixed Ruff formatting

* Added more info to chat response cancellation example

* Improved example

* Small fix
This commit is contained in:
Dmytro Struk
2025-08-28 13:41:18 -07:00
committed by GitHub
Unverified
parent 6a00c9d026
commit f5043e4fc1
5 changed files with 24 additions and 3 deletions
@@ -1,11 +1,13 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import ChatClientAgent, HostedFileSearchTool, HostedVectorStoreContent
from agent_framework.openai import OpenAIAssistantsClient
# Helper functions
async def create_vector_store(client: OpenAIAssistantsClient) -> tuple[str, HostedVectorStoreContent]:
"""Create a vector store with sample documents."""
file = await client.client.files.create(
@@ -42,12 +44,12 @@ async def main() -> None:
print(f"User: {query}")
print("Agent: ", end="", flush=True)
async for chunk in agent.run_streaming(
query,
tool_resources={"file_search": {"vector_store_ids": [vector_store.vector_store_id]}}
query, tool_resources={"file_search": {"vector_store_ids": [vector_store.vector_store_id]}}
):
if chunk.text:
print(chunk.text, end="", flush=True)
await delete_vector_store(client, file_id, vector_store.vector_store_id)
if __name__ == "__main__":
asyncio.run(main())
@@ -1,9 +1,11 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import HostedWebSearchTool
from agent_framework.openai import OpenAIChatClient
async def main() -> None:
client = OpenAIChatClient(ai_model_id="gpt-4o-search-preview")
@@ -35,5 +37,6 @@ async def main() -> None:
)
print(f"Assistant: {response}")
if __name__ == "__main__":
asyncio.run(main())
@@ -1,11 +1,13 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import HostedFileSearchTool, HostedVectorStoreContent
from agent_framework.openai import OpenAIResponsesClient
# Helper functions
async def create_vector_store(client: OpenAIResponsesClient) -> tuple[str, HostedVectorStoreContent]:
"""Create a vector store with sample documents."""
file = await client.client.files.create(
@@ -56,5 +58,6 @@ async def main() -> None:
print(f"Assistant: {response}")
await delete_vector_store(client, file_id, vector_store.vector_store_id)
if __name__ == "__main__":
asyncio.run(main())
@@ -1,9 +1,11 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import HostedWebSearchTool
from agent_framework.openai import OpenAIResponsesClient
async def main() -> None:
client = OpenAIResponsesClient()
@@ -35,5 +37,6 @@ async def main() -> None:
)
print(f"Assistant: {response}")
if __name__ == "__main__":
asyncio.run(main())
@@ -4,7 +4,16 @@ import asyncio
from agent_framework.openai import OpenAIChatClient
async def main():
async def main() -> None:
"""
Demonstrates cancelling a chat request after 1 second.
Creates a task for the chat request, waits briefly, then cancels it to show proper cleanup.
Configuration:
- OpenAI model ID: Use "ai_model_id" parameter or "OPENAI_CHAT_MODEL_ID" environment variable
- OpenAI API key: Use "api_key" parameter or "OPENAI_API_KEY" environment variable
"""
chat_client = OpenAIChatClient()
try:
@@ -15,5 +24,6 @@ async def main():
except asyncio.CancelledError:
print("Request was cancelled")
if __name__ == "__main__":
asyncio.run(main())