@@ -49,80 +49,7 @@ Console.WriteLine(await agent.RunAsync("What does the document say about today's
4949
5050The following example shows how to create an agent with the File Search tool and sample documents:
5151
52- ### Define sample documents
53-
54- ``` python
55- # Copyright (c) Microsoft. All rights reserved.
56-
57- import asyncio
58-
59- from agent_framework import Agent, Content
60- from agent_framework.openai import OpenAIChatClient
61-
62- """
63- OpenAI Responses Client with File Search Example
64-
65- This sample demonstrates using get_file_search_tool() with OpenAI Responses Client
66- for direct document-based question answering and information retrieval.
67- """
68-
69- # Helper functions
70-
71-
72- async def create_vector_store (client : OpenAIChatClient) -> tuple[str , Content]:
73- """ Create a vector store with sample documents."""
74- file = await client.client.files.create(
75- file = (" todays_weather.txt" , b " The weather today is sunny with a high of 75F." ), purpose = " user_data"
76- )
77- vector_store = await client.client.vector_stores.create(
78- name = " knowledge_base" ,
79- expires_after = {" anchor" : " last_active_at" , " days" : 1 },
80- )
81- result = await client.client.vector_stores.files.create_and_poll(vector_store_id = vector_store.id, file_id = file .id)
82- if result.last_error is not None :
83- raise Exception (f " Vector store file processing failed with status: { result.last_error.message} " )
84-
85- return file .id, Content.from_hosted_vector_store(vector_store_id = vector_store.id)
86-
87-
88- async def delete_vector_store (client : OpenAIChatClient, file_id : str , vector_store_id : str ) -> None :
89- """ Delete the vector store after using it."""
90- await client.client.vector_stores.delete(vector_store_id = vector_store_id)
91- await client.client.files.delete(file_id = file_id)
92-
93-
94- async def main () -> None :
95- client = OpenAIChatClient()
96-
97- message = " What is the weather today? Do a file search to find the answer."
98-
99- stream = False
100- print (f " User: { message} " )
101- file_id, vector_store_id = await create_vector_store(client)
102-
103- agent = Agent(
104- client = client,
105- instructions = " You are a helpful assistant that can search through files to find information." ,
106- tools = [client.get_file_search_tool(vector_store_ids = [vector_store_id])],
107- )
108-
109- if stream:
110- print (" Assistant: " , end = " " )
111- async for chunk in agent.run(message, stream = True ):
112- if chunk.text:
113- print (chunk.text, end = " " )
114- print (" " )
115- else :
116- response = await agent.run(message)
117- print (f " Assistant: { response} " )
118- await delete_vector_store(client, file_id, vector_store_id)
119-
120-
121- if __name__ == " __main__" :
122- asyncio.run(main())
123- ```
124-
125- ### Run the agent
52+ ### File Search Tool Example
12653
12754``` python
12855# Copyright (c) Microsoft. All rights reserved.
0 commit comments