-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
fix: GoogleGemini generation_config param #665
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Langchain | ||
module Utils | ||
class HashTransformer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we should've just yanked out the code out of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code here is 100% genuine, not copied from ActiveSupport in any way. In fact It is GPT generated. |
||
# 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 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not change:
to
everywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we always need to delete the parameter even if it is already defined in the generation_config. Otherwise actual payload will have additional unneeded parameters. This case already covered in the specs.