Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 18 additions & 1 deletion libs/acl/langfuse/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type eventBodyUnion struct {
Span *SpanEventBody `json:",inline,omitempty"`
Generation *GenerationEventBody `json:",inline,omitempty"`
Event *EventEventBody `json:",inline,omitempty"`
Score *ScoreEventBody `json:",inline,omitempty"`
Log *SDKLogEventBody `json:",inline,omitempty"`
}

Expand All @@ -91,6 +92,8 @@ func (e *eventBodyUnion) MarshalJSON() ([]byte, error) {
return sonic.Marshal(e.Generation)
} else if e.Event != nil {
return sonic.Marshal(e.Event)
} else if e.Score != nil {
return sonic.Marshal(e.Score)
}
return nil, fmt.Errorf("event body is empty")
}
Expand All @@ -104,6 +107,8 @@ func (e *eventBodyUnion) getTraceID() string {
return e.Generation.TraceID
} else if e.Event != nil {
return e.Event.TraceID
} else if e.Score != nil {
return e.Score.TraceID
}
return ""
}
Expand All @@ -115,6 +120,8 @@ func (e *eventBodyUnion) getObservationID() string {
return e.Generation.ID
} else if e.Event != nil {
return e.Event.ID
} else if e.Score != nil {
return e.Score.ObservationID
}
return ""
}
Expand Down Expand Up @@ -257,4 +264,14 @@ type SDKLogEventBody struct {
Log string `json:"log"`
}

// TODO: ScoreEvent
type ScoreEventBody struct {
ID string `json:"id,omitempty"`
TraceID string `json:"traceId"`
Name string `json:"name"`
Value float64 `json:"value"`
ObservationID string `json:"observationId,omitempty"`
Comment string `json:"comment,omitempty"`
DataType string `json:"dataType,omitempty"`
ConfigID string `json:"configId,omitempty"`
Environment string `json:"environment,omitempty"`
}
13 changes: 13 additions & 0 deletions libs/acl/langfuse/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ func TestEventBodyUnion(t *testing.T) {
assert.Equal(t, observationID, eventUnion.getObservationID())
assert.Equal(t, map[string]string{"key": "value"}, eventUnion.getMetadata())

scoreUnion := &eventBodyUnion{Score: &ScoreEventBody{
ID: "scoreID",
TraceID: traceID,
Name: "accuracy",
Value: 0.95,
ObservationID: observationID,
}}
assert.Equal(t, traceID, scoreUnion.getTraceID())
assert.Equal(t, observationID, scoreUnion.getObservationID())
assert.Equal(t, "", scoreUnion.getInput())
assert.Equal(t, "", scoreUnion.getOutput())
assert.Nil(t, scoreUnion.getMetadata())

generationUnion := &eventBodyUnion{Generation: &GenerationEventBody{
BaseObservationEventBody: BaseObservationEventBody{
BaseEventBody: BaseEventBody{
Expand Down
20 changes: 20 additions & 0 deletions libs/acl/langfuse/langfuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Langfuse interface {
CreateGeneration(body *GenerationEventBody) (string, error)
EndGeneration(body *GenerationEventBody) error
CreateEvent(body *EventEventBody) (string, error)
CreateScore(body *ScoreEventBody) (string, error)
Flush()
}

Expand Down Expand Up @@ -205,6 +206,25 @@ func (l *langfuseIns) CreateEvent(body *EventEventBody) (string, error) {
})
}

// CreateScore creates a new score for evaluating LLM outputs
//
// Parameters:
// - body: The score event details. If ID is empty, a new UUID will be generated
//
// Returns:
// - string: The ID of the created score
// - error: Any error that occurred during creation
func (l *langfuseIns) CreateScore(body *ScoreEventBody) (string, error) {
if len(body.ID) == 0 {
body.ID = uuid.NewString()
}
return body.ID, l.tm.push(&event{
ID: uuid.NewString(),
Type: EventTypeScoreCreate,
Body: eventBodyUnion{Score: body},
})
}

// Flush waits for all queued events to be processed and uploaded to Langfuse
//
// This method blocks until all pending events in the queue have been processed
Expand Down
15 changes: 15 additions & 0 deletions libs/acl/langfuse/mock/langfuse_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading