Skip to content

Commit

Permalink
Update gemini to new output adapter format
Browse files Browse the repository at this point in the history
  • Loading branch information
swerner committed Apr 30, 2024
1 parent 8fdacfa commit 7b7e150
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/sublayer/providers/gemini.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ def self.call(prompt:, output_adapter:)
Here are the tools available:
<tools>
<tool>
#{output_adapter.to_xml}
</tool>
<tool_description>
<tool_name>#{output_adapter.name}</tool_name>
<tool_description>#{output_adapter.description}</tool_description>
<parameters>
#{format_properties(output_adapter)}
</parameters>
</tool_description>
</tools>
Respond only with valid xml.
Expand All @@ -49,6 +53,15 @@ def self.call(prompt:, output_adapter:)
raise "Gemini did not format response, error: #{response.body}" unless tool_output
return tool_output
end

private
def self.format_properties(output_adapter)
output_adapter.properties.each_with_object("") do |property, xml|
xml << "<name>#{property.name}</name>"
xml << "<type>#{property.type}</type>"
xml << "<description>#{property.description}</description>"
end
end
end
end
end
25 changes: 25 additions & 0 deletions spec/generators/examples/code_from_description_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,30 @@ def generate(description:, technologies: ["ruby"])
end

end

context "Gemini" do
before do
Sublayer.configuration.ai_provider = Sublayer::Providers::Gemini
Sublayer.configuration.ai_model = "gemini-pro"
end

it "generates code from description" do
VCR.use_cassette("gemini/generators/code_from_description_generator/hello_world") do
code = generate(description: "a hello world app where I pass --who argument to set the 'world' value using optparser")
expect(code.strip).to eq %q(#!/usr/bin/env ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.on('--who WHO', 'Who to greet (default: World)') do |who|
options[:who] = who
end
end.parse!
puts "Hello, #{options[:who]}!")
end
end
end
end
end
33 changes: 33 additions & 0 deletions spec/generators/examples/description_from_code_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,37 @@ def generate(code)
end
end
end

context "Gemini" do
before do
Sublayer.configuration.ai_provider = Sublayer::Providers::Gemini
Sublayer.configuration.ai_model = "gemini-pro"
end

it "generates description from hello world code" do
VCR.use_cassette("gemini/generators/description_from_code_generator/hello_world") do
code = %q(#!/usr/bin/env ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: hello.rb [options]"
opts.on("-w", "--who PERSON", "Name of the person to greet") do |person|
options[:who] = person
end
end.parse!
who = options[:who] || "world"
puts "Hello, #{who}!")

description = generate(code)
expect(description.strip).to eq <<~DESCRIPTION.strip
This code is a simple command-line script that greets a person by name. It takes an optional argument, `--who`, to specify the name of the person to greet, and defaults to \"world\" if no name is provided. The script then prints a greeting to the specified person.
DESCRIPTION
end
end

end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
config.hook_into :webmock
config.filter_sensitive_data("<OPENAI_API_KEY>") { ENV.fetch("OPENAI_API_KEY") }
config.filter_sensitive_data("<ANTHROPIC_API_KEY>") { ENV.fetch("ANTHROPIC_API_KEY") }
config.filter_sensitive_data("<GEMINI_API_KEY>") { ENV.fetch("GEMINI_API_KEY") }
end

RSpec.configure do |config|
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b7e150

Please sign in to comment.