-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Update OpenAI with Azure Specific Code * Export Embeddings * Add deployment parameters * Check to fix build issue * Update libs/langchain-openai/src/azure/embeddings.ts * Update libs/langchain-openai/src/azure/embeddings.ts * nit changes * Update Test Descriptions * Format * Fix types --------- Co-authored-by: Sarangan Rajamanickam <[email protected]> Co-authored-by: Brace Sproul <[email protected]>
- Loading branch information
1 parent
5da2466
commit 58c2655
Showing
15 changed files
with
1,552 additions
and
45 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,98 @@ | ||
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) { | ||
newFields.azureOpenAIApiDeploymentName = newFields.deploymentName; | ||
newFields.azureOpenAIApiKey = newFields.apiKey; | ||
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, | ||
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; | ||
} | ||
|
||
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; | ||
} | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.