I’m walking through this guide, but I get error:
error - TypeError: resolver is not a function
in the following code:
import { openai } from '@ai-sdk/openai';
import { StreamingTextResponse, streamText } from 'ai';
export async function POST(req: Request) {
try {
const { messages } = await req.json();
const result = await streamText({
model: openai('gpt-4-turbo'),
messages,
});
return new StreamingTextResponse(result.toAIStream());
} catch (error) {
console.log(error);
}
}
Front end code:
'use client';
import React from 'react';
import { useChat } from 'ai/react';
export default function Chatbot() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div>
{messages.map((m) => (
<div key={m.id} className="whitespace-pre-wrap">
{m.role === 'user' ? 'User: ' : 'AI: '}
{m.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input
value={input}
placeholder="Say something..."
onChange={handleInputChange}
/>
</form>
</div>
);
}
I’ve tried adding default
after export (export default async function...
) and then I got this error which I can’t fix:
TypeError: req.json is not a function