geminize 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/geminize.rb CHANGED
@@ -33,6 +33,8 @@ require_relative "geminize/models/tool"
33
33
  require_relative "geminize/models/tool_config"
34
34
  require_relative "geminize/models/function_response"
35
35
  require_relative "geminize/models/safety_setting"
36
+ require_relative "geminize/models/code_execution/executable_code"
37
+ require_relative "geminize/models/code_execution/code_execution_result"
36
38
  require_relative "geminize/request_builder"
37
39
  require_relative "geminize/vector_utils"
38
40
  require_relative "geminize/text_generation"
@@ -46,8 +48,6 @@ require_relative "geminize/model_info"
46
48
  require_relative "geminize/models/content_request_extensions"
47
49
  require_relative "geminize/models/content_response_extensions"
48
50
  require_relative "geminize/models/content_request_safety"
49
- require_relative "geminize/module_extensions"
50
- require_relative "geminize/module_safety"
51
51
 
52
52
  # Main module for the Geminize gem
53
53
  module Geminize
@@ -99,7 +99,7 @@ module Geminize
99
99
  # Geminize.configure do |config|
100
100
  # config.api_key = "your-api-key"
101
101
  # config.api_version = "v1beta"
102
- # config.default_model = "gemini-1.5-pro-latest"
102
+ # config.default_model = "gemini-2.0-flash"
103
103
  # end
104
104
  def configure
105
105
  yield(configuration) if block_given?
@@ -515,7 +515,7 @@ module Geminize
515
515
  end
516
516
 
517
517
  # Get information about a specific model
518
- # @param model_name [String] The model name to retrieve (can be "models/gemini-1.5-pro" or just "gemini-1.5-pro")
518
+ # @param model_name [String] The model name to retrieve (can be "models/gemini-2.0-flash" or just "gemini-2.0-flash")
519
519
  # @param force_refresh [Boolean] Force a refresh from the API instead of using cache
520
520
  # @param client_options [Hash] Options to pass to the client
521
521
  # @return [Geminize::Models::Model] The model information
@@ -589,5 +589,413 @@ module Geminize
589
589
  validate_configuration!
590
590
  conversation_service.update_conversation_system_instruction(id, system_instruction)
591
591
  end
