-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathagent_workflow.py
More file actions
88 lines (71 loc) · 2.2 KB
/
Copy pathagent_workflow.py
File metadata and controls
88 lines (71 loc) · 2.2 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
"""Tool-calling style agent workflow example for Agnes AI."""
import json
import os
from typing import Any
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AGNES_API_KEY"],
base_url="https://apihub.agnes-ai.com/v1",
)
def get_model_status(model: str) -> dict[str, str]:
return {
"model": model,
"status": "available",
"base_url": "https://apihub.agnes-ai.com/v1",
}
TOOLS: list[dict[str, Any]] = [
{
"type": "function",
"function": {
"name": "get_model_status",
"description": "Return public status information for an Agnes AI model.",
"parameters": {
"type": "object",
"properties": {
"model": {
"type": "string",
"description": "Agnes AI model name.",
}
},
"required": ["model"],
},
},
}
]
def main() -> None:
messages: list[dict[str, Any]] = [
{
"role": "user",
"content": "Check whether agnes-2.0-flash is available, then summarize how to call it.",
}
]
first = client.chat.completions.create(
model="agnes-2.0-flash",
messages=messages,
tools=TOOLS,
tool_choice="auto",
)
assistant_message = first.choices[0].message
messages.append(assistant_message.model_dump(exclude_none=True))
if assistant_message.tool_calls:
for tool_call in assistant_message.tool_calls:
if tool_call.function.name != "get_model_status":
continue
args = json.loads(tool_call.function.arguments)
result = get_model_status(args["model"])
messages.append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result),
}
)
final = client.chat.completions.create(
model="agnes-2.0-flash",
messages=messages,
)
print(final.choices[0].message.content)
else:
print(assistant_message.content)
if __name__ == "__main__":
main()