-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.py
More file actions
65 lines (55 loc) · 1.82 KB
/
Copy pathquickstart.py
File metadata and controls
65 lines (55 loc) · 1.82 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
"""Build a small custom agent loop from CapSolver Agent JSON schemas."""
import asyncio
import json
import os
from typing import Any
from capsolver_agent import create_executor, get_all_tools
from openai import AsyncOpenAI
async def main() -> None:
client = AsyncOpenAI()
executor = create_executor()
tools = [tool.to_openai_function() for tool in get_all_tools()]
messages: list[dict[str, Any]] = [
{
"role": "system",
"content": (
"Use CapSolver only for lawful, user-authorized workflows. "
"Never invent target details or call a solve tool without required inputs."
),
},
{
"role": "user",
"content": os.getenv(
"DEMO_PROMPT",
"Check my CapSolver balance with the available tools.",
),
},
]
first = await client.chat.completions.create(
model=os.getenv("OPENAI_MODEL", "gpt-4.1-mini"),
messages=messages,
tools=tools,
)
assistant = first.choices[0].message
messages.append(assistant.model_dump(exclude_none=True))
if not assistant.tool_calls:
print(assistant.content)
return
for call in assistant.tool_calls:
arguments = json.loads(call.function.arguments)
result = await executor.execute(call.function.name, arguments)
messages.append(
{
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result, ensure_ascii=False),
}
)
final = await client.chat.completions.create(
model=os.getenv("OPENAI_MODEL", "gpt-4.1-mini"),
messages=messages,
tools=tools,
)
print(final.choices[0].message.content)
if __name__ == "__main__":
asyncio.run(main())