592
+
593
+ # Generate text with custom safety settings
594
+ # @param prompt [String] The input prompt
595
+ # @param safety_settings [Array<Hash>] Array of safety setting definitions
596
+ # @param model_name [String, nil] The model to use (optional)
597
+ # @param params [Hash] Additional generation parameters
598
+ # @option params [Float] :temperature Controls randomness (0.0-1.0)
599
+ # @option params [Integer] :max_tokens Maximum tokens to generate
600
+ # @option params [Float] :top_p Top-p value for nucleus sampling (0.0-1.0)
601
+ # @option params [Integer] :top_k Top-k value for sampling
602
+ # @option params [Array<String>] :stop_sequences Stop sequences to end generation
603
+ # @option params [String] :system_instruction System instruction to guide model behavior
604
+ # @option params [Boolean] :with_retries Enable retries for transient errors (default: true)
605
+ # @option params [Integer] :max_retries Maximum retry attempts (default: 3)
606
+ # @option params [Float] :retry_delay Initial delay between retries in seconds (default: 1.0)
607
+ # @option params [Hash] :client_options Options to pass to the client
608
+ # @return [Geminize::Models::ContentResponse] The generation response
609
+ # @raise [Geminize::GeminizeError] If the request fails
610
+ # @example Generate text with specific safety settings
611
+ # Geminize.generate_with_safety_settings(
612
+ # "Tell me a scary story",
613
+ # [
614
+ # {category: "HARM_CATEGORY_DANGEROUS_CONTENT", threshold: "BLOCK_MEDIUM_AND_ABOVE"},
615
+ # {category: "HARM_CATEGORY_HATE_SPEECH", threshold: "BLOCK_LOW_AND_ABOVE"}
616
+ # ]
617
+ # )
618
+ def generate_with_safety_settings(prompt, safety_settings, model_name = nil, params = {})
619
+ validate_configuration!
620
+
621
+ # Extract special options
622
+ with_retries = params.delete(:with_retries) != false # Default to true
623
+ max_retries = params.delete(:max_retries) || 3
624
+ retry_delay = params.delete(:retry_delay) || 1.0
625
+ client_options = params.delete(:client_options) || {}
626
+
627
+ # Create the generator and content request
628
+ generator = TextGeneration.new(nil, client_options)
629
+ content_request = Models::ContentRequest.new(
630
+ prompt,
631
+ model_name || configuration.default_model,
632
+ params
633
+ )
634
+
635
+ # Add safety settings to the request
636
+ safety_settings.each do |setting|
637
+ content_request.add_safety_setting(
638
+ setting[:category],
639
+ setting[:threshold]
640
+ )
641
+ end
642
+
643
+ # Generate with or without retries
644
+ if with_retries
645
+ generator.generate_with_retries(content_request, max_retries, retry_delay)
646
+ else
647
+ generator.generate(content_request)
648
+ end
649
+ end
650
+
651
+ # Generate text with maximum safety (blocks most potentially harmful content)
652
+ # @param prompt [String] The input prompt
653
+ # @param model_name [String, nil] The model to use (optional)
654
+ # @param params [Hash] Additional generation parameters
655
+ # @return [Geminize::Models::ContentResponse] The generation response
656
+ # @raise [Geminize::GeminizeError] If the request fails
657
+ # @example Generate text with maximum safety
658
+ # Geminize.generate_text_safe("Tell me about conflicts", nil, temperature: 0.7)
659
+ def generate_text_safe(prompt, model_name = nil, params = {})
660
+ validate_configuration!
661
+
662
+ # Extract special options
663
+ with_retries = params.delete(:with_retries) != false # Default to true
664
+ max_retries = params.delete(:max_retries) || 3
665
+ retry_delay = params.delete(:retry_delay) || 1.0
666
+ client_options = params.delete(:client_options) || {}
667
+
668
+ # Create the generator and content request
669
+ generator = TextGeneration.new(nil, client_options)
670
+ content_request = Models::ContentRequest.new(
671
+ prompt,
672
+ model_name || configuration.default_model,
673
+ params
674
+ )
675
+
676
+ # Set maximum safety (block low and above)
677
+ content_request.block_all_harmful_content
678
+
679
+ # Generate with or without retries
680
+ if with_retries
681
+ generator.generate_with_retries(content_request, max_retries, retry_delay)
682
+ else
683
+ generator.generate(content_request)
684
+ end
685
+ end
686
+
687
+ # Generate text with minimum safety (blocks only high-risk content)
688
+ # @param prompt [String] The input prompt
689
+ # @param model_name [String, nil] The model to use (optional)
690
+ # @param params [Hash] Additional generation parameters
691
+ # @return [Geminize::Models::ContentResponse] The generation response
692
+ # @raise [Geminize::GeminizeError] If the request fails
693
+ # @example Generate text with minimum safety
694
+ # Geminize.generate_text_permissive("Tell me about conflicts", nil, temperature: 0.7)
695
+ def generate_text_permissive(prompt, model_name = nil, params = {})
696
+ validate_configuration!
697
+
698
+ # Extract special options
699
+ with_retries = params.delete(:with_retries) != false # Default to true
700
+ max_retries = params.delete(:max_retries) || 3
701
+ retry_delay = params.delete(:retry_delay) || 1.0
702
+ client_options = params.delete(:client_options) || {}
703
+
704
+ # Create the generator and content request
705
+ generator = TextGeneration.new(nil, client_options)
706
+ content_request = Models::ContentRequest.new(
707
+ prompt,
708
+ model_name || configuration.default_model,
709
+ params
710
+ )
711
+
712
+ # Set minimum safety (block only high risk)
713
+ content_request.block_only_high_risk_content
714
+
715
+ # Generate with or without retries
716
+ if with_retries
717
+ generator.generate_with_retries(content_request, max_retries, retry_delay)
718
+ else
719
+ generator.generate(content_request)
720
+ end
721
+ end
722
+
723
+ # Generate text with function calling capabilities
724
+ # @param prompt [String] The input prompt
725
+ # @param functions [Array<Hash>] Array of function definitions
726
+ # @param model_name [String, nil] The model to use, defaults to the configured default model
727
+ # @param params [Hash] Additional parameters for generation
728
+ # @option params [Float] :temperature Controls randomness (0.0-1.0)
729
+ # @option params [Integer] :max_tokens Maximum tokens to generate
730
+ # @option params [Float] :top_p Top-p value for nucleus sampling (0.0-1.0)
731
+ # @option params [Integer] :top_k Top-k value for sampling
732
+ # @option params [Array<String>] :stop_sequences Stop sequences to end generation
733
+ # @option params [String] :system_instruction System instruction to guide model behavior
734
+ # @option params [String] :tool_execution_mode Tool execution mode ("AUTO", "MANUAL", or "NONE")
735
+ # @param with_retries [Boolean] Whether to retry the generation if it fails
736
+ # @param max_retries [Integer] Maximum number of retries
737
+ # @param retry_delay [Float] Delay between retries in seconds
738
+ # @param client_options [Hash] Options for the HTTP client
739
+ # @return [Geminize::Models::ContentResponse] The generated response
740
+ # @raise [Geminize::Error] If the generation fails
741
+ # @example Generate text with a function call
742
+ # Geminize.generate_with_functions(
743
+ # "What's the weather in New York?",
744
+ # [
745
+ # {
746
+ # name: "get_weather",
747
+ # description: "Get the current weather in a location",
748
+ # parameters: {
749
+ # type: "object",
750
+ # properties: {
751
+ # location: {
752
+ # type: "string",
753
+ # description: "The city and state, e.g. New York, NY"
754
+ # },
755
+ # unit: {
756
+ # type: "string",
757
+ # enum: ["celsius", "fahrenheit"],
758
+ # description: "The unit of temperature"
759
+ # }
760
+ # },
761
+ # required: ["location"]
762
+ # }
763
+ # }
764
+ # ]
765
+ # )
766
+ def generate_with_functions(prompt, functions, model_name = nil, params = {}, with_retries: true, max_retries: 3, retry_delay: 1.0, client_options: nil)
767
+ validate_configuration!
768
+
769
+ # Initialize the generator
770
+ client = client_options ? Client.new(client_options) : Client.new
771
+ generator = TextGeneration.new(client)
772
+
773
+ # Parse functions
774
+ if functions.nil? || !functions.is_a?(Array) || functions.empty?
775
+ raise Geminize::ValidationError.new(
776
+ "Functions must be a non-empty array",
777
+ "INVALID_ARGUMENT"
778
+ )
779
+ end
780
+
781
+ # Set up params with defaults
782
+ generation_params = params.dup
783
+ tool_execution_mode = generation_params.delete(:tool_execution_mode) || "AUTO"
784
+ with_retries = generation_params.delete(:with_retries) != false if generation_params.key?(:with_retries)
785
+
786
+ # Enhance the system instruction to ensure function calling
787
+ generation_params[:system_instruction] ||= ""
788
+ generation_params[:system_instruction] = "You are a helpful assistant. When you encounter a question that you can answer by calling a function, you must always use the provided function. Always respond using the function call format, not with your own text. " + generation_params[:system_instruction]
789
+
790
+ # Create the request
791
+ content_request = Models::ContentRequest.new(
792
+ prompt,
793
+ model_name || configuration.default_model,
794
+ generation_params
795
+ )
796
+
797
+ # Add functions to the request
798
+ functions.each do |function|
799
+ content_request.add_function(
800
+ function[:name],
801
+ function[:description],
802
+ function[:parameters]
803
+ )
804
+ end
805
+
806
+ # Set the tool config
807
+ content_request.set_tool_config(tool_execution_mode)
808
+
809
+ # Generate the response
810
+ if with_retries
811
+ generator.generate_with_retries(content_request, max_retries, retry_delay)
812
+ else
813
+ generator.generate(content_request)
814
+ end
815
+ end
816
+
817
+ # Generate JSON output from a prompt using the Gemini API
818
+ # @param prompt [String] The input prompt
819
+ # @param model_name [String, nil] The model to use, defaults to the configured default model
820
+ # @param params [Hash] Additional parameters for generation
821
+ # @option params [Float] :temperature Controls randomness (0.0-1.0)
822
+ # @option params [Integer] :max_tokens Maximum tokens to generate
823
+ # @option params [Float] :top_p Top-p value for nucleus sampling (0.0-1.0)
824
+ # @option params [Integer] :top_k Top-k value for sampling
825
+ # @option params [Array<String>] :stop_sequences Stop sequences to end generation
826
+ # @option params [String] :system_instruction System instruction to guide model behavior
827
+ # @param with_retries [Boolean] Whether to retry the generation if it fails
828
+ # @param max_retries [Integer] Maximum number of retries
829
+ # @param retry_delay [Float] Delay between retries in seconds
830
+ # @param client_options [Hash] Options for the HTTP client
831
+ # @option params [Hash] :json_schema Schema for the JSON output (optional)
832
+ # @return [Geminize::Models::ContentResponse] The generated response with JSON content
833
+ # @raise [Geminize::Error] If the generation fails
834
+ # @example Generate JSON output
835
+ # response = Geminize.generate_json(
836
+ # "List 3 planets with their diameter",
837
+ # nil,
838
+ # system_instruction: "Return the information as a JSON array"
839
+ # )
840
+ # planets = response.json_response # Returns parsed JSON
841
+ def generate_json(prompt, model_name = nil, params = {}, with_retries: true, max_retries: 3, retry_delay: 1.0, client_options: nil)
842
+ validate_configuration!
843
+
844
+ # Initialize the generator
845
+ client = client_options ? Client.new(client_options) : Client.new
846
+ generator = TextGeneration.new(client)
847
+
848
+ # Set up params with defaults
849
+ generation_params = params.dup
850
+ with_retries = generation_params.delete(:with_retries) != false if generation_params.key?(:with_retries)
851
+
852
+ # Enhance the system instruction for JSON output
853
+ generation_params[:system_instruction] ||= ""
854
+ generation_params[:system_instruction] = "You must respond with valid JSON only, with no explanation or other text. " + generation_params[:system_instruction]
855
+
856
+ # Create the request
857
+ content_request = Models::ContentRequest.new(
858
+ prompt,
859
+ model_name || configuration.default_model,
860
+ generation_params
861
+ )
862
+
863
+ # Enable JSON mode
864
+ content_request.enable_json_mode
865
+
866
+ # Generate the response
867
+ if with_retries
868
+ generator.generate_with_retries(content_request, max_retries, retry_delay)
869
+ else
870
+ generator.generate(content_request)
871
+ end
872
+ end
873
+
874
+ # Process a function call by executing a provided block and returning the result to Gemini
875
+ # @param response [Geminize::Models::ContentResponse] The response containing a function call
876
+ # @param model_name [String, nil] The model to use for the followup, defaults to the configured default model
877
+ # @param with_retries [Boolean] Whether to retry the generation if it fails
878
+ # @param max_retries [Integer] Maximum number of retries
879
+ # @param retry_delay [Float] Delay between retries in seconds
880
+ # @param client_options [Hash] Options for the HTTP client
881
+ # @yield [function_name, args] Block to execute the function
882
+ # @yieldparam function_name [String] The name of the function to execute
883
+ # @yieldparam args [Hash] The arguments to pass to the function
884
+ # @yieldreturn [Hash, Array, String, Numeric, Boolean, nil] The result of the function
885
+ # @return [Geminize::Models::ContentResponse] The response after processing the function
886
+ # @raise [Geminize::Error] If processing fails
887
+ # @example Process a function call
888
+ # response = Geminize.generate_with_functions("What's the weather in New York?", [...])
889
+ # if response.has_function_call?
890
+ # final_response = Geminize.process_function_call(response) do |function_name, args|
891
+ # if function_name == "get_weather"
892
+ # # Call a real weather API here
893
+ # { temperature: 72, conditions: "sunny" }
894
+ # end
895
+ # end
896
+ # puts final_response.text
897
+ # end
898
+ def process_function_call(response, model_name = nil, with_retries: true, max_retries: 3, retry_delay: 1.0, client_options: nil)
899
+ validate_configuration!
900
+
901
+ # Ensure a block is provided
902
+ unless block_given?
903
+ raise Geminize::ValidationError.new(
904
+ "A block must be provided to process the function call",
905
+ "INVALID_ARGUMENT"
906
+ )
907
+ end
908
+
909
+ # Ensure the response has a function call
910
+ unless response.has_function_call?
911
+ raise Geminize::ValidationError.new(
912
+ "The response does not contain a function call",
913
+ "INVALID_ARGUMENT"
914
+ )
915
+ end
916
+
917
+ # Extract function call information
918
+ function_call = response.function_call
919
+ function_name = function_call.name
920
+ function_args = function_call.response
921
+
922
+ # Call the provided block with the function information
923
+ result = yield(function_name, function_args)
924
+
925
+ # Create a function response
926
+ Models::FunctionResponse.new(function_name, result)
927
+
928
+ # Initialize the generator
929
+ client = client_options ? Client.new(client_options) : Client.new
930
+ generator = TextGeneration.new(client)
931
+
932
+ # Create a request with the function result
933
+ content_request = Models::ContentRequest.new(
934
+ "Function #{function_name} returned: #{result.inspect}",
935
+ model_name || configuration.default_model
936
+ )
937
+
938
+ # Generate the response
939
+ if with_retries
940
+ generator.generate_with_retries(content_request, max_retries, retry_delay)
941
+ else
942
+ generator.generate(content_request)
943
+ end
944
+ end
945
+
946
+ # Generate text with code execution capabilities
947
+ # @param prompt [String] The input prompt
948
+ # @param model_name [String, nil] The model to use, defaults to the configured default model
949
+ # @param params [Hash] Additional parameters for generation
950
+ # @option params [Float] :temperature Controls randomness (0.0-1.0)
951
+ # @option params [Integer] :max_tokens Maximum tokens to generate
952
+ # @option params [Float] :top_p Top-p value for nucleus sampling (0.0-1.0)
953
+ # @option params [Integer] :top_k Top-k value for sampling
954
+ # @option params [Array<String>] :stop_sequences Stop sequences to end generation
955
+ # @option params [String] :system_instruction System instruction to guide model behavior
956
+ # @param with_retries [Boolean] Whether to retry the generation if it fails
957
+ # @param max_retries [Integer] Maximum number of retries
958
+ # @param retry_delay [Float] Delay between retries in seconds
959
+ # @param client_options [Hash] Options for the HTTP client
960
+ # @return [Geminize::Models::ContentResponse] The generated response
961
+ # @raise [Geminize::Error] If the generation fails
962
+ # @example Generate text with code execution
963
+ # Geminize.generate_with_code_execution(
964
+ # "What is the sum of the first 50 prime numbers?",
965
+ # nil,
966
+ # { temperature: 0.2 }
967
+ # )
968
+ def generate_with_code_execution(prompt, model_name = nil, params = {}, with_retries: true, max_retries: 3, retry_delay: 1.0, client_options: nil)
969
+ validate_configuration!
970
+
971
+ # Initialize the generator
972
+ client = client_options ? Client.new(client_options) : Client.new
973
+ generator = TextGeneration.new(client)
974
+
975
+ # Set up params with defaults
976
+ generation_params = params.dup
977
+ with_retries = generation_params.delete(:with_retries) != false if generation_params.key?(:with_retries)
978
+
979
+ # Enhance the system instruction to ensure code execution is effective
980
+ generation_params[:system_instruction] ||= ""
981
+ generation_params[:system_instruction] = "You are a helpful assistant with the ability to generate and execute Python code. When appropriate, use code to solve problems or complete tasks. " + generation_params[:system_instruction]
982
+
983
+ # Create the request
984
+ content_request = Models::ContentRequest.new(
985
+ prompt,
986
+ model_name || configuration.default_model,
987
+ generation_params
988
+ )
989
+
990
+ # Enable code execution
991
+ content_request.enable_code_execution
992
+
993
+ # Generate the response
994
+ if with_retries
995
+ generator.generate_with_retries(content_request, max_retries, retry_delay)
996
+ else
997
+ generator.generate(content_request)
998
+ end
999
+ end
592
1000
  end
