forked from aws-samples/bedrock-claude-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aws-samples#287 from azaylamba/user-feedback
Enhancement: Add user feedback for responses
- Loading branch information
Showing
16 changed files
with
346 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
37 changes: 37 additions & 0 deletions
37
lib/chatbot-api/functions/api-handler/routes/user_feedback.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import genai_core.types | ||
import genai_core.auth | ||
import genai_core.user_feedback | ||
from pydantic import BaseModel | ||
from aws_lambda_powertools import Logger, Tracer | ||
from aws_lambda_powertools.event_handler.appsync import Router | ||
|
||
tracer = Tracer() | ||
router = Router() | ||
logger = Logger() | ||
|
||
|
||
class CreateUserFeedbackRequest(BaseModel): | ||
sessionId: str | ||
key: str | ||
feedback: str | ||
prompt: str | ||
completion: str | ||
model: str | ||
|
||
|
||
@router.resolver(field_name="addUserFeedback") | ||
@tracer.capture_method | ||
def user_feedback(input: dict): | ||
request = CreateUserFeedbackRequest(**input) | ||
|
||
userId = genai_core.auth.get_user_id(router) | ||
|
||
if userId is None: | ||
raise genai_core.types.CommonError("User not found") | ||
|
||
result = genai_core.user_feedback.add_user_feedback( | ||
request.sessionId, request.key, request.feedback, request.prompt, request.completion, request.model, userId) | ||
|
||
return { | ||
"feedback_id": result["feedback_id"], | ||
} |
This file contains 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 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 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
51 changes: 51 additions & 0 deletions
51
lib/shared/layers/python-sdk/python/genai_core/user_feedback.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
import uuid | ||
import boto3 | ||
import json | ||
from pydantic import BaseModel | ||
from datetime import datetime | ||
|
||
dynamodb = boto3.resource("dynamodb") | ||
s3_client = boto3.client("s3") | ||
|
||
USER_FEEDBACK_BUCKET_NAME = os.environ.get("USER_FEEDBACK_BUCKET_NAME") | ||
|
||
|
||
def add_user_feedback( | ||
sessionId: str, | ||
key: str, | ||
feedback: str, | ||
prompt: str, | ||
completion: str, | ||
model: str, | ||
userId: str | ||
): | ||
feedbackId = str(uuid.uuid4()) | ||
timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ") | ||
|
||
item = { | ||
"feedbackId": feedbackId, | ||
"sessionId": sessionId, | ||
"userId": userId, | ||
"key": key, | ||
"prompt": prompt, | ||
"completion": completion, | ||
"model": model, | ||
"feedback": feedback, | ||
"createdAt": timestamp | ||
} | ||
|
||
response = s3_client.put_object( | ||
Bucket=USER_FEEDBACK_BUCKET_NAME, | ||
Key=feedbackId, | ||
Body=json.dumps(item), | ||
ContentType="application/json", | ||
StorageClass='STANDARD_IA', | ||
) | ||
print(response) | ||
|
||
return { | ||
"feedback_id": feedbackId | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
31 changes: 31 additions & 0 deletions
31
lib/user-interface/react-app/src/common/api-client/user-feedback-client.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { GraphQLResult } from "@aws-amplify/api-graphql"; | ||
import { API, GraphQLQuery } from "@aws-amplify/api"; | ||
import { AddUserFeedbackMutation } from "../../API.ts"; | ||
import { | ||
addUserFeedback | ||
} from "../../graphql/mutations.ts"; | ||
import { FeedbackData } from "../../components/chatbot/types.ts"; | ||
|
||
export class UserFeedbackClient { | ||
|
||
async addUserFeedback(params: { | ||
feedbackData: FeedbackData | ||
} | ||
): Promise<GraphQLResult<GraphQLQuery<AddUserFeedbackMutation>>> { | ||
const result = API.graphql<GraphQLQuery<AddUserFeedbackMutation>>({ | ||
query: addUserFeedback, | ||
variables: { | ||
input: { | ||
sessionId: params.feedbackData.sessionId, | ||
key: params.feedbackData.key, | ||
feedback: params.feedbackData.feedback, | ||
prompt: params.feedbackData.prompt, | ||
completion: params.feedbackData.completion, | ||
model: params.feedbackData.model, | ||
}, | ||
}, | ||
}); | ||
return result; | ||
} | ||
|
||
} |
Oops, something went wrong.