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
相关产品推荐
相关产品推荐

