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

Issue on integrating chat resume. #120

Open
Rajatkhanna801 opened this issue Apr 2, 2024 · 13 comments
Open

Issue on integrating chat resume. #120

Rajatkhanna801 opened this issue Apr 2, 2024 · 13 comments

Comments

@Rajatkhanna801
Copy link

Hi All,

I am sqlite as data persistence layer and Microsoft Authentication. I want to fix chat resume feature for my application. Can anyone help me with that.

Here is my code.

@cl.on_chat_resume
async def on_chat_resume(thread: cl_data.ThreadDict):
memory = []
root_messages = [m for m in thread["steps"] if m["parentId"] == 'None']
for message in root_messages:
if message["type"] == "user_message":
memory.append({"role": "user", "content": message["output"]})
else:
memory.append({"role": "assistant", "content": message["output"]})
cl.user_session.set("memory", memory)
await cl.Message(memory).send()![Uploading Screenshot from 2024-04-02 17-51-59.png…]()

@dahifi
Copy link
Contributor

dahifi commented Apr 2, 2024

Use triple ticks ``` for multiline code:
I assume you mean this?:

@cl.on_chat_resume
async def on_chat_resume(thread: cl_data.ThreadDict):
    memory = []
    root_messages = [m for m in thread["steps"] if m["parentId"] == 'None']
    for message in root_messages:
        if message["type"] == "user_message":
            memory.append({"role": "user", "content": message["output"]})
        else:
            memory.append({"role": "assistant", "content": message["output"]})
    cl.user_session.set("memory", memory)
    await cl.Message(memory).send()

I suggest you set debugging in your app.py and step through it.

Just from the looks of it, you're trying to pass a list of messages to cl.Message's init, you'd need to interate through them individually and send

@Rajatkhanna801
Copy link
Author

@dahifi

Can you send me some examples how I can send the Message in iterations?

@dahifi
Copy link
Contributor

dahifi commented Apr 2, 2024

Was just looking at the example in the cookbook:

Setting cl.user_session memory like you're doing should be sufficient to bring the messages back into the UX, you only want to call Message.send if you want to provide an additional message back to the user.

@Rajatkhanna801
Copy link
Author

Hi @dahifi
I have set the user_session memory but still is not showing user previous messages. Can you please help me with that.
I have also tried the example given but that is also not working.

@Rajatkhanna801
Copy link
Author

My complete code file

from typing import Optional, Dict
import chainlit.data as cl_data
from datalayer import SQLiteDataLayer
from chainlit.types import ThreadDict
from openai import AsyncClient
import chainlit as cl
import asyncio
import os



# user Authentication
@cl.password_auth_callback
def auth_callback(username: str, password: str) -> Optional[cl.User]:
    if (username, password) == ("admin", "admin"):
        return cl.User(identifier="admin")
    else:
        return None


# Authentication with Microsoft
@cl.oauth_callback
def oauth_callback(provider_id: str, token: str,
    raw_user_data: Dict[str, str],
    default_user: cl.User,) -> Optional[cl.User]:
    return default_user


# setting up custom data layer.
cl_data._data_layer = SQLiteDataLayer("user_database/chainlit.db")


# Initilizing AsyncClient and integrating openai API.
openai_client = AsyncClient(api_key=os.environ.get("OPENAI_API_KEY"))
model_name = "gpt-3.5-turbo"
settings = {
    "temperature": 0.3,
    "max_tokens": 500,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
}


@cl.on_chat_start
async def start_chat():
    cl.user_session.set(
        "message_history", []
    )
    await cl.Avatar(
        name="Gilfoyle",
        url="https://static.wikia.nocookie.net/silicon-valley/images/2/20/Bertram_Gilfoyle.jpg",
    ).send()
    await cl.Avatar(
        name="Dinesh",
        url="https://static.wikia.nocookie.net/silicon-valley/images/e/e3/Dinesh_Chugtai.jpg",
    ).send()


# helper functiom to call openai API.
async def answer_as(name):
    message_history = cl.user_session.get("message_history")
    msg = cl.Message(author=name, content="")
    stream = await openai_client.chat.completions.create(
        model=model_name,
        messages=message_history,
        stream=True,
        **settings,
    )
    async for part in stream:
        if token := part.choices[0].delta.content or "":
            await msg.stream_token(token)
    await msg.send()


@cl.on_message
async def main(message: cl.Message):
    message_history = cl.user_session.get("message_history")
    message_history.append({"role": "user", "content": message.content})
    await asyncio.gather(answer_as("Gilfoyle"))


@cl.on_chat_resume
async def on_chat_resume(thread: ThreadDict):
    message_history = []
    root_messages = [m for m in thread["steps"] if m["parentId"] == 'None']
    for message in root_messages:
        if message["type"] == "user_message":
            message_history.append({"role": "user", "content": message["output"]})
        else:
            message_history.append({"role": "Gilfoyle", "content": message["output"]})
    cl.user_session.set("memory", message_history)
    

@Rajatkhanna801
Copy link
Author

In resume chat I am seting the user session memory but still does not show the previous message on browser.

@geekharsh
Copy link

geekharsh commented Apr 7, 2024

I am also facing same problem message history not updating on UI
@Rajatkhanna801 can you provide me the implementation of SQLiteDataLayer

You can take a look of my tries - https://github.com/geekharsh/python/blob/main/chainlit_ui.py

@Rajatkhanna801
Copy link
Author

@geekharsh
yes sure. I have already fix the issue.

@Rajatkhanna801
Copy link
Author

Provide me permission to acces this Repo.

@Rajatkhanna801
Copy link
Author

@geekharsh
Access this repo https://github.com/Rajatkhanna801/chainlit_sqlite. Let me know if you need any help.

@geekharsh
Copy link

Thanks a lot,
this will work, will let you know

@geekharsh
Copy link

@Rajatkhanna801 Good work bro, But i have a question, I am looking at the way in which, the user click on new chat, the current chat will go in chat history will reflect on chat history box, Right now it is appearing after manual page refresh.

@Rajatkhanna801
Copy link
Author

Yes right @geekharsh
I have also done that but that need change in frontend side.

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

3 participants