From fb2dca8155a4078dff33cad1fbd10bf8878bb5d8 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Mon, 13 May 2024 21:13:49 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20refactor=20OpenAI=20=5FgetHeader?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/llm/llms/OpenAI.ts | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index 013755534..2b3ae496d 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -94,16 +94,24 @@ class OpenAI extends BaseLLM { this.maxStopWords !== undefined ? options.stop?.slice(0, this.maxStopWords) : url.port === "1337" || - url.host === "api.openai.com" || - url.host === "api.groq.com" || - this.apiType === "azure" - ? options.stop?.slice(0, 4) - : options.stop, + url.host === "api.openai.com" || + url.host === "api.groq.com" || + this.apiType === "azure" + ? options.stop?.slice(0, 4) + : options.stop, }; return finalOptions; } + protected _getHeaders() { + return { + "Content-Type": "application/json", + Authorization: `Bearer ${this.apiKey}`, + "api-key": this.apiKey ?? "", // For Azure + }; + } + protected async _complete( prompt: string, options: CompletionOptions, @@ -160,11 +168,7 @@ class OpenAI extends BaseLLM { const response = await this.fetch(this._getEndpoint("completions"), { method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${this.apiKey}`, - "api-key": this.apiKey ?? "", // For Azure - }, + headers: this._getHeaders(), body: JSON.stringify({ ...args, stream: true, @@ -212,11 +216,7 @@ class OpenAI extends BaseLLM { })) as any; const response = await this.fetch(this._getEndpoint("chat/completions"), { method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${this.apiKey}`, - "api-key": this.apiKey ?? "", // For Azure - }, + headers: this._getHeaders(), body: JSON.stringify(body), }); @@ -230,10 +230,7 @@ class OpenAI extends BaseLLM { async listModels(): Promise { const response = await this.fetch(this._getEndpoint("models"), { method: "GET", - headers: { - Authorization: `Bearer ${this.apiKey}`, - "api-key": this.apiKey ?? "", // For Azure - }, + headers: this._getHeaders(), }); const data = await response.json();