尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

大模型连接工具类

大模型连接工具类 网站如何通过Function Calling 实现工具调用-大模型服务平台百炼(Model Studio)-阿里云帮助中心https://help.aliyun.com/zh/model-studio/qwen-function-calling产品列表 - 梦远数据https://www.apimy.cn/user/api-list实例case1# 初始化客户端 import json import os import random from openai import OpenAI # 初始化客户端 client OpenAI( # 若没有配置环境变量请用阿里云百炼API Key将下行替换为api_keysk-xxx, # 各地域的API Key不同。获取API Keyhttps://help.aliyun.com/zh/model-studio/get-api-key api_keyos.getenv(DASHSCOPE_API_KEY), # 以下为华北2北京地域的URL调用时请将 {WorkspaceId} 替换为真实的业务空间ID各地域的URL不同。 base_urlhttps://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1, ) # 模拟用户问题 USER_QUESTION 北京天气咋样 # 定义工具列表 tools [ { type: function, function: { name: get_current_weather, description: 当你想查询指定城市的天气时非常有用。, parameters: { type: object, properties: { location: { type: string, description: 城市或县区比如北京市、杭州市、余杭区等。, } }, required: [location], }, }, }, ] # 模拟天气查询工具 def get_current_weather(arguments: dict): weather_conditions [晴天, 多云, 雨天] random_weather random.choice(weather_conditions) location arguments.get(location) return f{location}今天是{random_weather}。 def get_ai_response(messages): completion client.chat.completions.create( # 模型列表https://help.aliyun.com/zh/model-studio/getting-started/models modelqwen-plus, messagesmessages, temperature0.75, toolstools ) return completion messages [] user_message {role: user, content: 明天北京天气怎么样} messages.append(user_message) completion get_ai_response(messages) print(completion.model_dump_json()) messages.append(completion.choices[0].message) # 判断第一次大模型返回的结果:是否需要调用工具 if completion.choices[0].message.tool_calls is None: print(不需要调用工具) 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 { 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})学历验证case2# 初始化客户端 import json import os import random import requests from openai import OpenAI from redis import Redis redis_client Redis( 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] keyfboss:llm:academic_credential_verification:{vcode} redis_verification_data redis_client.get(key) if redis_verification_data is None: BASE_URL https://www.apimy.cn/api/xxw/bgcx payload {key: os.get(YOUR_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() redis_client.set(key, json.dumps(data, ensure_asciiFalse)) return json.dumps(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_keysk-xxx, # 各地域的API Key不同。获取API Keyhttps://help.aliyun.com/zh/model-studio/get-api-key api_keyos.getenv(DASHSCOPE_API_KEY), # 以下为华北2北京地域的URL调用时请将 {WorkspaceId} 替换为真实的业务空间ID各地域的URL不同。 base_urlhttps://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1, ) messages [] def get_ai_response(messages): completion client.chat.completions.create( # 模型列表https://help.aliyun.com/zh/model-studio/getting-started/models modelqwen-plus, messagesmessages, temperature0.75, toolstools ) return completion user_message {role: user, content: 帮我查询一下学历, 验证码是:vcode} messages.append(user_message) completion get_ai_response(messages) print(completion.model_dump_json()) messages.append(completion.choices[0].message) # 定义工具函数映射 function_mapping { get_current_weather: get_current_weather, academic_credential_verification: academic_credential_verification } # 判断第一次大模型返回的结果:是否需要调用工具 if completion.choices[0].message.tool_calls is None: print(不需要调用工具) 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}) 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})
返回列表