()
| 403 | |
| 404 | |
| 405 | def test_function_call(): |
| 406 | client = LightLLMClient() |
| 407 | |
| 408 | # 定义函数 |
| 409 | tools = [ |
| 410 | { |
| 411 | "type": "function", |
| 412 | "function": { |
| 413 | "name": "get_weather", |
| 414 | "description": "获取指定城市的天气信息", |
| 415 | "parameters": { |
| 416 | "type": "object", |
| 417 | "properties": { |
| 418 | "city": {"type": "string", "description": "城市名称,例如:北京、上海"}, |
| 419 | "unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "description": "温度单位"}, |
| 420 | }, |
| 421 | "required": ["city"], |
| 422 | }, |
| 423 | }, |
| 424 | }, |
| 425 | { |
| 426 | "type": "function", |
| 427 | "function": { |
| 428 | "name": "calculate", |
| 429 | "description": "执行数学计算", |
| 430 | "parameters": { |
| 431 | "type": "object", |
| 432 | "properties": {"expression": {"type": "string", "description": "数学表达式,例如:2+3*4"}}, |
| 433 | "required": ["expression"], |
| 434 | }, |
| 435 | }, |
| 436 | }, |
| 437 | ] |
| 438 | |
| 439 | try: |
| 440 | # 测试天气查询 |
| 441 | print("用户: 北京今天天气怎么样?") |
| 442 | result = client.function_call("北京今天天气怎么样?", tools) |
| 443 | message = result["choices"][0]["message"] |
| 444 | |
| 445 | if message.get("tool_calls"): |
| 446 | print("助手决定调用函数:") |
| 447 | for tool_call in message["tool_calls"]: |
| 448 | print(f" 函数名: {tool_call['function']['name']}") |
| 449 | print(f" 参数: {tool_call['function']['arguments']}") |
| 450 | else: |
| 451 | print("助手:", message["content"]) |
| 452 | print() |
| 453 | |
| 454 | # 测试数学计算 |
| 455 | print("用户: 请计算 25 * 4 + 10 的结果") |
| 456 | result = client.function_call("请计算 25 * 4 + 10 的结果", tools) |
| 457 | message = result["choices"][0]["message"] |
| 458 | |
| 459 | if message.get("tool_calls"): |
| 460 | print("助手决定调用函数:") |
| 461 | for tool_call in message["tool_calls"]: |
| 462 | print(f" 函数名: {tool_call['function']['name']}") |
no test coverage detected