-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
langchain[minor]: feat(embedding integration): Gradient AI #3475
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b6e96dc
initial gradient embeddings implementation
chasemcdo c0951d5
format
chasemcdo 78ce39b
remove modelslug
chasemcdo 105ecbc
update package and entrypoint -> yarn build
chasemcdo 6ab4ea6
map texts and change response reading
chasemcdo 5b3b005
add example
chasemcdo aa9c357
add `caller.call` wrapper
chasemcdo 31b2574
add docs
chasemcdo 795b0d8
remove run form example
chasemcdo e43d29e
Merge branch 'main' into gradient-embeddings
bracesproul f469ec0
Update gradient_ai.mdx
jacoblee93 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 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
39 changes: 39 additions & 0 deletions
39
docs/core_docs/docs/integrations/text_embedding/gradient_ai.mdx
This file contains 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,39 @@ | ||
--- | ||
sidebar_class_name: node-only | ||
--- | ||
|
||
# Gradient AI | ||
|
||
The `GradientEmbeddings` class uses the Gradient AI API to generate embeddings for a given text. | ||
|
||
## Setup | ||
|
||
You'll need to install the official Gradient Node SDK as a peer dependency: | ||
|
||
```bash npm2yarn | ||
npm i @gradientai/nodejs-sdk | ||
``` | ||
|
||
You will need to set the following environment variables for using the Gradient AI API. | ||
|
||
``` | ||
export GRADIENT_ACCESS_TOKEN=<YOUR_ACCESS_TOKEN> | ||
export GRADIENT_WORKSPACE_ID=<YOUR_WORKSPACE_ID> | ||
``` | ||
|
||
Alternatively, these can be set during the GradientAI Class instantiation as `gradientAccessKey` and `workspaceId` respectively. | ||
For example: | ||
|
||
```typescript | ||
const model = new GradientEmbeddings({ | ||
gradientAccessKey: "My secret Access Token" | ||
workspaceId: "My secret workspace id" | ||
}); | ||
``` | ||
|
||
## Usage | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import GradientEmbeddingsExample from "@examples/embeddings/gradient_ai.ts"; | ||
|
||
<CodeBlock language="typescript">{GradientEmbeddingsExample}</CodeBlock> |
This file contains 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,7 @@ | ||
import { GradientEmbeddings } from "langchain/embeddings/gradient_ai"; | ||
|
||
const model = new GradientEmbeddings(); | ||
const res = await model.embedQuery( | ||
"What would be a good company name a company that makes colorful socks?" | ||
); | ||
console.log({ res }); |
This file contains 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 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 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 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,117 @@ | ||
import { Gradient } from "@gradientai/nodejs-sdk"; | ||
import { getEnvironmentVariable } from "../util/env.js"; | ||
import { chunkArray } from "../util/chunk.js"; | ||
import { Embeddings, EmbeddingsParams } from "./base.js"; | ||
|
||
/** | ||
* Interface for GradientEmbeddings parameters. Extends EmbeddingsParams and | ||
* defines additional parameters specific to the GradientEmbeddings class. | ||
*/ | ||
export interface GradientEmbeddingsParams extends EmbeddingsParams { | ||
/** | ||
* Gradient AI Access Token. | ||
* Provide Access Token if you do not wish to automatically pull from env. | ||
*/ | ||
gradientAccessKey?: string; | ||
/** | ||
* Gradient Workspace Id. | ||
* Provide workspace id if you do not wish to automatically pull from env. | ||
*/ | ||
workspaceId?: string; | ||
} | ||
|
||
/** | ||
* Class for generating embeddings using the Gradient AI's API. Extends the | ||
* Embeddings class and implements GradientEmbeddingsParams and | ||
*/ | ||
export class GradientEmbeddings | ||
extends Embeddings | ||
implements GradientEmbeddingsParams | ||
{ | ||
gradientAccessKey?: string; | ||
|
||
workspaceId?: string; | ||
|
||
batchSize = 128; | ||
|
||
model: any; | ||
|
||
constructor(fields: GradientEmbeddingsParams) { | ||
super(fields); | ||
|
||
this.gradientAccessKey = | ||
fields?.gradientAccessKey ?? | ||
getEnvironmentVariable("GRADIENT_ACCESS_TOKEN"); | ||
this.workspaceId = | ||
fields?.workspaceId ?? getEnvironmentVariable("GRADIENT_WORKSPACE_ID"); | ||
|
||
if (!this.gradientAccessKey) { | ||
throw new Error("Missing Gradient AI Access Token"); | ||
} | ||
|
||
if (!this.workspaceId) { | ||
throw new Error("Missing Gradient AI Workspace ID"); | ||
} | ||
} | ||
|
||
/** | ||
* Method to generate embeddings for an array of documents. Splits the | ||
* documents into batches and makes requests to the Gradient API to generate | ||
* embeddings. | ||
* @param texts Array of documents to generate embeddings for. | ||
* @returns Promise that resolves to a 2D array of embeddings for each document. | ||
*/ | ||
async embedDocuments(texts: string[]): Promise<number[][]> { | ||
await this.setModel(); | ||
|
||
const mappedTexts = texts.map((text) => ({ input: text })); | ||
|
||
const batches = chunkArray(mappedTexts, this.batchSize); | ||
|
||
const batchRequests = batches.map((batch) => | ||
this.caller.call(async () => | ||
this.model.generateEmbeddings({ | ||
inputs: batch, | ||
}) | ||
) | ||
); | ||
const batchResponses = await Promise.all(batchRequests); | ||
|
||
const embeddings: number[][] = []; | ||
for (let i = 0; i < batchResponses.length; i += 1) { | ||
const batch = batches[i]; | ||
const { embeddings: batchResponse } = batchResponses[i]; | ||
for (let j = 0; j < batch.length; j += 1) { | ||
embeddings.push(batchResponse[j].embedding); | ||
} | ||
} | ||
return embeddings; | ||
} | ||
|
||
/** | ||
* Method to generate an embedding for a single document. Calls the | ||
* embedDocuments method with the document as the input. | ||
* @param text Document to generate an embedding for. | ||
* @returns Promise that resolves to an embedding for the document. | ||
*/ | ||
async embedQuery(text: string): Promise<number[]> { | ||
const data = await this.embedDocuments([text]); | ||
return data[0]; | ||
} | ||
|
||
/** | ||
* Method to set the model to use for generating embeddings. | ||
* @sets the class' `model` value to that of the retrieved Embeddings Model. | ||
*/ | ||
async setModel() { | ||
if (this.model) return; | ||
chasemcdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const gradient = new Gradient({ | ||
accessToken: this.gradientAccessKey, | ||
workspaceId: this.workspaceId, | ||
}); | ||
this.model = await gradient.getEmbeddingsModel({ | ||
slug: "bge-large", | ||
}); | ||
} | ||
} |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR adds a new feature that requires an environment variable to be set. Please review the code changes related to accessing the environment variable.