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

Feedback and Response are not connected #1518

Open
utsav-sharma2020 opened this issue Nov 13, 2024 · 1 comment
Open

Feedback and Response are not connected #1518

utsav-sharma2020 opened this issue Nov 13, 2024 · 1 comment
Labels
bug Something isn't working data layer Pertains to data layers. needs-triage

Comments

@utsav-sharma2020
Copy link

utsav-sharma2020 commented Nov 13, 2024

Describe the bug
I am trying to save the user's question, AI response, and its corresponding feedback together but it is saving the user's question, AI response as one entry and feedback as another.

To Reproduce
Code:

import chainlit as cl
import chainlit.data as cl_data
import json
import os
def save_conversation_entry(entry):
    filename = "results.json"
    # Ensure the file exists or create it
    if not os.path.exists(filename):
        with open(filename, "w") as file:
            json.dump([], file)
    with open(filename, "r") as file:
        try:
            existing_data = json.load(file)
        except json.JSONDecodeError:
            existing_data = []
    # Update existing entry if it exists
    found = False
    for i, data in enumerate(existing_data):
        if data.get('id') == entry['id']:
            existing_data[i] = entry
            found = True
            break
    if not found:
        existing_data.append(entry)
    with open(filename, "w") as file:
        json.dump(existing_data, file, indent=4)
class CustomDataLayer(cl_data.BaseDataLayer):
    async def upsert_feedback(self, feedback: cl_data.Feedback) -> str:
        filename = "results.json"
        with open(filename, "r") as file:
            try:
                existing_data = json.load(file)
            except json.JSONDecodeError:
                existing_data = []
        # Find the entry with the matching message ID
        found = False
        for data in existing_data:
            if data.get('id') == feedback.forId:
                data['feedback'] = {
                    'value': feedback.value,
                    'comment': feedback.comment,
                    'positiveORnegative': "Positive" if feedback.value == 1 else "Negative"
                }
                found = True
                break
        if not found:
            # If no matching ID, add a new entry with feedback only
            new_entry = {
                'id': feedback.forId,
                'feedback': {
                    'value': feedback.value,
                    'comment': feedback.comment,
                    'positiveORnegative': "Positive" if feedback.value == 1 else "Negative"
                }
            }
            existing_data.append(new_entry)
        with open(filename, "w") as file:
            json.dump(existing_data, file, indent=4)
        return await super().upsert_feedback(feedback)
cl_data._data_layer = CustomDataLayer()
@cl.on_message
async def on_message(message):
    user_message = message.content
    ai_response = "message content 1"
    # Create the message without a custom ID
    msg = cl.Message(
        content=ai_response,
        elements=[
            cl.Text(
                name="Sources",
                content="how you doin"
            )
        ]
    )
    await msg.send()
    # Retrieve the message ID after sending
    msg_id = msg.id
    # Save the conversation entry using the message ID
    entry = {
        'id': msg_id,
        'user_message': user_message,
        'ai_response': ai_response
    }
    save_conversation_entry(entry)

Expected behavior
[
{
"id": "2faf7398-9978-4075-a74d-1bcebe8b2215",
"user_message": "Who are you?",
"ai_response": "message content 1",
"feedback": {
"value": 1,
"comment": "Great",
"positiveORnegative": "Positive"
}
}
]

Screenshots
[
{
"id": "f27bc3cd-ac30-4b99-8be2-aacb41a2b493",
"user_message": "Who are you?",
"ai_response": "message content 1"
},
{
"id": "7ce7cf4e-dcc3-4f1c-8a81-188ef82d7c4a",
"feedback": {
"value": 1,
"comment": "Great",
"positiveORnegative": "Positive"
}
}
]

@dosubot dosubot bot added bug Something isn't working data layer Pertains to data layers. labels Nov 13, 2024
@saimanoj1206
Copy link

I am facing similar issue. The forId is a unique identifier for the on_chat action, while the msgId is a unique identifier for the assistant's message, so they aren't directly linked.

Currently, I'm using the steps table saved by the data layer to join feedback, the user query, and the assistant's response. I'm relying on the flow order in the steps table, which should follow the sequence: user_message, run on_chat, assistant_message. However, the feedback for the assistant's message is getting associated with the "run on_chat" record instead.

I'm seeking a better solution for retrieving the correct forId. Any suggestions would be greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working data layer Pertains to data layers. needs-triage
Projects
None yet
Development

No branches or pull requests

2 participants