You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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.
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:
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"
}
}
]
The text was updated successfully, but these errors were encountered: