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

feat(llm-observability): metric and feedback methods #1709

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

skoob13
Copy link
Contributor

@skoob13 skoob13 commented Jan 31, 2025

Problem

It should be straightforward to capture a LLM metric or feedback. Mirrors PostHog/posthog-js-lite#365.

Changes

  • Add methods to capture user feedback and metrics.
  • Convert values like numbers and booleans to strings to unify the property type.

Checklist

  • Tests for new code (see advice on the tests we use)
  • Accounted for the impact of any changes across different browsers
  • Accounted for backwards compatibility of any changes (no breaking changes in posthog-js!)

@skoob13 skoob13 added the bump minor Bump minor version when this PR gets merged label Jan 31, 2025
@skoob13 skoob13 requested review from Twixes and k11kirky January 31, 2025 15:29
Copy link

vercel bot commented Jan 31, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js 🛑 Canceled (Inspect) Jan 31, 2025 3:40pm

@skoob13 skoob13 changed the title feat: metric and feedback methods feat(llm-observability): metric and feedback methods Jan 31, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR adds two new methods to the PostHog JavaScript library for capturing AI-related metrics and feedback, enabling straightforward tracking of LLM interactions.

  • Added captureTraceMetric() in src/posthog-core.ts to track AI metrics with trace ID, name, and value
  • Added captureTraceFeedback() in src/posthog-core.ts to capture user feedback with trace ID
  • Both methods automatically convert numeric/boolean values to strings for consistent property types
  • Added comprehensive test coverage in src/__tests__/ai.test.ts verifying string conversion and event capture
  • Methods use existing $ai_metric event type with different property structures for metrics vs feedback

2 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile


const setup = (config: Partial<PostHogConfig> = {}, token: string = uuidv7()) => {
const beforeSendMock = jest.fn().mockImplementation((e) => e)
const posthog = defaultPostHog().init(token, { ...config, before_send: beforeSendMock }, token)!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Using the token as both the token and name parameter could cause confusion in tests. Consider using distinct values.

})

describe('captureTraceFeedback()', () => {
it('should capture metric', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Test description says 'should capture metric' but this is testing feedback capture. Should be 'should capture feedback'.

Suggested change
it('should capture metric', () => {
it('should capture feedback', () => {

Comment on lines 2139 to 2143
captureTraceFeedback(traceId: string | number, userFeedback: string) {
this.capture('$ai_metric', {
$ai_trace_id: String(traceId),
$ai_feedback_text: userFeedback,
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding validation for empty/null userFeedback parameter

Comment on lines +2152 to +2158
captureTraceMetric(traceId: string | number, metricName: string, metricValue: string | number | boolean) {
this.capture('$ai_metric', {
$ai_trace_id: String(traceId),
$ai_metric_name: metricName,
$ai_metric_value: String(metricValue),
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding validation for empty/null metricName parameter

Copy link

github-actions bot commented Jan 31, 2025

Size Change: +2.28 kB (+0.07%)

Total Size: 3.3 MB

Filename Size Change
dist/array.full.es5.js 268 kB +271 B (+0.1%)
dist/array.full.js 371 kB +223 B (+0.06%)
dist/array.full.no-external.js 369 kB +223 B (+0.06%)
dist/array.js 183 kB +223 B (+0.12%)
dist/array.no-external.js 182 kB +223 B (+0.12%)
dist/main.js 184 kB +223 B (+0.12%)
dist/module.full.js 371 kB +223 B (+0.06%)
dist/module.full.no-external.js 369 kB +223 B (+0.06%)
dist/module.js 183 kB +223 B (+0.12%)
dist/module.no-external.js 182 kB +223 B (+0.12%)
ℹ️ View Unchanged
Filename Size
dist/all-external-dependencies.js 215 kB
dist/customizations.full.js 13.8 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/exception-autocapture.js 9.48 kB
dist/external-scripts-loader.js 2.64 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/surveys-preview.js 69.4 kB
dist/surveys.js 72.4 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

captureTraceFeedback(traceId: string | number, userFeedback: string) {
this.capture('$ai_feedback', {
$ai_trace_id: String(traceId),
$ai_feedback_text: userFeedback,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just thinking here do we want to allow the user to also provide a label?

For example, a common pattern is to get a thumbs up or thumbs down and then ask why. it would be great to capture the text with the action

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gut feeling is that you typically want to capture a single piece of feedback, but I can imagine cases when this is useful, such as multi-step agents. Should we do that in this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump minor Bump minor version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants