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

OpenAI GPT-3 API TypeScript报错求助:'text'与'prompt'属性不存在

OpenAI createChatCompletion TypeScript错误解决方法

错误原因及修复方案


1. "prompt"不存在于CreateChatCompletionRequest类型中

createChatCompletion是针对聊天模型的接口,和旧文本补全接口createCompletion参数结构不同:

  • 不能直接传prompt参数,必须用messages数组定义对话上下文,数组内每个元素是包含role(可选值:user/assistant/system)和content的对象。
  • 你的场景下,把传入的prompt包装成用户消息即可。

2. Property 'text' does not exist on type 'CreateChatCompletionResponseChoicesInner'

聊天接口的返回结构和文本补全接口不同:

  • 结果内容不在choices[0].text,而是在choices[0].message.content。

修正后的完整代码

import openai from "./zggpt";

const query = async (prompt:string,  chatId:string, model:string) => {
    const res= await openai
    .createChatCompletion({
        model,
        // 替换prompt为messages数组
        messages: [
            { role: "user", content: prompt }
        ],
        temperature: 0.9,
        top_p: 1,
        max_tokens: 1000,
        frequency_penalty: 0,
        presence_penalty: 0,
    })
    // 替换text为message.content
    .then((res) => res.data.choices[0].message.content)
    .catch((err)=>
    `ZG was unable to find an answer for that!
     (Error: ${err.message})`
     );
     return res;
};

export default query;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 21:23:41