Skip to content

Commit

Permalink
Fix rendering online with note references post streaming chat response
Browse files Browse the repository at this point in the history
Previously only the notes references would get rendered post response
streaming when when both online and notes references were used to
respond to the user's message
  • Loading branch information
debanjum committed Mar 13, 2024
1 parent 1aeea3d commit a1ce122
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
23 changes: 14 additions & 9 deletions src/interface/desktop/chat.html
Expand Up @@ -357,15 +357,16 @@

let numReferences = 0;

if (Array.isArray(references)) {
numReferences = references.length;
if (references.hasOwnProperty("notes")) {
numReferences += references["notes"].length;

references.forEach((reference, index) => {
references["notes"].forEach((reference, index) => {
let polishedReference = generateReference(reference, index);
referenceSection.appendChild(polishedReference);
});
} else {
numReferences += processOnlineReferences(referenceSection, references);
}
if (references.hasOwnProperty("online")){
numReferences += processOnlineReferences(referenceSection, references["online"]);
}

let referenceExpandButton = document.createElement('button');
Expand Down Expand Up @@ -511,16 +512,16 @@
// Handle streamed response of type text/event-stream or text/plain
const reader = response.body.getReader();
const decoder = new TextDecoder();
let references = null;
let references = {};

readStream();

function readStream() {
reader.read().then(({ done, value }) => {
if (done) {
// Append any references after all the data has been streamed
if (references != null) {
newResponseText.appendChild(references);
if (references != {}) {
newResponseText.appendChild(createReferenceSection(references));
}
document.getElementById("chat-body").scrollTop = document.getElementById("chat-body").scrollHeight;
document.getElementById("chat-input").removeAttribute("disabled");
Expand All @@ -538,7 +539,11 @@

const rawReference = chunk.split("### compiled references:")[1];
const rawReferenceAsJson = JSON.parse(rawReference);
references = createReferenceSection(rawReferenceAsJson);
if (rawReferenceAsJson instanceof Array) {
references["notes"] = rawReferenceAsJson;
} else if (typeof rawReferenceAsJson === "object" && rawReferenceAsJson !== null) {
references["online"] = rawReferenceAsJson;
}
readStream();
} else {
// Display response from Khoj
Expand Down
23 changes: 14 additions & 9 deletions src/khoj/interface/web/chat.html
Expand Up @@ -368,15 +368,16 @@

let numReferences = 0;

if (Array.isArray(references)) {
numReferences = references.length;
if (references.hasOwnProperty("notes")) {
numReferences += references["notes"].length;

references.forEach((reference, index) => {
references["notes"].forEach((reference, index) => {
let polishedReference = generateReference(reference, index);
referenceSection.appendChild(polishedReference);
});
} else {
numReferences += processOnlineReferences(referenceSection, references);
}
if (references.hasOwnProperty("online")) {
numReferences += processOnlineReferences(referenceSection, references["online"]);
}

let referenceExpandButton = document.createElement('button');
Expand Down Expand Up @@ -518,16 +519,16 @@
// Handle streamed response of type text/event-stream or text/plain
const reader = response.body.getReader();
const decoder = new TextDecoder();
let references = null;
let references = {};

readStream();

function readStream() {
reader.read().then(({ done, value }) => {
if (done) {
// Append any references after all the data has been streamed
if (references != null) {
newResponseText.appendChild(references);
if (references != {}) {
newResponseText.appendChild(createReferenceSection(references));
}
document.getElementById("chat-body").scrollTop = document.getElementById("chat-body").scrollHeight;
document.getElementById("chat-input").removeAttribute("disabled");
Expand All @@ -545,7 +546,11 @@

const rawReference = chunk.split("### compiled references:")[1];
const rawReferenceAsJson = JSON.parse(rawReference);
references = createReferenceSection(rawReferenceAsJson);
if (rawReferenceAsJson instanceof Array) {
references["notes"] = rawReferenceAsJson;
} else if (typeof rawReferenceAsJson === "object" && rawReferenceAsJson !== null) {
references["online"] = rawReferenceAsJson;
}
readStream();
} else {
// Display response from Khoj
Expand Down
2 changes: 1 addition & 1 deletion src/khoj/processor/conversation/utils.py
Expand Up @@ -64,7 +64,7 @@ def send(self, data):
def close(self):
if self.compiled_references and len(self.compiled_references) > 0:
self.queue.put(f"### compiled references:{json.dumps(self.compiled_references)}")
elif self.online_results and len(self.online_results) > 0:
if self.online_results and len(self.online_results) > 0:
self.queue.put(f"### compiled references:{json.dumps(self.online_results)}")
self.queue.put(StopIteration)

Expand Down

0 comments on commit a1ce122

Please sign in to comment.