-
Notifications
You must be signed in to change notification settings - Fork 46
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
How to include the context used to generate the message in history? #42
Comments
When fetching from history, all metadata fields are undefined const res = await ragChat.history.getMessages({ sessionId: pb.authStore.model?.id }); But here when I create a new message, metadata is populated with the context used from the vector store. const res = await ragChat.chat(text, {
// options
}) |
This is what I did to solve that exact problem on our console RAG Chat UI. async function handleChatResponse(ragChat: RAGChat, question: string, payload: MessagePayload) {
//SOME CODE HERE
let messages: UpstashMessage[] = []
let context: PrepareChatResult = []
const response = await ragChat.chat(question, {
onChatHistoryFetched(_messages) {
messages = _messages
this.metadata = {
...this.metadata,
//@ts-expect-error hacky way to set metadata without waiting chat to finish
usedHistory: JSON.stringify(
messages.map((message) => {
delete message.metadata?.usedHistory
delete message.metadata?.usedContext
return message
})
),
}
return _messages
},
onContextFetched(_context) {
context = _context
//@ts-expect-error hacky way to set metadata without waiting chat to finish
this.metadata = { ...this.metadata, usedContext: context.map((x) => x.data.replace("-", "")) }
return _context
},
streaming: true,
sessionId,
historyLength: 5,
ratelimitSessionId: userEmail,
topK,
similarityThreshold,
})
return aiUseChatAdapter(response, {
messages: JSON.stringify(
messages
.map((message) => {
delete message.metadata?.usedHistory
delete message.metadata?.usedContext
return message
})
.reverse()
),
context: context.map((x) => x.data),
})
} This is just one of the way I came up with to solve this quickly. This, overrides your inner metadata via this.metadata so in your following calls you can access them through our hooks. |
I can get the context from the chat method's return value.
But when fetching from the history, I lose that information.
I want to get that in the history for me to parse and include the sources at the bottom of the message.
The text was updated successfully, but these errors were encountered: