-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_function_call.py
More file actions
77 lines (73 loc) · 2.87 KB
/
test_function_call.py
File metadata and controls
77 lines (73 loc) · 2.87 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
import os
import json
import traceback
from openai import OpenAI, APIError
from termcolor import colored
# --- Configuration ---
WRAPPER_BASE_URL = os.environ.get("WRAPPER_URL", "http://localhost:10003/v1")
TARGET_MODEL_NAME = os.environ.get("TARGET_MODEL", "DeepSeek-V3-0324")
WRAPPER_API_KEY = os.environ.get("WRAPPER_API_KEY", "sk-1111")
# --- Tool Definitions ---
multi_tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a specific city.",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location", "unit"]
}
}
},
{
"type": "function",
"function": {
"name": "calculator",
"description": "Perform calculations.",
"parameters": {
"type": "object",
"properties": {"expression": {"type": "string"}},
"required": ["expression"]
}
}
}
]
# --- Test Case Function ---
def test_multi_tool_call():
"""R2: Tests the wrapper's ability to handle standard tool call requests."""
print(colored("\n=== Test Case: R2 - Multi-Tool Call (Success) ===", 'magenta', attrs=['bold']))
messages = [{"role": "user", "content": "What is the weather in London, UK, and what is 85 * 13?"}]
try:
client = OpenAI(
base_url=WRAPPER_BASE_URL,
api_key=WRAPPER_API_KEY,
timeout=180.0,
max_retries=0
)
response = client.chat.completions.create(
model=TARGET_MODEL_NAME,
messages=messages,
tools=multi_tools,
tool_choice="auto"
)
if response.choices and response.choices[0].message and response.choices[0].message.tool_calls:
tool_calls = response.choices[0].message.tool_calls
print(colored(f"SUCCESS: Received {len(tool_calls)} tool call(s).", 'green'))
for tc in tool_calls:
args = tc.function.arguments
print(f" - Tool Call ID: {colored(tc.id, 'cyan')}, Function: {colored(tc.function.name, 'yellow')}, Args: {args}")
else:
print(colored("WARNING: No tool calls or content found in response.", 'yellow'))
except APIError as e:
print(colored(f"!!! API Error: Status Code {e.status_code}", 'red', attrs=['bold']))
print(f"Error Details: {e.message} (Type: {e.body.get('type') if e.body else 'unknown'})")
except Exception as e:
print(colored(f"!!! Unexpected Error: {type(e).__name__} - {e}", 'red', attrs=['bold']))
traceback.print_exc()
if __name__ == "__main__":
test_multi_tool_call()