From 2ba7af01b189358042844dd6077e564799157942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Calcedo?= <49457798+gcalcedo@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:48:36 +0200 Subject: [PATCH] feat: allow to yield metadata in chat stream (#43) --- package.json | 2 +- src/hooks/useChatStream.ts | 8 ++++++++ src/types.ts | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 514b1d7..1f3fd93 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@magicul/react-chat-stream", "description": "A React hook that lets you easily integrate your custom ChatGPT-like chat in React.", - "version": "0.4.0", + "version": "0.5.0", "main": "dist/index.js", "types": "dist/index.d.ts", "homepage": "https://github.com/XD2Sketch/react-chat-stream#readme", diff --git a/src/hooks/useChatStream.ts b/src/hooks/useChatStream.ts index 5e85936..9fb66f6 100644 --- a/src/hooks/useChatStream.ts +++ b/src/hooks/useChatStream.ts @@ -41,6 +41,7 @@ const useChatStream = (input: UseChatStreamInput) => { const stream = await getStream(message, input.options, input.method); const initialMessage = addMessage({ content: '', role: 'bot' }); let response = ''; + let metadata = {}; for await (const chunk of decodeStreamToJson(stream)) { if (!charactersPerSecond) { @@ -49,6 +50,13 @@ const useChatStream = (input: UseChatStreamInput) => { continue; } + if (input.options.useMetadata) { + try { + metadata = JSON.parse(chunk.trim()); + return { ...initialMessage, content: response, metadata: metadata }; + } catch {} + } + // Stream characters one by one based on the characters per second that is set. for (const char of chunk) { appendMessageToChat(char); diff --git a/src/types.ts b/src/types.ts index 25f5ea8..f36a1b5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,6 +7,7 @@ export type UseChatStreamRole = 'bot' | 'user'; export type UseChatStreamChatMessage = { role: UseChatStreamRole; content: string; + metadata?: {}; id: string, } @@ -17,6 +18,7 @@ export type UseChatStreamOptions = { headers?: HeadersInit; body?: Record; fakeCharactersPerSecond?: number; + useMetadata?: boolean; } export type UseChatStreamEventHandlers = {