Skip to content

feat: implement csv upload #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions helpers/env-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ const getEngineEnvs = (): EnvVar[] => {
"The number of similar embeddings to return when retrieving documents.",
value: "3",
},
{
name: "STREAM_TIMEOUT",
description:
"The time in milliseconds to wait for the stream to return a response.",
value: "60000",
},
];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {
LlamaIndexStream,
convertMessageContent,
} from "./llamaindex-stream";
import { createCallbackManager } from "./stream-helper";
import { createCallbackManager, createStreamTimeout } from "./stream-helper";

export const chat = async (req: Request, res: Response) => {
// Init Vercel AI StreamData and timeout
const vercelStreamData = new StreamData();
const streamTimeout = createStreamTimeout(vercelStreamData);
try {
const {
messages,
Expand All @@ -28,9 +31,6 @@ export const chat = async (req: Request, res: Response) => {
// Convert message content from Vercel/AI format to LlamaIndex/OpenAI format
const userMessageContent = convertMessageContent(userMessage.content, data);

// Init Vercel AI StreamData
const vercelStreamData = new StreamData();

// Setup callbacks
const callbackManager = createCallbackManager(vercelStreamData);

Expand All @@ -57,5 +57,7 @@ export const chat = async (req: Request, res: Response) => {
return res.status(500).json({
detail: (error as Error).message,
});
} finally {
clearTimeout(streamTimeout);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ export function appendToolData(
});
}

export function createStreamTimeout(stream: StreamData) {
const timeout = Number(process.env.STREAM_TIMEOUT ?? 1000 * 60 * 5); // default to 5 minutes
const t = setTimeout(() => {
appendEventData(stream, `Stream timed out after ${timeout / 1000} seconds`);
stream.close();
}, timeout);
return t;
}
Comment on lines +85 to +92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define the default timeout value as a constant for better readability and maintainability.

+ const DEFAULT_TIMEOUT = 1000 * 60 * 5; // 5 minutes
  const timeout = Number(process.env.STREAM_TIMEOUT ?? DEFAULT_TIMEOUT);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export function createStreamTimeout(stream: StreamData) {
const timeout = Number(process.env.STREAM_TIMEOUT ?? 1000 * 60 * 5); // default to 5 minutes
const t = setTimeout(() => {
appendEventData(stream, `Stream timed out after ${timeout / 1000} seconds`);
stream.close();
}, timeout);
return t;
}
export function createStreamTimeout(stream: StreamData) {
const DEFAULT_TIMEOUT = 1000 * 60 * 5; // 5 minutes
const timeout = Number(process.env.STREAM_TIMEOUT ?? DEFAULT_TIMEOUT);
const t = setTimeout(() => {
appendEventData(stream, `Stream timed out after ${timeout / 1000} seconds`);
stream.close();
}, timeout);
return t;
}


export function createCallbackManager(stream: StreamData) {
const callbackManager = new CallbackManager();

Expand Down
11 changes: 7 additions & 4 deletions templates/types/streaming/nextjs/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
LlamaIndexStream,
convertMessageContent,
} from "./llamaindex-stream";
import { createCallbackManager } from "./stream-helper";
import { createCallbackManager, createStreamTimeout } from "./stream-helper";

initObservability();
initSettings();
Expand All @@ -18,6 +18,10 @@ export const runtime = "nodejs";
export const dynamic = "force-dynamic";

export async function POST(request: NextRequest) {
// Init Vercel AI StreamData and timeout
const vercelStreamData = new StreamData();
const streamTimeout = createStreamTimeout(vercelStreamData);

try {
const body = await request.json();
const {
Expand All @@ -40,9 +44,6 @@ export async function POST(request: NextRequest) {
// Convert message content from Vercel/AI format to LlamaIndex/OpenAI format
const userMessageContent = convertMessageContent(userMessage.content, data);

// Init Vercel AI StreamData
const vercelStreamData = new StreamData();

// Setup callbacks
const callbackManager = createCallbackManager(vercelStreamData);

Expand Down Expand Up @@ -75,5 +76,7 @@ export async function POST(request: NextRequest) {
status: 500,
},
);
} finally {
clearTimeout(streamTimeout);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ export function appendToolData(
});
}

export function createStreamTimeout(stream: StreamData) {
const timeout = Number(process.env.STREAM_TIMEOUT ?? 1000 * 60 * 5); // default to 5 minutes
const t = setTimeout(() => {
appendEventData(stream, `Stream timed out after ${timeout / 1000} seconds`);
stream.close();
}, timeout);
return t;
}

export function createCallbackManager(stream: StreamData) {
const callbackManager = new CallbackManager();

Expand Down
Loading