Skip to content

Commit

Permalink
Debug: llm streaming I/O (default: off)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Oct 24, 2023
1 parent 9a80b88 commit 622bde0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/modules/llms/transports/server/openai.streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { anthropicAccess, anthropicAccessSchema, anthropicChatCompletionPayload
import { openAIAccess, openAIAccessSchema, openAIChatCompletionPayload, openAIHistorySchema, openAIModelSchema } from './openai.router';


// enable to print to console the outgoing and incoming data
const DEBUG_WIRE = false;


/**
* Vendor stream parsers
* - The vendor can decide to terminate the connection (close: true), transmitting anything in 'text' before doing so
Expand Down Expand Up @@ -99,9 +103,20 @@ function createEventStreamTransformer(vendorTextParser: AIStreamParser): Transfo

return new TransformStream({
start: async (controller): Promise<void> => {

// only used for debugging
let debugLastMs: number | null = null;

eventSourceParser = createEventsourceParser(
(event: ParsedEvent | ReconnectInterval) => {

if (DEBUG_WIRE) {
const nowMs = Date.now();
const elapsedMs = debugLastMs ? nowMs - debugLastMs : 0;
debugLastMs = nowMs;
console.warn(`<- SSE (${elapsedMs} ms):`, event);
}

// ignore 'reconnect-interval' and events with no data
if (event.type !== 'event' || !('data' in event))
return;
Expand Down Expand Up @@ -185,6 +200,8 @@ export async function openaiStreamingResponse(req: NextRequest): Promise<Respons
}

// POST to our API route
if (DEBUG_WIRE)
console.warn('-> POST', headersUrl.url, body);
upstreamResponse = await fetch(headersUrl.url, {
method: 'POST',
headers: headersUrl.headers,
Expand Down

1 comment on commit 622bde0

@vercel
Copy link

@vercel vercel bot commented on 622bde0 Oct 24, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

big-agi – ./

big-agi-git-main-enricoros.vercel.app
big-agi-enricoros.vercel.app
get.big-agi.com

Please sign in to comment.