Skip to content

Commit 31068e2

Browse files
authored
feat: adds online evals changes for go sdk (#12)
# Add Evaluation Functionality and Improve Documentation ### TL;DR Added evaluation functionality to the logging package and improved documentation with package overview comments. ### What changed? - Added new `evaluate.go` file with evaluation functionality through the `evaluateContainer` type - Added `Evaluate()` methods to Session, Trace, Generation, and Retrieval entities - Added accessor methods to CommitLog (GetEntity, GetEntityID, GetAction, GetData) - Added package documentation to the Anthropic logging module - Created a MockWriter implementation for testing - Added utility function `removeDuplicateStrings()` to handle duplicate evaluators ### How to test? Test the new evaluation functionality with: ```go // Initialize client and logger as usual logger, _ := client.GetLogger(&logging.LoggerConfig{Id: "your-log-repo-id"}) // Create a trace and evaluate it trace := logger.CreateTrace(&logging.TraceConfig{}) err := trace.Evaluate().WithEvaluators([]string{"evaluator1", "evaluator2"}) // Or evaluate with variables variables := map[string]string{"key1": "value1", "key2": "value2"} err = trace.Evaluate().WithVariablesAndEvaluators(variables, []string{"evaluator1"}) // Check for errors if err != nil { // handle error } ``` ### Why make this change? This change enables evaluation capabilities for different entities in the logging package, allowing users to evaluate Traces, Sessions, Generations, and Retrievals using Maxim's evaluators. The improved documentation provides better context for developers using the package, and the new MockWriter facilitates more comprehensive testing.
2 parents f5e53a9 + c0bd5ea commit 31068e2

File tree

11 files changed

+333
-1
lines changed

11 files changed

+333
-1
lines changed

logging/anthropic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77
)
88

