文章摘要
这篇文章介绍了一段使用JavaScript编写的功能,该功能利用OpenAI的API(gpt-3.5-turbo模型)来生成文本内容。代码中定义了两个函数:`runCompletion`和`loader`。`runCompletion`函数接收输入消息,通过`fetch`方法调用预设的APIURL,发送POST请求,解析JSON响应,并返回生成文本。`loader`函数从URL参数中提取文本内容,并调用`runCompletion`获取结果。文章核心内容围绕API调用和文本生成功能。
async function runCompletion(messages: any) {
const response=await fetch(
“https://api.openai-proxy.com/v1/chat/completions”,
{
method: “POST”,
headers: {
“Content-Type”: “application/json”,
Authorization: “Bearer ” + process.env.OPENAI_API_KEY,
},
body: JSON.stringify({
model: “gpt-3.5-turbo”,
messages: [{ role: “user”, content: messages }],
}),
}
).then((res)=> res.json())
return await response.choices[0].message.content;
}
export async function loader({ request }: any) {
const url=new URL(request.url);
const text=url.searchParams.get(“text”)!;
return runCompletion(text);
}
const response=await fetch(
“https://api.openai-proxy.com/v1/chat/completions”,
{
method: “POST”,
headers: {
“Content-Type”: “application/json”,
Authorization: “Bearer ” + process.env.OPENAI_API_KEY,
},
body: JSON.stringify({
model: “gpt-3.5-turbo”,
messages: [{ role: “user”, content: messages }],
}),
}
).then((res)=> res.json())
return await response.choices[0].message.content;
}
export async function loader({ request }: any) {
const url=new URL(request.url);
const text=url.searchParams.get(“text”)!;
return runCompletion(text);
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。