From 5f29de95da2dc41656ce596e3aa918991a6cc8d1 Mon Sep 17 00:00:00 2001 From: timothycarambat Date: Sun, 31 Dec 2023 13:42:07 -0800 Subject: [PATCH] normalize header requests Chroma --- .../vectordatabases/providers/chroma/index.js | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/backend/utils/vectordatabases/providers/chroma/index.js b/backend/utils/vectordatabases/providers/chroma/index.js index f0db1626..11f2ad2b 100644 --- a/backend/utils/vectordatabases/providers/chroma/index.js +++ b/backend/utils/vectordatabases/providers/chroma/index.js @@ -12,6 +12,34 @@ class Chroma { this.config = this.setConfig(connector); } + // For use with ChromaClient SDK only. + #appendClientAuthHeaders() { + const { settings } = this.config + if (!settings?.authToken) return {}; + + const headerName = settings.authTokenHeader || "X-Api-Key"; + const authToken = headerName === 'Authorization' ? `Bearer ${settings.authToken}` : settings.authToken + return { + fetchOptions: { + headers: { + [headerName]: authToken, + }, + }, + } + } + + // For use with fetch API endpoints only. + #appendRawAuthHeaders() { + const { settings } = this.config + if (!settings?.authToken) return {}; + + const headerName = settings.authTokenHeader || "X-Api-Key"; + const authToken = headerName === 'Authorization' ? `Bearer ${settings.authToken}` : settings.authToken + return { + [headerName]: authToken, + } + } + setConfig(config) { var { type, settings } = config; if (typeof settings === "string") settings = JSON.parse(settings); @@ -34,15 +62,7 @@ class Chroma { const client = new ChromaClient({ path: settings.instanceURL, - ...(settings?.authToken - ? { - fetchOptions: { - headers: { - [settings.authTokenHeader || "X-Api-Key"]: settings.authToken, - }, - }, - } - : {}), + ...this.#appendClientAuthHeaders(), }); const isAlive = await client.heartbeat(); @@ -114,23 +134,14 @@ class Chroma { } async rawGet(collectionId, pageSize = 10, offset = 0) { - const { settings } = this.config; return await fetch( `${this.config.settings.instanceURL}/api/v1/collections/${collectionId}/get`, { method: "POST", headers: { - accept: "application/json", "Content-Type": "application/json", - ...(settings?.authToken - ? { - [settings.authTokenHeader || "X-Api-Key"]: - settings.authTokenHeader === 'Authorization' - ? `Bearer ${settings.authToken}` : - settings.authToken, - } - : {}), + ...this.#appendRawAuthHeaders(), }, body: JSON.stringify({ limit: pageSize,