UNPKG

quivr-typechat

Version:

A TypeChatLanguageModel compatible with Quivr API to run TpeChat on your own Quivr istance with the possibility of using a private LLM

90 lines (89 loc) 4.51 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createQuivrLanguageModelExplicit = exports.createQuivrLanguageModel = void 0; const typechat_1 = require("typechat"); const axios_1 = require("axios"); const axiosConfig = { validateStatus: status => true }; const defaultQuivrModelConfig = { endpoint: "https://api.quivr.app", model: "gpt-3.5-turbo-0613", max_tokens: 256 }; /** * Creates a language model encapsulation of a Quivr REST API from environnement variables. * @param env Envirement variables * @param additionalConfig The additionnal configuration for the QuivrLanguageModel : * endpoint: API endpoint for Quivr. Defaults to https://api.quivr.app * model: Model to use in the Quivr instance. Defaults to gpt-3.5-turbo-0613 * max_tokens: Maximum tokens. Defaults to 250 */ function createQuivrLanguageModel(env, additionalConfig) { if (env.QUIVR_API_KEY) { if (env.QUIVR_BRAIN_ID) { return (0, exports.createQuivrLanguageModelExplicit)(env.QUIVR_API_KEY, env.QUIVR_BRAIN, Object.assign(Object.assign({}, defaultQuivrModelConfig), (additionalConfig !== null && additionalConfig !== void 0 ? additionalConfig : {}))); } else throw new Error(`"Missing environment variable: QUIVR_BRAIN`); } else throw new Error(`"Missing environment variable: QUIVR_API_KEY`); } exports.createQuivrLanguageModel = createQuivrLanguageModel; /** * Creates a language model encapsulation of a Quivr REST API endpoint with parameters. * @param apiKey The Quivr API key. * @param brain_id The brain to use for this model * @param options The additionnal configuration for the QuivrLanguageModel : * endpoint: API endpoint for Quivr. Defaults to https://api.quivr.app * model: Model to use in the Quivr instance. Defaults to gpt-3.5-turbo-0613 * max_tokens: Maximum tokens. Defaults to 250 */ const createQuivrLanguageModelExplicit = (apiKey, brain_id, options) => { const { endpoint, model, max_tokens } = options; const client = axios_1.default.create({ headers: { Authorization: `Bearer ${apiKey}`, "user-agent": "quivr-typechat", } }); return { complete }; function complete(question) { return __awaiter(this, void 0, void 0, function* () { console.log(`Calling Quivr instance ${endpoint} using brain_id ${brain_id}`, options); const chatParams = { model, max_tokens, temperature: 0, question }; const newChatName = `Typechat ${Date.now()} - ${Math.round(Math.random() * 10)}`; const result = yield client.post(`${endpoint}/chat`, { name: newChatName }, axiosConfig); if (result.status === 200) { const chat_id = result.data.chat_id; console.log("Created new chat on Quivr ", result.data.chat_id); const resultChatQuestion = yield client.post(`${endpoint}/chat/${chat_id}/question?brain_id=${brain_id}`, chatParams, axiosConfig); if (resultChatQuestion.status === 200) { console.log("Result: ", resultChatQuestion.data.assistant); return (0, typechat_1.success)(resultChatQuestion.data.assistant); } else return (0, typechat_1.error)(`Quivr API Error on question ${resultChatQuestion.status}: ${resultChatQuestion.statusText}`); } return (0, typechat_1.error)(`Quivr API Error on creating chat ${result.status}: ${result.statusText}`); }); } }; exports.createQuivrLanguageModelExplicit = createQuivrLanguageModelExplicit;