Module: Instructor::OpenAI::Patch
- Defined in:
- lib/instructor/openai/patch.rb
Overview
The ‘Patch` module provides methods for patching and modifying the OpenAI client behavior.
Instance Method Summary collapse
-
#apply_validation_context(parameters, validation_context) ⇒ Hash
Applies the validation context to the parameters.
-
#build_function(model) ⇒ Hash
Builds the function details for the API request.
-
#chat(parameters:, response_model: nil, max_retries: 0, validation_context: nil) ⇒ Object
Sends a chat request to the API and processes the response.
-
#determine_model(response_model) ⇒ Class
Determines the response model based on the provided value.
-
#generate_description(model) ⇒ String
Generates the description for the function.
- #generate_function_name(model) ⇒ Object
-
#iterable?(response) ⇒ Boolean
Checks if the response is iterable.
-
#prepare_parameters(parameters, validation_context, function) ⇒ Hash
Prepares the parameters for the chat request.
-
#process_multiple_responses(parsed_response, model) ⇒ Array<Object>
Processes multiple responses from the API.
-
#process_response(response, model) ⇒ Object
Processes the API response.
-
#process_single_response(parsed_response, model) ⇒ Object
Processes a single response from the API.
- #resolve_tool_choice(function) ⇒ Object
-
#with_retries(max_retries, exceptions) { ... } ⇒ Object
Executes a block of code with retries in case of specific exceptions.
Instance Method Details
#apply_validation_context(parameters, validation_context) ⇒ Hash
Applies the validation context to the parameters.
124 125 126 127 128 129 130 131 132 |
# File 'lib/instructor/openai/patch.rb', line 124 def apply_validation_context(parameters, validation_context) return parameters unless validation_context.is_a?(Hash) Array[validation_context].each_with_index do |, index| parameters[:messages][index][:content] = parameters[:messages][index][:content] % end parameters end |
#build_function(model) ⇒ Hash
Builds the function details for the API request.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/instructor/openai/patch.rb', line 138 def build_function(model) { type: 'function', function: { name: generate_function_name(model), description: generate_description(model), parameters: model.json_schema } } end |
#chat(parameters:, response_model: nil, max_retries: 0, validation_context: nil) ⇒ Object
Sends a chat request to the API and processes the response.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/instructor/openai/patch.rb', line 31 def chat(parameters:, response_model: nil, max_retries: 0, validation_context: nil) return json_post(path: '/chat/completions', parameters:) if response_model.nil? with_retries(max_retries, [JSON::ParserError, Instructor::ValidationError, Faraday::ParsingError]) do model = determine_model(response_model) function = build_function(model) parameters = prepare_parameters(parameters, validation_context, function) response = json_post(path: '/chat/completions', parameters:) process_response(response, model) end end |
#determine_model(response_model) ⇒ Class
Determines the response model based on the provided value.
109 110 111 112 113 114 115 116 117 |
# File 'lib/instructor/openai/patch.rb', line 109 def determine_model(response_model) if response_model.is_a?(T::Types::TypedArray) @iterable = true response_model.type.raw_type else @iterable = false response_model end end |
#generate_description(model) ⇒ String
Generates the description for the function.
157 158 159 160 161 162 163 164 165 |
# File 'lib/instructor/openai/patch.rb', line 157 def generate_description(model) if model.respond_to?(:instructions) raise Instructor::Error, 'The instructions must be a string' unless model.instructions.is_a?(String) model.instructions else "Correctly extracted `#{model.name}` with all the required parameters with correct types" end end |
#generate_function_name(model) ⇒ Object
149 150 151 |
# File 'lib/instructor/openai/patch.rb', line 149 def generate_function_name(model) model.schema.fetch(:title, model.name) end |
#iterable?(response) ⇒ Boolean
Checks if the response is iterable.
170 171 172 |
# File 'lib/instructor/openai/patch.rb', line 170 def iterable?(response) @iterable && response.is_a?(Array) end |
#prepare_parameters(parameters, validation_context, function) ⇒ Hash
Prepares the parameters for the chat request.
49 50 51 52 53 54 |
# File 'lib/instructor/openai/patch.rb', line 49 def prepare_parameters(parameters, validation_context, function) parameters = apply_validation_context(parameters, validation_context) parameters.merge!(tools: [function]) tool_choice = resolve_tool_choice(function) parameters.merge!(tool_choice:) end |
#process_multiple_responses(parsed_response, model) ⇒ Array<Object>
Processes multiple responses from the API.
88 89 90 91 92 93 |
# File 'lib/instructor/openai/patch.rb', line 88 def process_multiple_responses(parsed_response, model) parsed_response.map do |response| instance = model.new(response) instance.valid? ? instance : raise(Instructor::ValidationError) end end |
#process_response(response, model) ⇒ Object
Processes the API response.
74 75 76 77 78 79 80 81 |
# File 'lib/instructor/openai/patch.rb', line 74 def process_response(response, model) parsed_response = Response.new(response).parse if iterable?(parsed_response) process_multiple_responses(parsed_response, model) else process_single_response(parsed_response, model) end end |
#process_single_response(parsed_response, model) ⇒ Object
Processes a single response from the API.
100 101 102 103 |
# File 'lib/instructor/openai/patch.rb', line 100 def process_single_response(parsed_response, model) instance = model.new(parsed_response) instance.valid? ? instance : raise(Instructor::ValidationError) end |
#resolve_tool_choice(function) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/instructor/openai/patch.rb', line 56 def resolve_tool_choice(function) case Instructor.mode when Instructor::Mode::TOOLS.function { type: 'function', function: { name: function[:function][:name] } } when Instructor::Mode::TOOLS.auto 'auto' when Instructor::Mode::TOOLS.required 'required' when Instructor::Mode::TOOLS.none 'none' end end |
#with_retries(max_retries, exceptions) { ... } ⇒ Object
Executes a block of code with retries in case of specific exceptions.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/instructor/openai/patch.rb', line 13 def with_retries(max_retries, exceptions, &block) attempts = 0 begin block.call rescue *exceptions attempts += 1 retry if attempts < max_retries raise end end |