api_request.py 2.99 KB
Newer Older
dengjb's avatar
update  
dengjb committed
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
from openai import OpenAI

openai_api_key = "EMPTY"
openai_api_base = "http://127.0.0.1:8000/v1"
import json

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current temperature for a given location.",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City and country e.g. Bogotá, Colombia",
                    }
                },
                "required": ["location"],
                "additionalProperties": False,
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "return_delivered_order_items",
            "description": "Return items from a delivered order",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {
                        "type": "string",
                        "description": "The order ID, e.g. #W4794911",
                    },
                    "item_ids": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "List of item IDs to return",
                    },
                    "payment_method_id": {
                        "type": "string",
                        "description": "Payment method ID for processing the return, e.g. paypal_7503218",
                    },
                },
                "required": ["order_id", "item_ids", "payment_method_id"],
                "additionalProperties": False,
            },
        },
    },
]
tools_messages = [
    {
        "role": "tool",
        "tool_call_id": "tool-call-bf208d1d-9b5f-407f-8c6e-c35e54aa2fef",
        "content": '{"city": "北京", "date": "2024-06-27", "weather": "晴", "temperature": "26C"}',
    },
]
messages = [
    {"role": "system", "content": "请你调用工具,用中文回答问题。"},
    {"role": "user", "content": "请帮我查询一下北京的天气。"},
]

client = OpenAI(
    api_key=openai_api_key,
    base_url=openai_api_base,
)
completion = client.chat.completions.create(
dengjb's avatar
dengjb committed
71
    model="glm-4.5",
dengjb's avatar
update  
dengjb committed
72
73
74
75
76
    messages=messages,
    max_tokens=4096,
    temperature=0.0,
    # extra_body={"chat_template_kwargs": {"enable_thinking": False}} # Uncomment this line to disable thinking
)
dengjb's avatar
dengjb committed
77
78
79
80
81
print(completion.choices[0].message)
# tool_call = completion.choices[0].message.tool_calls[0]
# args = json.loads(tool_call.function.arguments)
# print("===== TOOL CALL =====")
# print(tool_call)
dengjb's avatar
update  
dengjb committed
82
83
84
85
86
messages.append(completion.choices[0].message)
messages.append(tools_messages[0])

## This part is to simulate the tool response
completion_2 = client.chat.completions.create(
dengjb's avatar
dengjb committed
87
    model="glm-4.5",
dengjb's avatar
update  
dengjb committed
88
89
90
91
92
93
    messages=messages,
    tools=tools,
    extra_body={"chat_template_kwargs": {"enable_thinking": False}},
)
print("===== RESPONSE =====")
print(completion_2.choices[0].message.content)