Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
strategy:
matrix:
ruby-version:
- '3.0'
- '3.1'
- '3.2'
- '3.4'
- jruby-9.4

steps:
Expand All @@ -43,7 +43,7 @@ jobs:

- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
ruby-version: 3.2

- name: Install dependencies
run: bundle install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/manual-publish-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
ruby-version: 3.1

- uses: ./.github/actions/build-docs

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
name: Build and Test
uses: ./.github/actions/ci
with:
ruby-version: 3.0
ruby-version: 3.1

- id: build-docs
name: Build docs
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins:
- rubocop-rspec

AllCops:
TargetRubyVersion: 3.0
TargetRubyVersion: 3.1
Include:
- lib/**/*.rb
- spec/**/*.rb
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ LaunchDarkly overview
Supported Ruby versions
-----------------------

This version of the library has a minimum Ruby version of 3.0.0, or 9.4.0 for JRuby.
This version of the library has a minimum Ruby version of 3.1.0, or 9.4.0 for JRuby.

Getting started
-----------
Expand Down
2 changes: 1 addition & 1 deletion examples/chatbot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ We've built a simple console application that demonstrates how LaunchDarkly's Ru

Below, you'll find the build procedure. For more comprehensive instructions, you can visit your [Quickstart page](https://docs.launchdarkly.com/home/ai-configs/quickstart) or the [Ruby reference guide](https://docs.launchdarkly.com/sdk/ai/ruby).

This demo requires Ruby 3.0 or higher.
This demo requires Ruby 3.1 or higher.

## Build Instructions

Expand Down
2 changes: 1 addition & 1 deletion examples/chatbot/aws-bedrock/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source 'https://rubygems.org'

ruby '>= 3.0.0'
ruby '>= 3.1.0'

gem 'aws-sdk-bedrockruntime', '~> 1.0'
gem 'launchdarkly-server-sdk-ai', path: '../../..'
32 changes: 11 additions & 21 deletions examples/chatbot/aws-bedrock/hello_bedrock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Set sdk_key to your LaunchDarkly SDK key.
sdk_key = ENV['LAUNCHDARKLY_SDK_KEY']

# Set config_key to the AI Config key you want to evaluate.
# Set key to the AI Config key you want to evaluate.
ai_config_key = ENV['LAUNCHDARKLY_AI_CONFIG_KEY'] || 'sample-ai-config'

region = ENV['AWS_REGION'] || 'us-east-1'
Expand Down Expand Up @@ -87,26 +87,16 @@ def map_converse_arguments(model_id, messages)
region: region
)


DEFAULT_VALUE = LaunchDarkly::Server::AI::AIConfig.new(
enabled: true,
model: LaunchDarkly::Server::AI::ModelConfig.new(name: 'replace-with-your-model'),
messages: [
LaunchDarkly::Server::AI::Message.new('system',
'You are the backup assistant when something prevents retrieving LaunchDarkly configured assistant. You have the persona of HAL 9000 talking with {{ldctx.name}}'),
]
)

# You can also default to disabled if you are unable to connect to LaunchDarkly services.
# DEFAULT_VALUE = LaunchDarkly::Server::AI::AIConfig.new(
# enabled: false
# )

ai_config = ai_client.completion_config(
ai_config_key,
context,
DEFAULT_VALUE
)
# Pass a default for improved resiliency when the flag is unavailable or LaunchDarkly is unreachable; omit for a disabled default.
# Example:
# default = LaunchDarkly::Server::AI::AIConfig.new(
# enabled: true,
# model: LaunchDarkly::Server::AI::ModelConfig.new(name: 'my-model'),
# provider: LaunchDarkly::Server::AI::ProviderConfig.new(name: 'my-provider'),
# messages: [LaunchDarkly::Server::AI::Message.new('system', 'Fallback system prompt')]
# )
# ai_config = ai_client.completion_config(key: ai_config_key, context:, default:)
ai_config = ai_client.completion_config(key: ai_config_key, context:)

unless ai_config.enabled
puts '*** AI features are disabled'
Expand Down
2 changes: 1 addition & 1 deletion examples/chatbot/openai/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source 'https://rubygems.org'

ruby '>= 3.0.0'
ruby '>= 3.1.0'

gem 'launchdarkly-server-sdk-ai', path: '../../..'
gem 'openai', '~> 0.7.0'
30 changes: 11 additions & 19 deletions examples/chatbot/openai/hello_openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Set sdk_key to your LaunchDarkly SDK key.
sdk_key = ENV['LAUNCHDARKLY_SDK_KEY']

# Set config_key to the AI Config key you want to evaluate.
# Set key to the AI Config key you want to evaluate.
ai_config_key = ENV['LAUNCHDARKLY_AI_CONFIG_KEY'] || 'sample-ai-config'

# Set openai_api_key to your OpenAI API key.
Expand Down Expand Up @@ -58,19 +58,6 @@ def agent_was_helpful(helpful)
end
end

