-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
54 lines (45 loc) · 1.56 KB
/
agent.py
File metadata and controls
54 lines (45 loc) · 1.56 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
import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import MessageRole, FilePurpose, FunctionTool, FileSearchTool, ToolSet
from dotenv import load_dotenv
# Load environment variables
load_dotenv(override=True)
# Create AIProjectClient instance
project_client = AIProjectClient(
endpoint=os.environ["PROJECT_ENDPOINT"],
credential=DefaultAzureCredential()
)
# Create the agent
agent = project_client.agents.create_agent(
model="gpt-4o",
name="my-agent"
)
print(f"Created agent, ID: {agent.id}")
# Create a thread
thread = project_client.agents.threads.create()
print(f"Created thread, ID: {thread.id}")
try:
while True:
# Get the user input
user_input = input("You: ")
# Break out of the loop
if user_input.lower() in ["exit", "quit"]:
break
# Add a message to the thread
message = project_client.agents.messages.create(
thread_id=thread.id,
role=MessageRole.USER,
content=user_input
)
run = project_client.agents.runs.create_and_process(
thread_id=thread.id,
agent_id=agent.id
)
messages = project_client.agents.messages.list(thread_id=thread.id)
first_message = next(iter(messages), None)
if first_message:
print(next((item["text"]["value"] for item in first_message.content if item.get("type") == "text"), ""))
finally:
project_client.agents.delete_agent(agent.id)
print("Deleted agent")