我按照 T 教程进行操作,但无法从 API 获得响应。我不确定 OpenAI API 是否有更新。
任何帮助将不胜感激。
/app/(主)/page.tsx
'use client'
import React from 'react';
import Navbar from './_components/navbar';
const HomePage = () => {
return (
<div className="w-full min-h-full">
<Navbar />
<div className="flex flex-col justify-center">
<h1 className="text-4xl">Welcome to Mood Palette</h1>
<button
onClick={async () => {
const response = await fetch('/api/chat-gpt', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: 'Hello, World!',
}),
});
console.log("Response", response);
}}
>
Create Palette
</button>
</div>
</div>
);
}
export default HomePage;
/api/route.ts
import { NextRequest, NextResponse } from "next/server";
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: `Bearer ${process.env.OPENAI_API_KEY}`,
project: 'project_id',
});
export async function POST(request: Request) {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are Jarvis from Iron Manl" },
{ role: "user", content: "Who are you?"}
],
temperature: 0,
max_tokens: 1024,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
});
return NextResponse.json(response);
}
我刚刚按照 Open AI 文档的“入门”和教程进行操作,但在 API 没有响应后无法继续。
根据您的详细信息,您似乎指向了一个不存在的端点:
您的 api 文件应位于:
/app/api/chat-gpt/route.ts