diff --git a/src/components/message/message.tsx b/src/components/message/message.tsx index c900b7b..317b026 100644 --- a/src/components/message/message.tsx +++ b/src/components/message/message.tsx @@ -7,7 +7,12 @@ import { USER_REFERENCE_NAME } from "@/config/ui-config"; import { separateLinksFromApiMessage } from "@/utils/links"; import MarkdownWrapper from "./markdownWrapper/markdownWrapper"; -type MessageType = "userMessage" | "authorMessage" | "apiMessage" | "errorMessage" | "apiStream"; +type MessageType = + | "userMessage" + | "authorMessage" + | "apiMessage" + | "errorMessage" + | "apiStream"; export interface Message { message: string; type: MessageType; @@ -67,23 +72,37 @@ const MessageBox = ({ return ( - + {type === "userMessage" ? USER_REFERENCE_NAME : author} {loading ? ( - + ) : streamLoading ? ( - + ) : ( - + )} ); @@ -94,7 +113,12 @@ export default MessageBox; const ClickableLink = ({ linkString }: { linkString: string }) => { let url = linkString.split(" ")[1]?.trim(); return ( - + {linkString}{" "} @@ -103,112 +127,96 @@ const ClickableLink = ({ linkString }: { linkString: string }) => { ); }; -const ClickableQuestions = ({ question, handleFollowUpQuestion }: { question: string; handleFollowUpQuestion: (question: string) => void }) => { +const ClickableQuestions = ({ + question, + handleFollowUpQuestion, +}: { + question: string; + handleFollowUpQuestion: (question: string) => void; +}) => { return ( ); }; -const MessageContent = ({ message, type, handleFollowUpQuestion }: Message & { handleFollowUpQuestion: (question: string) => void }) => { +const MessageContent = ({ + message, + type, + handleFollowUpQuestion, +}: Message & { handleFollowUpQuestion: (question: string) => void }) => { if (!message?.trim()) return null; - const { messageBody, messageLinks, messageQuestions } = separateLinksFromApiMessage(message); + const { messageBody, messageLinks, messageQuestions, isErrorMessage } = + separateLinksFromApiMessage(message); - if (!messageBody.trim()) return null; + if (!messageBody?.trim()) return null; return ( <> - + - {Boolean(messageLinks.length) && ( + {isErrorMessage ? null : ( {Boolean(messageQuestions.length) && ( - - + + Follow up questions - = 2 ? `scroll` : "hidden"} gap='24px'> + = 2 ? `scroll` : "hidden"} + gap="24px" + > {messageQuestions.map((question, idx) => ( handleFollowUpQuestion(question.trim())} + handleFollowUpQuestion={() => { + if (type === "apiMessage") { + handleFollowUpQuestion(question.trim()); + } + }} /> ))} )} - - Sources - - {messageLinks.map((link, idx) => ( -
- -
- ))} + {Boolean(messageLinks.length) && ( + <> + + Sources + + {messageLinks.map((link, idx) => ( +
+ +
+ ))} + + )}
)} ); }; - -const StreamMessageContent = ({ message, type, handleFollowUpQuestion }: Message & { handleFollowUpQuestion: (question: string) => void }) => { - const { messageBody, messageLinks, messageQuestions } = separateLinksFromApiMessage(message); - - return ( - <> - - - - - {messageQuestions.length ? ( - - Follow up questions - - ) : null} - - {messageQuestions.map((question, idx) => ( - handleFollowUpQuestion(question.trim())} - /> - ))} - - - - {messageLinks.length ? ( - - Sources - - ) : null} - {messageLinks.map((link, idx) => ( -
- -
- ))} -
- - ); -}; diff --git a/src/config/chatAPI-config.ts b/src/config/chatAPI-config.ts index b465d8f..11b2b76 100644 --- a/src/config/chatAPI-config.ts +++ b/src/config/chatAPI-config.ts @@ -11,7 +11,7 @@ You are a helpful assistant. You are given a list of user questions, the questio export const guidelines = { BASE_INSTRUCTION: "You are an AI assistant providing helpful answers. You are given the following extracted parts of a long document called CONTEXT BLOCK and a conversation. Provide a conversational detailed answer in the same writing style as based on the context provided. DO NOT include any external references or links in the answers.", - NO_ANSWER: `If you are absolutely certain that the answer cannot be found in the CONTEXT BLOCK, just say this phrase '${ERROR_MESSAGES.NO_ANSWER_WITH_LINKS}' EXACTLY. DO NOT try to make up an answer that is not in the CONTEXT BLOCK.`, + NO_ANSWER: `If you are absolutely certain that the answer cannot be found in the CONTEXT BLOCK, just say this phrase '${ERROR_MESSAGES.NO_ANSWER}' EXACTLY. DO NOT try to make up an answer that is not in the CONTEXT BLOCK.`, UNRELATED_QUESTION: `If the question is not related to the context, say this phrase EXACTLY '${ERROR_MESSAGES.NO_ANSWER}'`, LINKING: `DO NOT explicity mention the existence of the context provided, however, references can and should be made to the links provided in the context e.g '[0]'.`, FOLLOW_UP_QUESTIONS: 'If you have an answer, generate four relevant follow up questions, do not include introductory text about follow-up questions. Each question must be in this format: `--{{ what problems did segwit solve }}--` in a new line.', diff --git a/src/utils/error.ts b/src/utils/error.ts new file mode 100644 index 0000000..9f4bb27 --- /dev/null +++ b/src/utils/error.ts @@ -0,0 +1,6 @@ +import { getAllErrorMessages } from "@/config/error-config"; + +export const messageIsErrorMessage = (message: string) => { + const errorMessages = getAllErrorMessages(); + return errorMessages.includes(message.trim()); +}; \ No newline at end of file diff --git a/src/utils/links.ts b/src/utils/links.ts index 4c37513..2589564 100644 --- a/src/utils/links.ts +++ b/src/utils/links.ts @@ -1,3 +1,5 @@ +import { messageIsErrorMessage } from "./error"; + const linksRegex = /(^\[\d+\]:\s.*)/gm; const questionRegex = /--\{\{([^}]+)\}\}--/g; @@ -10,9 +12,9 @@ export const separateLinksFromApiMessage = (message: string) => { const body = body_and_questions_chunks[0] const questions = body_and_questions_chunks.slice(1).map(question => question.trim()) ?? [] - const messageBody = body; const messageQuestions = questions; const messageLinks = links; + const isErrorMessage = messageIsErrorMessage(body ?? "") - return { messageBody, messageLinks, messageQuestions }; + return { messageBody: body, messageLinks, messageQuestions, isErrorMessage }; }; diff --git a/src/utils/openaiChat.ts b/src/utils/openaiChat.ts index 1091139..8ea332d 100644 --- a/src/utils/openaiChat.ts +++ b/src/utils/openaiChat.ts @@ -138,7 +138,7 @@ const generateContextBlock = (summaries: SummaryData[]): string => { let text = "" try { if (data === "[DONE]") { - text = getFinalAnswer("", link).data + text = formatLinksToSourcesList(link) const queue = encoder.encode(text); controller.enqueue(queue); controller.close() @@ -185,17 +185,16 @@ function removeDuplicatesByID(arr: CustomContent[]): CustomContent[] { return filteredArr; } -function getFinalAnswer( - summary: string, +function formatLinksToSourcesList( content: SummaryData[] ) { - let data = summary.trim() + "\n\n"; + let sourcesListString = "\n\n"; content.forEach((d: SummaryData, i: number) => { - data += `[${i}]: ${d.link}\n`; + sourcesListString += `[${i}]: ${d.link}\n`; }); - return { data }; + return sourcesListString; } export async function processInput(