Building chat bots: How to save a Conversation for later use? #437
-
I'm using Langchain::Conversation to build a chatbot. Since we need the conversation memory, I'm saving the class in the db and later using it to continue the Conversation, as shown below. chat = Langchain::Conversation.new(llm: llm)
chat.message("Hello")
# encode to save in the database
encoded_chat = Base64.encode64(Marshal.dump(chat))
# Retrieve for later use
chat = Marshal.load(Base64.decode64(encoded_chat))
chat.message("Tell me about future technologies") The problem here is that it breaks after updating the gem, which changes this class. A better solution would be to save not the encoded class but just the history in the DB, which can be passed to a new instance of Conversation whenever a new message comes in from the user. I don't see a way to set the conversation history. There is Curious to know how others handle such scenarios. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
@onerinas Take a look at the Assistants that we'll be rolling out soon. That PR will deprecate the When using Assistants, you'll be able to save the hashes. For example for OpenAI you'll be able to call something like: assistant.thread.messages.map &: to_openai_format We'll probably have similar methods for other LLMs, but basically you'll need to figure out how to best store the |
Beta Was this translation helpful? Give feedback.
-
The question mentions saving the messages to a db, so if it's a rails app is it best to do it yourself? Or does langchainrb_rails offer any anything? P.S. Just found |
Beta Was this translation helpful? Give feedback.
-
Looks like you are already enhanching the |
Beta Was this translation helpful? Give feedback.
-
@mattlindsey Did you get a chance to try it out? |
Beta Was this translation helpful? Give feedback.
@onerinas You'd instantiate a
Langchain::Thread
and pass the messages to it here: https://github.com/andreibondarev/langchainrb/pull/422/files#diff-6ec65bec0259e981b815da287e8340a8d0241372cd5efd973887db9786d8453eR8Keep in mind that the
thread.messages
also contain tool information and tool calls and outputs from tools. All of this will be needed to rebuild the full conversation history.Would that work for you?