Skip to content
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

Open
joetifa2003 opened this issue Aug 10, 2024 · 2 comments
Open

Comments

@joetifa2003
Copy link

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
Copy link
Author

joetifa2003 commented Aug 10, 2024

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

@ogzhanolguncu
Copy link
Contributor

ogzhanolguncu commented Aug 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants