mongodb-chatbot-server
Version:
A chatbot server for retrieval augmented generation (RAG).
157 lines (152 loc) • 7.48 kB
TypeScript
import { Request, RequestHandler, Response } from "express";
import { Options as RateLimitOptions } from "express-rate-limit";
import { Options as SlowDownOptions } from "express-slow-down";
import { ChatLlm, SystemPrompt, ConversationCustomData, ConversationsService } from "mongodb-rag-core";
import { AddMessageToConversationRouteParams } from "./addMessageToConversation";
import { ParamsDictionary } from "express-serve-static-core";
import { FilterPreviousMessages, GenerateUserPromptFunc } from "../../processors";
import { UpdateTraceFunc } from "./UpdateTraceFunc";
import { Logger } from "mongodb-rag-core/braintrust";
/**
Configuration for rate limiting on the /conversations/* routes.
*/
export interface ConversationsRateLimitConfig {
/**
Configuration for rate limiting on ALL /conversations/* routes.
*/
routerRateLimitConfig?: Partial<RateLimitOptions>;
/**
Configuration for rate limiting on the POST /conversations/:conversationId/messages route.
Since this is the most "expensive" route as it calls the LLM,
it could be more restrictive than the global rate limit.
*/
addMessageRateLimitConfig?: Partial<RateLimitOptions>;
/**
Configuration for slow down on ALL /conversations/* routes.
*/
routerSlowDownConfig?: Partial<SlowDownOptions>;
/**
Configuration for slow down on the POST /conversations/:conversationId/messages route.
Since this is the most "expensive" route as it calls the LLM,
it could be more restrictive than the global slow down.
*/
addMessageSlowDownConfig?: Partial<SlowDownOptions>;
}
/**
Function to add custom data to the {@link Conversation} persisted to the database.
Has access to the Express.js request and response plus the {@link ConversationsRouterLocals}
from the {@link Response.locals} object.
*/
export type AddCustomDataFunc = (request: Request, response: ConversationsRouterResponse) => Promise<ConversationCustomData>;
/**
Express.js Request that exposes the app's {@link ConversationsService}.
This is useful if you want to do authentication or dynamic data validation.
*/
export interface ConversationsRouterLocals {
conversations: ConversationsService;
customData: Record<string, unknown>;
}
/**
Express.js Response from the app's {@link ConversationsService}.
*/
export type ConversationsRouterResponse = Response<any, ConversationsRouterLocals>;
/**
Middleware to put in front of all the routes in the conversationsRouter.
This middleware is useful for things like authentication, data validation, etc.
It exposes the app's {@link ConversationsService}.
It also lets you access {@link ConversationsRouterLocals} via {@link Response.locals}
([docs](https://expressjs.com/en/api.html#res.locals)).
You can use the locals in other middleware or persist when you create the conversation
with the `POST /conversations` endpoint with the {@link AddCustomDataFunc}.
*/
export type ConversationsMiddleware = RequestHandler<ParamsDictionary, any, any, any, ConversationsRouterLocals>;
/**
Configuration for the /conversations/* routes.
*/
export interface ConversationsRouterParams {
llm: ChatLlm;
conversations: ConversationsService;
systemPrompt: SystemPrompt;
/**
Function to generate the user prompt sent to the {@link ChatLlm}.
You can perform any preprocessing of the user's message
including retrieval augmented generation here.
*/
generateUserPrompt?: GenerateUserPromptFunc;
/**
Maximum number of characters in user input.
Server returns 400 error if user input is longer than this.
*/
maxInputLengthCharacters?: number;
/**
Function to filter which previous messages are sent to the {@link ChatLlm}.
For example, you may only want to send the system prompt to the LLM
with the user message or the system prompt and X prior messages.
Defaults to sending only the system prompt.
*/
filterPreviousMessages?: FilterPreviousMessages;
/**
Maximum number of user-sent messages in a conversation.
Server returns 400 error if user tries to add a message to a conversation
that has this many messages.
*/
maxUserMessagesInConversation?: number;
rateLimitConfig?: ConversationsRateLimitConfig;
/**
Middleware to put in front of all the routes in the conversationsRouter.
You can use this to do things like authentication, data validation, etc.
If you want the middleware to run only on certain routes,
you can add conditional logic inside the middleware. For example:
```ts
const someMiddleware: ConversationsMiddleware = (req, res, next) => {
if (req.path === "/conversations") {
// Do something
}
next();
}
```
*/
middleware?: ConversationsMiddleware[];
/**
Function that takes the request + response and returns any custom data you want to include
in the {@link Conversation} persisted to the database.
For example, you might want to store the user's email address with the conversation.
The custom data is persisted to the database with the Conversation in the
{@link Conversation.customData} field.
*/
createConversationCustomData?: AddCustomDataFunc;
/**
Function that takes the request + response and returns any custom data you want to include
in the {@link Message} persisted to the database.
For example, you might want to store details about what LLM was used to generate the response.
The custom data is persisted to the database with the `Message` in the
{@link Message.customData} field inside of the {@link Conversation.messages} array.
*/
addMessageToConversationCustomData?: AddCustomDataFunc;
addMessageToConversationUpdateTrace?: AddMessageToConversationRouteParams["updateTrace"];
rateMessageUpdateTrace?: UpdateTraceFunc;
commentMessageUpdateTrace?: UpdateTraceFunc;
/**
Maximum number of characters allowed in a user's comment on an assistant {@link Message}.
If not specified, user comments may be of any length.
*/
maxUserCommentLength?: number;
/**
Whether to create a new conversation if the message ID is "null"
on the addMessageToConversation route.
@default true
*/
createConversationOnNullMessageId?: boolean;
braintrustLogger?: Logger<true>;
}
export declare const rateLimitResponse: {
error: string;
};
export type AddDefinedCustomDataFunc = (...args: Parameters<AddCustomDataFunc>) => Promise<Exclude<ConversationCustomData, undefined>>;
export declare const defaultCreateConversationCustomData: AddDefinedCustomDataFunc;
export declare const defaultAddMessageToConversationCustomData: AddDefinedCustomDataFunc;
/**
Constructor function to make the /conversations/* Express.js router.
*/
export declare function makeConversationsRouter({ llm, conversations, systemPrompt, maxInputLengthCharacters, maxUserMessagesInConversation, filterPreviousMessages, rateLimitConfig, generateUserPrompt, middleware, createConversationCustomData, addMessageToConversationCustomData, addMessageToConversationUpdateTrace, rateMessageUpdateTrace, commentMessageUpdateTrace, maxUserCommentLength, createConversationOnNullMessageId, braintrustLogger, }: ConversationsRouterParams): import("express").Router;
//# sourceMappingURL=conversationsRouter.d.ts.map