-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
81 lines (63 loc) · 2.72 KB
/
app.py
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
import asyncio
import os
import dotenv
from semantic_kernel.agents.open_ai.azure_assistant_agent import AzureAssistantAgent
from semantic_kernel.contents.annotation_content import AnnotationContent
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.kernel import Kernel
dotenv.load_dotenv(override=True)
AZURE_OPENAI_DEPLOYMENT_NAME: str = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME", "")
AZURE_OPENAI_ENDPOINT: str = os.getenv("AZURE_OPENAI_ENDPOINT", "")
AZURE_OPENAI_API_KEY: str = os.getenv("AZURE_OPENAI_API_KEY", "")
# A helper method to invoke the agent with the user input
async def invoke_agent(agent: AzureAssistantAgent, thread_id: str, input: str) -> None:
await agent.add_chat_message(
thread_id=thread_id,
message=ChatMessageContent(role=AuthorRole.USER, content=input),
)
print("= " * 20, "thread", "= " * 20)
print(f"# {AuthorRole.USER}: '{input}'")
async for content in agent.invoke(thread_id=thread_id):
print("- " * 20, "message", "- " * 20)
print(f"# {content.role}: {content.content}")
# view code when included generated code
if len(content.items) > 0:
for item in content.items:
if isinstance(item, AnnotationContent) and item.file_id:
print("- " * 20, "generated file", "- " * 20)
print(f"\n`{item.quote}` => {item.file_id}")
response_content = await agent.client.files.content(item.file_id)
print(response_content.text)
async def main():
kernel = Kernel()
agent = AzureAssistantAgent(
kernel=kernel,
service_id="agent",
name="FileManipulation",
instructions="Find answers to the user's questions in the provided file.",
enable_file_search=True,
enable_code_interpreter=True,
api_key=AZURE_OPENAI_API_KEY,
endpoint=AZURE_OPENAI_ENDPOINT,
deployment_name=AZURE_OPENAI_DEPLOYMENT_NAME,
api_version="2024-05-01-preview",
)
await agent.create_assistant()
csv_file_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "sample_sales_data.csv"
)
file_id = await agent.add_file(csv_file_path, purpose="assistants")
thread_id = await agent.create_thread(code_interpreter_file_ids=[file_id])
try:
await invoke_agent(
agent,
thread_id=thread_id,
input="Which category had the most sales?",
)
finally:
await agent.client.files.delete(file_id)
await agent.delete_thread(thread_id)
await agent.delete()
if __name__ == "__main__":
asyncio.run(main())