@sap-ai-sdk/orchestration
Version:
SAP Cloud SDK for AI is the official Software Development Kit (SDK) for **SAP AI Core**, **SAP Generative AI Hub**, and **Orchestration Service**.
187 lines • 6.3 kB
JavaScript
import { createLogger } from '@sap-cloud-sdk/util';
const logger = createLogger({
package: 'orchestration',
messageContext: 'orchestration-stream-response'
});
/**
* Orchestration stream response.
*/
export class OrchestrationStreamResponse {
_openStream = true;
_data = {};
_stream;
_rawResponse;
constructor(rawResponse) {
if (!rawResponse) {
logger.warn('Constructing OrchestrationStreamResponse without raw HTTP response is deprecated and can lead to runtime errors when accessing `rawResponse`.');
return;
}
this._rawResponse = rawResponse;
}
/**
* Gets the raw HTTP response from the orchestration service. SSE data is not part of the immediate response.
* @returns The raw HTTP response.
* @throws {Error} When OrchestrationStreamResponse was 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 `OrchestrationStreamResponse`');
}
return this._rawResponse;
}
/**
* Gets the token usage for the response.
* @returns The token usage for the response.
*/
getTokenUsage() {
if (this.isStreamOpen()) {
return;
}
return this._data.final_result?.usage;
}
/**
* Gets the request ID for the stream response.
* @returns The request ID, or undefined if the first chunk has not been received yet.
*/
getRequestId() {
return this._data.request_id;
}
/**
* Gets the finish reason for a specific choice index.
* @param choiceIndex - The index of the choice to get the finish reason for.
* @returns The finish reason for the specified choice index.
*/
getFinishReason(choiceIndex = 0) {
if (this.isStreamOpen()) {
return;
}
return this.findChoiceByIndex(choiceIndex)?.finish_reason;
}
/**
* Parses the orchestration response and returns the content.
* If the response was filtered, an error is thrown.
* @param choiceIndex - The index of the choice to parse.
* @returns The message content.
*/
getContent(choiceIndex = 0) {
if (this.isStreamOpen()) {
return;
}
const choice = this.findChoiceByIndex(choiceIndex);
return choice?.message?.content;
}
/**
* Parses the orchestration 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) {
if (this.isStreamOpen()) {
return;
}
const choice = this.findChoiceByIndex(choiceIndex);
return choice?.message?.tool_calls;
}
/**
* Parses the orchestration 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) {
if (this.isStreamOpen()) {
return;
}
const choice = this.findChoiceByIndex(choiceIndex);
return choice?.message?.refusal;
}
/**
* Messages that can be used for subsequent prompts as message history.
* @param choiceIndex - The index of the choice to parse.
* @returns A list of all messages.
*/
getAllMessages(choiceIndex = 0) {
if (this.isStreamOpen()) {
return;
}
const messages = this._data.intermediate_results?.templating ?? [];
const content = this.findChoiceByIndex(choiceIndex)?.message;
return content ? [...messages, content] : messages;
}
/**
* 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) {
if (this.isStreamOpen()) {
return;
}
return this.findChoiceByIndex(choiceIndex)?.message;
}
/**
* Gets the intermediate results from the orchestration response.
* @returns The intermediate results.
*/
getIntermediateResults() {
if (this.isStreamOpen()) {
return;
}
return this._data.intermediate_results;
}
/**
* Gets the intermediate failures from the orchestration response.
* When using module fallback, this contains errors from module configurations
* that failed before a successful one was found.
* @returns The intermediate failures, or undefined if there were none.
*/
getIntermediateFailures() {
if (this.isStreamOpen()) {
return;
}
return this._data.intermediate_failures;
}
/**
* 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) {
if (this.isStreamOpen()) {
return;
}
return this.getChoices().find((c) => c.index === index);
}
/**
* Gets the citations from the orchestration response.
* Citations are returned by models like Perplexity Sonar that provide source references.
* @returns The citations, or undefined if there are none.
*/
getCitations() {
if (this.isStreamOpen()) {
return;
}
return this._data.final_result?.citations;
}
get stream() {
if (!this._stream) {
throw new Error('Response stream is undefined.');
}
return this._stream;
}
/**
* @internal
*/
set stream(stream) {
this._stream = stream;
}
getChoices() {
return this._data.final_result?.choices ?? [];
}
isStreamOpen() {
if (this._openStream) {
throw Error('The stream is still open, the requested data is not available yet. Please wait until the stream is closed.');
}
return this._openStream;
}
}
//# sourceMappingURL=orchestration-stream-response.js.map