Documentation
¶
Overview ¶
Package ldai contains an AI SDK suitable for usage with generative AI applications.
Index ¶
- Constants
- type Client
- type Config
- func (c *Config) AsLdValue() ldvalue.Value
- func (c *Config) CreateTracker() *Tracker
- func (c *Config) CustomModelParam(key string) (ldvalue.Value, bool)
- func (c *Config) Enabled() bool
- func (c *Config) EvaluationMetricKey() string
- func (c *Config) EvaluationMetricKeys() []string
- func (c *Config) JudgeConfiguration() *datamodel.JudgeConfiguration
- func (c *Config) Messages() []datamodel.Message
- func (c *Config) Mode() string
- func (c *Config) ModelName() string
- func (c *Config) ModelParam(key string) (ldvalue.Value, bool)
- func (c *Config) ProviderName() string
- func (c *Config) VariationKey() string
- func (c *Config) Version() int
- type ConfigBuilder
- func (cb *ConfigBuilder) Build() Config
- func (cb *ConfigBuilder) Disable() *ConfigBuilder
- func (cb *ConfigBuilder) Enable() *ConfigBuilder
- func (cb *ConfigBuilder) WithCustomModelParam(key string, value ldvalue.Value) *ConfigBuilder
- func (cb *ConfigBuilder) WithEnabled(enabled bool) *ConfigBuilder
- func (cb *ConfigBuilder) WithEvaluationMetricKey(key string) *ConfigBuilder
- func (cb *ConfigBuilder) WithEvaluationMetricKeys(keys []string) *ConfigBuilder
- func (cb *ConfigBuilder) WithJudgeConfiguration(judgeConfig *datamodel.JudgeConfiguration) *ConfigBuilder
- func (cb *ConfigBuilder) WithMessage(content string, role datamodel.Role) *ConfigBuilder
- func (cb *ConfigBuilder) WithMode(mode string) *ConfigBuilder
- func (cb *ConfigBuilder) WithModelName(modelName string) *ConfigBuilder
- func (cb *ConfigBuilder) WithModelParam(key string, value ldvalue.Value) *ConfigBuilder
- func (cb *ConfigBuilder) WithProviderName(providerName string) *ConfigBuilder
- type EventSink
- type Feedback
- type MetricSummary
- type Metrics
- type ProviderResponse
- type ServerSDK
- type Stopwatch
- type TokenUsage
- type Tracker
- func (t *Tracker) GetSummary() MetricSummary
- func (t *Tracker) ResumptionToken() string
- func (t *Tracker) TrackDuration(dur time.Duration) error
- func (t *Tracker) TrackError() error
- func (t *Tracker) TrackFeedback(feedback Feedback) error
- func (t *Tracker) TrackJudgeResponse(response datamodel.JudgeResponse) error
- func (t *Tracker) TrackRequest(task func(c *Config) (ProviderResponse, error)) (ProviderResponse, error)
- func (t *Tracker) TrackSuccess() error
- func (t *Tracker) TrackTimeToFirstToken(dur time.Duration) error
- func (t *Tracker) TrackTokens(usage TokenUsage) error
- func (t *Tracker) TrackUsage(usage TokenUsage) errordeprecated
Constants ¶
const ( JudgePlaceholderMessageHistory = "{{message_history}}" JudgePlaceholderResponseToEvaluate = "{{response_to_evaluate}}" )
JudgePlaceholderMessageHistory and JudgePlaceholderResponseToEvaluate are the literal placeholder strings shared between JudgeConfig (pass 1) and Judge.buildMessages (pass 2). Both must use the same values or substitution silently fails.
const ( // Version is the current version string of the ldai package. This is updated by our release scripts. Version = "0.9.3" // {{ x-release-please-version }} // SDKName is the canonical name of this AI SDK package. SDKName = "go-server-sdk/ldai" // SDKLanguage is the programming language of this AI SDK. SDKLanguage = "go" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main entrypoint for the AI SDK. A client can be used to obtain an AI Config from LaunchDarkly. Unless otherwise noted, the Client's method are not safe for concurrent use.
func NewClient ¶
NewClient creates a new AI Client. The provided SDK interface must not be nil. The client will use the provided SDK's loggers to log warnings and errors.
func (*Client) CompletionConfig ¶ added in v0.8.1
func (c *Client) CompletionConfig( key string, context ldcontext.Context, defaultValue Config, variables map[string]interface{}, ) Config
CompletionConfig retrieves an AI Config and interpolates its message templates using the provided variables. Returns the default value if the config cannot be evaluated. Template interpolation is not applied to the default value's messages.
To send analytic events to LaunchDarkly, call CreateTracker on the returned Config to obtain a Tracker.
func (*Client) CreateTracker ¶ added in v0.9.0
CreateTracker reconstructs a Tracker from a resumption token and the given context. This delegates to TrackerFromResumptionToken. See that function for details.
func (*Client) JudgeConfig ¶ added in v0.8.0
func (c *Client) JudgeConfig( key string, context ldcontext.Context, defaultValue Config, variables map[string]interface{}, ) Config
JudgeConfig retrieves a Judge AI Config and interpolates its message templates. The reserved variables message_history and response_to_evaluate are preserved as literal placeholders for substitution by Judge.buildMessages during evaluation.
To send analytic events to LaunchDarkly, call CreateTracker on the returned Config to obtain a Tracker.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config represents an AI Config.
func Disabled ¶
func Disabled() Config
Disabled is a helper that returns a built Config that is disabled and contains no messages.
func (*Config) CreateTracker ¶ added in v0.9.0
CreateTracker returns a new Tracker for a fresh AI run. Each call mints a new runId (a UUIDv4) that LaunchDarkly uses to correlate the run's events in metrics views. Call this once per AI run; metrics from different runIds cannot be combined.
Returns nil if the config was not obtained via the Client.
func (*Config) CustomModelParam ¶
CustomModelParam returns the custom model parameter named by key. The second parameter is true if the key exists.
func (*Config) EvaluationMetricKey ¶ added in v0.8.0
EvaluationMetricKey returns the evaluation metric key for judge mode configs.
func (*Config) EvaluationMetricKeys ¶ added in v0.8.0
EvaluationMetricKeys returns the deprecated array of evaluation metric keys. Use EvaluationMetricKey instead.
func (*Config) JudgeConfiguration ¶ added in v0.8.0
func (c *Config) JudgeConfiguration() *datamodel.JudgeConfiguration
JudgeConfiguration returns the judge configuration attached to this config, if any. Returns a defensive copy to prevent mutations.
func (*Config) Messages ¶
Messages returns the messages defined by the config. The series of messages may be passed to an AI model provider.
func (*Config) Mode ¶ added in v0.8.0
Mode returns the AI Config mode (e.g., "completion", "agent", "judge").
func (*Config) ModelName ¶ added in v0.2.0
ModelName returns the model name associated with the config.
func (*Config) ModelParam ¶
ModelParam returns the model parameter named by key. The second parameter is true if the key exists.
func (*Config) ProviderName ¶ added in v0.2.0
ProviderName returns the provider name associated with the config.
func (*Config) VariationKey ¶ added in v0.3.0
VariationKey is used internally by LaunchDarkly.
type ConfigBuilder ¶
type ConfigBuilder struct {
// contains filtered or unexported fields
}
ConfigBuilder is used to define a default AI Config, returned when LaunchDarkly is unreachable or there is an error evaluating the Config.
func NewConfig ¶
func NewConfig() *ConfigBuilder
NewConfig returns a new ConfigBuilder. By default, the Config is disabled.
func (*ConfigBuilder) Build ¶
func (cb *ConfigBuilder) Build() Config
Build creates a Config from the current builder state.
func (*ConfigBuilder) Disable ¶
func (cb *ConfigBuilder) Disable() *ConfigBuilder
Disable disables the config.
func (*ConfigBuilder) Enable ¶
func (cb *ConfigBuilder) Enable() *ConfigBuilder
Enable enables the config.
func (*ConfigBuilder) WithCustomModelParam ¶
func (cb *ConfigBuilder) WithCustomModelParam(key string, value ldvalue.Value) *ConfigBuilder
WithCustomModelParam sets a custom model parameter named by key to the given value. If the key already exists, it will be overwritten.
func (*ConfigBuilder) WithEnabled ¶
func (cb *ConfigBuilder) WithEnabled(enabled bool) *ConfigBuilder
WithEnabled sets whether the config is enabled. See also Enable and Disable.
func (*ConfigBuilder) WithEvaluationMetricKey ¶ added in v0.8.0
func (cb *ConfigBuilder) WithEvaluationMetricKey(key string) *ConfigBuilder
WithEvaluationMetricKey sets the evaluation metric key for judge mode configs.
func (*ConfigBuilder) WithEvaluationMetricKeys ¶ added in v0.8.0
func (cb *ConfigBuilder) WithEvaluationMetricKeys(keys []string) *ConfigBuilder
WithEvaluationMetricKeys sets the deprecated array of evaluation metric keys. Use WithEvaluationMetricKey instead.
func (*ConfigBuilder) WithJudgeConfiguration ¶ added in v0.8.0
func (cb *ConfigBuilder) WithJudgeConfiguration(judgeConfig *datamodel.JudgeConfiguration) *ConfigBuilder
WithJudgeConfiguration sets the judge configuration for this config. The provided judgeConfig is defensively copied.
func (*ConfigBuilder) WithMessage ¶
func (cb *ConfigBuilder) WithMessage(content string, role datamodel.Role) *ConfigBuilder
WithMessage appends a message to the config with the given role.
func (*ConfigBuilder) WithMode ¶ added in v0.8.0
func (cb *ConfigBuilder) WithMode(mode string) *ConfigBuilder
WithMode sets the AI Config mode (e.g., "completion", "agent", "judge").
func (*ConfigBuilder) WithModelName ¶ added in v0.2.0
func (cb *ConfigBuilder) WithModelName(modelName string) *ConfigBuilder
WithModelName sets the model name associated with the config.
func (*ConfigBuilder) WithModelParam ¶
func (cb *ConfigBuilder) WithModelParam(key string, value ldvalue.Value) *ConfigBuilder
WithModelParam sets a model parameter named by key to the given value. If the key already exists, it will be overwritten. Model parameters are generally set by LaunchDarkly; for custom parameters not recognized by LaunchDarkly, use WithModelCustomParam.
func (*ConfigBuilder) WithProviderName ¶ added in v0.2.0
func (cb *ConfigBuilder) WithProviderName(providerName string) *ConfigBuilder
WithProviderName sets the provider name associated with the config.
type EventSink ¶
type EventSink interface {
// TrackMetric sends a named analytic event to LaunchDarkly relevant to a particular context, and containing a
// metric value and additional data.
TrackMetric(
eventName string,
context ldcontext.Context,
metricValue float64,
data ldvalue.Value,
) error
}
EventSink represents the Tracker's requirements for delivering analytic events. This is generally satisfied by the LaunchDarkly SDK's TrackMetric method.
type Feedback ¶
type Feedback string
Feedback represents the feedback provided by a user for a model evaluation.
type MetricSummary ¶ added in v0.7.0
type MetricSummary struct {
// Duration is the tracked duration in milliseconds.
Duration ldcommon.Option[time.Duration]
// Feedback is the tracked user feedback (positive or negative).
Feedback ldcommon.Option[Feedback]
// Tokens contains information about token usage.
Tokens ldcommon.Option[TokenUsage]
// Success indicates whether the operation was successful.
Success ldcommon.Option[bool]
// TimeToFirstToken is the time to the first token in milliseconds.
TimeToFirstToken ldcommon.Option[time.Duration]
}
MetricSummary represents a summary of metrics tracked by the tracker.
type Metrics ¶
type Metrics struct {
// Latency is the latency of the request.
Latency time.Duration
// TimeToFirstToken is the time to the first token of the streamed response.
TimeToFirstToken time.Duration
}
Metrics represents the metrics returned by a model provider for a specific request.
type ProviderResponse ¶
type ProviderResponse struct {
// Usage is the token usage.
Usage TokenUsage
// Metrics is the request metrics.
Metrics Metrics
}
ProviderResponse represents the response from a model provider for a specific request.
type ServerSDK ¶
type ServerSDK interface {
JSONVariation(
key string,
context ldcontext.Context,
defaultVal ldvalue.Value,
) (ldvalue.Value, error)
Loggers() interfaces.LDLoggers
TrackMetric(
eventName string,
context ldcontext.Context,
metricValue float64,
data ldvalue.Value,
) error
}
ServerSDK defines the required methods for the AI SDK to interact with LaunchDarkly. These methods are satisfied by the LaunchDarkly Go Server SDK.
type Stopwatch ¶
type Stopwatch interface {
// Start starts the stopwatch.
Start()
// Stop stops the stopwatch and returns the duration since Start was called.
Stop() time.Duration
}
Stopwatch is used to measure the duration of a task. Start will always be called before Stop. If an implementation is not provided, the Tracker uses a default implementation that delegates to time.Now and time.Since.
type TokenUsage ¶
type TokenUsage struct {
// Total is the total number of tokens used.
Total int
// Input is the number of input tokens used.
Input int
// Output is the number of output tokens used.
Output int
}
TokenUsage represents the token usage returned by a model provider for a specific request.
func (TokenUsage) Set ¶
func (t TokenUsage) Set() bool
Set returns true if any of the fields are non-zero.
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker records metrics for a single AI run. Unless otherwise noted, the Tracker's methods are not safe for concurrent use.
All events a Tracker emits share a runId (a UUIDv4) so LaunchDarkly can correlate them in metrics views. See individual track methods for their specific semantics. Call CreateTracker on the AI Config to start a new run. A ResumptionToken preserves the runId, so events emitted by a Tracker reconstructed in another process correlate with the original run.
func TrackerFromResumptionToken ¶ added in v0.9.0
func TrackerFromResumptionToken(token string, sdk ServerSDK, context ldcontext.Context) (*Tracker, error)
TrackerFromResumptionToken reconstructs a Tracker from a resumption token and the given context. This is used for cross-process scenarios (e.g., deferred feedback) where the original tracker is no longer available but its runId must be reused. The token is obtained from Tracker.ResumptionToken(). The reconstructed tracker will have empty modelName and providerName since these are not included in the token.
func (*Tracker) GetSummary ¶ added in v0.7.0
func (t *Tracker) GetSummary() MetricSummary
GetSummary returns a summary of all metrics that have been tracked using this tracker.
func (*Tracker) ResumptionToken ¶ added in v0.9.0
ResumptionToken returns a URL-safe Base64-encoded token that can be used to reconstruct a tracker in a different process (e.g., for deferred feedback). The token contains the runId, configKey, variationKey, and version. It does not contain modelName or providerName.
func (*Tracker) TrackDuration ¶
TrackDuration tracks the duration of a task. For example, the duration of a model evaluation request may be tracked here. See also TrackRequest. The duration in milliseconds must fit within a float64.
Records at most once per Tracker; further calls are ignored.
func (*Tracker) TrackError ¶ added in v0.4.0
TrackError tracks an unsuccessful model evaluation.
Records at most once per Tracker. TrackSuccess and TrackError share state; only one of the two can record per Tracker, and subsequent calls are ignored.
func (*Tracker) TrackFeedback ¶
TrackFeedback tracks the feedback provided by a user for a model evaluation. If the feedback is not FeedbackPositive or FeedbackNegative, returns an error and does not track anything.
Records at most once per Tracker; further calls are ignored.
func (*Tracker) TrackJudgeResponse ¶ added in v0.8.0
func (t *Tracker) TrackJudgeResponse(response datamodel.JudgeResponse) error
TrackJudgeResponse tracks the evaluation scores from a judge response.
May be called multiple times per Tracker; each call records the scores from the given response.
func (*Tracker) TrackRequest ¶
func (t *Tracker) TrackRequest(task func(c *Config) (ProviderResponse, error)) (ProviderResponse, error)
TrackRequest tracks metrics for a model evaluation request. The task function should return a ProviderResponse which can be used to specify request metrics and token usage. All fields of the returned ProviderResponse are optional.
The task function will be passed the current AI Config, which can be used to obtain any parameters or messages relevant to the request.
If the task returns an error, then the request is not considered successful and no metrics are tracked. Otherwise, the following metrics are tracked:
- Successful model evaluation.
- Any metrics that were that set in the ProviderResponse 2a) If Latency was not set in the ProviderResponse's Metrics field, an automatically measured duration.
- Any token usage that was set in the ProviderResponse.
Subsequent calls re-run the task but emit only metrics not already recorded on this Tracker. Call CreateTracker on the AI Config to start a new run.
func (*Tracker) TrackSuccess ¶
TrackSuccess tracks a successful model evaluation.
Records at most once per Tracker. TrackSuccess and TrackError share state; only one of the two can record per Tracker, and subsequent calls are ignored.
func (*Tracker) TrackTimeToFirstToken ¶ added in v0.5.0
TrackTimeToFirstToken tracks the time to the first token of the streamed response.
Records at most once per Tracker; further calls are ignored.
func (*Tracker) TrackTokens ¶ added in v0.9.0
func (t *Tracker) TrackTokens(usage TokenUsage) error
TrackTokens tracks the token usage for a model evaluation.
Records at most once per Tracker; further calls are ignored.
func (*Tracker) TrackUsage
deprecated
func (t *Tracker) TrackUsage(usage TokenUsage) error
TrackUsage tracks token usage.
Deprecated: Use TrackTokens instead.