Repository navigation
Expand file tree
/
Copy pathazure_openai_responses_agent_declarative_file_search.py
More file actions
95 lines (76 loc) · 2.83 KB
/
Copy pathazure_openai_responses_agent_declarative_file_search.py
File metadata and controls
95 lines (76 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import os
from azure.identity import AzureCliCredential
from semantic_kernel.agents import AgentRegistry, AzureResponsesAgent
"""
The following sample demonstrates how to create an Azure Responses Agent that answers
user questions using the file search tool.
"""
# Define the YAML string for the sample
spec = """
type: azure_responses
name: FileSearchAgent
description: Agent with file search tool.
instructions: >
Use the file search tool to answer questions from the user.
model:
id: ${AzureOpenAI:ChatModelId}
connection:
endpoint: ${AzureOpenAI:Endpoint}
tools:
- type: file_search
options:
vector_store_ids:
- ${AzureOpenAI:VectorStoreId}
"""
async def main():
# Setup the Azure OpenAI client
client = AzureResponsesAgent.create_client(credential=AzureCliCredential())
# Read and upload the file to the OpenAI AI service
pdf_file_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))),
"resources",
"file_search",
"employees.pdf",
)
# Upload the pdf file to the server
with open(pdf_file_path, "rb") as file:
file = await client.files.create(file=file, purpose="assistants")
vector_store = await client.vector_stores.create(
name="responses_file_search",
file_ids=[file.id],
)
try:
# Create the Responses Agent from the YAML spec
# Note: the extras can be provided in the short-format (shown below) or
# in the long-format (as shown in the YAML spec, with the `AzureOpenAI:` prefix).
# The short-format is used here for brevity
agent: AzureResponsesAgent = await AgentRegistry.create_from_yaml(
yaml_str=spec,
client=client,
extras={"AzureOpenAI:VectorStoreId": vector_store.id},
)
# Define the task for the agent
TASK = "Who can help me if I have a sales question?"
print(f"# User: '{TASK}'")
# Invoke the agent for the specified task
async for response in agent.invoke(
messages=TASK,
):
print(f"# {response.name}: {response}")
finally:
# Cleanup: Delete the agent, vector store, and file
await client.vector_stores.delete(vector_store.id)
await client.files.delete(file.id)
"""
Sample output:
# User: 'Who can help me if I have a sales question?'
# FileSearchAgent: If you have a sales question, you may contact the following individuals:
1. **Hicran Bea** - Sales Manager
2. **Mariam Jaslyn** - Sales Representative
3. **Angelino Embla** - Sales Representative
This information comes from the employee records【4:0†source】.
"""
if __name__ == "__main__":
asyncio.run(main())