forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nc/tensorflow embeddings (langchain-ai#1020)
* Add tensorflow embeddings * Add entrypoints * Lint * Update name
- Loading branch information
Showing
8 changed files
with
231 additions
and
2 deletions.
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
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,44 @@ | ||
import { load } from "@tensorflow-models/universal-sentence-encoder"; | ||
import * as tf from "@tensorflow/tfjs-core"; | ||
|
||
import { Embeddings, EmbeddingsParams } from "./base.js"; | ||
|
||
export interface TensorFlowEmbeddingsParams extends EmbeddingsParams {} | ||
|
||
export class TensorFlowEmbeddings extends Embeddings { | ||
constructor(fields?: TensorFlowEmbeddingsParams) { | ||
super(fields ?? {}); | ||
|
||
try { | ||
tf.backend(); | ||
} catch (e) { | ||
throw new Error("No TensorFlow backend found, see instructions at ..."); | ||
} | ||
} | ||
|
||
_cached: ReturnType<typeof load>; | ||
|
||
private async load() { | ||
if (this._cached === undefined) { | ||
this._cached = load(); | ||
} | ||
return this._cached; | ||
} | ||
|
||
private _embed(texts: string[]) { | ||
return this.caller.call(async () => { | ||
const model = await this.load(); | ||
return model.embed(texts); | ||
}); | ||
} | ||
|
||
embedQuery(document: string): Promise<number[]> { | ||
return this._embed([document]) | ||
.then((embeddings) => embeddings.array()) | ||
.then((embeddings) => embeddings[0]); | ||
} | ||
|
||
embedDocuments(documents: string[]): Promise<number[][]> { | ||
return this._embed(documents).then((embeddings) => embeddings.array()); | ||
} | ||
} |
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,41 @@ | ||
import { test, expect } from "@jest/globals"; | ||
import "@tensorflow/tfjs-backend-cpu"; | ||
import { TensorFlowEmbeddings } from "../tensorflow.js"; | ||
import { MemoryVectorStore } from "../../vectorstores/memory.js"; | ||
import { Document } from "../../document.js"; | ||
|
||
test("TensorflowEmbeddings", async () => { | ||
const embeddings = new TensorFlowEmbeddings(); | ||
|
||
const documents = [ | ||
"Hello world!", | ||
"Hello bad world!", | ||
"Hello nice world!", | ||
"Hello good world!", | ||
"1 + 1 = 2", | ||
"1 + 1 = 3", | ||
]; | ||
|
||
const queryEmbedding = await embeddings.embedQuery(documents[0]); | ||
expect(queryEmbedding).toHaveLength(512); | ||
expect(typeof queryEmbedding[0]).toBe("number"); | ||
|
||
const store = new MemoryVectorStore(embeddings); | ||
|
||
await store.addDocuments( | ||
documents.map((pageContent) => new Document({ pageContent })) | ||
); | ||
|
||
expect(await store.similaritySearch(documents[4], 2)).toMatchInlineSnapshot(` | ||
[ | ||
Document { | ||
"metadata": {}, | ||
"pageContent": "1 + 1 = 2", | ||
}, | ||
Document { | ||
"metadata": {}, | ||
"pageContent": "1 + 1 = 3", | ||
}, | ||
] | ||
`); | ||
}); |
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