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

api: add feedback api for interactions #60

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 50 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 @@ -285,6 +289,52 @@ 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(
lucklove marked this conversation as resolved.
Show resolved Hide resolved
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,
)

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