chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

The ability to get Thread Id when a new thread starts.

Open itonskie opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. I am using the SQLAlchemyDataLayer for my chainlit backend and langgraph for my agent logic. I used session_id as the id for the thread_id configuration of langgraph memory. But now I am trying to implement on_chat_resume, where session id changes when you resume a thread. The only consistent id is thread['id'] which is generated when you create a thread while sending a message at the start of the chat lifecycle and can only be currently retrieved in on_chat_resume.

Describe the solution you'd like Add a feature to get generated Thread Id when it is generated on on_message. kind of like cl.user_session.get('thread_id') to get a consistent id when you resume a session

itonskie avatar Sep 30 '24 01:09 itonskie

feels related: https://github.com/Chainlit/chainlit/issues/837

rpgeddam avatar Sep 30 '24 13:09 rpgeddam

Hey @itonskie , not sure if it solves your particular use case, but I think I have implemented something in my application that may provide a temporary solution for your problem (I am also using SQLAlchemyDataLayer):

@cl.on_chat_start
async def on_chat_start():
    chainlit_thread_id = get_current_chainlit_thread_id()
    # logger.info(rf"Chainlit::Chat started with thread id {chainlit_thread_id}")

with get_current_chainlit_thread_id() defined like so:

def get_current_chainlit_thread_id() -> str:
    return cl.context.session.thread_id

Seems to consistently return the Chainlit-generated Thread ID as soon as the chat is started, which in my case I can then pair to an OpenAI thread (similar to what you seem to be doing with the session_id).

This complements on_chat_resume fairly well - I believe I am doing the same thing as you are there:

@cl.on_chat_resume
async def on_chat_resume(thread: ThreadDict):
    chainlit_thread_id = thread.get("id")
    # logger.info(rf"Chainlit::Resuming chat with thread id {chainlit_thread_id}")

Please let me know if this helped you in any way :)

hadarsharon avatar Oct 02 '24 11:10 hadarsharon

Thank you so much for this! This was exactly what I needed!

itonskie avatar Oct 04 '24 16:10 itonskie