Hk-Gosuto / ChatGPT-Next-Web-LangChain

一键拥有你自己的 ChatGPT 网页服务。 One-Click to deploy your own ChatGPT web UI.(基于 langchain 实现的插件版本 Plugin version implemented based on langchain)
https://n3xt.chat
MIT License
1.13k stars 400 forks source link

[Bug] Azure OpenAI 出现 400 Invalid parameter 错误 #193

Open ding113 opened 6 months ago

ding113 commented 6 months ago

描述错误 当尝试在聊天中使用工具时出现400 Invalid parameter错误,猜测是与Azure对工具调用的API格式变动有关。

复现步骤

  1. 使用gpt-4-1106-preview模型,使用 one-api 程序反代的 Azure OpenAI 端点,Azure API 版本为 2024-02-15-preview2023-12-01-preview 时均能稳定复现
  2. 发送请求到API
  3. 观察到错误400 Invalid parameter: messages with role 'tool' must be a response to a preceding message with 'tool_calls'.

预期行为 根据最新的Azure OpenAI API指南处理新的toolstool_calls参数

部署方式

桌面端(请提供以下信息):

程序版本

附加日志

发送如下请求

{
  "model": "gpt-4",
  "messages": [
    {
      "role": "system",
      "content": "\nYou are ChatGPT, a large language model trained by OpenAI.\nKnowledge cutoff: 2023-04\nCurrent model: gpt-4-1106-preview\nCurrent time: 2/18/2024, 1:22:39 PM\nLatex inline: $x^2$ \nLatex block: $$e=mc^2$$\n\n"
    },
    {
      "role": "user",
      "content": "上网查询一下纽约的天气"
    },
    {
      "role": "assistant",
      "content": ""
    },
    {
      "role": "tool",
      "content": "[{\"title\":\"10-Day Weather Forecast for Manhattan, NY - The Weather Channel ...\",\"link\":\"https://weather.com/weather/tenday/l/Manhattan+NY?canonicalCityId=fc47c333c5d13e34e34c9fdb6e047ceb70f7891e01bc9e1d574b5f93f58aa76d\",\"snippet\":\"Be prepared with the most accurate 10-day forecast for Manhattan, NY with highs, lows, chance of precipitation from The Weather Channel and Weather.com.\"},{\"title\":\"New York City, NY Weather Forecast | AccuWeather\",\"link\":\"https://www.accuweather.com/en/us/new-york/10021/weather-forecast/14-349727_1_al\",\"snippet\":\"The air has reached a high level of pollution and is unhealthy for sensitive groups. Reduce time spent outside if you are feeling symptoms such as difficulty ...\"},{\"title\":\"Weather Forecast and Conditions for New York City, NY - The ...\",\"link\":\"https://weather.com/weather/today/l/96f2f84af9a5f5d452eb0574d4e4d8a840c71b05e22264ebdc0056433a642c84\",\"snippet\":\"Today's and tonight's New York City, NY weather forecast, weather conditions and Doppler radar from The Weather Channel and Weather.com.\"},{\"title\":\"Weather.gov > Buffalo, NY\",\"link\":\"https://www.weather.gov/buf/\",\"snippet\":\"NWS Forecast Office Buffalo, NY · Local KBUF Standard Radar (low bandwidth) · Local KBUF Enhanced Radar · Regional Standard Radar (low bandwidth) · National ...\"},{\"title\":\"Weather Forecast and Conditions for Manhattan, NY - The Weather ...\",\"link\":\"https://weather.com/weather/today/l/Manhattan+NY?canonicalCityId=fc47c333c5d13e34e34c9fdb6e047ceb70f7891e01bc9e1d574b5f93f58aa76d\",\"snippet\":\"Today's and tonight's Manhattan, NY weather forecast, weather conditions and Doppler radar from The Weather Channel and Weather.com.\"},{\"title\":\"511NY| New York Traffic | Commuter Information | Road Conditions\",\"link\":\"http://511ny.org/\",\"snippet\":\"Provides up to the minute traffic and transit information for New York. View the real time traffic map with travel times, traffic accident details, ...\"},{\"title\":\"10-Day Weather Forecast for Manhattan, NY - The Weather Channel ...\",\"link\":\"https://weather.com/weather/tenday/l/New+York+NY+USNY0996:1:US\",\"snippet\":\"Rain and snow showers this evening. Then becoming clear overnight. Low around 35F. Winds WSW at 15 to 25 mph. Chance of precip 30%. Higher wind gusts possible.\"},{\"title\":\"7-Day Forecast 40.71N 74.01W\",\"link\":\"https://forecast.weather.gov/MapClick.php?lat=40.7146&lon=-74.0071\",\"snippet\":\"Local Forecast OfficeMore Local Wx3 Day HistoryMobile WeatherHourly Weather Forecast. Extended Forecast for. New York NY. Tonight. Tonight: Mostly clear, with a ...\"},{\"title\":\"Weather.gov > New York, NY\",\"link\":\"https://www.weather.gov/okx/\",\"snippet\":\"NWS Forecast Office New York, NY · Local KOKX Standard Radar (low bandwidth) · Local KOKX/KJFK/KEWR Enhanced Radars · Regional Standard Radar (low bandwidth) ...\"},{\"title\":\"Winter storm path: Flash flooding, high wind expected to hit NYC, Tri ...\",\"link\":\"https://abc7ny.com/winter-storm-flooding-flood-threat-flash/14291061/\",\"snippet\":\"Get the AccuWeather forecast delivered to your inbox every day. Sign up for our weather newsletter. Email Address*. Yes! I ...\"}]"
    }
  ],
  "stream": true,
  "temperature": 0.5,
  "top_p": 1,
  "n": 1,
  "tools": [
    {
      "function": {
        "description": "a custom search engine. useful for when you need to answer questions about current events. input should be a search query. outputs a JSON array of results.",
        "name": "google_custom_search",
        "parameters": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "additionalProperties": false,
          "properties": {
            "input": {
              "type": "string"
            }
          },
          "type": "object"
        }
      },
      "type": "function"
    },
    {
      "function": {
        "description": "Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.",
        "name": "calculator",
        "parameters": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "additionalProperties": false,
          "properties": {
            "input": {
              "type": "string"
            }
          },
          "type": "object"
        }
      },
      "type": "function"
    },
    {
      "function": {
        "description": "useful for when you need to find something on or summarize a webpage. input should be a comma separated list of \"ONE valid http URL including protocol\",\"what you want to find on the page or empty string for a summary\".",
        "name": "web-browser",
        "parameters": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "additionalProperties": false,
          "properties": {
            "input": {
              "type": "string"
            }
          },
          "type": "object"
        }
      },
      "type": "function"
    },
    {
      "function": {
        "description": "Run Arxiv search and get the article information.",
        "name": "arxiv",
        "parameters": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "additionalProperties": false,
          "properties": {
            "maxResults": {
              "default": 10,
              "description": "the number of results returned by the query.",
              "type": "number"
            },
            "searchQuery": {
              "description": "same as the search_query parameter rules of the arxiv API.",
              "type": "string"
            },
            "sortBy": {
              "description": "can be \"relevance\", \"lastUpdatedDate\", \"submittedDate\".",
              "type": "string"
            },
            "sortOrder": {
              "description": "can be either \"ascending\" or \"descending\".",
              "type": "string"
            },
            "start": {
              "default": 0,
              "description": "the index of the first returned result.",
              "type": "number"
            }
          },
          "required": [
            "searchQuery",
            "sortBy",
            "sortOrder"
          ],
          "type": "object"
        }
      },
      "type": "function"
    },
    {
      "function": {
        "description": "A wrapper around Wolfram Alpha. Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Input should be a search query. If the result contains an image link, use the markdown syntax to return the image.",
        "name": "wolfram_alpha_llm",
        "parameters": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "additionalProperties": false,
          "properties": {
            "input": {
              "type": "string"
            }
          },
          "type": "object"
        }
      },
      "type": "function"
    },
    {
      "function": {
        "description": "useful for when you need to find something on or summarize a pdf file. input should be a comma separated list of \"ONE valid http URL including protocol\",\"what you want to find on the pdf page or empty string for a summary\".",
        "name": "pdf-browser",
        "parameters": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "additionalProperties": false,
          "properties": {
            "input": {
              "type": "string"
            }
          },
          "type": "object"
        }
      },
      "type": "function"
    },
    {
      "function": {
        "description": "A tool for interacting with and fetching data from the Wikipedia API.",
        "name": "wikipedia-api",
        "parameters": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "additionalProperties": false,
          "properties": {
            "input": {
              "type": "string"
            }
          },
          "type": "object"
        }
      },
      "type": "function"
    }
  ]
}

报错如下:

{
  "error": true,
  "message": "400 Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'. (request id: 20240218132251646818282DeKbP94s)"
}

详细信息请见:Azure OpenAI服务中的函数调用

Issues-translate-bot commented 6 months ago

Bot detected the issue body's language is not English, translate it automatically.


Title: [Bug] 400 Invalid parameter error occurs in Azure OpenAI

Hk-Gosuto commented 6 months ago

我使用之前部署的 auzre 模型中确实也复现了这个问题,建议你删除已经部署的模型重新部署个新的试试。 后面复现时我重新在瑞典中部部署了一个 gpt-4-1106-preview 模型,并在 one-api 中指定了 api 版本为 2024-02-15-preview ,插件调用就正常了。 目前主版本的插件调用模式已经从 function_call 切换为了 tool_choice (因为 function_call 参数已经过时),而且 api 参数产生完全由 langchain 实现的,这部分无法进行干预,所以即使 azure 参数导致的问题也完全无法修改(当然你也可以把问题提交给 langchianjs)。 image

Hk-Gosuto commented 6 months ago

我又花了半天时间调查该问题,发现当模型名为 gpt-4gpt-4-0613 时该问题时必现的,但如果把模型名从 gpt-4-0613 改为 gpt-4-0613-azure 时函数调用又正常了。 怀疑是 one-api 针对上面两个 azure 服务模型的函数调用使用了老的 function call 模式导致 tool_call 参数未正确传递产生的该问题。

Issues-translate-bot commented 6 months ago

Bot detected the issue body's language is not English, translate it automatically.


I spent another half day investigating the problem and found that the problem must occur when the model name is gpt-4 and gpt-4-0613, but if the model name is changed from gpt-4-0613 When it is gpt-4-0613-azure, the function call is normal again. It is suspected that one-api uses the old function call mode for the function calls of the above two azure service models, resulting in the tool_call parameter not being passed correctly.

Hk-Gosuto commented 6 months ago

相关问题已经提交到了 one-api 项目,可以关注该 issue:https://github.com/songquanpeng/one-api/issues/1019