Skip to content
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

Handle chat response 'end' message #353

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 52 additions & 1 deletion components/Chat/Chat.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jest.mock("@/context/search-context", () => {
chat: {
answer: "",
documents: [],
end: "stop",
question: "",
},
searchFixed: false,
Expand All @@ -45,7 +46,7 @@ jest.mock("@/hooks/useChatSocket");
(useChatSocket as jest.Mock).mockImplementation(() => ({
authToken: "fake-token-1",
isConnected: false,
message: { answer: "fake-answer-1" },
message: { answer: "fake-answer-1", end: "stop" },
sendMessage: mockSendMessage,
}));

Expand Down Expand Up @@ -120,4 +121,54 @@ describe("Chat component", () => {

expect(mockSendMessage).not.toHaveBeenCalled();
});

it("displays an error message when the response hits the LLM token limit", () => {
(useChatSocket as jest.Mock).mockImplementation(() => ({
authToken: "fake",
isConnected: true,
message: {
end: {
reason: "length",
ref: "fake",
},
},
sendMessage: mockSendMessage,
}));

mockRouter.setCurrentUrl("/search?q=boats");

render(
<SearchProvider>
<Chat />
</SearchProvider>,
);

const error = screen.getByText("The response has hit the LLM token limit.");
expect(error).toBeInTheDocument();
});

it("displays an error message when the response times out", () => {
(useChatSocket as jest.Mock).mockImplementation(() => ({
authToken: "fake",
isConnected: true,
message: {
end: {
reason: "timeout",
ref: "fake",
},
},
sendMessage: mockSendMessage,
}));

mockRouter.setCurrentUrl("/search?q=boats");

render(
<SearchProvider>
<Chat />
</SearchProvider>,
);

const error = screen.getByText("The response has timed out.");
expect(error).toBeInTheDocument();
});
});
26 changes: 26 additions & 0 deletions components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useState } from "react";

import Announcement from "@/components/Shared/Announcement";
import { Button } from "@nulib/design-system";
import ChatFeedback from "@/components/Chat/Feedback/Feedback";
import ChatResponse from "@/components/Chat/Response/Response";
Expand All @@ -19,6 +20,8 @@ const Chat = ({ totalResults }: { totalResults?: number }) => {
const { chat } = searchState;
const { answer, documents = [], question } = chat;

const [streamingError, setStreamingError] = useState("");

const sameQuestionExists = !!question && searchTerm === question;

const [sourceDocuments, setSourceDocuments] = useState<Work[]>([]);
Expand Down Expand Up @@ -63,6 +66,22 @@ const Chat = ({ totalResults }: { totalResults?: number }) => {
return;
}

if (message.end) {
switch (message.end.reason) {
case "length":
setStreamingError("The response has hit the LLM token limit.");
break;
case "timeout":
setStreamingError("The response has timed out.");
break;
case "eos_token":
setStreamingError("This should never happen.");
Copy link
Member

Choose a reason for hiding this comment

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

🤣

break;
default:
break;
}
}

if (message.answer) {
updateChat();
}
Expand Down Expand Up @@ -94,6 +113,13 @@ const Chat = ({ totalResults }: { totalResults?: number }) => {
sourceDocuments={sameQuestionExists ? documents : sourceDocuments}
streamedAnswer={sameQuestionExists ? answer : streamedAnswer}
/>
{streamingError && (
<Container>
<Announcement css={{ marginTop: "1rem" }}>
{streamingError}
</Announcement>
</Container>
)}
Comment on lines +116 to +122
Copy link
Member

Choose a reason for hiding this comment

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

Nice 🫰

{answer && (
<>
<Container>
Expand Down
4 changes: 4 additions & 0 deletions types/components/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export type Answer = {

export type StreamingMessage = {
answer?: string;
end?: {
reason: "stop" | "length" | "timeout" | "eos_token";
ref: string;
};
question?: string;
ref: string;
source_documents?: Array<Work>;
Expand Down