Skip to content

Commit 910a7f0

Browse files
committed
added agent features for openai clients
1 parent 433d458 commit 910a7f0

3 files changed

Lines changed: 376 additions & 7 deletions

File tree

agent-framework/user-guide/agents/agent-types/openai-assistants-agent.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,100 @@ async def code_interpreter_example():
242242
print(result.text)
243243
```
244244

245+
### File Search
246+
247+
Enable your assistant to search through uploaded documents:
248+
249+
```python
250+
from agent_framework import HostedFileSearchTool, HostedVectorStoreContent
251+
252+
async def file_search_example():
253+
client = OpenAIAssistantsClient()
254+
255+
# Create a vector store with documents
256+
file = await client.client.files.create(
257+
file=("knowledge.txt", b"The weather today is sunny with a high of 75F."),
258+
purpose="user_data"
259+
)
260+
vector_store = await client.client.vector_stores.create(
261+
name="knowledge_base",
262+
expires_after={"anchor": "last_active_at", "days": 1},
263+
)
264+
await client.client.vector_stores.files.create_and_poll(
265+
vector_store_id=vector_store.id,
266+
file_id=file.id
267+
)
268+
269+
try:
270+
async with client.create_agent(
271+
name="KnowledgeBot",
272+
instructions="You are a helpful assistant that searches files in a knowledge base.",
273+
tools=HostedFileSearchTool(
274+
vector_stores=[HostedVectorStoreContent(vector_store_id=vector_store.id)]
275+
),
276+
) as agent:
277+
result = await agent.run("What does the document say about weather?")
278+
print(result.text)
279+
finally:
280+
# Cleanup
281+
await client.client.vector_stores.delete(vector_store.id)
282+
await client.client.files.delete(file.id)
283+
```
284+
285+
### Thread Management
286+
287+
Maintain conversation context across multiple interactions:
288+
289+
```python
290+
from agent_framework import AgentThread
291+
292+
async def thread_example():
293+
async with OpenAIAssistantsClient().create_agent(
294+
name="Assistant",
295+
instructions="You are a helpful assistant.",
296+
) as agent:
297+
# Create a persistent thread for conversation context
298+
async with AgentThread() as thread:
299+
# First interaction
300+
result1 = await agent.run("My name is Alice", thread=thread)
301+
print(f"Agent: {result1.text}")
302+
303+
# Second interaction - agent remembers the context
304+
result2 = await agent.run("What's my name?", thread=thread)
305+
print(f"Agent: {result2.text}") # Should remember "Alice"
306+
```
307+
308+
### Working with Existing Assistants
309+
310+
You can reuse existing OpenAI assistants by providing their IDs:
311+
312+
```python
313+
from openai import AsyncOpenAI
314+
315+
async def existing_assistant_example():
316+
# Create OpenAI client directly
317+
client = AsyncOpenAI()
318+
319+
# Create or get an existing assistant
320+
assistant = await client.beta.assistants.create(
321+
model="gpt-4o-mini",
322+
name="WeatherAssistant",
323+
instructions="You are a weather forecasting assistant."
324+
)
325+
326+
try:
327+
# Use the existing assistant with Agent Framework
328+
async with OpenAIAssistantsClient(
329+
async_client=client,
330+
assistant_id=assistant.id
331+
).create_agent() as agent:
332+
result = await agent.run("What's the weather like in Seattle?")
333+
print(result.text)
334+
finally:
335+
# Clean up the assistant
336+
await client.beta.assistants.delete(assistant.id)
337+
```
338+
245339
### Streaming Responses
246340

247341
Get responses as they are generated for better user experience:

agent-framework/user-guide/agents/agent-types/openai-chat-completion-agent.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,77 @@ async def tools_example():
162162
print(result.text)
163163
```
164164

165+
### Web Search
166+
167+
Enable real-time web search capabilities:
168+
169+
```python
170+
from agent_framework import HostedWebSearchTool
171+
172+
async def web_search_example():
173+
# Configure location for better search results
174+
additional_properties = {
175+
"user_location": {
176+
"country": "US",
177+
"city": "Seattle",
178+
}
179+
}
180+
181+
agent = OpenAIChatClient(model_id="gpt-4o-search-preview").create_agent(
182+
name="SearchBot",
183+
instructions="You are a helpful assistant that can search the web for current information.",
184+
tools=HostedWebSearchTool(additional_properties=additional_properties),
185+
)
186+
187+
result = await agent.run("What's the current weather in Seattle?")
188+
print(result.text)
189+
```
190+
191+
### Model Context Protocol (MCP) Tools
192+
193+
Connect to local MCP servers for extended capabilities:
194+
195+
```python
196+
from agent_framework import MCPStreamableHTTPTool
197+
198+
async def local_mcp_example():
199+
agent = OpenAIChatClient().create_agent(
200+
name="DocsAgent",
201+
instructions="You are a helpful assistant that can help with Microsoft documentation.",
202+
tools=MCPStreamableHTTPTool(
203+
name="Microsoft Learn MCP",
204+
url="https://learn.microsoft.com/api/mcp",
205+
),
206+
)
207+
208+
result = await agent.run("How do I create an Azure storage account using az cli?")
209+
print(result.text)
210+
```
211+
212+
### Thread Management
213+
214+
Maintain conversation context across multiple interactions:
215+
216+
```python
217+
from agent_framework import AgentThread
218+
219+
async def thread_example():
220+
agent = OpenAIChatClient().create_agent(
221+
name="Assistant",
222+
instructions="You are a helpful assistant.",
223+
)
224+
225+
# Create a persistent thread for conversation context
226+
async with AgentThread() as thread:
227+
# First interaction
228+
result1 = await agent.run("My name is Alice", thread=thread)
229+
print(f"Agent: {result1.text}")
230+
231+
# Second interaction - agent remembers the context
232+
result2 = await agent.run("What's my name?", thread=thread)
233+
print(f"Agent: {result2.text}") # Should remember "Alice"
234+
```
235+
165236
### Streaming Responses
166237

167238
Get responses as they are generated for better user experience:

0 commit comments

Comments
 (0)