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

use liquid templates #2

Merged
merged 1 commit into from
Nov 29, 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
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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/
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion lib/llm_eval_ruby/prompt_adapters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 0 additions & 10 deletions lib/llm_eval_ruby/prompt_adapters/langfuse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %<variable>s
def convert_prompt(prompt)
prompt.gsub(/\{\{([^}]+)\}\}/, '%<\1>s')
end
end
end
end
Expand Down
13 changes: 1 addition & 12 deletions lib/llm_eval_ruby/prompt_adapters/local.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "liquid"
require_relative "base"

module LlmEvalRuby
Expand All @@ -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 %<variable>s
def convert_prompt(prompt)
prompt.gsub(/\{\{([^}]+)\}\}/, '%<\1>s')
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/llm_eval_ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module LlmEvalRuby
VERSION = "0.2.0"
VERSION = "0.2.1"
end
1 change: 1 addition & 0 deletions llm_eval_ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/llm_eval_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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