I have a Nuxt 3 chatbot app that works locally. However, when I deploy to Netlify using the netlify-edge
preset, I get a 404 error when calling the server/api/chat.post.js
endpoint. It seems that although the endpoint is reachable, there was something wrong with the Nitro server (404 is very vague error it seems).
Again, the app works locally with no problems. It's just that when deployed to Netlify, the Nitro server for the chat api fails.
Checking the Edge Function log on Netlify, I see the following message:
[nitro server handler] (e.adapter || ls.adapter) is not a function
Anyone know what that means?
Here's my code:
server/api/chat.post.js
:
import { getChatStream } from '../utils/ai'
export default defineEventHandler(async (event) => {
try {
const { messages } = await readBody(event)
const stream = await getChatStream({ messages })
return stream
} catch (error) {
console.error('error on server', error)
}
})
/utils/ai
:
import { Configuration, OpenAIApi } from 'openai'
const config = useRuntimeConfig()
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY || config.OPENAI_API_KEY
})
const openai = new OpenAIApi(configuration)
const systemPrompts = [..removed..]
export const getChatStream = async ({ messages }) => {
try {
const response = await openai.createChatCompletion(
{
model: 'gpt-3.5-turbo',
messages: [
...systemPrompts, ...messages
],
temperature: 0.5,
stream: true // Not supported in OpenAI Node SDK yet
},
{
timeout: 15000,
responseType: "stream" // see above
}
);
return response.data;
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
}
};