UNPKG

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

83 lines 3.94 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OllamaApiFacade = void 0; const express_1 = __importDefault(require("express")); const messages_1 = require("@langchain/core/messages"); const ChatResponse_1 = require("./ChatResponse"); const ApiRoutes_1 = require("./ApiRoutes"); const ChatModels_1 = require("../models/ChatModels"); /** * Class representing the API facade for handling chat requests and responses. * This class sets up the necessary routes and middleware for the Express application * to handle chat requests from Ollama clients and stream responses back to them. */ class OllamaApiFacade { constructor(app, baseChatModel, modelName, toolService) { this.app = app; this.baseChatModel = baseChatModel; this.toolService = toolService; this.MODEL_NAME = ''; this.DEFAULT_PORT = 11434; this.DEFAULT_HOST = 'localhost'; this.MODEL_NAME = modelName; this.apiRoutes = new ApiRoutes_1.ApiRoutes(app, modelName); this.applyMiddleware(app); } applyMiddleware(app) { app.use(express_1.default.json()); } /** * Sets up the POST /api/chat route to handle chat requests from Ollama clients. * @param {ChatHandler} chatHandler - The handler function to process chat requests. */ postApiChat(chatHandler) { this.app.post('/api/chat', (request, response) => __awaiter(this, void 0, void 0, function* () { const chatRequest = new ChatModels_1.ChatRequest(request.body); chatRequest.messages = request.body.messages.map((nativeChatMessage) => { switch (nativeChatMessage.role) { case 'system': return new messages_1.SystemMessage(nativeChatMessage.content); case 'user': return new messages_1.HumanMessage(nativeChatMessage.content); case 'assistant': return new messages_1.AIMessage(nativeChatMessage.content); default: throw new Error(`Unknown role: ${nativeChatMessage.role}`); } }); const chatResponse = new ChatResponse_1.ChatResponse(response); yield chatHandler(chatRequest, this.baseChatModel, chatResponse, this.toolService); })); } /** * Starts the Express server and listens on the default Ollama API port 11434. * @param {Function} [callback] - Optional callback function to execute once the server is running. * @returns {Server} - The HTTP server instance. */ listen(callback) { const server = this.app.listen(this.DEFAULT_PORT, () => { const address = `http://${this.DEFAULT_HOST}:${this.DEFAULT_PORT}`; if (callback) { callback(address); } else { console.log(`✅ Server running at ${address}`); } }); return server; } } exports.OllamaApiFacade = OllamaApiFacade; //# sourceMappingURL=OllamaApiFacade.js.map