Not accepting post request
Unanswered
Bonga shad posted this in #help-forum
Bonga shadOP
import { GoogleGenerativeAI } from '@google/generative-ai';
import { GoogleGenerativeAIStream, Message, StreamingTextResponse } from 'ai';
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
export const runtime = 'edge';
const buildGoogleGenAIPrompt = (messages: Message[]) => ({
contents: messages
.filter(message => message.role === 'user' || message.role === 'assistant')
.map(message => ({
role: message.role === 'user' ? 'user' : 'model',
parts: [{ text: message.content }],
})),
});
export async function POST(req: Request,res:Response) {
try {
const { messages } = await req.json();
console.log("Received messages:", messages);
// const genAI = new GoogleGenerativeAI(apiKey);
const geminiStream = await genAI
.getGenerativeModel({ model: 'gemini-pro' })
.generateContentStream(buildGoogleGenAIPrompt(messages));
console.log("Generated Gemini stream:", geminiStream);
// Convert the response into a friendly text-stream
const stream = GoogleGenerativeAIStream(geminiStream);
console.log("Converted stream:", stream);
return new StreamingTextResponse(stream);
} catch (error) {
console.error("Internal server error:", error);
return new Response("Internal server error", { status: 500 });
}
}
this is my post request function , and i am getting the following error Internal server error: Error: [403 Forbidden] Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API. So to remove this error i have to pass the API key assigned to a query parameter named key. how to do that ,
import { GoogleGenerativeAIStream, Message, StreamingTextResponse } from 'ai';
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
export const runtime = 'edge';
const buildGoogleGenAIPrompt = (messages: Message[]) => ({
contents: messages
.filter(message => message.role === 'user' || message.role === 'assistant')
.map(message => ({
role: message.role === 'user' ? 'user' : 'model',
parts: [{ text: message.content }],
})),
});
export async function POST(req: Request,res:Response) {
try {
const { messages } = await req.json();
console.log("Received messages:", messages);
// const genAI = new GoogleGenerativeAI(apiKey);
const geminiStream = await genAI
.getGenerativeModel({ model: 'gemini-pro' })
.generateContentStream(buildGoogleGenAIPrompt(messages));
console.log("Generated Gemini stream:", geminiStream);
// Convert the response into a friendly text-stream
const stream = GoogleGenerativeAIStream(geminiStream);
console.log("Converted stream:", stream);
return new StreamingTextResponse(stream);
} catch (error) {
console.error("Internal server error:", error);
return new Response("Internal server error", { status: 500 });
}
}
this is my post request function , and i am getting the following error Internal server error: Error: [403 Forbidden] Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API. So to remove this error i have to pass the API key assigned to a query parameter named key. how to do that ,