Skip to content

Commit

Permalink
feat: more uniform output
Browse files Browse the repository at this point in the history
  • Loading branch information
sunls24 committed Dec 11, 2024
1 parent f22c637 commit 7004d6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
32 changes: 30 additions & 2 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import { tools } from "@/app/api/chat/tools";
import { NextResponse } from "next/server";
import { ERROR_PREFIX, MODE_TRANSLATE } from "@/lib/constants";

const STREAM_INTERVAL = 60;
const MAX_SIZE = 6;

export async function POST(req: Request) {
const { messages, config } = await req.json();

try {
const controller = new AbortController();
const { fullStream } = streamText({
abortSignal: controller.signal,
temperature: config.temperature,
model: getOpenAI(config.apiKey).chat(config.model),
system: getSystem(config.systemPrompt),
Expand All @@ -21,12 +26,31 @@ export async function POST(req: Request) {
),
});

let intervalId: any;
const stream = new ReadableStream({
async start(controller) {
let buffer = "";
let done = false;
intervalId = setInterval(() => {
if (done && buffer.length === 0) {
clearInterval(intervalId);
controller.close();
return;
}
if (buffer.length <= MAX_SIZE) {
controller.enqueue(buffer);
buffer = "";
} else {
const chunk = buffer.slice(0, MAX_SIZE);
buffer = buffer.slice(MAX_SIZE);
controller.enqueue(chunk);
}
}, STREAM_INTERVAL);

for await (const part of fullStream) {
switch (part.type) {
case "text-delta": {
controller.enqueue(part.textDelta);
buffer += part.textDelta;
break;
}
case "error": {
Expand All @@ -38,7 +62,11 @@ export async function POST(req: Request) {
}
}
}
controller.close();
done = true;
},
cancel() {
clearInterval(intervalId);
controller.abort();
},
});

Expand Down
1 change: 0 additions & 1 deletion components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function Chat() {
reload,
stop,
} = useChat({
experimental_throttle: 80,
streamProtocol: "text",
onError(err) {
toast.error(err.message);
Expand Down

0 comments on commit 7004d6d

Please sign in to comment.