-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
embeddings.ts
110 lines (101 loc) · 3.34 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
import {
type ClientOptions,
AzureOpenAI as AzureOpenAIClient,
OpenAI as OpenAIClient,
} from "openai";
import { OpenAIEmbeddings, OpenAIEmbeddingsParams } from "../embeddings.js";
import {
AzureOpenAIInput,
OpenAICoreRequestOptions,
LegacyOpenAIInput,
} from "../types.js";
import { getEndpoint, OpenAIEndpointConfig } from "../utils/azure.js";
import { wrapOpenAIClientError } from "../utils/openai.js";
export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
constructor(
fields?: Partial<OpenAIEmbeddingsParams> &
Partial<AzureOpenAIInput> & {
verbose?: boolean;
/** The OpenAI API key to use. */
apiKey?: string;
configuration?: ClientOptions;
deploymentName?: string;
openAIApiVersion?: string;
},
configuration?: ClientOptions & LegacyOpenAIInput
) {
const newFields = { ...fields };
if (Object.entries(newFields).length) {
// don't rewrite the fields if they are already set
newFields.azureOpenAIApiDeploymentName =
newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
newFields.azureOpenAIApiKey =
newFields.azureOpenAIApiKey ?? newFields.apiKey;
newFields.azureOpenAIApiVersion =
newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
}
super(newFields, configuration);
}
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,
requestOptions
);
return res;
} catch (e) {
const error = wrapOpenAIClientError(e);
throw error;
}
});
}
}