JS/TS SDK (Web)
The langfuse JS/TS SDK can be used to report scores client-side directly from the browser. It is commonly used to ingest scores into Langfuse which are based on implicit user interactions and feedback.
â Read more on Scores in Langfuse and collecting user feedback
Example
import { LangfuseWeb } from "langfuse";
export function UserFeedbackComponent(props: { traceId: string }) {
const langfuseWeb = new LangfuseWeb({
publicKey: env.NEXT_PUBLIC_LANGFUSE_PUBLIC_KEY,
});
const handleUserFeedback = async (value: number) =>
await langfuseWeb.score({
traceId: props.traceId,
name: "user_feedback",
value,
});
return (
<div>
<button onClick={() => handleUserFeedback(1)}>đ</button>
<button onClick={() => handleUserFeedback(0)}>đ</button>
</div>
);
}
We integrated the Web SDK into the Vercel AI Chatbot project to collect user feedback on individual messages. Read the blog post for more details and code examples.
Installation
npm i langfuse
In your application, set the public api key to create a client.
import { LangfuseWeb } from "langfuse";
const langfuseWeb = new LangfuseWeb({
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
});
Hint for Next.js users: you need to prefix the public key with NEXT_PUBLIC_
to expose it in the frontend.
Options
Variable | Description | Default value |
---|---|---|
baseUrl | BaseUrl of the Langfuse API; for US data region, set to "https://us.cloud.langfuse.com" | "https://cloud.langfuse.com" |
release | The release number/hash of the application to provide analytics grouped by release. | process.env.LANGFUSE_RELEASE or common system environment names (opens in a new tab) |
requestTimeout | Timeout in ms for requests | 10000 |
Create scores
Scores are used to evaluate executions/traces. They are attached to a single trace. If the score relates to a specific step of the trace, the score can optionally also be attached to the observation to enable evaluating it specifically.
â Read more on Scores in Langfuse
While integrating Langfuse, it is important to either include the Langfuse Ids in the response to the frontend or to use an own id as the trace id which is available in both backend and frontend.
// pass traceId and observationId to front end
await langfuseWeb.score({
traceId: message.traceId,
observationId: message.observationId,
name: "user-feedback",
value: 1,
comment: "I like how personalized the response is",
});
langfuseWeb.score()
takes the following parameters
parameter | type | optional | description |
---|---|---|---|
traceId | string | no | The id of the trace to which the score should be attached. Pass trace.id from backend or use a shared id that's available in backend and frontend (then set it in backend as trace id via langfuse.trace({id: <yourTraceId>}) ). |
observationId | string | yes | The id of the observation to which the score should be attached. Pass from backend trace. E.g. {generation, event, span}.id |
name | string | no | Identifier of the score. |
value | number | no | The value of the score. Can be any number, often standardized to 0..1 |
comment | string | yes | Additional context/explanation of the score. |