-
Notifications
You must be signed in to change notification settings - Fork 0
chatbot: Add Langfuse feedback support #94
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
Merged
+48
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,16 @@ | |
|
|
||
| import logging | ||
| import os | ||
| from typing import Optional | ||
|
|
||
| import chainlit as cl | ||
| from chainlit.data import get_data_layer | ||
| from chainlit.data.chainlit_data_layer import ChainlitDataLayer | ||
| from chainlit.types import ThreadDict, CommandDict | ||
| from chainlit.types import ThreadDict, CommandDict, Feedback | ||
| from chainlit.step import StepDict | ||
|
|
||
| from willa.chatbot import Chatbot | ||
| from willa.config import CONFIG | ||
| from willa.config import CONFIG, get_langfuse_client | ||
| from willa.web.cas_provider import CASProvider | ||
| from willa.web.inject_custom_auth import add_custom_oauth_provider | ||
|
|
||
|
|
@@ -34,6 +37,22 @@ | |
| ] | ||
|
|
||
|
|
||
| async def get_step(self: ChainlitDataLayer, step_id: str) -> Optional[StepDict]: | ||
| """Get step and related feedback""" | ||
| query = """ | ||
| SELECT s.*, | ||
| f.id feedback_id, | ||
| f.value feedback_value, | ||
| f."comment" feedback_comment | ||
| FROM "Step" s LEFT JOIN "Feedback" f ON s.id = f."stepId" | ||
| WHERE s.id = $1 | ||
| """ | ||
| result = await self.execute_query(query, {"step_id": step_id}) | ||
| if not result: | ||
| return None | ||
| return self._convert_step_row_to_dict(result[0]) # pylint: disable="protected-access" | ||
|
|
||
|
|
||
| @cl.on_chat_start | ||
| async def ocs() -> None: | ||
| """loaded when new chat is started""" | ||
|
|
@@ -48,6 +67,25 @@ async def on_chat_resume(thread: ThreadDict) -> None: | |
| # pylint: enable="unused-argument" | ||
|
|
||
|
|
||
| @cl.on_feedback | ||
| async def on_feedback(feedback: Feedback) -> None: | ||
| """Handle feedback.""" | ||
| step: Optional[StepDict] = await get_data_layer().get_step(feedback.forId) | ||
| if step is None: | ||
| LOGGER.warning("Feedback left for unknown step %s", feedback.forId) | ||
| return | ||
|
|
||
| trace_id: Optional[str] = step['metadata'].get('langfuse_trace_id') | ||
| get_langfuse_client().create_score( | ||
| name='feedback', | ||
| value=float(feedback.value), | ||
| session_id=step['threadId'] if not trace_id else None, | ||
| trace_id=trace_id, | ||
| data_type='BOOLEAN', | ||
| comment=feedback.comment | ||
| ) | ||
|
|
||
|
|
||
| @cl.data_layer | ||
| def data_layer() -> ChainlitDataLayer: | ||
| """Retrieve the data layer to use with Chainlit. | ||
|
|
@@ -68,7 +106,11 @@ def _secret() -> str: | |
| database_url = os.environ.get( | ||
| 'DATABASE_URL', f"postgresql://{_pg('USER')}:{_secret()}@{_pg('HOST')}/{_pg('DB')}" | ||
| ) | ||
| return ChainlitDataLayer(database_url=database_url) | ||
| dl = ChainlitDataLayer(database_url=database_url) | ||
| # pylint: disable="no-value-for-parameter" | ||
| dl.get_step = get_step.__get__(dl) # type: ignore[attr-defined] | ||
| # pylint: enable="no-value-for-parameter" | ||
| return dl | ||
|
|
||
|
|
||
| def _get_history() -> str: | ||
|
|
@@ -117,6 +159,7 @@ async def chat(message: cl.Message) -> None: | |
|
|
||
| if 'ai_message' in reply: | ||
| await cl.Message(content=reply['ai_message']).send() | ||
| cl.context.current_run.metadata['langfuse_trace_id'] = reply['langfuse_trace_id'] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Save the trace ID into the run metadata, which is where Chainlit stores Feedback objects (forId -> the run, not the message). |
||
|
|
||
| if 'tind_message' in reply: | ||
| tind_refs = cl.CustomElement( | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on 29816ad