mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-07-25 16:13:10 +08:00
add optional config for users who belong to multiple organizations pass a header OpenAI-Organization
to specify which organization is used for an API request (#145)
Co-authored-by: lushenle <lushenle@bytedance.com>
This commit is contained in:
4
process.d.ts
vendored
4
process.d.ts
vendored
@ -13,6 +13,10 @@ declare namespace NodeJS {
|
|||||||
NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY: string;
|
NEXT_PUBLIC_ALLOW_SELF_OPENAI_KEY: string;
|
||||||
// Required. Do not share your OpenAI API key with anyone! It should remain a secret.
|
// Required. Do not share your OpenAI API key with anyone! It should remain a secret.
|
||||||
OPENAI_API_KEY: string;
|
OPENAI_API_KEY: string;
|
||||||
|
// Optional.For users who belong to multiple organizations,
|
||||||
|
// you can pass a header to specify which organization is used for an API request.
|
||||||
|
// Usage from these API requests will count as usage for the specified organization.
|
||||||
|
OPENAI_ORGANIZATION: string;
|
||||||
// Optional. OpenAI API endpoint. Defaults to https://api.openai.com.
|
// Optional. OpenAI API endpoint. Defaults to https://api.openai.com.
|
||||||
OPENAI_API_ENDPOINT: string;
|
OPENAI_API_ENDPOINT: string;
|
||||||
// Optional. NextAuth.js URL. Defaults to the current domain.
|
// Optional. NextAuth.js URL. Defaults to the current domain.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
|
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
|
||||||
import { NextRequest } from "next/server";
|
import { NextRequest } from "next/server";
|
||||||
import { openAIApiEndpoint, openAIApiKey, hasFeature, getModel } from "@/utils";
|
import { openAIApiEndpoint, openAIApiKey, openAIOrganization, hasFeature, getModel } from "@/utils";
|
||||||
|
|
||||||
// Needs Edge for streaming response.
|
// Needs Edge for streaming response.
|
||||||
export const config = {
|
export const config = {
|
||||||
@ -93,13 +93,19 @@ const handler = async (req: NextRequest) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let headers: { [key: string]: string } = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (openAIOrganization) {
|
||||||
|
headers["OpenAI-Organization"] = openAIOrganization;
|
||||||
|
}
|
||||||
|
|
||||||
const apiEndpoint = getApiEndpoint(req.headers.get("x-openai-endpoint") || openAIApiEndpoint);
|
const apiEndpoint = getApiEndpoint(req.headers.get("x-openai-endpoint") || openAIApiEndpoint);
|
||||||
const model = getModel(req.headers.get("x-openai-model") || "");
|
const model = getModel(req.headers.get("x-openai-model") || "");
|
||||||
const remoteRes = await fetch(apiEndpoint, {
|
const remoteRes = await fetch(apiEndpoint, {
|
||||||
headers: {
|
headers: headers,
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: `Bearer ${apiKey}`,
|
|
||||||
},
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model: model.name,
|
model: model.name,
|
||||||
|
@ -7,6 +7,9 @@ export const openAIApiKey = process.env.OPENAI_API_KEY;
|
|||||||
// openAIApiEndpoint is the API endpoint for OpenAI API. Defaults to https://api.openai.com.
|
// openAIApiEndpoint is the API endpoint for OpenAI API. Defaults to https://api.openai.com.
|
||||||
export const openAIApiEndpoint = process.env.OPENAI_API_ENDPOINT || "https://api.openai.com";
|
export const openAIApiEndpoint = process.env.OPENAI_API_ENDPOINT || "https://api.openai.com";
|
||||||
|
|
||||||
|
// openAIOrganization a header to specify which organization is used for an API request.
|
||||||
|
export const openAIOrganization = process.env.OPENAI_ORGANIZATION;
|
||||||
|
|
||||||
export const countTextTokens = (text: string) => {
|
export const countTextTokens = (text: string) => {
|
||||||
return encode(text).length;
|
return encode(text).length;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user