@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**.
101 lines • 2.94 kB
JavaScript
import { createLogger, pickValueIgnoreCase } from '@sap-cloud-sdk/util';
const logger = createLogger({
package: 'foundation-models',
messageContext: 'azure-openai-chat-completion-stream-response'
});
/**
* Azure OpenAI chat completion stream response.
*/
export class AzureOpenAiChatCompletionStreamResponse {
_usage;
/**
* Finish reasons for all choices.
*/
_finishReasons = new Map();
_toolCallsAccumulators = new Map();
_toolCalls = new Map();
_stream;
_rawResponse;
constructor(rawResponse) {
if (!rawResponse) {
logger.warn('Constructing AzureOpenAiChatCompletionStreamResponse without raw HTTP response is deprecated and can lead to runtime errors when accessing `rawResponse`.');
return;
}
this._rawResponse = rawResponse;
}
/**
* Gets the raw HTTP response. SSE data is not part of the immediate response.
* @returns The raw HTTP response.
* @throws {Error} When constructed without a raw response parameter (deprecated).
*/
get rawResponse() {
if (!this._rawResponse) {
throw new Error('The raw response is not available. Please provide the raw response when constructing `AzureOpenAiChatCompletionStreamResponse`.');
}
return this._rawResponse;
}
/**
* Gets the request ID from the response headers.
* @returns The request ID, or undefined if the header is not present.
*/
getRequestId() {
return pickValueIgnoreCase(this._rawResponse?.headers, 'x-aicore-request-id');
}
getTokenUsage() {
return this._usage;
}
/**
* @internal
*/
_setTokenUsage(usage) {
this._usage = usage;
}
getFinishReason(choiceIndex = 0) {
return this._finishReasons.get(choiceIndex);
}
/**
* @internal
*/
_getFinishReasons() {
return this._finishReasons;
}
/**
* @internal
*/
_setFinishReasons(finishReasons) {
this._finishReasons = finishReasons;
}
/**
* Gets the tool calls for a specific choice index.
* @param choiceIndex - The index of the choice to get the tool calls for.
* @returns The tool calls for the specified choice index.
*/
getToolCalls(choiceIndex = 0) {
return this._toolCalls.get(choiceIndex);
}
/**
* @internal
*/
_setToolCalls(choiceIndex, toolCalls) {
this._toolCalls.set(choiceIndex, toolCalls);
}
/**
* @internal
*/
_getToolCallsAccumulators() {
return this._toolCallsAccumulators;
}
get stream() {
if (!this._stream) {
throw new Error('Response stream is undefined.');
}
return this._stream;
}
/**
* @internal
*/
set stream(stream) {
this._stream = stream;
}
}
//# sourceMappingURL=azure-openai-chat-completion-stream-response.js.map