-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathpython-function-calling-streaming.py
More file actions
85 lines (66 loc) · 3.23 KB
/
python-function-calling-streaming.py
File metadata and controls
85 lines (66 loc) · 3.23 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
import os
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
MessageDeltaChunk,
MessageDeltaTextContent,
RunStep,
ThreadMessage,
ThreadRun,
)
from azure.ai.projects.models import AgentEventHandler
from azure.identity import DefaultAzureCredential
from azure.ai.projects.models import FunctionTool, ToolSet
from user_functions import user_functions
from typing import Dict, Union
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
project_client = AIProjectClient.from_connection_string(
credential=DefaultAzureCredential(), conn_str=os.environ["PROJECT_CONNECTION_STRING"]
)
# When using FunctionTool with ToolSet in agent creation, the tool call events are handled inside the create_stream
# method and functions gets automatically called by default.
class MyEventHandler(AgentEventHandler):
def on_message_delta(self, delta: "MessageDeltaChunk") -> None:
for content_part in delta.delta.content:
if isinstance(content_part, MessageDeltaTextContent):
text_value = content_part.text.value if content_part.text else "No text"
print(f"Text delta received: {text_value}")
def on_thread_message(self, message: "ThreadMessage") -> None:
print(f"ThreadMessage created. ID: {message.id}, Status: {message.status}")
def on_thread_run(self, run: "ThreadRun") -> None:
print(f"ThreadRun status: {run.status}")
if run.status == "failed":
print(f"Run failed. Error: {run.last_error}")
def on_run_step(self, step: "RunStep") -> None:
print(f"RunStep type: {step.type}, Status: {step.status}")
def on_error(self, data: str) -> None:
print(f"An error occurred. Data: {data}")
def on_done(self) -> None:
print("Stream completed.")
def on_unhandled_event(self, event_type: str, event_data: Union[str, Dict[str, any]]) -> None:
print(f"Unhandled Event Type: {event_type}, Data: {event_data}")
with project_client:
functions = FunctionTool(user_functions)
toolset = ToolSet()
toolset.add(functions)
agent = project_client.agents.create_agent(
model="gpt-4o-mini", name="my-assistant", instructions="You are a helpful assistant", toolset=toolset
)
print(f"Created agent, ID: {agent.id}")
thread = project_client.agents.create_thread()
print(f"Created thread, thread ID {thread.id}")
message = project_client.agents.create_message(
thread_id=thread.id,
role="user",
content="Hello, send an email with the datetime and weather information in New York? Also let me know the details",
)
print(f"Created message, message ID {message.id}")
with project_client.agents.create_stream(
thread_id=thread.id, agent_id=agent.id, event_handler=MyEventHandler()
) as stream:
stream.until_done()
project_client.agents.delete_agent(agent.id)
print("Deleted agent")
messages = project_client.agents.list_messages(thread_id=thread.id)
print(f"Messages: {messages}")