@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**.
133 lines • 4.79 kB
JavaScript
/**
* Representation of an orchestration response.
*/
export class OrchestrationResponse {
rawResponse;
/**
* The completion post response.
*/
_data;
constructor(rawResponse) {
this.rawResponse = rawResponse;
this._data = rawResponse.data;
}
/**
* Usage of tokens in the response.
* @returns Token usage.
*/
getTokenUsage() {
return this._data.final_result.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 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) {
const choice = this.findChoiceByIndex(choiceIndex);
if (choice?.message?.content === '' &&
choice?.finish_reason === 'content_filter') {
throw new Error('Content generated by the LLM was filtered by the output filter. Please try again with a different prompt or filter configuration.');
}
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) {
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) {
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) {
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) {
return this.findChoiceByIndex(choiceIndex)?.message;
}
/**
* Gets the intermediate results from the orchestration response.
* @returns The intermediate results.
*/
getIntermediateResults() {
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.
*
* Each failure includes:
* - message: Description of what went wrong
* - code: Error code identifying the type of failure
* - location: Position in the fallback chain (e.g., "config[0]", "config[1]").
* @example
* const response = await client.chatCompletion();
* const failures = response.getIntermediateFailures();
* if (failures) {
* failures.forEach(f => console.log(`${f.location}: ${f.message}`));
* }
* @returns The intermediate failures, or undefined if there were none.
*/
getIntermediateFailures() {
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) {
return this.getChoices().find((c) => c.index === index);
}
/**
* Gets the request ID from the orchestration response.
* @returns The request ID.
*/
getRequestId() {
return this._data.request_id;
}
/**
* 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() {
return this._data.final_result.citations;
}
getChoices() {
return this._data.final_result.choices;
}
}
//# sourceMappingURL=orchestration-response.js.map