rag-chat icon indicating copy to clipboard operation
rag-chat copied to clipboard

How to include the context used to generate the message in history?

Open joetifa2003 opened this issue 1 year ago • 2 comments

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.

joetifa2003 avatar Aug 10 '24 17:08 joetifa2003

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
})

joetifa2003 avatar Aug 10 '24 17:08 joetifa2003

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.

This is how it looks on our console UI feel free to check. image

ogzhanolguncu avatar Aug 11 '24 13:08 ogzhanolguncu