大模型工具调用和学历验证

📅 2026/8/6 4:46:32
大模型工具调用和学历验证
文章目录一、大模型工具调用二、学历验证一、大模型工具调用后端代码# 初始化客户端 import json,os,random from openai import OpenAI def get_current_weather(arguments): weather_conditions [晴天, 多云, 雨天] random_weather random.choice(weather_conditions) location arguments.get(location) return f{location}今天是{random_weather}。 tools [ { type: function, function: { name: get_current_weather, description: 当你想查询指定城市的天气时非常有用。, parameters: { type: object, properties: { location: { type: string, description: 城市或县区比如北京市、杭州市、余杭区等。, } }, required: [location], }, }, } ] client OpenAI( # 各地域的API Key不同。获取API Keyhttps://help.aliyun.com/zh/model-studio/get-api-key # 若没有配置环境变量请用阿里云百炼API Key将下行替换为api_keysk-xxx, api_keyos.getenv(DASHSCOPE_API_KEY), # 以下为华北2北京地域的URL调用时请将{WorkspaceId}替换为真实的业务空间ID各地域的URL不同。 base_urlhttps://llm-bsilu0g23xla7myv.cn-beijing.maas.aliyuncs.com/compatible-mode/v1, ) messages [] def get_ai_response(messages): completions client.chat.completions.create( modelqwen-plus, messagesmessages, toolstools, temperature0.75, ) return completions user_message {role: user, content: 北京今天的天气} messages.append(user_message) completion get_ai_response(messages) print(completion.model_dump_json()) if completion.choices[0].message.tool_calls is None: print(f不需要调用工具) print(completion.choices[0].message.content) else: print(需要调用工具) tool_calls completion.choices[0].message.tool_calls for tool_call in tool_calls: tool_id tool_call.id func_name tool_call.function.name func_arguments tool_call.function.arguments print(f大模型告诉程序要调用这个工具:{func_name},参数是:{func_arguments}) function_mapping { get_current_weather: get_current_weather } print(type(func_arguments)) tool_result function_mapping[func_name](json.loads(func_arguments)) print(f工具返回的结果是:{tool_result}) tool_message { role: tool, content: tool_result, tool_call_id: tool_id } messages.append(tool_message) completion get_ai_response(messages) print(completion.model_dump_json()) print(f最终的结果是:{completion.choices[0].message.content})二、学历验证后端代码# 初始化客户端 import json,os,random,requests from asyncpg import DataError from openai import OpenAI from redis import Redis r Redis(host127.0.0.1,port6379,db2,decode_responsesTrue) # 模拟天气查询工具{location:北京} def get_current_weather(arguments): weather_conditions [晴天, 多云, 雨天] random_weather random.choice(weather_conditions) location arguments[location] return f{location}今天是{random_weather}。 def academic_credential_verification(arguments): vcode arguments[vcode] key f4cc:llm:academic_credential_verification:vcode_{vcode} redis_verification_data r.get(key) if redis_verification_data is None: BASE_URL https://www.apimy.cn/api/xxw/bgcx payload {key: os.getenv(MY_XXW_BGCX_API_KEY), vcode: arguments[vcode]} headers {Content-Type: application/json} response requests.post(BASE_URL, jsonpayload, headersheaders, timeout30) response.raise_for_status() data response.json() r.set(key, json.dumps(data, ensure_asciiFalse)) return json.loads(data, ensure_asciiFalse) else: return redis_verification_data tools [ { type: function, function: { name: get_current_weather, description: 当你想查询指定城市的天气时非常有用。, parameters: { type: object, properties: { location: { type: string, description: 城市或县区比如北京市、杭州市、余杭区等。, } }, required: [location], }, }, }, { type: function, function: { name: academic_credential_verification, description: 当你想查询学历或者验证学历时非常有用。, parameters: { type: object, properties: { vcode: { type: string, description: 学历验证码, } }, required: [vcode], }, }, } ] client OpenAI( # 各地域的API Key不同。获取API Keyhttps://help.aliyun.com/zh/model-studio/get-api-key # 若没有配置环境变量请用阿里云百炼API Key将下行替换为api_keysk-xxx, api_keyos.getenv(DASHSCOPE_API_KEY), # 以下为华北2北京地域的URL调用时请将{WorkspaceId}替换为真实的业务空间ID各地域的URL不同。 base_urlhttps://llm-bsilu0g23xla7myv.cn-beijing.maas.aliyuncs.com/compatible-mode/v1, ) messages [] def get_ai_response(messages): completions client.chat.completions.create( modelqwen-plus, messagesmessages, toolstools, temperature0.75, ) return completions user_message {role: user, content: 北京今天的天气} messages.append(user_message) completion get_ai_response(messages) messages.append(completion.choices[0].message) if completion.choices[0].message.tool_calls is None: print(f不需要调用工具) print(completion.choices[0].message.content) else: print(需要调用工具) tool_calls completion.choices[0].message.tool_calls for tool_call in tool_calls: tool_id tool_call.id func_name tool_call.function.name func_arguments tool_call.function.arguments print() print(f大模型告诉程序要调用这个工具:{func_name},参数是:{func_arguments}) function_mapping { get_current_weather: get_current_weather, academic_credential_verification: academic_credential_verification } print(type(func_arguments)) tool_result function_mapping[func_name](json.loads(func_arguments)) print(f工具返回的结果是:{tool_result}) tool_message { content: tool_result, role: tool, tool_call_id: tool_id } messages.append(tool_message) completion get_ai_response(messages) print(completion.model_dump_json()) print(f最终的结果是:{completion.choices[0].message.content})