diff options
author | A.J. Shulman <Shulman.aj@gmail.com> | 2024-08-16 15:45:23 -0400 |
---|---|---|
committer | A.J. Shulman <Shulman.aj@gmail.com> | 2024-08-16 15:45:23 -0400 |
commit | daa72b906e3364c2b6a836533fc1980bb63ba303 (patch) | |
tree | da6b0d25f7ff547e460832b1de823cd2a909ac85 /src/client/views/nodes/ChatBox/ChatBox.tsx | |
parent | d97405e0a172b03a759452a1e9a7291974d89248 (diff) |
now shows thoughts in real time
next steps:
integrate everything with the AnswerParser
make sure citations work perfectly (right now clicking citations isn't perfect for urls and multiple citations for the same url source are generated—check examples for mistakes)
Diffstat (limited to 'src/client/views/nodes/ChatBox/ChatBox.tsx')
-rw-r--r-- | src/client/views/nodes/ChatBox/ChatBox.tsx | 62 |
1 files changed, 48 insertions, 14 deletions
diff --git a/src/client/views/nodes/ChatBox/ChatBox.tsx b/src/client/views/nodes/ChatBox/ChatBox.tsx index 8d09cde1e..d38c71810 100644 --- a/src/client/views/nodes/ChatBox/ChatBox.tsx +++ b/src/client/views/nodes/ChatBox/ChatBox.tsx @@ -149,26 +149,50 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { if (trimmedText) { try { + //Make everything go through the answer parser + //Pass in all the updates to the AnswrParser and it will create the assistant messasge that will be the current message including adding in the thoughts and also waiting for the asnwer and also showing tool progress textInput.value = ''; - runInAction(() => { - this.history.push({ role: ASSISTANT_ROLE.USER, content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: trimmedText, citation_ids: null }] }); - this.isLoading = true; - }); + this.history.push({ role: ASSISTANT_ROLE.USER, content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: trimmedText, citation_ids: null }] }); + this.isLoading = true; + this.current_message = { role: ASSISTANT_ROLE.ASSISTANT, content: [], thoughts: [] }; + + let currentThought = ''; + + this.current_message?.thoughts?.push(currentThought); + + const onUpdate = (update: string) => { + const thoughtNumber = Number(update.match(/^THOUGHT(\d+):/)?.[1] ?? 0); + const regex = /<thought>\s*([\s\S]*?)(?:<\/thought>|$)/; + const match = update.match(regex); + const currentThought = match ? match[1].trim() : ''; + //const numericPrefix = Number(update.match(/^\d+/)?.[0]); + if (update.startsWith('THOUGHT')) { + console.log('Thought:', currentThought, thoughtNumber); + if (this.current_message?.thoughts) { + if (this.current_message.thoughts.length <= thoughtNumber) { + this.current_message.thoughts.push(currentThought); + } else { + this.current_message.thoughts[thoughtNumber] = currentThought; + } + } + console.log('Thoughts:', this.current_message?.thoughts); + } + }; - const response = await this.agent.askAgent(trimmedText); // Use the chatbot to get the response - runInAction(() => { - this.history.push(AnswerParser.parse(response)); - }); + const response = await this.agent.askAgent(trimmedText, 20, onUpdate); + const parsedAnswer = AnswerParser.parse(response); + parsedAnswer.thoughts = this.current_message?.thoughts; + + if (this.current_message) { + this.history.push(parsedAnswer); + this.current_message = undefined; + } this.dataDoc.data = JSON.stringify(this.history); } catch (err) { console.error('Error:', err); - runInAction(() => { - this.history.push({ role: ASSISTANT_ROLE.ASSISTANT, content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: 'Sorry, I encountered an error while processing your request.', citation_ids: null }] }); - }); + this.history.push({ role: ASSISTANT_ROLE.ASSISTANT, content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: 'Sorry, I encountered an error while processing your request.', citation_ids: null }] }); } finally { - runInAction(() => { - this.isLoading = false; - }); + this.isLoading = false; } } }; @@ -416,6 +440,16 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { {this.history.map((message, index) => ( <MessageComponentBox key={index} message={message} index={index} onFollowUpClick={this.handleFollowUpClick} onCitationClick={this.handleCitationClick} updateMessageCitations={this.updateMessageCitations} /> ))} + {!this.current_message ? null : ( + <MessageComponentBox + key={this.history.length} + message={this.current_message} + index={this.history.length} + onFollowUpClick={this.handleFollowUpClick} + onCitationClick={this.handleCitationClick} + updateMessageCitations={this.updateMessageCitations} + /> + )} </div> </div> <form onSubmit={this.askGPT} className="chat-form"> |