593
1001
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geminize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nhat Long Nguyen
@@ -182,6 +182,7 @@ files:
182
182
  - README.md
183
183
  - Rakefile
184
184
  - examples/README.md
185
+ - examples/code_execution.rb
185
186
  - examples/configuration.rb
186
187
  - examples/embeddings.rb
187
188
  - examples/function_calling.rb
@@ -204,6 +205,8 @@ files:
204
205
  - lib/geminize/model_info.rb
205
206
  - lib/geminize/models/chat_request.rb
206
207
  - lib/geminize/models/chat_response.rb
208
+ - lib/geminize/models/code_execution/code_execution_result.rb
209
+ - lib/geminize/models/code_execution/executable_code.rb
207
210
  - lib/geminize/models/content_request.rb
208
211
  - lib/geminize/models/content_request_extensions.rb
209
212
  - lib/geminize/models/content_request_safety.rb
@@ -222,8 +225,6 @@ files:
222
225
  - lib/geminize/models/stream_response.rb
223
226
  - lib/geminize/models/tool.rb
224
227
  - lib/geminize/models/tool_config.rb
225
- - lib/geminize/module_extensions.rb
226
- - lib/geminize/module_safety.rb
227
228
  - lib/geminize/request_builder.rb
228
229
  - lib/geminize/text_generation.rb
229
230
  - lib/geminize/validators.rb