@sap-ai-sdk/langchain
Version:
SAP Cloud SDK for AI is the official Software Development Kit (SDK) for **SAP AI Core**, **SAP Generative AI Hub**, and **Orchestration Service**.
60 lines • 2.27 kB
JavaScript
import { AzureOpenAiChatClient as AzureOpenAiChatClientBase } from '@sap-ai-sdk/foundation-models';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { mapLangChainToAiClient, mapOutputToChatResult, mapToolToOpenAiTool } from './util.js';
/**
* LangChain chat client for Azure OpenAI consumption on SAP BTP.
*/
export class AzureOpenAiChatClient extends BaseChatModel {
temperature;
top_p;
logit_bias;
user;
presence_penalty;
frequency_penalty;
stop;
max_tokens;
supportsStrictToolCalling;
openAiChatClient;
constructor(fields, destination) {
super(fields);
this.openAiChatClient = new AzureOpenAiChatClientBase(fields, destination);
this.temperature = fields.temperature;
this.top_p = fields.top_p;
this.logit_bias = fields.logit_bias;
this.user = fields.user;
this.stop = fields.stop;
this.presence_penalty = fields.presence_penalty;
this.frequency_penalty = fields.frequency_penalty;
this.max_tokens = fields.max_tokens;
if (fields.supportsStrictToolCalling !== undefined) {
this.supportsStrictToolCalling = fields.supportsStrictToolCalling;
}
}
_llmType() {
return 'azure_openai';
}
async _generate(messages, options, runManager) {
const res = await this.caller.callWithOptions({
signal: options.signal
}, () => this.openAiChatClient.run(mapLangChainToAiClient(this, messages, options), options.requestConfig));
const content = res.getContent();
// we currently do not support streaming
await runManager?.handleLLMNewToken(typeof content === 'string' ? content : '');
return mapOutputToChatResult(res.data);
}
bindTools(tools, kwargs) {
let strict;
if (kwargs?.strict !== undefined) {
strict = kwargs.strict;
}
else if (this.supportsStrictToolCalling !== undefined) {
strict = this.supportsStrictToolCalling;
}
const newTools = tools.map(tool => mapToolToOpenAiTool(tool, strict));
return this.withConfig({
tools: newTools,
...kwargs
});
}
}
//# sourceMappingURL=chat.js.map