-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
[FEAT] Generic OpenAI embedding provider #1664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timothycarambat
merged 6 commits into
master
from
1436-feat-generic-openai-api-for-embeddings
Jun 21, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bc80e2d
implement generic openai embedding provider
shatfield4 c18cee4
linting
shatfield4 3c90dbf
Merge branch 'master' into 1436-feat-generic-openai-api-for-embeddings
shatfield4 181a34b
comment & description update for generic openai embedding provider
shatfield4 1acace5
Merge branch 'master' into 1436-feat-generic-openai-api-for-embeddings
shatfield4 54e084c
fix privacy for generic
timothycarambat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
frontend/src/components/EmbeddingSelection/GenericOpenAiOptions/index.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| export default function GenericOpenAiEmbeddingOptions({ settings }) { | ||
| return ( | ||
| <div className="w-full flex flex-col gap-y-4"> | ||
| <div className="w-full flex items-center gap-4 flex-wrap"> | ||
| <div className="flex flex-col w-60"> | ||
| <label className="text-white text-sm font-semibold block mb-4"> | ||
| Base URL | ||
| </label> | ||
| <input | ||
| type="url" | ||
| name="EmbeddingBasePath" | ||
| className="bg-zinc-900 text-white placeholder:text-white/20 text-sm rounded-lg focus:border-white block w-full p-2.5" | ||
| placeholder="https://api.openai.com/v1" | ||
| defaultValue={settings?.EmbeddingBasePath} | ||
| required={true} | ||
| autoComplete="off" | ||
| spellCheck={false} | ||
| /> | ||
| </div> | ||
| <div className="flex flex-col w-60"> | ||
| <label className="text-white text-sm font-semibold block mb-4"> | ||
| Embedding Model | ||
| </label> | ||
| <input | ||
| type="text" | ||
| name="EmbeddingModelPref" | ||
| className="bg-zinc-900 text-white placeholder:text-white/20 text-sm rounded-lg focus:border-white block w-full p-2.5" | ||
| placeholder="text-embedding-ada-002" | ||
| defaultValue={settings?.EmbeddingModelPref} | ||
| required={true} | ||
| autoComplete="off" | ||
| spellCheck={false} | ||
| /> | ||
| </div> | ||
| <div className="flex flex-col w-60"> | ||
| <label className="text-white text-sm font-semibold block mb-4"> | ||
| Max embedding chunk length | ||
| </label> | ||
| <input | ||
| type="number" | ||
| name="EmbeddingModelMaxChunkLength" | ||
| className="bg-zinc-900 text-white placeholder-white/20 text-sm rounded-lg focus:border-white block w-full p-2.5" | ||
| placeholder="8192" | ||
| min={1} | ||
| onScroll={(e) => e.target.blur()} | ||
| defaultValue={settings?.EmbeddingModelMaxChunkLength} | ||
| required={false} | ||
| autoComplete="off" | ||
| /> | ||
| </div> | ||
| </div> | ||
| <div className="w-full flex items-center gap-4"> | ||
| <div className="flex flex-col w-60"> | ||
| <div className="flex flex-col gap-y-1 mb-4"> | ||
| <label className="text-white text-sm font-semibold flex items-center gap-x-2"> | ||
| API Key <p className="!text-xs !italic !font-thin">optional</p> | ||
| </label> | ||
| </div> | ||
| <input | ||
| type="password" | ||
| name="GenericOpenAiEmbeddingApiKey" | ||
| className="bg-zinc-900 text-white placeholder:text-white/20 text-sm rounded-lg focus:border-white block w-full p-2.5" | ||
| placeholder="sk-mysecretkey" | ||
| defaultValue={ | ||
| settings?.GenericOpenAiEmbeddingApiKey ? "*".repeat(20) : "" | ||
| } | ||
| autoComplete="off" | ||
| spellCheck={false} | ||
| /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| const { toChunks } = require("../../helpers"); | ||
|
|
||
| class GenericOpenAiEmbedder { | ||
| constructor() { | ||
| if (!process.env.EMBEDDING_BASE_PATH) | ||
| throw new Error( | ||
| "GenericOpenAI must have a valid base path to use for the api." | ||
| ); | ||
| const { OpenAI: OpenAIApi } = require("openai"); | ||
| this.basePath = process.env.EMBEDDING_BASE_PATH; | ||
| this.openai = new OpenAIApi({ | ||
| baseURL: this.basePath, | ||
| apiKey: process.env.GENERIC_OPEN_AI_EMBEDDING_API_KEY ?? null, | ||
| }); | ||
| this.model = process.env.EMBEDDING_MODEL_PREF ?? null; | ||
|
|
||
| // Limit of how many strings we can process in a single pass to stay with resource or network limits | ||
| this.maxConcurrentChunks = 500; | ||
|
|
||
| // Refer to your specific model and provider you use this class with to determine a valid maxChunkLength | ||
| this.embeddingMaxChunkLength = 8_191; | ||
| } | ||
|
|
||
| async embedTextInput(textInput) { | ||
| const result = await this.embedChunks( | ||
| Array.isArray(textInput) ? textInput : [textInput] | ||
| ); | ||
| return result?.[0] || []; | ||
| } | ||
|
|
||
| async embedChunks(textChunks = []) { | ||
| // Because there is a hard POST limit on how many chunks can be sent at once to OpenAI (~8mb) | ||
| // we concurrently execute each max batch of text chunks possible. | ||
| // Refer to constructor maxConcurrentChunks for more info. | ||
| const embeddingRequests = []; | ||
| for (const chunk of toChunks(textChunks, this.maxConcurrentChunks)) { | ||
| embeddingRequests.push( | ||
| new Promise((resolve) => { | ||
| this.openai.embeddings | ||
| .create({ | ||
| model: this.model, | ||
| input: chunk, | ||
| }) | ||
| .then((result) => { | ||
| resolve({ data: result?.data, error: null }); | ||
| }) | ||
| .catch((e) => { | ||
| e.type = | ||
| e?.response?.data?.error?.code || | ||
| e?.response?.status || | ||
| "failed_to_embed"; | ||
| e.message = e?.response?.data?.error?.message || e.message; | ||
| resolve({ data: [], error: e }); | ||
| }); | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| const { data = [], error = null } = await Promise.all( | ||
| embeddingRequests | ||
| ).then((results) => { | ||
| // If any errors were returned from OpenAI abort the entire sequence because the embeddings | ||
| // will be incomplete. | ||
| const errors = results | ||
| .filter((res) => !!res.error) | ||
| .map((res) => res.error) | ||
| .flat(); | ||
| if (errors.length > 0) { | ||
| let uniqueErrors = new Set(); | ||
| errors.map((error) => | ||
| uniqueErrors.add(`[${error.type}]: ${error.message}`) | ||
| ); | ||
|
|
||
| return { | ||
| data: [], | ||
| error: Array.from(uniqueErrors).join(", "), | ||
| }; | ||
| } | ||
| return { | ||
| data: results.map((res) => res?.data || []).flat(), | ||
| error: null, | ||
| }; | ||
| }); | ||
|
|
||
| if (!!error) throw new Error(`GenericOpenAI Failed to embed: ${error}`); | ||
| return data.length > 0 && | ||
| data.every((embd) => embd.hasOwnProperty("embedding")) | ||
| ? data.map((embd) => embd.embedding) | ||
| : null; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| GenericOpenAiEmbedder, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.