DEFAULT_VALUE = LaunchDarkly::Server::AI::AIConfig.new(
enabled: true,
model: LaunchDarkly::Server::AI::ModelConfig.new(name: 'replace-with-your-model'),
messages: [
LaunchDarkly::Server::AI::Message.new('system',
'You are the backup assistant when something prevents retrieving LaunchDarkly configured assistant. You have the persona of HAL 9000 talking with {{ldctx.name}}'),
]
)

# You can also default to disabled if you are unable to connect to LaunchDarkly services.
# DEFAULT_VALUE = LaunchDarkly::Server::AI::AIConfig.new(
# enabled: false
# )

ld_client = LaunchDarkly::LDClient.new(sdk_key)
ai_client = LaunchDarkly::Server::AI::Client.new(ld_client)
Expand All @@ -89,11 +76,16 @@ def agent_was_helpful(helpful)
name: 'Lucy',
})

ai_config = ai_client.completion_config(
ai_config_key,
context,
DEFAULT_VALUE
)
# Pass a default for improved resiliency when the flag is unavailable or LaunchDarkly is unreachable; omit for a disabled default.
# Example:
# default = LaunchDarkly::Server::AI::AIConfig.new(
# enabled: true,
# model: LaunchDarkly::Server::AI::ModelConfig.new(name: 'my-model'),
# provider: LaunchDarkly::Server::AI::ProviderConfig.new(name: 'my-provider'),
# messages: [LaunchDarkly::Server::AI::Message.new('system', 'Fallback system prompt')]
# )
# ai_config = ai_client.completion_config(key: ai_config_key, context:, default:)
ai_config = ai_client.completion_config(key: ai_config_key, context:)

unless ai_config.enabled
puts '*** AI features are disabled'
Expand Down
2 changes: 1 addition & 1 deletion launchdarkly-server-sdk-ai.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.files = Dir['{lib}/**/*.rb', 'bin/*', 'LICENSE', '*.md']
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 3.0.0'
spec.required_ruby_version = '>= 3.1.0'

spec.add_dependency 'launchdarkly-server-sdk', '~> 8.5'
spec.add_dependency 'logger'
Expand Down
39 changes: 24 additions & 15 deletions lib/server/ai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ def initialize(enabled: nil, model: nil, messages: nil, tracker: nil, provider:
@provider = provider
end

#
# Returns a new disabled AIConfig instance.
#
# @return [AIConfig] a new disabled config
#
def self.disabled
new(enabled: false)
end

def to_h
{
_ldMeta: {
Expand Down Expand Up @@ -161,31 +170,31 @@ def initialize(ld_client)
#
# Retrieves the AIConfig
#
# @param config_key [String] The key of the configuration flag
# @param key [String] The key of the configuration flag
# @param context [LDContext] The context used when evaluating the flag
# @param default_value [AIConfig] The default value to use if the flag is not found
# @param default [AIConfig] The default value to use if the flag is not found
# @param variables [Hash] Optional variables for rendering messages
# @return [AIConfig] An AIConfig instance containing the configuration data
#
def completion_config(config_key, context, default_value = nil, variables = nil)
@ld_client.track(TRACK_USAGE_COMPLETION_CONFIG, context, config_key, 1)
def completion_config(key:, context:, default: nil, variables: nil)
@ld_client.track(TRACK_USAGE_COMPLETION_CONFIG, context, key, 1)

_completion_config(config_key, context, default_value, variables)
_completion_config(key:, context:, default: default || AIConfig.disabled, variables:)
end

# @deprecated Use {#completion_config} instead.
def config(config_key, context, default_value = nil, variables = nil)
def config(key:, context:, default: nil, variables: nil)
warn '[DEPRECATION] `config` is deprecated. Use `completion_config` instead.'
completion_config(config_key, context, default_value, variables)
completion_config(key:, context:, default:, variables:)
end

private

def _completion_config(config_key, context, default_value = nil, variables = nil)
def _completion_config(key:, context:, default:, variables: nil)
variation = @ld_client.variation(
config_key,
key,
context,
default_value.respond_to?(:to_h) ? default_value.to_h : nil
default.respond_to?(:to_h) ? default.to_h : nil
)

all_variables = variables ? variables.dup : {}
Expand Down Expand Up @@ -220,18 +229,18 @@ def _completion_config(config_key, context, default_value = nil, variables = nil
tracker = LaunchDarkly::Server::AI::AIConfigTracker.new(
ld_client: @ld_client,
variation_key: variation.dig(:_ldMeta, :variationKey) || '',
config_key: config_key,
config_key: key,
version: variation.dig(:_ldMeta, :version) || 1,
model_name: model&.name || '',
provider_name: provider_config&.name || '',
context: context
context:
)

AIConfig.new(
enabled: variation.dig(:_ldMeta, :enabled) || false,
messages: messages,
tracker: tracker,
model: model,
messages:,
tracker:,
model:,
provider: provider_config
)
end
Expand Down
Loading