Skip to content

Commit

Permalink
feat: allow to yield metadata in chat stream (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcalcedo authored Sep 20, 2024
1 parent a826d81 commit 2ba7af0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/useChatStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type UseChatStreamRole = 'bot' | 'user';
export type UseChatStreamChatMessage = {
role: UseChatStreamRole;
content: string;
metadata?: {};
id: string,
}

Expand All @@ -17,6 +18,7 @@ export type UseChatStreamOptions = {
headers?: HeadersInit;
body?: Record<string, string>;
fakeCharactersPerSecond?: number;
useMetadata?: boolean;
}

export type UseChatStreamEventHandlers = {
Expand Down

0 comments on commit 2ba7af0

Please # to comment.