Skip to content

Commit

Permalink
api: add feedback api for interactions (#60)
Browse files Browse the repository at this point in the history
* api: add feedback api for interactions

* Add comment to describle risk
  • Loading branch information
lucklove authored Mar 28, 2024
1 parent ecd8ba9 commit fe8a94c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
22 changes: 20 additions & 2 deletions api/api_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,18 @@ class ApplicationCreate(APIModel):
langfuse_secret_key: Optional[str]


class InteractionScore(APIModel):
"""
The request model for scoring interaction.
"""

value: float
comment: Optional[str]


class ItemDeleteResponse(APIModel):
"""
The response model for app/version deletion.
The response model for item deletion.
"""

success: bool
Expand All @@ -260,7 +269,16 @@ class ItemDeleteResponse(APIModel):

class ItemUpdateResponse(APIModel):
"""
The response model for app/version update.
The response model for item update.
"""

success: bool
message: str


class ItemCreateResponse(APIModel):
"""
The response model for item create.
"""

success: bool
Expand Down
54 changes: 54 additions & 0 deletions api/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from fastapi.responses import JSONResponse
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter
from langfuse import Langfuse
from sqlalchemy import create_engine

import patterns
Expand All @@ -29,6 +30,8 @@
BlockInfo,
InteractionInfo,
InteractionInfoResponse,
InteractionScore,
ItemCreateResponse,
ItemDeleteResponse,
ItemUpdateResponse,
Parameter,
Expand All @@ -41,6 +44,7 @@
)
from blocks import AsyncInvoker
from database import Database
from exceptions import ApplicationNotFound, InteractionNotFound
from model import Application, ApplicationVersion
from resolver import Resolver

Expand Down Expand Up @@ -286,6 +290,56 @@ def get_interaction(self, interaction_id: str) -> InteractionInfoResponse:
)
)

@router.post("/interactions/{interaction_id}/scores")
def score_interaction(
self,
request: Request,
interaction_id: str,
score: InteractionScore,
) -> ItemCreateResponse:
"""
Give an interaction a feedback to measure if the result is good.
Args:
interaction_id (str): The ID of the interaction to score.
score (InteractionScore): The score detail
Returns:
ItemCreateResponse: An object indicating the success or failure of the score operation.
"""

# get langfuse configuration
interaction = self.database.get_interaction(interaction_id)
if not interaction:
raise InteractionNotFound(interaction_id)
app = self.database.get_application(interaction.app_id)
if not app:
raise ApplicationNotFound(app_id)
if not app.langfuse_public_key or not app.langfuse_secret_key:
return ItemCreateResponse(
success=False,
message=f"Langfuse was not configured correctly application {interaction.app_id}.",
)

# create score
langfuse = Langfuse(
public_key=app.langfuse_public_key,
secret_key=app.langfuse_secret_key,
)
langfuse.score(
trace_id=interaction_id,
# use user name as score name since a interaction may be scored by different users
name=request.state.user,
value=score.value,
comment=score.comment,
)

# XXX:
# The Langfuse SDK executes network requests in the background on a separate thread.
# Any exception on that thread cannot be caught here. Therefore, a response with success=True
# does not necessarily mean that the Langfuse server has accepted the score.
return ItemCreateResponse(success=True, message=f"Score has been created.")

@router.put("/applications/{application_id}")
def update_app_meta(
self, application_id: str, metadata: AppMetadata
Expand Down

0 comments on commit fe8a94c

Please sign in to comment.