diff --git a/Gemfile.lock b/Gemfile.lock index 5b4c74d..559286b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,9 @@ PATH remote: . specs: - llm_eval_ruby (0.2.0) + llm_eval_ruby (0.2.1) httparty (~> 0.22.0) + liquid (~> 5.5.0) GEM remote: https://rubygems.org/ @@ -17,6 +18,7 @@ GEM multi_xml (>= 0.5.2) json (2.7.5) language_server-protocol (3.17.0.3) + liquid (5.5.1) mini_mime (1.1.5) multi_xml (0.7.1) bigdecimal (~> 3.1) diff --git a/lib/llm_eval_ruby/prompt_adapters/base.rb b/lib/llm_eval_ruby/prompt_adapters/base.rb index b9440ff..abf9452 100644 --- a/lib/llm_eval_ruby/prompt_adapters/base.rb +++ b/lib/llm_eval_ruby/prompt_adapters/base.rb @@ -14,7 +14,8 @@ def fetch_prompt(name:, version: nil) end def compile(prompt:, variables:) - raise NotImplementedError + compiled = render_template(prompt.content, variables) + LlmEvalRuby::PromptTypes::Compiled.new(adapter: self, role: prompt.role, content: compiled) end private @@ -39,6 +40,11 @@ def wrap_response(response) end end end + + def render_template(template, variables) + template = Liquid::Template.parse(template) + template.render(variables.stringify_keys) + end end end end diff --git a/lib/llm_eval_ruby/prompt_adapters/langfuse.rb b/lib/llm_eval_ruby/prompt_adapters/langfuse.rb index 6056af8..95fd7c9 100644 --- a/lib/llm_eval_ruby/prompt_adapters/langfuse.rb +++ b/lib/llm_eval_ruby/prompt_adapters/langfuse.rb @@ -12,21 +12,11 @@ def fetch_prompt(name:, version: nil) handle_response(response) end - def compile(prompt:, variables:) - compiled = format(convert_prompt(prompt.content), variables) - LlmEvalRuby::PromptTypes::Compiled.new(adapter: self, role: prompt.role, content: compiled) - end - private def client @client ||= ApiClients::Langfuse.new(**LlmEvalRuby.config.langfuse_options) end - - # convert {{variable}} to %s - def convert_prompt(prompt) - prompt.gsub(/\{\{([^}]+)\}\}/, '%<\1>s') - end end end end diff --git a/lib/llm_eval_ruby/prompt_adapters/local.rb b/lib/llm_eval_ruby/prompt_adapters/local.rb index dc1f94d..731c89b 100644 --- a/lib/llm_eval_ruby/prompt_adapters/local.rb +++ b/lib/llm_eval_ruby/prompt_adapters/local.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require "liquid" require_relative "base" module LlmEvalRuby @@ -19,18 +20,6 @@ def fetch_prompt(name:, version: nil) # rubocop:disable Lint/UnusedMethodArgumen handle_response(system_prompts + user_prompt) end - - def compile(prompt:, variables:) - compiled = format(convert_prompt(prompt.content), variables) - LlmEvalRuby::PromptTypes::Compiled.new(adapter: self, role: prompt.role, content: compiled) - end - - private - - # convert {{variable}} to %s - def convert_prompt(prompt) - prompt.gsub(/\{\{([^}]+)\}\}/, '%<\1>s') - end end end end diff --git a/lib/llm_eval_ruby/version.rb b/lib/llm_eval_ruby/version.rb index bdffaa6..e864b4a 100644 --- a/lib/llm_eval_ruby/version.rb +++ b/lib/llm_eval_ruby/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module LlmEvalRuby - VERSION = "0.2.0" + VERSION = "0.2.1" end diff --git a/llm_eval_ruby.gemspec b/llm_eval_ruby.gemspec index b39bee0..6ea2f1f 100644 --- a/llm_eval_ruby.gemspec +++ b/llm_eval_ruby.gemspec @@ -36,6 +36,7 @@ Gem::Specification.new do |spec| # Uncomment to register a new dependency of your gem # spec.add_dependency "example-gem", "~> 1.0" spec.add_dependency "httparty", "~> 0.22.0" + spec.add_dependency "liquid", "~> 5.5.0" # For more information and examples about making a new gem, check out our # guide at: https://bundler.io/guides/creating_gem.html diff --git a/spec/llm_eval_ruby_spec.rb b/spec/llm_eval_ruby_spec.rb index 5d530a7..428d335 100644 --- a/spec/llm_eval_ruby_spec.rb +++ b/spec/llm_eval_ruby_spec.rb @@ -2,6 +2,6 @@ RSpec.describe LlmEvalRuby do it "has a version number" do - expect(LlmEvalRuby::VERSION).to be("0.2.0") + expect(LlmEvalRuby::VERSION).to be("0.2.1") end end