-
Notifications
You must be signed in to change notification settings - Fork 458
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 #161 from ahmad2b/ahmad2b/add-chat-feedback
Fixes #122: Implement thumbs up/down feedback in chat
- Loading branch information
Showing
7 changed files
with
367 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Client, Feedback } from "langsmith"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const body = await req.json(); | ||
const { runId, feedbackKey, score, comment } = body; | ||
|
||
if (!runId || !feedbackKey) { | ||
return NextResponse.json( | ||
{ error: "`runId` and `feedbackKey` are required." }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const lsClient = new Client({ | ||
apiKey: process.env.LANGCHAIN_API_KEY, | ||
}); | ||
|
||
const feedback = await lsClient.createFeedback(runId, feedbackKey, { | ||
score, | ||
comment, | ||
}); | ||
|
||
return NextResponse.json( | ||
{ success: true, feedback: feedback }, | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error("Failed to process feedback request:", error); | ||
|
||
return NextResponse.json( | ||
{ error: "Failed to submit feedback." }, | ||
{ status: 500 } | ||
); | ||
} | ||
} | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
const searchParams = req.nextUrl.searchParams; | ||
const runId = searchParams.get("runId"); | ||
const feedbackKey = searchParams.get("feedbackKey"); | ||
|
||
if (!runId || !feedbackKey) { | ||
return new NextResponse( | ||
JSON.stringify({ | ||
error: "`runId` and `feedbackKey` are required.", | ||
}), | ||
{ | ||
status: 400, | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} | ||
|
||
const lsClient = new Client({ | ||
apiKey: process.env.LANGCHAIN_API_KEY, | ||
}); | ||
|
||
const runFeedback: Feedback[] = []; | ||
|
||
const run_feedback = await lsClient.listFeedback({ | ||
runIds: [runId], | ||
feedbackKeys: [feedbackKey], | ||
}); | ||
|
||
for await (const feedback of run_feedback) { | ||
runFeedback.push(feedback); | ||
} | ||
|
||
return new NextResponse( | ||
JSON.stringify({ | ||
feedback: runFeedback, | ||
}), | ||
{ | ||
status: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} catch (error) { | ||
console.error("Failed to fetch feedback:", error); | ||
return NextResponse.json( | ||
{ error: "Failed to fetch feedback." }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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,73 @@ | ||
import { useToast } from "@/hooks/use-toast"; | ||
import { FeedbackResponse } from "@/hooks/useFeedback"; | ||
import { ThumbsUpIcon, ThumbsDownIcon } from "lucide-react"; | ||
import { Dispatch, FC, SetStateAction } from "react"; | ||
import { cn } from "@/lib/utils"; | ||
import { TooltipIconButton } from "../ui/assistant-ui/tooltip-icon-button"; | ||
|
||
interface FeedbackButtonProps { | ||
runId: string; | ||
setFeedbackSubmitted: Dispatch<SetStateAction<boolean>>; | ||
sendFeedback: ( | ||
runId: string, | ||
feedbackKey: string, | ||
score: number, | ||
comment?: string | ||
) => Promise<FeedbackResponse | undefined>; | ||
feedbackValue: number; | ||
icon: "thumbs-up" | "thumbs-down"; | ||
isLoading: boolean; | ||
} | ||
|
||
export const FeedbackButton: FC<FeedbackButtonProps> = ({ | ||
runId, | ||
setFeedbackSubmitted, | ||
sendFeedback, | ||
isLoading, | ||
feedbackValue, | ||
icon, | ||
}) => { | ||
const { toast } = useToast(); | ||
|
||
const handleClick = async () => { | ||
try { | ||
const res = await sendFeedback(runId, "feedback", feedbackValue); | ||
if (res?.success) { | ||
setFeedbackSubmitted(true); | ||
} else { | ||
toast({ | ||
title: "Failed to submit feedback", | ||
description: "Please try again later.", | ||
variant: "destructive", | ||
}); | ||
} | ||
} catch (_) { | ||
toast({ | ||
title: "Failed to submit feedback", | ||
description: "Please try again later.", | ||
variant: "destructive", | ||
}); | ||
} | ||
}; | ||
|
||
const tooltip = `Give ${icon === "thumbs-up" ? "positive" : "negative"} feedback on this run`; | ||
|
||
return ( | ||
<TooltipIconButton | ||
variant="ghost" | ||
size="icon" | ||
onClick={handleClick} | ||
aria-label={tooltip} | ||
tooltip={tooltip} | ||
disabled={isLoading} | ||
> | ||
{icon === "thumbs-up" ? ( | ||
<ThumbsUpIcon className={cn("size-4", isLoading && "text-gray-300")} /> | ||
) : ( | ||
<ThumbsDownIcon | ||
className={cn("size-4", isLoading && "text-gray-300")} | ||
/> | ||
)} | ||
</TooltipIconButton> | ||
); | ||
}; |
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
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
Oops, something went wrong.