Skip to content
Snippets Groups Projects
Commit d758c87a authored by thucpn's avatar thucpn
Browse files

support callback

parent 2ebbb6f2
No related branches found
No related tags found
No related merge requests found
import type { Message } from "ai";
import express from "express";
import type { ChatMessage } from "llamaindex";
import { getUserMessageContent, pipeExpressResponse } from "./helper";
import { chatWithWorkflow } from "./workflow/stream";
import type { ServerWorkflow } from "./workflow/type";
......@@ -7,12 +8,16 @@ import type { ServerWorkflow } from "./workflow/type";
export interface LlamaIndexServerParams {
workflow: ServerWorkflow;
port?: number;
callbacks?: {
beforeChat?: (messages: ChatMessage[]) => void;
};
}
export class LlamaIndexServer {
app: express.Application;
workflow: ServerWorkflow;
port: number;
callbacks?: LlamaIndexServerParams["callbacks"];
constructor({ workflow, port = 3000 }: LlamaIndexServerParams) {
this.app = express();
......@@ -28,6 +33,7 @@ export class LlamaIndexServer {
try {
const { messages } = req.body as { messages: Message[] };
const userMessageContent = getUserMessageContent(messages);
this.callbacks?.beforeChat?.(messages as ChatMessage[]);
const streamResponse = await chatWithWorkflow(
userMessageContent,
this.workflow,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment