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

代码从text-davinci-003升级到gpt-3.5-turbo遇问题求助

解决gpt-3.5-turbo替换text-davinci-003后程序崩溃的问题

gpt-3.5-turbo属于聊天类模型,和text-davinci-003的API调用逻辑存在本质差异,仅修改model参数必然导致请求失败,需从以下三个核心点调整:

1. 更换API端点

text-davinci-003使用/v1/completions端点,而gpt-3.5-turbo需要调用/v1/chat/completions端点。如果使用官方SDK,对应方法从openai.Completion.create()改为openai.ChatCompletion.create()。

2. 重构请求参数结构

原prompt参数需替换为messages数组,数组内每个对象包含role(可选值:user/assistant/system)和content(消息内容)两个字段。示例对比:

原text-davinci-003请求代码

import openai

openai.api_key = "YOUR_API_KEY"

response = openai.Completion.create(
    model="text-davinci-003",
    prompt="请介绍一下Python",
    max_tokens=200
)

修改后gpt-3.5-turbo请求代码

import openai

openai.api_key = "YOUR_API_KEY"

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "user", "content": "请介绍一下Python"}
    ],
    max_tokens=200
)

3. 调整响应解析逻辑

text-davinci-003的响应文本在response.choices[0].text,而gpt-3.5-turbo的响应文本在response.choices[0].message.content。解析代码需对应修改:

原解析代码

print(response.choices[0].text.strip())

修改后解析代码

print(response.choices[0].message.content.strip())

额外注意事项

  • 若需实现多轮对话,需将历史对话消息按顺序存入messages数组,例如:
    messages = [
        {"role": "user", "content": "请介绍一下Python"},
        {"role": "assistant", "content": "Python是一种高级编程语言..."},
        {"role": "user", "content": "它的主要应用场景有哪些?"}
    ]
    
  • 部分参数(如temperature、max_tokens)的作用与text-davinci-003一致,可直接保留。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 19:13:07