Skip to content

Request for allowing trace id and session id to be kept in ChatMessage gradio component for langfuse. #12032

@Jaryllan

Description

@Jaryllan
  • I have searched to see if a similar issue already exists.

Is your feature request related to a problem? Please describe.
As recommended by @yvrjsharma in the discord discussion, I opened an issue for feature request.

Currently the ChatMessage gradio component does not allow adding additional custom value that are unrelated to gradio. For example, I have a use case, where i need to keep the trace id and session id of the specific conversation, so that it is properly traced in the langfuse. But in current gradio design, if I want to vote a specific message returned by the bot, the current trace id and session id is not retrievable, so not able to tell for that particular reply by the bot is belongs to which trace and session.

The following code demonstrate the issue. I had to do hacky way by "borrowing" existing gradio's property called id to allow trace id and session id to be kept in the history. Then the liked event able to retrieve and mapped to the correct trace id and session id. Personally I would not prefer to do it in this manner as in the future, i may utilize gradio's metadata for other purposes, that may ended up breaking the current hacky design.

https://www.gradio.app/docs/gradio/chatbot#metadata-dict

import json
import uuid
import gradio as gr
import time

from langfuse import Langfuse, observe
from dotenv import load_dotenv

# Load .env file
load_dotenv()
langfuse = Langfuse()

def print_like_dislike(x: gr.LikeData, history):
    print(x.index, x.value, x.liked)
    voted_chat = history[x.index]
    print(voted_chat)

    trace = json.loads(voted_chat["metadata"]["id"])
    trace_id = trace["trace_id"]
    session_id = trace["session_id"]

    if x.liked:
        langfuse.create_score(name="score", value=1, trace_id=trace_id, data_type="NUMERIC")
        langfuse.create_score(name="score", value=1, session_id=session_id, data_type="NUMERIC")
    else:
        langfuse.create_score(name="score", value=-1, trace_id=trace_id, data_type="NUMERIC")
        langfuse.create_score(name="score", value=-1, session_id=session_id, data_type="NUMERIC")

def response(question, history, current_session: str):
    content = []
    response, trace_id = ask_bot(question, history, current_session)
    for character in response:
        time.sleep(0.05)
        content.append(character)
        yield { "content": "".join(content), "metadata": { "id": json.dumps({"trace_id": trace_id, "session_id": current_session}) } }

@observe
def ask_bot(question, history, current_session):
    trace_id = langfuse.get_current_trace_id()
    langfuse.update_current_trace(
        session_id=current_session,
        input=question
    )

    return "**That's cool!**", trace_id

with gr.Blocks() as demo:
    current_session = gr.State(str(uuid.uuid4()))
    current_trace_id = None

    chatbot = gr.Chatbot(container=False, 
                         type="messages")
    chat_input = gr.MultimodalTextbox(
                        file_types=[".png"], 
                        interactive=True,
                        file_count="multiple",
                        placeholder="Enter message or upload file...",
                        show_label=False,
                        autofocus=True,
                        sources=["microphone", "upload"],
    )

    gr.ChatInterface(fn=response, 
                     chatbot=chatbot,
                     type="messages", 
                     fill_height=True, 
                     fill_width=False,
                     textbox=chat_input,
                     additional_inputs=[current_session])

    chatbot.like(print_like_dislike, chatbot, None, like_user_message=True)

if __name__ == "__main__":
    demo.launch()

Describe the solution you'd like
I would like the ChatMessage object to allow adding additional value for langfuse purposes. The value does not requires to be displayed to the end user.

I'm open to better design and suggestion.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions