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

切换至GPT-3.5 Turbo后遇Invalid URL (POST /v1/chat/completions)错误求助

问题描述

我有一个基于Python的TTS程序,可处理语音转文本数据,之前调用GPT davinci-003 API交互能正常返回结果,但切换至GPT-3.5 Turbo后,程序报错:Invalid URL (POST /v1/chat/completions)。我已查阅模型端点兼容性页面、咨询过GPT-3/GPT-4,还在Reddit、Stack Overflow上搜索解决方案,均未得到有效结果。

尝试的API端点URL

使用的API端点为 https://api.openai.com/v1/chat/completions

当前GPT-3.5 Turbo代码

import openai

openai.api_key = "你的API密钥"

def get_gpt_response(text):
    response = openai.Completion.create(
        model="gpt-3.5-turbo",
        prompt=text,
        max_tokens=100
    )
    return response.choices[0].text.strip()
解决方案

问题核心是GPT-3.5 Turbo属于聊天模型,不能使用针对文本补全模型(如davinci-003)的openai.Completion.create()接口,必须改用聊天专用的openai.ChatCompletion.create()接口,同时请求参数结构也需调整。

修改后的正确代码示例:

import openai

openai.api_key = "你的API密钥"

def get_gpt_response(text):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "user", "content": text}
        ],
        max_tokens=100
    )
    return response.choices[0].message["content"].strip()

关键修改点

  • 接口从openai.Completion.create()替换为openai.ChatCompletion.create()
  • 请求参数移除prompt,改用messages数组,每个元素需包含role(可选值:user/assistant/system)和content字段
  • 响应解析从response.choices[0].text调整为response.choices[0].message["content"]

注:你使用的API端点https://api.openai.com/v1/chat/completions是正确的,报错根源是接口调用方式错误,而非URL问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 09:12:38