@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**.
83 lines • 2.75 kB
JavaScript
import { pickValueIgnoreCase } from '@sap-cloud-sdk/util';
/**
* Azure OpenAI chat completion response.
*/
export class AzureOpenAiChatCompletionResponse {
rawResponse;
/**
* The chat completion response.
*/
_data;
constructor(rawResponse) {
this.rawResponse = rawResponse;
this._data = rawResponse.data;
}
/**
* 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');
}
/**
* Usage of tokens in the response.
* @returns Token usage.
*/
getTokenUsage() {
return this._data.usage;
}
/**
* Reason for stopping the completion.
* @param choiceIndex - The index of the choice to parse.
* @returns The finish reason.
*/
getFinishReason(choiceIndex = 0) {
return this.findChoiceByIndex(choiceIndex)?.finish_reason;
}
/**
* Parses the Azure OpenAI response and returns the content.
* @param choiceIndex - The index of the choice to parse.
* @returns The message content.
*/
getContent(choiceIndex = 0) {
return this.findChoiceByIndex(choiceIndex)?.message?.content;
}
/**
* Parses the Azure OpenAI response and returns the tool calls generated by the model.
* @param choiceIndex - The index of the choice to parse.
* @returns The message tool calls.
*/
getToolCalls(choiceIndex = 0) {
const choice = this.findChoiceByIndex(choiceIndex);
return choice?.message?.tool_calls;
}
/**
* Parses the Azure OpenAI response and returns the refusal message generated by the model.
* @param choiceIndex - The index of the choice to parse.
* @returns The refusal string.
*/
getRefusal(choiceIndex = 0) {
const choice = this.findChoiceByIndex(choiceIndex);
return choice?.message?.refusal;
}
/**
* Gets the assistant message from the response.
* @param choiceIndex - The index of the choice to use (default is 0).
* @returns The assistant message.
*/
getAssistantMessage(choiceIndex = 0) {
return this.findChoiceByIndex(choiceIndex)?.message;
}
/**
* Parses the response and returns the choice by index.
* @param index - The index of the choice to find.
* @returns An {@link LLMChoice} object associated with the index.
*/
findChoiceByIndex(index) {
return this.getChoices().find((c) => c.index === index);
}
getChoices() {
return this._data.choices;
}
}
//# sourceMappingURL=azure-openai-chat-completion-response.js.map