UNPKG

@sap-ai-sdk/foundation-models

Version:

SAP Cloud SDK for AI is the official Software Development Kit (SDK) for **SAP AI Core**, **SAP Generative AI Hub**, and **Orchestration Service**.

71 lines 3.34 kB
import { executeRequest } from '@sap-ai-sdk/core'; import { getDeploymentId, getResourceGroup } from '@sap-ai-sdk/ai-api/internal.js'; import { apiVersion } from './model-types.js'; import { AzureOpenAiChatCompletionResponse } from './azure-openai-chat-completion-response.js'; import { AzureOpenAiChatCompletionStreamResponse } from './azure-openai-chat-completion-stream-response.js'; import { AzureOpenAiChatCompletionStream } from './azure-openai-chat-completion-stream.js'; /** * Azure OpenAI client for chat completion. */ export class AzureOpenAiChatClient { modelDeployment; destination; /** * Creates an instance of the Azure OpenAI chat client. * @param modelDeployment - This configuration is used to retrieve a deployment. Depending on the configuration use either the given deployment ID or the model name to retrieve matching deployments. If model and deployment ID are given, the model is verified against the deployment. * @param destination - The destination to use for the request. */ constructor(modelDeployment, destination) { this.modelDeployment = modelDeployment; this.destination = destination; } /** * Creates a completion for the chat messages. * @param data - The input parameters for the chat completion. * @param requestConfig - The request configuration. * @returns The completion result. */ async run(data, requestConfig) { const response = await this.executeRequest(data, requestConfig); return new AzureOpenAiChatCompletionResponse(response); } /** * Creates a completion stream for the chat messages. * @param data - The input parameters for the chat completion. * @param controller - The abort controller. * @param requestConfig - The request configuration. * @returns A response containing the chat completion stream. */ async stream(data, controller = new AbortController(), requestConfig) { const response = new AzureOpenAiChatCompletionStreamResponse(); response.stream = (await this.createStream(data, controller, requestConfig)) ._pipe(AzureOpenAiChatCompletionStream._processChunk) ._pipe(AzureOpenAiChatCompletionStream._processFinishReason, response) ._pipe(AzureOpenAiChatCompletionStream._processTokenUsage, response); return response; } async executeRequest(data, requestConfig) { const deploymentId = await getDeploymentId(this.modelDeployment, 'azure-openai', this.destination); const resourceGroup = getResourceGroup(this.modelDeployment); return executeRequest({ url: `/inference/deployments/${deploymentId}/chat/completions`, apiVersion, resourceGroup }, data, requestConfig, this.destination); } async createStream(data, controller, requestConfig) { const response = await this.executeRequest({ ...data, stream: true, stream_options: { include_usage: true } }, { ...requestConfig, responseType: 'stream', signal: controller.signal }); return AzureOpenAiChatCompletionStream._create(response, controller); } } //# sourceMappingURL=azure-openai-chat-client.js.map