Persist Conversation History
winxalex opened this issue · 1 comments
I've done web api fast api/uvcorn, and have dictionary that keep ChatSession by cid. This way I can call web api with cid and continue chat session. But if I restart server I can't restore the chat session. If I create new ChatSession with cid (i've written down on paper for test),
print("create new chat_session with conversation id")
# create new chat_session with conversation id
cookies=extract_bard_cookie(True, browser_fn=browser_cookie3.brave)
Secure_1PSID = cookies["__Secure-1PSID"]
Secure_1PSIDTS =cookies["__Secure-1PSIDTS"]
client = GeminiClient(Secure_1PSID, Secure_1PSIDTS, proxy=None)
await client.init(timeout=30)
chat_session=client.start_chat()
chat_session.cid=conversation_id
gemini_chat_session_dict[conversation_id]=chat_session
This will start new session on same cid (I can see that is browser), all previous history will be erased. Please advice is it possible to have similar functionality as in browser, so I can later continue with the session. Don't know if would work if I serialised pickle the chat session and unpickle later. I think also SNlM0e or access token should be restored and kept for the session
To retrieve conversation history, you must provide all 3 values of cid
, rid
and rcid
. They are stored in ChatSession.metadata
and will be automatically updated every time after ChatSession.send_message()
is called.
Try the following code instead:
# Restore chat session from your record
chat_session=client.start_chat(metadata=gemini_chat_session_dict[conversation_id])
# Metadata will be updated after sending message
await chat_session.send_message("...")
# Update the stored metadata
gemini_chat_session_dict[conversation_id]=chat_session.metadata