You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用OpenAI API函数调用获取指定建筑的描述与经纬度坐标?

问题修复:通过ChatGPT函数调用获取建筑描述与坐标

先梳理你代码中的核心问题,再给出可运行的修正版本:

代码中的关键错误

  • 函数参数调用不匹配:调用get_local_attractions时传了lat参数,但函数定义里是coordinates
  • JSON Schema格式错误:functions里的location属性包含无效的coord字段,不符合OpenAI函数调用的参数规范
  • 变量拼写错误:fuction_to_call应为function_to_call
  • 函数名不匹配:最后调用的run_conversation()未定义,实际定义的是run_query()
  • 占位符未替换:用户消息、函数描述、参数描述都是占位符,GPT无法理解需求
  • 逻辑冗余:你的get_local_attractions只是返回传入的参数,无需二次调用GPT,直接让GPT通过函数调用返回结构化数据即可

修正后的代码

import json
import openai

# 配置OpenAI API密钥(替换为你的密钥)
openai.api_key = "你的API密钥"

def get_building_info(location, description, coordinates):
    """
    获取指定建筑的描述与坐标信息
    :param location: 建筑所在区域(如"北京市")
    :param description: 建筑的详细描述文本
    :param coordinates: 建筑的经纬度,格式为"lat xx.xxx, lng xx.xxx"
    :return: 结构化的JSON字符串
    """
    building_info = {
        "location": location,
        "description": description,
        "coordinates": coordinates
    }
    return json.dumps(building_info)

def run_query():
    # 替换为实际的用户查询,比如"告诉我上海东方明珠的描述和坐标"
    user_query = "告诉我上海东方明珠的描述和坐标"
    messages = [{"role": "user", "content": user_query}]
    
    functions = [
        {
            "name": "get_building_info",
            "description": "根据用户提供的建筑信息,返回该建筑的详细描述和经纬度坐标",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "建筑所在的城市或区域,例如'上海市'"
                    },
                    "description": {
                        "type": "string",
                        "description": "建筑的详细描述,包括历史、功能、特色等"
                    },
                    "coordinates": {
                        "type": "string",
                        "description": "建筑的经纬度,格式为'lat xx.xxx, lng xx.xxx',例如'lat 31.2304, lng 121.4737'"
                    }
                },
                "required": ["location", "description", "coordinates"]
            }
        }
    ]
    
    # 发起函数调用请求
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=messages,
        functions=functions,
        function_call={"name": "get_building_info"}  # 指定强制调用该函数,确保返回结构化数据
    )
    
    response_message = response["choices"][0]["message"]
    
    # 解析函数调用的参数
    if response_message.get("function_call"):
        function_args = json.loads(response_message["function_call"]["arguments"])
        # 直接使用GPT返回的参数生成结构化结果
        result = get_building_info(
            location=function_args.get("location"),
            description=function_args.get("description"),
            coordinates=function_args.get("coordinates")
        )
        return result

# 执行查询并打印结果
print(run_query())

关键修改说明

  1. 明确函数定义:将函数名改为get_building_info,清晰描述函数用途,参数说明更具体
  2. 修正JSON Schema:规范parameters的格式,明确每个参数的类型和描述,确保GPT能正确生成参数
  3. 强制函数调用:将function_call设置为{"name": "get_building_info"},确保GPT返回结构化的参数,而非自然语言
  4. 简化逻辑:去掉二次调用GPT的步骤,直接解析GPT返回的函数参数生成结果
  5. 替换占位符:填充实际的用户查询和参数描述,让GPT明确需求
  6. 修复拼写和命名错误:修正变量名和函数名的不匹配问题

运行修正后的代码,你将得到格式规范的JSON结果,例如:

{"location": "上海市", "description": "东方明珠广播电视塔是上海的标志性文化景观之一,位于浦东新区陆家嘴,塔高468米,是集观光、餐饮、购物、娱乐于一体的综合性建筑", "coordinates": "lat 31.2304, lng 121.4737"}

内容的提问来源于stack exchange,提问作者Myuser03

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 07:23:08