use*_*158 6 javascript cors firebase google-cloud-functions openai-api
下面是一些使用 ChatGPT API 的简单 Firebase 云函数的代码。部署此代码并从我的应用程序访问它会导致 CORS 错误。
import * as functions from "firebase-functions";
import {defineString} from "firebase-functions/v2/params";
import {Configuration, OpenAIApi} from "openai";
const openAIKey = defineString("OPEN_API_KEY");
export const getSummary = functions.https.onCall(async (data) => {
const configuration = new Configuration({
apiKey: openAIKey.value(),
});
const openai = new OpenAIApi(configuration);
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: data.prompt,
},
],
});
const [choice] = completion.data.choices;
return {
response: choice.message ?? "no response",
};
});
Run Code Online (Sandbox Code Playgroud)
当我使用函数模拟器从我的应用程序访问该云函数时,它可以完美运行。仅当我将其部署到云并尝试使用它时,才会出现 CORS 错误。
另外,我helloWorld在这个函数旁边部署了一个函数,这样我就可以检查我的整个函数设置是否没有问题,并且它也工作得很好。此外,当我进入云函数控制台并直接测试该函数时,它也可以工作。因此,这个问题显然与专门通过云函数生产环境和应用程序访问 API 有关。
更新:这是客户端代码和确切的错误:
const getSummary = httpsCallable(functions, "getSummary");
async function askGPT() {
const result = await getSummary({
prompt: "Please summarize the question in the following text. Phrase your response in the form of a question, and use Markdown for any formatting you might need.\n\n" + question.text
});
question.question_summary = (
(question.question_summary ?? "") // @ts-ignore
+ (result?.data?.response?.content || "").trim()
);
}
Run Code Online (Sandbox Code Playgroud)
错误:
从源“http://localhost:5173”获取“https://us-central1-my-documentation.cloudfunctions.net/getSummary”的访问已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明响应满足您的需求,请将请求模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。
onCall解决此问题的最佳方法是在出现 CORS 问题时避免使用。反而:
总之,来自客户端的新请求会导致这种级联:
需要这种替代方法的原因是onCall云函数与 Firestore 事件触发的云函数具有不同的 CORS 行为。
| 归档时间: |
|
| 查看次数: |
1885 次 |
| 最近记录: |