
在使用openai微调模型时,若遇到“the model `xxxxx` does not exist”错误,通常是由于api端点选择不当。本教程将详细阐述如何根据微调模型的基础类型(gpt-3或gpt-3.5 turbo)选择正确的api端点(completions api或chat completions api),并提供相应的代码示例,确保您能成功调用您的微调模型。
当您在OpenAI平台上成功训练并部署了一个微调模型后,尝试通过API调用它时,可能会遇到“The model xxxxx does not exist”的错误提示,即使您已经确认该模型确实存在于您的账户中。这个问题的核心在于,不同基础模型(如GPT-3系列和GPT-3.5 Turbo)的微调模型需要使用不同的API端点进行调用。
OpenAI提供了两种主要的API端点用于文本生成:
在2023年8月22日之前,gpt-3.5-turbo模型尚不支持微调,因此所有微调模型都基于传统的GPT-3系列模型,必须通过Completions API调用。然而,自2023年8月22日起,OpenAI正式开放了gpt-3.5-turbo的微调功能。这意味着:
如果您在调用一个基于GPT-3的微调模型时使用了Chat Completions API,或者反之,就会收到“模型不存在”的错误。
要解决此问题,您需要根据您的微调模型所基于的基础模型类型,选择相应的API端点。
1. 确认微调模型的基础类型
当您的微调任务成功完成后,您可以通过查询微调任务的详情,查看fine_tuned_model字段。这个字段会显示您的微调模型的名称,其命名规则通常会暗示其基础模型。例如,ft-gpt-3.5-turbo-...表示基于gpt-3.5-turbo,而ft-davinci-002-...则表示基于davinci-002。
2. 调用API的正确姿势
对于这类模型,您必须使用Completions API。请求体中应包含prompt字段,而不是messages字段。
const API_KEY = "YOUR_OPENAI_API_KEY";
const ORG_ID = "YOUR_OPENAI_ORG_ID"; // 组织ID可选,但建议提供
const headers = {
"Content-Type": "application/json",
Authorization: "Bearer " + API_KEY,
// "OpenAI-Organization": ORG_ID, // 如果需要,请取消注释
};
const fineTunedModelName = "ft-your-gpt3-model"; // 替换为您的GPT-3微调模型名称
const userPrompt = "Say this is a test"; // 替换为您的输入提示
try {
const res = await axios.post(
"https://api.openai.com/v1/completions", // 注意:这里是 /v1/completions
{
model: fineTunedModelName,
prompt: userPrompt, // 使用 'prompt' 字段
max_tokens: 50, // 根据需要调整
},
{ headers }
);
console.log(res.data.choices[0].text);
} catch (error) {
console.error("API Error:", error.response ? error.response.data : error.message);
}Node.js (使用 OpenAI 官方库)
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const fineTunedModelName = "ft-your-gpt3-model"; // 替换为您的GPT-3微调模型名称
const userPrompt = "Say this is a test"; // 替换为您的输入提示
try {
const response = await openai.createCompletion({
model: fineTunedModelName,
prompt: userPrompt,
max_tokens: 50,
});
console.log(response.data.choices[0].text);
} catch (error) {
console.error("API Error:", error.response ? error.response.data : error.message);
}Python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
fine_tuned_model = "ft-your-gpt3-model" # 替换为您的GPT-3微调模型名称
your_prompt = "Say this is a test" # 替换为您的输入提示
try:
response = openai.Completion.create(
model=fine_tuned_model,
prompt=your_prompt,
max_tokens=50
)
print(response.choices[0].text)
except openai.error.OpenAIError as e:
print(f"API Error: {e}")cURL
curl https://api.openai.com/v1/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "ft-your-gpt3-model",
"prompt": "Say this is a test",
"max_tokens": 50
}'对于这类模型,您应该使用Chat Completions API。请求体中应包含messages数组,以对话格式提供输入。
Node.js (使用 Axios)
const API_KEY = "YOUR_OPENAI_API_KEY";
const ORG_ID = "YOUR_OPENAI_ORG_ID"; // 组织ID可选,但建议提供
const headers = {
"Content-Type": "application/json",
Authorization: "Bearer " + API_KEY,
// "OpenAI-Organization": ORG_ID, // 如果需要,请取消注释
};
const fineTunedModelName = "ft-your-gpt35-turbo-model"; // 替换为您的GPT-3.5 Turbo微调模型名称
const userMessage = "Hello, what is your name?"; // 替换为您的用户消息
try {
const res = await axios.post(
"https://api.openai.com/v1/chat/completions", // 注意:这里是 /v1/chat/completions
{
model: fineTunedModelName,
messages: [ // 使用 'messages' 字段
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: userMessage },
],
max_tokens: 50, // 根据需要调整
},
{ headers }
);
console.log(res.data.choices[0].message.content);
} catch (error) {
console.error("API Error:", error.response ? error.response.data : error.message);
}通过遵循这些指南,您将能够准确无误地调用您的OpenAI微调模型,避免“模型不存在”的常见错误,并充分利用微调模型在特定任务上的优化表现。始终建议查阅OpenAI的官方文档以获取最新和最详细的API使用信息。
以上就是解决OpenAI微调模型“模型不存在”错误的指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号