-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathembeddings.ts
137 lines (118 loc) Β· 4.47 KB
/
embeddings.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
type ClientOptions,
AzureOpenAI as AzureOpenAIClient,
OpenAI as OpenAIClient,
} from "openai";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { OpenAIEmbeddings, OpenAIEmbeddingsParams } from "../embeddings.js";
import { AzureOpenAIInput, OpenAICoreRequestOptions } from "../types.js";
import { getEndpoint, OpenAIEndpointConfig } from "../utils/azure.js";
import { wrapOpenAIClientError } from "../utils/openai.js";
export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
azureOpenAIApiVersion?: string;
azureOpenAIApiKey?: string;
azureADTokenProvider?: () => Promise<string>;
azureOpenAIApiInstanceName?: string;
azureOpenAIApiDeploymentName?: string;
azureOpenAIBasePath?: string;
constructor(
fields?: Partial<OpenAIEmbeddingsParams> &
Partial<AzureOpenAIInput> & {
verbose?: boolean;
/** The OpenAI API key to use. */
apiKey?: string;
configuration?: ClientOptions;
deploymentName?: string;
openAIApiVersion?: string;
}
) {
super(fields);
this.batchSize = fields?.batchSize ?? 1;
this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
fields?.apiKey ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
this.azureOpenAIApiVersion =
fields?.azureOpenAIApiVersion ??
fields?.openAIApiVersion ??
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
this.azureOpenAIBasePath =
fields?.azureOpenAIBasePath ??
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
this.azureOpenAIApiInstanceName =
fields?.azureOpenAIApiInstanceName ??
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
this.azureOpenAIApiDeploymentName =
(fields?.azureOpenAIApiEmbeddingsDeploymentName ||
fields?.azureOpenAIApiDeploymentName) ??
(getEnvironmentVariable("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") ||
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
this.azureADTokenProvider = fields?.azureADTokenProvider;
}
protected async embeddingWithRetry(
request: OpenAIClient.EmbeddingCreateParams
) {
if (!this.client) {
const openAIEndpointConfig: OpenAIEndpointConfig = {
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
azureOpenAIApiKey: this.azureOpenAIApiKey,
azureOpenAIBasePath: this.azureOpenAIBasePath,
azureADTokenProvider: this.azureADTokenProvider,
baseURL: this.clientConfig.baseURL,
};
const endpoint = getEndpoint(openAIEndpointConfig);
const params = {
...this.clientConfig,
baseURL: endpoint,
timeout: this.timeout,
maxRetries: 0,
};
if (!this.azureADTokenProvider) {
params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
}
if (!params.baseURL) {
delete params.baseURL;
}
params.defaultHeaders = {
...params.defaultHeaders,
"User-Agent": params.defaultHeaders?.["User-Agent"]
? `${params.defaultHeaders["User-Agent"]}: langchainjs-azure-openai-v2`
: `langchainjs-azure-openai-v2`,
};
this.client = new AzureOpenAIClient({
apiVersion: this.azureOpenAIApiVersion,
azureADTokenProvider: this.azureADTokenProvider,
deployment: this.azureOpenAIApiDeploymentName,
...params,
});
}
const requestOptions: OpenAICoreRequestOptions = {};
if (this.azureOpenAIApiKey) {
requestOptions.headers = {
"api-key": this.azureOpenAIApiKey,
...requestOptions.headers,
};
requestOptions.query = {
"api-version": this.azureOpenAIApiVersion,
...requestOptions.query,
};
}
return this.caller.call(async () => {
try {
const res = await this.client.embeddings.create(
request,
// This unknown cast is required because OpenAI types seem to be incorrect here
// without it, we can't pass arbitrary keys via the `query` field in the options,
// which means we can't specify the `api-version` as required by Azure OpenAI.
// See https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning
requestOptions as unknown as OpenAICoreRequestOptions<OpenAIClient.EmbeddingCreateParams>
);
return res;
} catch (e) {
const error = wrapOpenAIClientError(e);
throw error;
}
});
}
}