-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-calling-python.py
More file actions
65 lines (50 loc) · 1.84 KB
/
Copy pathfunction-calling-python.py
File metadata and controls
65 lines (50 loc) · 1.84 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
#!/usr/bin/env python3
"""Function calling (tool use) via the OpenAI SDK against APIVAI.
Shows the standard two-step flow: the model returns a tool call, you run the tool,
then send the result back for a final answer.
pip install openai
"""
import json
import os
from openai import OpenAI
API_KEY = os.getenv("APIVAI_API_KEY", "YOUR_APIVAI_API_KEY")
BASE_URL = os.getenv("APIVAI_BASE_URL", "https://api.apivai.com/v1")
MODEL = os.getenv("APIVAI_MODEL", "claude-sonnet-4-6")
TOOLS = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
def get_weather(city: str) -> dict:
# Stub — replace with a real API call.
return {"city": city, "temp_c": 21, "conditions": "clear"}
def main() -> None:
client = OpenAI(api_key=API_KEY, base_url=BASE_URL)
messages = [{"role": "user", "content": "What's the weather in Tokyo?"}]
first = client.chat.completions.create(model=MODEL, messages=messages, tools=TOOLS)
choice = first.choices[0].message
if not choice.tool_calls:
print(choice.content)
return
messages.append(choice)
for call in choice.tool_calls:
args = json.loads(call.function.arguments)
result = get_weather(**args)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
final = client.chat.completions.create(model=MODEL, messages=messages, tools=TOOLS)
print(final.choices[0].message.content)
if __name__ == "__main__":
if API_KEY == "YOUR_APIVAI_API_KEY":
print("Warning: set APIVAI_API_KEY before sending real requests.")
main()