diff --git a/README.md b/README.md index 8131ca27..e844a040 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,15 @@ In separate terminal windows from project root: On first boot and visiting of the homepage, you will be automatically redirected to create your primary admin account, organization, and database connection. + +### Support for OpenAI Compatible API embedding model. +Use below variables in your server or docker .env settings. +``` +OPENAI_BASE_PATH="https://api.openai.com/v1" +OPENAI_MODEL_NAME="text-embedding-ada-002" +MODEL_DIMENSIONS=1536 +``` + ## Contributing - create issue - create PR with branch name format of `-` diff --git a/backend/utils/openAi/index.js b/backend/utils/openAi/index.js index 9baad98f..7cc73f53 100644 --- a/backend/utils/openAi/index.js +++ b/backend/utils/openAi/index.js @@ -1,16 +1,23 @@ const { Configuration, OpenAIApi } = require("openai"); + class OpenAi { constructor(apiKey = "") { - const config = new Configuration({ apiKey }); + const basePath = process.env.OPENAI_BASE_PATH || "https://api.openai.com/v1"; + const modelName = process.env.OPENAI_MODEL_NAME || "text-embedding-ada-002"; + const config = new Configuration({ + apiKey, + basePath, + }); const openai = new OpenAIApi(config); this.openai = openai; + this.modelName = modelName; } async embedTextChunk(textChunk = "") { const { data: { data }, } = await this.openai.createEmbedding({ - model: "text-embedding-ada-002", + model: this.modelName, input: textChunk, }); return data.length > 0 && data[0].hasOwnProperty("embedding") @@ -22,7 +29,7 @@ class OpenAi { const { data: { data }, } = await this.openai.createEmbedding({ - model: "text-embedding-ada-002", + model: this.modelName, input: chunks, }); return data.length > 0 && diff --git a/docker/.env.example b/docker/.env.example index ff8d11d1..0cb273a9 100644 --- a/docker/.env.example +++ b/docker/.env.example @@ -5,4 +5,9 @@ JWT_SECRET="your-random-string-here" INNGEST_EVENT_KEY="background_workers" INNGEST_SIGNING_KEY="random-string-goes-here" INNGEST_LANDING_PAGE="true" -DATABASE_CONNECTION_STRING="postgresql://vectoradmin:password@host.docker.internal:5433/vdbms" \ No newline at end of file +DATABASE_CONNECTION_STRING="postgresql://vectoradmin:password@host.docker.internal:5433/vdbms" + +## Support for OpenAI Compatible API model support. +# OPENAI_BASE_PATH="https://api.openai.com/v1" +# OPENAI_MODEL_NAME="text-embedding-ada-002" +# MODEL_DIMENSIONS=1536 \ No newline at end of file diff --git a/workers/.env.example b/workers/.env.example index 06406f44..bc242115 100644 --- a/workers/.env.example +++ b/workers/.env.example @@ -1,3 +1,9 @@ INNGEST_EVENT_KEY="background_workers" INNGEST_SIGNING_KEY="" -# DISABLE_TELEMETRY="true" # Uncomment to disable telemetry on workers for any ENV. \ No newline at end of file +# DISABLE_TELEMETRY="true" # Uncomment to disable telemetry on workers for any ENV. + +## Support for OpenAI Compatible API model support. + +# OPENAI_BASE_PATH="https://api.openai.com/v1" +# OPENAI_MODEL_NAME="text-embedding-ada-002" +# MODEL_DIMENSIONS=1536 diff --git a/workers/functions/newWorkspace/index.js b/workers/functions/newWorkspace/index.js index 94fcb392..2e858e62 100644 --- a/workers/functions/newWorkspace/index.js +++ b/workers/functions/newWorkspace/index.js @@ -64,7 +64,7 @@ const newWorkspaceCreated = InngestClient.createFunction( workspace.fname, { vectors: { - size: 1536, // TODO: Fixed to OpenAI models - when other embeddings exist make variable. + size: parseInt(process.env.MODEL_DIMENSIONS) || 1536, // TODO: Fixed to OpenAI models - when other embeddings exist make variable. distance: 'Cosine', }, }