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

refactor!: Rename versionKey to variationKey #221

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion ldai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *Client) Config(
}

cfg := builder.Build()
return cfg, newTracker(key, parsed.Meta.VersionKey, c.sdk, &cfg, context, c.logger)
return cfg, newTracker(key, parsed.Meta.VariationKey, c.sdk, &cfg, context, c.logger)
}

func getAllAttributes(context ldcontext.Context) map[string]interface{} {
Expand Down
8 changes: 4 additions & 4 deletions ldai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestEvalErrorReturnsDefault(t *testing.T) {

func TestParseMultipleMessages(t *testing.T) {
json := []byte(`{
"_ldMeta": {"versionKey": "1", "enabled": true},
"_ldMeta": {"variationKey": "1", "enabled": true},
"messages": [
{"content": "hello", "role": "user"},
{"content": "world", "role": "system"}
Expand Down Expand Up @@ -175,8 +175,8 @@ func TestParseDisabledConfigs(t *testing.T) {
}{
{"empty object", []byte("{}")},
{"missing meta field", []byte(`{"model": {}, "messages": []}`)},
{"meta disabled explicitly", []byte(`{"meta": {"enabled": false, "versionKey": "1"}, "model": {}, "messages": []}`)},
{"meta disable implicitly", []byte(`{"meta": { "versionKey": "1"}, "model": {}, "messages": []}`)},
{"meta disabled explicitly", []byte(`{"meta": {"enabled": false, "variationKey": "1"}, "model": {}, "messages": []}`)},
{"meta disable implicitly", []byte(`{"meta": { "variationKey": "1"}, "model": {}, "messages": []}`)},
}

defaultVal := NewConfig().Enable().WithMessage("hello", datamodel.User).Build()
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestCannotOverwriteMessages(t *testing.T) {
func eval(t *testing.T, prompt string, ctx ldcontext.Context, variables map[string]interface{}) (string, error) {
t.Helper()
json := []byte(`{
"_ldMeta": {"versionKey": "1", "enabled": true},
"_ldMeta": {"variationKey": "1", "enabled": true},
"messages": [
{"content": "` + prompt + `", "role": "user"}
]
Expand Down
6 changes: 3 additions & 3 deletions ldai/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type Config struct {
c datamodel.Config
}

// VersionKey is used internally by LaunchDarkly.
func (c *Config) VersionKey() string {
return c.c.Meta.VersionKey
// VariationKey is used internally by LaunchDarkly.
func (c *Config) VariationKey() string {
return c.c.Meta.VariationKey
}

// Messages returns the messages defined by the config. The series of messages may be
Expand Down
4 changes: 2 additions & 2 deletions ldai/datamodel/datamodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "github.com/launchdarkly/go-sdk-common/v3/ldvalue"

// Meta defines the serialization format for config metadata.
type Meta struct {
// VersionKey is the version key.
VersionKey string `json:"versionKey,omitempty"`
// VariationKey is the variation key.
VariationKey string `json:"variationKey,omitempty"`

// Enabled is true if the config is enabled.
Enabled bool `json:"enabled,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions ldai/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,19 @@ func (d *defaultStopwatch) Stop() time.Duration {
}

// newTracker creates a new Tracker with the specified key, event sink, config, context, and loggers.
func newTracker(key string, versionKey string, events EventSink, config *Config, ctx ldcontext.Context, loggers interfaces.LDLoggers) *Tracker {
return newTrackerWithStopwatch(key, versionKey, events, config, ctx, loggers, &defaultStopwatch{})
func newTracker(key string, variationKey string, events EventSink, config *Config, ctx ldcontext.Context, loggers interfaces.LDLoggers) *Tracker {
return newTrackerWithStopwatch(key, variationKey, events, config, ctx, loggers, &defaultStopwatch{})
}

// newTrackerWithStopwatch creates a new Tracker with the specified key, event sink, config, context, loggers, and
// stopwatch. This method is used for testing purposes.
func newTrackerWithStopwatch(key string, versionKey string, events EventSink, config *Config, ctx ldcontext.Context, loggers interfaces.LDLoggers, stopwatch Stopwatch) *Tracker {
func newTrackerWithStopwatch(key string, variationKey string, events EventSink, config *Config, ctx ldcontext.Context, loggers interfaces.LDLoggers, stopwatch Stopwatch) *Tracker {
if config == nil {
panic("LaunchDarkly SDK programmer error: config must never be nil")
}

trackData := ldvalue.ObjectBuild().
Set("versionKey", ldvalue.String(versionKey)).
Set("variationKey", ldvalue.String(variationKey)).
Set("configKey", ldvalue.String(key)).Build()

return &Tracker{
Expand Down
46 changes: 23 additions & 23 deletions ldai/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,40 @@ func (m *mockEvents) TrackMetric(eventName string, context ldcontext.Context, me

func TestTracker_NewPanicsWithNilConfig(t *testing.T) {
assert.Panics(t, func() {
newTracker("key", "versionKey", newMockEvents(), nil, ldcontext.New("key"), nil)
newTracker("key", "variationKey", newMockEvents(), nil, ldcontext.New("key"), nil)
})
}

func TestTracker_NewDoesNotPanicWithConfig(t *testing.T) {
assert.NotPanics(t, func() {
newTracker("key", "versionKey", newMockEvents(), &Config{}, ldcontext.New("key"), nil)
newTracker("key", "variationKey", newMockEvents(), &Config{}, ldcontext.New("key"), nil)
})
}

func makeTrackData(configKey, versionKey string) ldvalue.Value {
func makeTrackData(configKey, variationKey string) ldvalue.Value {
return ldvalue.ObjectBuild().
Set("versionKey", ldvalue.String(versionKey)).
Set("variationKey", ldvalue.String(variationKey)).
Set("configKey", ldvalue.String(configKey)).Build()
}

func TestTracker_TrackSuccess(t *testing.T) {
events := newMockEvents()
tracker := newTracker("key", "versionKey", events, &Config{}, ldcontext.New("key"), nil)
tracker := newTracker("key", "variationKey", events, &Config{}, ldcontext.New("key"), nil)
assert.NoError(t, tracker.TrackSuccess())

expectedEvent := trackEvent{
name: "$ld:ai:generation",
context: ldcontext.New("key"),
metricValue: 1.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

assert.ElementsMatch(t, []trackEvent{expectedEvent}, events.events)
}

func TestTracker_TrackRequest(t *testing.T) {
events := newMockEvents()
tracker := newTracker("key", "versionKey", events, &Config{}, ldcontext.New("key"), nil)
tracker := newTracker("key", "variationKey", events, &Config{}, ldcontext.New("key"), nil)

expectedResponse := ProviderResponse{
Usage: TokenUsage{
Expand All @@ -91,21 +91,21 @@ func TestTracker_TrackRequest(t *testing.T) {
name: "$ld:ai:generation",
context: ldcontext.New("key"),
metricValue: 1,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

expectedDurationEvent := trackEvent{
name: "$ld:ai:duration:total",
context: ldcontext.New("key"),
metricValue: 10.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

expectedTokenUsageEvent := trackEvent{
name: "$ld:ai:tokens:total",
context: ldcontext.New("key"),
metricValue: 1,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

expectedEvents := []trackEvent{expectedSuccessEvent, expectedDurationEvent, expectedTokenUsageEvent}
Expand All @@ -124,7 +124,7 @@ func TestTracker_TrackRequestReceivesConfig(t *testing.T) {
Enable().
Build()

tracker := newTracker("key", "versionKey", events, &expectedConfig, ldcontext.New("key"), nil)
tracker := newTracker("key", "variationKey", events, &expectedConfig, ldcontext.New("key"), nil)

var gotConfig *Config
_, _ = tracker.TrackRequest(func(c *Config) (ProviderResponse, error) {
Expand All @@ -147,7 +147,7 @@ func TestTracker_LatencyMeasuredIfNotProvided(t *testing.T) {
events := newMockEvents()

tracker := newTrackerWithStopwatch(
"key", "versionKey", events, &Config{}, ldcontext.New("key"), nil, mockStopwatch(42*time.Millisecond))
"key", "variationKey", events, &Config{}, ldcontext.New("key"), nil, mockStopwatch(42*time.Millisecond))

expectedResponse := ProviderResponse{
Usage: TokenUsage{
Expand All @@ -170,23 +170,23 @@ func TestTracker_LatencyMeasuredIfNotProvided(t *testing.T) {

func TestTracker_TrackDuration(t *testing.T) {
events := newMockEvents()
tracker := newTracker("key", "versionKey", events, &Config{}, ldcontext.New("key"), nil)
tracker := newTracker("key", "variationKey", events, &Config{}, ldcontext.New("key"), nil)

assert.NoError(t, tracker.TrackDuration(time.Millisecond*10))

expectedEvent := trackEvent{
name: "$ld:ai:duration:total",
context: ldcontext.New("key"),
metricValue: 10.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

assert.ElementsMatch(t, []trackEvent{expectedEvent}, events.events)
}

func TestTracker_TrackFeedback(t *testing.T) {
events := newMockEvents()
tracker := newTracker("key", "versionKey", events, &Config{}, ldcontext.New("key"), nil)
tracker := newTracker("key", "variationKey", events, &Config{}, ldcontext.New("key"), nil)

assert.NoError(t, tracker.TrackFeedback(FeedbackPositive))
assert.NoError(t, tracker.TrackFeedback(FeedbackNegative))
Expand All @@ -196,14 +196,14 @@ func TestTracker_TrackFeedback(t *testing.T) {
name: "$ld:ai:feedback:user:positive",
context: ldcontext.New("key"),
metricValue: 1.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

expectedNegativeEvent := trackEvent{
name: "$ld:ai:feedback:user:negative",
context: ldcontext.New("key"),
metricValue: 1.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

assert.ElementsMatch(t, []trackEvent{expectedPositiveEvent, expectedNegativeEvent}, events.events)
Expand All @@ -212,7 +212,7 @@ func TestTracker_TrackFeedback(t *testing.T) {
func TestTracker_TrackUsage(t *testing.T) {
t.Run("only one field set, only one event", func(t *testing.T) {
events := newMockEvents()
tracker := newTracker("key", "versionKey", events, &Config{}, ldcontext.New("key"), nil)
tracker := newTracker("key", "variationKey", events, &Config{}, ldcontext.New("key"), nil)

assert.NoError(t, tracker.TrackUsage(TokenUsage{
Total: 42,
Expand All @@ -222,15 +222,15 @@ func TestTracker_TrackUsage(t *testing.T) {
name: "$ld:ai:tokens:total",
context: ldcontext.New("key"),
metricValue: 42.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

assert.ElementsMatch(t, []trackEvent{expectedEvent}, events.events)
})

t.Run("all fields set, all events", func(t *testing.T) {
events := newMockEvents()
tracker := newTracker("key", "versionKey", events, &Config{}, ldcontext.New("key"), nil)
tracker := newTracker("key", "variationKey", events, &Config{}, ldcontext.New("key"), nil)

assert.NoError(t, tracker.TrackUsage(TokenUsage{
Total: 42,
Expand All @@ -242,21 +242,21 @@ func TestTracker_TrackUsage(t *testing.T) {
name: "$ld:ai:tokens:total",
context: ldcontext.New("key"),
metricValue: 42.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

expectedInput := trackEvent{
name: "$ld:ai:tokens:input",
context: ldcontext.New("key"),
metricValue: 20.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

expectedOutput := trackEvent{
name: "$ld:ai:tokens:output",
context: ldcontext.New("key"),
metricValue: 22.0,
data: makeTrackData("key", "versionKey"),
data: makeTrackData("key", "variationKey"),
}

assert.ElementsMatch(t, []trackEvent{expectedTotal, expectedInput, expectedOutput}, events.events)
Expand Down
Loading