@@ -267,97 +267,134 @@ async def main():
267267asyncio.run(main())
268268```
269269
270- ### File Search
270+ #### Code Interpreter with File Upload
271271
272- Enable document search capabilities using the hosted file search tool :
272+ For data analysis tasks, you can upload files and analyze them with code :
273273
274274``` python
275275import asyncio
276- from agent_framework import ChatAgent, HostedFileSearchTool, HostedVectorStoreContent
276+ import os
277+ import tempfile
278+ from agent_framework import ChatAgent, HostedCodeInterpreterTool
277279from agent_framework.azure import AzureOpenAIResponsesClient
278280from azure.identity import AzureCliCredential
279-
280- async def main ():
281- # Create a vector store content reference
282- vector_store = HostedVectorStoreContent(vector_store_id = " vs_123" )
281+ from openai import AsyncAzureOpenAI
282+
283+ async def create_sample_file_and_upload (openai_client : AsyncAzureOpenAI) -> tuple[str , str ]:
284+ """ Create a sample CSV file and upload it to Azure OpenAI."""
285+ csv_data = """ name,department,salary,years_experience
286+ Alice Johnson,Engineering,95000,5
287+ Bob Smith,Sales,75000,3
288+ Carol Williams,Engineering,105000,8
289+ David Brown,Marketing,68000,2
290+ Emma Davis,Sales,82000,4
291+ Frank Wilson,Engineering,88000,6
292+ """
283293
284- async with ChatAgent(
285- chat_client = AzureOpenAIResponsesClient(credential = AzureCliCredential()),
286- instructions = " You are a helpful assistant that can search through files." ,
287- tools = HostedFileSearchTool(
288- inputs = vector_store,
289- max_results = 10
294+ # Create temporary CSV file
295+ with tempfile.NamedTemporaryFile(mode = " w" , suffix = " .csv" , delete = False ) as temp_file:
296+ temp_file.write(csv_data)
297+ temp_file_path = temp_file.name
298+
299+ # Upload file to Azure OpenAI
300+ print (" Uploading file to Azure OpenAI..." )
301+ with open (temp_file_path, " rb" ) as file :
302+ uploaded_file = await openai_client.files.create(
303+ file = file ,
304+ purpose = " assistants" , # Required for code interpreter
290305 )
291- ) as agent:
292- result = await agent.run(" Find information about quarterly reports" )
293- print (result.text)
294306
295- asyncio.run(main() )
296- ```
307+ print ( f " File uploaded with ID: { uploaded_file.id } " )
308+ return temp_file_path, uploaded_file.id
297309
298- ### Web Search
310+ async def cleanup_files (openai_client : AsyncAzureOpenAI, temp_file_path : str , file_id : str ) -> None :
311+ """ Clean up both local temporary file and uploaded file."""
312+ # Clean up: delete the uploaded file
313+ await openai_client.files.delete(file_id)
314+ print (f " Cleaned up uploaded file: { file_id} " )
299315
300- Access real-time information using the hosted web search tool:
301-
302- ``` python
303- import asyncio
304- from agent_framework import ChatAgent, HostedWebSearchTool
305- from agent_framework.azure import AzureOpenAIResponsesClient
306- from azure.identity import AzureCliCredential
316+ # Clean up temporary local file
317+ os.unlink(temp_file_path)
318+ print (f " Cleaned up temporary file: { temp_file_path} " )
307319
308320async def main ():
321+ print (" === Azure OpenAI Code Interpreter with File Upload ===" )
322+
323+ # Initialize Azure OpenAI client for file operations
324+ credential = AzureCliCredential()
325+
326+ async def get_token ():
327+ token = credential.get_token(" https://cognitiveservices.azure.com/.default" )
328+ return token.token
329+
330+ openai_client = AsyncAzureOpenAI(
331+ azure_ad_token_provider = get_token,
332+ api_version = " 2024-05-01-preview" ,
333+ )
334+
335+ temp_file_path, file_id = await create_sample_file_and_upload(openai_client)
336+
337+ # Create agent using Azure OpenAI Responses client
309338 async with ChatAgent(
310- chat_client = AzureOpenAIResponsesClient(credential = AzureCliCredential()),
311- instructions = " You are a helpful assistant that can search the web for current information." ,
312- tools = HostedWebSearchTool(
313- description = " Search the web for current information"
314- )
339+ chat_client = AzureOpenAIResponsesClient(credential = credential),
340+ instructions = " You are a helpful assistant that can analyze data files using Python code." ,
341+ tools = HostedCodeInterpreterTool(inputs = [{" file_id" : file_id}]),
315342 ) as agent:
316- result = await agent.run(" What are the latest developments in artificial intelligence?" )
317- print (result.text)
343+ # Test the code interpreter with the uploaded file
344+ query = " Analyze the employee data in the uploaded CSV file. Calculate average salary by department."
345+ print (f " User: { query} " )
346+ result = await agent.run(query)
347+ print (f " Agent: { result.text} " )
348+
349+ await cleanup_files(openai_client, temp_file_path, file_id)
318350
319351asyncio.run(main())
320352```
321353
322- ### Image Analysis and Generation
354+ ### Model Context Protocol (MCP) Tools
323355
324- Azure OpenAI Responses agents support multimodal interactions including image analysis:
356+ #### Local MCP Tools
357+
358+ Connect to local MCP servers for extended capabilities:
325359
326360``` python
327361import asyncio
328- from agent_framework import ChatMessage, TextContent, UriContent
362+ from agent_framework import ChatAgent, MCPStreamableHTTPTool
329363from agent_framework.azure import AzureOpenAIResponsesClient
330364from azure.identity import AzureCliCredential
331365
332366async def main ():
333- agent = AzureOpenAIResponsesClient(
334- deployment_name = " gpt-4o" , # Use vision-capable model
335- credential = AzureCliCredential()
336- ).create_agent(
337- instructions = " You are a helpful assistant that can analyze images."
338- )
367+ """ Example showing local MCP tools for Azure OpenAI Responses Agent."""
368+ # Create Azure OpenAI Responses client
369+ responses_client = AzureOpenAIResponsesClient(credential = AzureCliCredential())
339370
340- # Create a message with both text and image content
341- message = ChatMessage(
342- role = " user" ,
343- contents = [
344- TextContent(text = " What do you see in this image?" ),
345- UriContent(
346- uri = " https://example.com/image.jpg" ,
347- media_type = " image/jpeg"
348- )
349- ]
371+ # Create agent
372+ agent = responses_client.create_agent(
373+ name = " DocsAgent" ,
374+ instructions = " You are a helpful assistant that can help with Microsoft documentation questions." ,
350375 )
351-
352- result = await agent.run(message)
353- print (result.text)
376+
377+ # Connect to the MCP server (Streamable HTTP)
378+ async with MCPStreamableHTTPTool(
379+ name = " Microsoft Learn MCP" ,
380+ url = " https://learn.microsoft.com/api/mcp" ,
381+ ) as mcp_tool:
382+ # First query — expect the agent to use the MCP tool if it helps
383+ first_query = " How to create an Azure storage account using az cli?"
384+ first_result = await agent.run(first_query, tools = mcp_tool)
385+ print (" \n === Answer 1 ===\n " , first_result.text)
386+
387+ # Follow-up query (connection is reused)
388+ second_query = " What is Microsoft Agent Framework?"
389+ second_result = await agent.run(second_query, tools = mcp_tool)
390+ print (" \n === Answer 2 ===\n " , second_result.text)
354391
355392asyncio.run(main())
356393```
357394
358- ### MCP (Model Context Protocol) Tools
395+ #### Hosted MCP Tools
359396
360- Connect to external services and APIs using MCP tools :
397+ Use hosted MCP tools with approval workflows :
361398
362399``` python
363400import asyncio
@@ -366,17 +403,72 @@ from agent_framework.azure import AzureOpenAIResponsesClient
366403from azure.identity import AzureCliCredential
367404
368405async def main ():
406+ """ Example showing hosted MCP tools without approvals."""
407+ credential = AzureCliCredential()
408+
369409 async with ChatAgent(
370- chat_client = AzureOpenAIResponsesClient(credential = AzureCliCredential()),
371- instructions = " You are a helpful assistant that can search Microsoft documentation." ,
410+ chat_client = AzureOpenAIResponsesClient(credential = credential),
411+ name = " DocsAgent" ,
412+ instructions = " You are a helpful assistant that can help with microsoft documentation questions." ,
372413 tools = HostedMCPTool(
373414 name = " Microsoft Learn MCP" ,
374415 url = " https://learn.microsoft.com/api/mcp" ,
375- approval_mode = " never_require" # Auto-approve for documentation searches
376- )
416+ # Auto-approve all function calls for seamless experience
417+ approval_mode = " never_require" ,
418+ ),
377419 ) as agent:
378- result = await agent.run(" How do I create an Azure storage account using Azure CLI?" )
379- print (result.text)
420+ # First query
421+ first_query = " How to create an Azure storage account using az cli?"
422+ print (f " User: { first_query} " )
423+ first_result = await agent.run(first_query)
424+ print (f " Agent: { first_result.text} \n " )
425+
426+ print (" \n =======================================\n " )
427+
428+ # Second query
429+ second_query = " What is Microsoft Agent Framework?"
430+ print (f " User: { second_query} " )
431+ second_result = await agent.run(second_query)
432+ print (f " Agent: { second_result.text} \n " )
433+
434+ asyncio.run(main())
435+ ```
436+
437+ ### Image Analysis
438+
439+ Azure OpenAI Responses agents support multimodal interactions including image analysis:
440+
441+ ``` python
442+ import asyncio
443+ from agent_framework import ChatMessage, TextContent, UriContent
444+ from agent_framework.azure import AzureOpenAIResponsesClient
445+ from azure.identity import AzureCliCredential
446+
447+ async def main ():
448+ print (" === Azure Responses Agent with Image Analysis ===" )
449+
450+ # Create an Azure Responses agent with vision capabilities
451+ agent = AzureOpenAIResponsesClient(credential = AzureCliCredential()).create_agent(
452+ name = " VisionAgent" ,
453+ instructions = " You are a helpful agent that can analyze images." ,
454+ )
455+
456+ # Create a message with both text and image content
457+ user_message = ChatMessage(
458+ role = " user" ,
459+ contents = [
460+ TextContent(text = " What do you see in this image?" ),
461+ UriContent(
462+ uri = " https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" ,
463+ media_type = " image/jpeg" ,
464+ ),
465+ ],
466+ )
467+
468+ # Get the agent's response
469+ print (" User: What do you see in this image? [Image provided]" )
470+ result = await agent.run(user_message)
471+ print (f " Agent: { result.text} " )
380472
381473asyncio.run(main())
382474```
0 commit comments