Abhishek
Abhishek•10mo ago

Use Query needs additional functions

I need help with this, how do we know the use query value is not changing anymore? Maybe an additional hook with useQuery so an event is fired when there are no changes. Currently, I am streaming the response storing it in DB and using query to show on the frontend I want to render a button when a response is complete so for this case I am not able to figure out how can we handle this.
4 Replies
ballingt
ballingt•10mo ago
In general the result of a useQuery call always might change again in the future. You could detect that it hasn't changed so far for say ten seconds though. But since you control the result of your query you could return a different value when you know the query has stopped changing: return whether the result is finished, like
{ complete: false, data: "This is the resp" }
{ complete: true, data: "This is the response." }
{ complete: false, data: "This is the resp" }
{ complete: true, data: "This is the response." }
Abhishek
AbhishekOP•10mo ago
@ballingt result is streaming from LLM so even I don't know when it will be complete so I am not sure how to implement the second approach. That's why I thought It would be nice if useQuery could give additional helper callback functions
Michal Srb
Michal Srb•10mo ago
@Abhishek assuming your code looks something like mine here: https://stack.convex.dev/ai-chat-with-convex-vector-search#serving-traffic-answering-a-question After the for loop you know the response is done, and you can mutate the document again to mark it as such.
let text = "";
for await (const { choices } of stream) {
const chunk = choices[0].delta.content;
if (typeof chunk === "string" && chunk.length > 0) {
text += choices[0].delta.content;
await ctx.runMutation(internal.serve.updateBotMessage, {
messageId,
text,
done: false,
});
}
}
await ctx.runMutation(internal.serve.updateBotMessage, {
messageId,
text,
done: true,
});
let text = "";
for await (const { choices } of stream) {
const chunk = choices[0].delta.content;
if (typeof chunk === "string" && chunk.length > 0) {
text += choices[0].delta.content;
await ctx.runMutation(internal.serve.updateBotMessage, {
messageId,
text,
done: false,
});
}
}
await ctx.runMutation(internal.serve.updateBotMessage, {
messageId,
text,
done: true,
});
Or use a while loop instead of a for loop, then you can probably detect that the stream is over along with the last chunk (to avoid 2 writes at the end).
Build AI Chat with Convex Vector Search
Convex is a full-stack development platform and cloud database, including built-in vector search. In this third post in our [series](https://stack.con...
Abhishek
AbhishekOP•10mo ago
Thanks @Michal Srb this is what I was looking for 🤗

Did you find this page helpful?