ollama-api-facade-js
Version:
OllamaApiFacadeJS is an open-source library for running an ExpressJS backend as an Ollama API using LangChainJS. It supports local language models services like LmStudio and allows seamless message conversion and streaming between LangChainJS and Ollama c
60 lines • 2.72 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolCallService = void 0;
/**
* ToolCallService acts as a Facade for handling LangChainJS tools.
* It automatically binds tools to the chat model and processes tool calls.
*/
class ToolCallService {
constructor(chatModel) {
this.tools = [];
this.chatModel = chatModel;
}
/**
* Registers tools and returns an instance that manages them.
* @param {StructuredTool[]} tools - The tools to register.
* @returns {{ invoke: (messages: BaseMessage[]) => Promise<AIMessageChunk> }} - New instance with tools bound.
*/
with(tools) {
this.tools = tools;
return { invoke: this.invoke.bind(this) };
}
/**
* Invokes the chat model with automatic tool execution.
* @param {BaseMessage[]} messages - The chat history.
* @returns {Promise<AIMessageChunk>} - The final AI response.
*/
invoke(messages) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (!this.chatModel || !this.chatModel.bindTools) {
throw new Error('Chat model or bindTools method is undefined');
}
const modelWithTools = this.chatModel.bindTools(this.tools);
let response = yield modelWithTools.invoke(messages);
if ((_a = response.tool_calls) === null || _a === void 0 ? void 0 : _a.length) {
messages.push(response);
for (const toolCall of response.tool_calls) {
const tool = this.tools.find((t) => t.name === toolCall.name);
if (tool) {
const toolResult = yield tool.invoke(toolCall);
messages.push(toolResult);
}
}
response = yield modelWithTools.invoke(messages);
}
return response;
});
}
}
exports.ToolCallService = ToolCallService;
//# sourceMappingURL=ToolCallService.js.map