Skip to content

Commit

Permalink
feat: update route and stream for express template
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn committed Mar 14, 2024
1 parent 2f07cc9 commit 4a96da5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { streamToResponse } from "ai";
import { Request, Response } from "express";
import { ChatMessage, MessageContent, OpenAI } from "llamaindex";
import { createChatEngine } from "./engine";
import { createChatEngine } from "./engine/chat";
import { LlamaIndexStream } from "./llamaindex-stream";

const convertMessageContent = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
trimStartOfStreamHelper,
type AIStreamCallbacksAndOptions,
} from "ai";
import { Response } from "llamaindex";
import { Response, StreamingAgentChatResponse } from "llamaindex";

type ParserOptions = {
image_url?: string;
Expand Down Expand Up @@ -52,13 +52,14 @@ function createParser(
}

export function LlamaIndexStream(
res: AsyncIterable<Response>,
response: StreamingAgentChatResponse | AsyncIterable<Response>,
opts?: {
callbacks?: AIStreamCallbacksAndOptions;
parserOptions?: ParserOptions;
},
): { stream: ReadableStream; data: experimental_StreamData } {
const data = new experimental_StreamData();
const res = response instanceof StreamingAgentChatResponse ? response.response : response;
return {
stream: createParser(res, data, opts?.parserOptions)
.pipeThrough(createCallbacksTransformer(opts?.callbacks))
Expand Down

0 comments on commit 4a96da5

Please sign in to comment.