Unable to solve CORS error, how to fix this error?
Unanswered
Golden-crowned Warbler posted this in #help-forum
Golden-crowned WarblerOP
I am building an extension, which makes request to "localhost:3000/api/chat", but it is giving cors error. While testing from postman it is working fine.
import { HfInference } from '@huggingface/inference';
import { HuggingFaceStream, StreamingTextResponse } from 'ai';
import { experimental_buildOpenAssistantPrompt } from 'ai/prompts';
// Create a new HuggingFace Inference instance
const Hf = new HfInference(process.env.HUGGINGFACE_API_KEY);
// IMPORTANT! Set the runtime to edge
export const runtime = 'edge';
export async function POST(req: Request) {
console.log("-------req-------------------");
console.log(req);
// Extract the
const { messages } = await req.json();
console.log("-------messages-------------------");
console.log(messages);
const response = Hf.textGenerationStream({
model: 'OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5',
inputs: experimental_buildOpenAssistantPrompt(messages),
parameters: {
max_new_tokens: 500,
// @ts-ignore (this is a valid parameter specifically in OpenAssistant models)
typical_p: 0.2,
repetition_penalty: 1,
truncate: 1000,
return_full_text: false,
},
});
console.log("-------response-------------------");
console.log(response);
// Convert the response into a friendly text-stream
const stream = HuggingFaceStream(response);
// console.log("-------stream-------------------");
// console.log(stream);
// Enable CORS
const headers = new Headers({
'Access-Control-Allow-Origin': '', // Replace with your Chrome extension URL
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
'Access-Control-Allow-Headers': '',
'Access-Control-Max-Age': '86400',
'Referrer-Policy': 'no-referrer', // Setting the Referrer-Policy
});
// Respond with the stream and CORS headers
return new StreamingTextResponse(stream, { headers });
}
import { HfInference } from '@huggingface/inference';
import { HuggingFaceStream, StreamingTextResponse } from 'ai';
import { experimental_buildOpenAssistantPrompt } from 'ai/prompts';
// Create a new HuggingFace Inference instance
const Hf = new HfInference(process.env.HUGGINGFACE_API_KEY);
// IMPORTANT! Set the runtime to edge
export const runtime = 'edge';
export async function POST(req: Request) {
console.log("-------req-------------------");
console.log(req);
// Extract the
messages from the body of the requestconst { messages } = await req.json();
console.log("-------messages-------------------");
console.log(messages);
const response = Hf.textGenerationStream({
model: 'OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5',
inputs: experimental_buildOpenAssistantPrompt(messages),
parameters: {
max_new_tokens: 500,
// @ts-ignore (this is a valid parameter specifically in OpenAssistant models)
typical_p: 0.2,
repetition_penalty: 1,
truncate: 1000,
return_full_text: false,
},
});
console.log("-------response-------------------");
console.log(response);
// Convert the response into a friendly text-stream
const stream = HuggingFaceStream(response);
// console.log("-------stream-------------------");
// console.log(stream);
// Enable CORS
const headers = new Headers({
'Access-Control-Allow-Origin': '', // Replace with your Chrome extension URL
'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
'Access-Control-Allow-Headers': '',
'Access-Control-Max-Age': '86400',
'Referrer-Policy': 'no-referrer', // Setting the Referrer-Policy
});
// Respond with the stream and CORS headers
return new StreamingTextResponse(stream, { headers });
}