Allow specifying OpenAI-Organization header: (#313)

See https://platform.openai.com/docs/api-reference/introduction

> 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 against the specified organization's
> subscription quota.
This commit is contained in:
Burke Libbey 2023-03-30 15:30:34 -04:00 committed by GitHub
parent 3631a5ac75
commit ef8c1b2c33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 1 deletions

View File

@ -94,7 +94,9 @@ Create a .env.local file in the root of the repo with your OpenAI API Key:
OPENAI_API_KEY=YOUR_KEY OPENAI_API_KEY=YOUR_KEY
``` ```
> you can set `OPENAI_API_HOST` where access to the official OpenAI host is restricted or unavailable, allowing users to configure an alternative host for their specific needs. > You can set `OPENAI_API_HOST` where access to the official OpenAI host is restricted or unavailable, allowing users to configure an alternative host for their specific needs.
> Additionally, if you have multiple OpenAI Organizations, you can set `OPENAI_ORGANIZATION` to specify one.
**4. Run App** **4. Run App**

View File

@ -15,6 +15,9 @@ const handler = async (req: Request): Promise<Response> => {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`, Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`,
...(process.env.OPENAI_ORGANIZATION && {
'OpenAI-Organization': process.env.OPENAI_ORGANIZATION,
})
}, },
}); });

View File

@ -1,4 +1,5 @@
export interface ProcessEnv { export interface ProcessEnv {
OPENAI_API_KEY: string; OPENAI_API_KEY: string;
OPENAI_API_HOST?: string; OPENAI_API_HOST?: string;
OPENAI_ORGANIZATION?: string;
} }

View File

@ -17,6 +17,9 @@ export const OpenAIStream = async (
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`, Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`,
...(process.env.OPENAI_ORGANIZATION && {
'OpenAI-Organization': process.env.OPENAI_ORGANIZATION,
})
}, },
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({