@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**.
89 lines • 3.29 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.orchestration_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.module_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;
}
getChoices() {
return this.data.orchestration_result.choices;
}
findChoiceByIndex(index) {
// TODO: replace cast with LLMChoice[] after the bug in orchestration, where
// 'role' in ResponseChatMessage is optional when it should be mandatory, is fixed.
// https://github.com/SAP/ai-sdk-js-backlog/issues/306
return this.getChoices().find((c) => c.index === index);
}
}
//# sourceMappingURL=orchestration-response.js.map