-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[ENH] VoyageAI embedding function #1871
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
Closed
Closed
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4bd82f9
VoyageAI Embedding function
fodizoltan f095270
VoyageAI Embedding function, TS
fodizoltan 8e4a553
VoyageAI Embedding function, TS
fodizoltan 1199e70
VoyageAI Embedding function, TS
fodizoltan 7a9abf0
Remove default value
fodizoltan 71b4df8
Export the VoyageAI embedded function
fodizoltan cbebe89
Change Cohere 2 VoyageAI in comments
fodizoltan 8630b7c
Change Cohere 2 VoyageAI in comments
fodizoltan 298493f
Merge pull request #1 from voyage-ai/voyageai_embedding_function
Liuhong99 6f3050e
input_type is None
fodizoltan 2c04b1c
input_type enum
fodizoltan e5320f8
Merge pull request #2 from voyage-ai/voyageai_embedding_function
Liuhong99 048d8d0
a commit 2 ping...
fzowl f712cb5
a commit 2 ping...
fzowl 8c6fc6f
Merge pull request #3 from voyage-ai/voyageai_embedding_function
fzowl cb31e6b
Merge branch 'main' into main
fzowl 3693ccf
Corrections due to the comments
fzowl acab2fa
Merge branch 'main' into voyageai_embedding_function
fzowl a7e5d55
Merge pull request #4 from voyage-ai/voyageai_embedding_function
fzowl 31172dd
Corrections due to the comments
fzowl 5b75e4c
Merge pull request #5 from voyage-ai/voyageai_embedding_function
fzowl cbeb89b
Corrections due to the comments: removing the loop, raising exception
fzowl aef5d92
Merge pull request #6 from voyage-ai/voyageai_embedding_function
fzowl c1fc7e4
Merge branch 'main' into main
fzowl 85c78f5
Corrections due to the comments: removing the loop, raising exception
fzowl f143007
Merge branch 'main' into voyageai_embedding_function
fzowl 008eb0e
Merge pull request #7 from voyage-ai/voyageai_embedding_function
fzowl 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
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,87 @@ | ||
| import { IEmbeddingFunction } from "./IEmbeddingFunction"; | ||
|
|
||
| export enum InputType { | ||
| DOCUMENT = "document", | ||
| QUERY = "query" | ||
| } | ||
|
|
||
| export class VoyageAIEmbeddingFunction implements IEmbeddingFunction { | ||
| private model_name: string; | ||
| private api_url: string; | ||
| private batch_size: number; | ||
| private truncation?: boolean; | ||
| private input_type?: InputType; | ||
| private headers: { [key: string]: string }; | ||
|
|
||
| constructor({ | ||
| voyageai_api_key, | ||
fzowl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| model_name, | ||
| batch_size, | ||
| truncation, | ||
| input_type, | ||
| }: { | ||
| voyageai_api_key: string; | ||
| model_name: string; | ||
| batch_size?: number; | ||
| truncation?: boolean; | ||
| input_type?: InputType; | ||
| }) { | ||
| this.api_url = "https://api.voyageai.com/v1/embeddings"; | ||
| this.headers = { | ||
| Authorization: `Bearer ${voyageai_api_key}`, | ||
| "Content-Type": "application/json", | ||
| }; | ||
|
|
||
| this.model_name = model_name; | ||
| this.truncation = truncation; | ||
| this.input_type = input_type; | ||
| if (batch_size) { | ||
| this.batch_size = batch_size; | ||
| } else { | ||
| if (model_name in ["voyage-2", "voyage-02"]) { | ||
fzowl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.batch_size = 72; | ||
| } else { | ||
| this.batch_size = 7; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public async generate(texts: string[]) { | ||
| try { | ||
| const result: number[][] = []; | ||
| let index = 0; | ||
|
|
||
| while (index < texts.length) { | ||
fzowl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const response = await fetch(this.api_url, { | ||
| method: 'POST', | ||
| headers: this.headers, | ||
| body: JSON.stringify({ | ||
| input: texts.slice(index, index + this.batch_size), | ||
| model: this.model_name, | ||
| truncation: this.truncation, | ||
|
||
| input_type: this.input_type, | ||
| }), | ||
| }); | ||
|
|
||
| const data = (await response.json()) as { data: any[]; detail: string }; | ||
| if (!data || !data.data) { | ||
| throw new Error(data.detail); | ||
| } | ||
|
|
||
| const embeddings: any[] = data.data; | ||
| const sortedEmbeddings = embeddings.sort((a, b) => a.index - b.index); | ||
|
|
||
| const embeddingsChunks = sortedEmbeddings.map((result) => result.embedding); | ||
| result.push(...embeddingsChunks); | ||
| index += this.batch_size; | ||
| } | ||
| return result; | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| throw new Error(`Error calling VoyageAI API: ${error.message}`); | ||
| } else { | ||
| throw new Error(`Error calling VoyageAI API: ${error}`); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.