From ef8c1b2c33aeeee34528c031f3ea2e14f599b535 Mon Sep 17 00:00:00 2001 From: Burke Libbey Date: Thu, 30 Mar 2023 15:30:34 -0400 Subject: [PATCH] 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. --- README.md | 4 +++- pages/api/models.ts | 3 +++ types/env.ts | 1 + utils/server/index.ts | 3 +++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 031420b..b391d51 100644 --- a/README.md +++ b/README.md @@ -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 ``` -> 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** diff --git a/pages/api/models.ts b/pages/api/models.ts index d45c5b7..310a8e2 100644 --- a/pages/api/models.ts +++ b/pages/api/models.ts @@ -15,6 +15,9 @@ const handler = async (req: Request): Promise => { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`, + ...(process.env.OPENAI_ORGANIZATION && { + 'OpenAI-Organization': process.env.OPENAI_ORGANIZATION, + }) }, }); diff --git a/types/env.ts b/types/env.ts index 4d709be..b6e4a54 100644 --- a/types/env.ts +++ b/types/env.ts @@ -1,4 +1,5 @@ export interface ProcessEnv { OPENAI_API_KEY: string; OPENAI_API_HOST?: string; + OPENAI_ORGANIZATION?: string; } diff --git a/utils/server/index.ts b/utils/server/index.ts index 4bc5f30..9366af8 100644 --- a/utils/server/index.ts +++ b/utils/server/index.ts @@ -17,6 +17,9 @@ export const OpenAIStream = async ( headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key ? key : process.env.OPENAI_API_KEY}`, + ...(process.env.OPENAI_ORGANIZATION && { + 'OpenAI-Organization': process.env.OPENAI_ORGANIZATION, + }) }, method: 'POST', body: JSON.stringify({