From 1730922c048d3057971513768712e20b9da23f21 Mon Sep 17 00:00:00 2001 From: mic Date: Tue, 1 Oct 2024 20:45:15 +0200 Subject: [PATCH] added an optional api key. see #144 --- js/api/openai_comp.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/js/api/openai_comp.js b/js/api/openai_comp.js index d37d2341..0b5fefe2 100644 --- a/js/api/openai_comp.js +++ b/js/api/openai_comp.js @@ -23,21 +23,26 @@ export class OpenAIComp { host = ''; model = ''; + apiKey = ''; stream = false; - constructor(host, model, stream = false) { + constructor(host, model, apiKey = '', stream = false) { this.host = host.trim().replace(/\/+$/, ""); this.model = model; this.stream = stream; + this.apiKey = apiKey; } fetchModels = async () => { + const curr_headers = { + "Content-Type": "application/json", + }; + if(this.apiKey !== '') curr_headers["Authorization"] = "Bearer "+ this.apiKey; + const response = await fetch(this.host + "/v1/models", { method: "GET", - headers: { - "Content-Type": "application/json", - }, + headers: curr_headers, }); if (!response.ok) { @@ -59,12 +64,15 @@ export class OpenAIComp { } fetchResponse = async (messages, maxTokens = 0) => { + const curr_headers = { + "Content-Type": "application/json", + }; + if(this.apiKey !== '') curr_headers["Authorization"] = "Bearer "+ this.apiKey; + try { const response = await fetch(this.host + "/v1/chat/completions", { method: "POST", - headers: { - "Content-Type": "application/json", - }, + headers: curr_headers, body: JSON.stringify({ model: this.model, messages: messages,