-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
googlepalm.ts
119 lines (107 loc) Β· 3.55 KB
/
googlepalm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { TextServiceClient } from "@google-ai/generativelanguage";
import { GoogleAuth } from "google-auth-library";
import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
/**
* Interface that extends EmbeddingsParams and defines additional
* parameters specific to the GooglePaLMEmbeddings class.
*/
export interface GooglePaLMEmbeddingsParams extends EmbeddingsParams {
/**
* Model Name to use
*
* Alias for `model`
*
* Note: The format must follow the pattern - `models/{model}`
*/
modelName?: string;
/**
* Model Name to use
*
* Note: The format must follow the pattern - `models/{model}`
*/
model?: string;
/**
* Google Palm API key to use
*/
apiKey?: string;
}
/**
* Class that extends the Embeddings class and provides methods for
* generating embeddings using the Google Palm API.
*
* @example
* ```typescript
* const model = new GooglePaLMEmbeddings({
* apiKey: "<YOUR API KEY>",
* model: "models/embedding-gecko-001",
* });
*
* // Embed a single query
* const res = await model.embedQuery(
* "What would be a good company name for a company that makes colorful socks?"
* );
* console.log({ res });
*
* // Embed multiple documents
* const documentRes = await model.embedDocuments(["Hello world", "Bye bye"]);
* console.log({ documentRes });
* ```
*/
export class GooglePaLMEmbeddings
extends Embeddings
implements GooglePaLMEmbeddingsParams
{
apiKey?: string;
modelName = "models/embedding-gecko-001";
model = "models/embedding-gecko-001";
private client: TextServiceClient;
constructor(fields?: GooglePaLMEmbeddingsParams) {
super(fields ?? {});
this.modelName = fields?.model ?? fields?.modelName ?? this.model;
this.model = this.modelName;
this.apiKey =
fields?.apiKey ?? getEnvironmentVariable("GOOGLE_PALM_API_KEY");
if (!this.apiKey) {
throw new Error(
"Please set an API key for Google Palm 2 in the environment variable GOOGLE_PALM_API_KEY or in the `apiKey` field of the GooglePalm constructor"
);
}
this.client = new TextServiceClient({
authClient: new GoogleAuth().fromAPIKey(this.apiKey),
});
}
protected async _embedText(text: string): Promise<number[]> {
// replace newlines, which can negatively affect performance.
const cleanedText = text.replace(/\n/g, " ");
const res = await this.client.embedText({
model: this.model,
text: cleanedText,
});
return res[0].embedding?.value ?? [];
}
/**
* Method that takes a document as input and returns a promise that
* resolves to an embedding for the document. It calls the _embedText
* method with the document as the input.
* @param document Document for which to generate an embedding.
* @returns Promise that resolves to an embedding for the input document.
*/
embedQuery(document: string): Promise<number[]> {
return this.caller.callWithOptions(
{},
this._embedText.bind(this),
document
);
}
/**
* Method that takes an array of documents as input and returns a promise
* that resolves to a 2D array of embeddings for each document. It calls
* the _embedText method for each document in the array.
* @param documents Array of documents for which to generate embeddings.
* @returns Promise that resolves to a 2D array of embeddings for each input document.
*/
embedDocuments(documents: string[]): Promise<number[][]> {
return Promise.all(documents.map((document) => this._embedText(document)));
}
}