-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: GoogleGemini generation_config param (#665)
* fix generation_config param * sync all parameters to match gemini API * Migrate from active_support to in-house utils --------- Co-authored-by: Andrei Bondarev <andrei@sourcelabs.io>
- Loading branch information
1 parent
da045bb
commit 4c2dad0
Showing
4 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Langchain | ||
module Utils | ||
class HashTransformer | ||
# Converts a string to camelCase | ||
def self.camelize_lower(str) | ||
str.split("_").inject([]) { |buffer, e| buffer.push(buffer.empty? ? e : e.capitalize) }.join | ||
end | ||
|
||
# Recursively transforms the keys of a hash to camel case | ||
def self.deep_transform_keys(hash, &block) | ||
case hash | ||
when Hash | ||
hash.each_with_object({}) do |(key, value), result| | ||
new_key = block.call(key) | ||
result[new_key] = deep_transform_keys(value, &block) | ||
end | ||
when Array | ||
hash.map { |item| deep_transform_keys(item, &block) } | ||
else | ||
hash | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
RSpec.describe Langchain::Utils::HashTransformer do | ||
describe ".camelize_lower" do | ||
it "converts snake_case to camelCase" do | ||
expect(described_class.camelize_lower("example_key")).to eq("exampleKey") | ||
expect(described_class.camelize_lower("nested_key_example")).to eq("nestedKeyExample") | ||
end | ||
|
||
it "handles strings without underscores" do | ||
expect(described_class.camelize_lower("example")).to eq("example") | ||
end | ||
|
||
it "handles empty strings" do | ||
expect(described_class.camelize_lower("")).to eq("") | ||
end | ||
end | ||
|
||
describe ".deep_transform_keys" do | ||
it "transforms keys of a simple hash" do | ||
hash = {example_key: "value", another_key: "another_value"} | ||
result = described_class.deep_transform_keys(hash) { |key| described_class.camelize_lower(key.to_s).to_sym } | ||
|
||
expect(result).to eq({exampleKey: "value", anotherKey: "another_value"}) | ||
end | ||
|
||
it "transforms keys of a nested hash" do | ||
hash = {example_key: {nested_key: "value"}} | ||
result = described_class.deep_transform_keys(hash) { |key| described_class.camelize_lower(key.to_s).to_sym } | ||
|
||
expect(result).to eq({exampleKey: {nestedKey: "value"}}) | ||
end | ||
|
||
it "transforms keys of an array of hashes" do | ||
hash = {array_key: [{nested_key: "value"}, {another_key: "another_value"}]} | ||
result = described_class.deep_transform_keys(hash) { |key| described_class.camelize_lower(key.to_s).to_sym } | ||
|
||
expect(result).to eq({arrayKey: [{nestedKey: "value"}, {anotherKey: "another_value"}]}) | ||
end | ||
|
||
it "handles arrays of non-hash elements" do | ||
hash = {array_key: ["string", 123, :symbol]} | ||
result = described_class.deep_transform_keys(hash) { |key| described_class.camelize_lower(key.to_s).to_sym } | ||
|
||
expect(result).to eq({arrayKey: ["string", 123, :symbol]}) | ||
end | ||
|
||
it "handles non-hash, non-array values" do | ||
hash = {simple_key: "value"} | ||
result = described_class.deep_transform_keys(hash) { |key| described_class.camelize_lower(key.to_s).to_sym } | ||
|
||
expect(result).to eq({simpleKey: "value"}) | ||
end | ||
|
||
it "handles empty hashes" do | ||
hash = {} | ||
result = described_class.deep_transform_keys(hash) { |key| described_class.camelize_lower(key.to_s).to_sym } | ||
|
||
expect(result).to eq({}) | ||
end | ||
|
||
it "handles deeply nested structures" do | ||
hash = {level_one: {level_two: {level_three: {nested_key: "value"}}}} | ||
result = described_class.deep_transform_keys(hash) { |key| described_class.camelize_lower(key.to_s).to_sym } | ||
|
||
expect(result).to eq({levelOne: {levelTwo: {levelThree: {nestedKey: "value"}}}}) | ||
end | ||
end | ||
end |