9+
// ParseAnthropicResult parses a JSON response from Anthropic's API and returns a MaximLLMResult.
910
func ParseAnthropicResult(jsonData []byte) (*MaximLLMResult, error) {
1011
var anthropicResp struct {
1112
ID string `json:"id"`

logging/base.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
ProviderBedrock = "aws"
1414
)
1515

16+
// baseConfig is the configuration for a base entity.
1617
type baseConfig struct {
1718
Id string `json:"id"`
1819
SpanId *string `json:"spanId,omitempty"`
@@ -21,6 +22,7 @@ type baseConfig struct {
2122
Tags *map[string]string `json:"tags,omitempty"`
2223
}
2324

25+
// base is the base entity for all entities.
2426
type base struct {
2527
entity Entity
2628
id string
@@ -32,6 +34,7 @@ type base struct {
3234
writer *writer
3335
}
3436

37+
// sanitizeMetadata sanitizes the metadata for a base entity.
3538
func sanitizeMetadata(metadata map[string]interface{}) map[string]string {
3639
sanitizedMetadata := make(map[string]string)
3740
for key, value := range metadata {
@@ -48,6 +51,7 @@ func sanitizeMetadata(metadata map[string]interface{}) map[string]string {
4851
return sanitizedMetadata
4952
}
5053

54+
// newBase creates a new base entity.
5155
func newBase(e Entity, id string, c *baseConfig, w *writer) *base {
5256
return &base{
5357
entity: e,
@@ -60,14 +64,17 @@ func newBase(e Entity, id string, c *baseConfig, w *writer) *base {
6064
}
6165
}
6266

67+
// commit commits a new commit log to the writer.
6368
func (b *base) commit(action string, data interface{}) {
6469
b.writer.commit(newCommitLog(b.entity, b.id, action, data))
6570
}
6671

72+
// Id returns the id of the base entity.
6773
func (b *base) Id() string {
6874
return b.id
6975
}
7076

77+
// AddTag adds a tag to the base entity.
7178
func (b *base) AddTag(key, value string) {
7279
if b.tags == nil {
7380
b.tags = &map[string]string{}
@@ -78,20 +85,23 @@ func (b *base) AddTag(key, value string) {
7885
})
7986
}
8087

88+
// AddMetadata adds metadata to the base entity.
8189
func (b *base) AddMetadata(metadata map[string]interface{}) {
8290
sanitizedMetadata := sanitizeMetadata(metadata)
8391
b.commit("update", map[string]interface{}{
8492
"metadata": sanitizedMetadata,
8593
})
8694
}
8795

96+
// End ends the base entity.
8897
func (b *base) End() {
8998
b.endTimestamp = utcNowPtr()
9099
b.commit("end", map[string]interface{}{
91100
"endTimestamp": b.endTimestamp,
92101
})
93102
}
94103

104+
// data returns the data of the base entity.
95105
func (b *base) data() map[string]interface{} {
96106
data := map[string]interface{}{
97107
"startTimestamp": b.startTimestamp,
@@ -113,6 +123,7 @@ func (b *base) data() map[string]interface{} {
113123

114124
// Static methods
115125

126+
// addTag adds a tag to the base entity.
116127
func addTag(w *writer, entity Entity, id, key, value string) {
117128
w.commit(newCommitLog(entity, id, "update", map[string]interface{}{
118129
"tags": map[string]string{
@@ -121,6 +132,7 @@ func addTag(w *writer, entity Entity, id, key, value string) {
121132
}))
122133
}
123134

135+
// addEvent adds an event to the base entity.
124136
func addEvent(w *writer, entity Entity, entityId, eId, event string, tags *map[string]string) {
125137
eventData := map[string]interface{}{
126138
"id": eId,
@@ -133,10 +145,12 @@ func addEvent(w *writer, entity Entity, entityId, eId, event string, tags *map[s
133145
w.commit(newCommitLog(entity, entityId, "add-event", eventData))
134146
}
135147

148+
// addFeedback adds feedback to the base entity.
136149
func addFeedback(w *writer, entity Entity, id string, feedback *Feedback) {
137150
w.commit(newCommitLog(entity, id, "add-feedback", feedback))
138151
}
139152

153+
// end ends the base entity.
140154
func end(w *writer, entity Entity, id string) {
141155
w.commit(newCommitLog(entity, id, "end", map[string]interface{}{
142156
"endTimestamp": utcNow(),

logging/commitLog.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type CommitLog struct {
2727
data interface{}
2828
}
2929

30-
// NewCommitLog creates a new CommitLog instance
30+
// newCommitLog creates a new CommitLog instance (internal use)
3131
func newCommitLog(entity Entity, entityID, action string, data interface{}) *CommitLog {
3232
return &CommitLog{
3333
entity: entity,
@@ -52,3 +52,23 @@ func (cl *CommitLog) Serialize() string {
5252
}
5353
return fmt.Sprintf("%s{id=%s,action=%s,data=%s}", cl.entity, cl.entityID, cl.action, string(dataJSON))
5454
}
55+
56+
// GetEntity returns the entity type of the commit log
57+
func (cl *CommitLog) GetEntity() Entity {
58+
return cl.entity
59+
}
60+
61+
// GetEntityID returns the entity ID of the commit log
62+
func (cl *CommitLog) GetEntityID() string {
63+
return cl.entityID
64+
}
65+
66+
// GetAction returns the action of the commit log
67+
func (cl *CommitLog) GetAction() string {
68+
return cl.action
69+
}
70+
71+
// GetData returns the data of the commit log
72+
func (cl *CommitLog) GetData() interface{} {
73+
return cl.data
74+
}

logging/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package logging
2+
3+
import "errors"
4+
5+
var ErrNoEvaluators = errors.New("at least one evaluator is required")
6+
var ErrNoVariables = errors.New("at least one variable is required")

logging/evaluate.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package logging
2+
3+
type evaluateContainer struct {
4+
entity Entity
5+
id string
6+
writer *writer
7+
}
8+
9+
func newEvaluateContainer(entity Entity, id string, writer *writer) *evaluateContainer {
10+
return &evaluateContainer{
11+
entity: entity,
12+
id: id,
13+
writer: writer,
14+
}
15+
}
16+
17+
func (ec *evaluateContainer) WithVariablesAndEvaluators(variables map[string]string, evaluators []string) error {
18+
if len(evaluators) == 0 {
19+
return ErrNoEvaluators
20+
}
21+
// Make sure there are no duplicates in the evaluators
22+
evaluators = removeDuplicateStrings(evaluators)
23+
ec.writer.commit(newCommitLog(ec.entity, ec.id, "evaluate", map[string]interface{}{
24+
"with": "variables",
25+
"variables": variables,
26+
"evaluators": evaluators,
27+
"timestamp": utcNow(),
28+
}))
29+
return nil
30+
}
31+
32+
func (ec *evaluateContainer) WithEvaluators(evaluators []string) error {
33+
if len(evaluators) == 0 {
34+
return ErrNoEvaluators
35+
}
36+
evaluators = removeDuplicateStrings(evaluators)
37+
ec.writer.commit(newCommitLog(ec.entity, ec.id, "evaluate", map[string]interface{}{
38+
"with": "evaluators",
39+
"evaluators": evaluators,
40+
"timestamp": utcNow(),
41+
}))
42+
return nil
43+
}

logging/generation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ func (g *Generation) SetMaximPromptID(pId string) {
238238
})
239239
}
240240

241+
func (g *Generation) Evaluate() *evaluateContainer {
242+
return newEvaluateContainer(EntityGeneration, g.Id(), g.writer)
243+
}
244+
241245
func (g *Generation) SetResult(r interface{}) {
242246
var finalResult *MaximLLMResult
243247
var err error

0 commit comments

Comments
 (0)