Skip to content

Commit

Permalink
Allow openai client passthrough (default) behaviour
Browse files Browse the repository at this point in the history
When the `response_model` parameter in the `chat` method is not defined, it should let the openai client operate as originally intended.
  • Loading branch information
sergiobayona committed May 20, 2024
1 parent 83e31fe commit b3bf0c6
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/instructor/openai/patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def with_retries(max_retries, exceptions, &block)
# @param validation_context [Hash] The validation context for the parameters. Optional.
# @return [Object] The processed response.
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)
Expand Down Expand Up @@ -103,7 +105,7 @@ def process_single_response(parsed_response, model)
# Determines the response model based on the provided value.
#
# @param response_model [Class] The response model class or typed array.
# @return [Class] The determined response model class.
# @return [Class] The response model.
def determine_model(response_model)
if response_model.is_a?(T::Types::TypedArray)
@iterable = true
Expand Down
14 changes: 14 additions & 0 deletions spec/openai/patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,18 @@ def self.name
end.to raise_error(Instructor::ValidationError)
end
end

describe 'when the client is used ia a standard manner' do
it 'does not raise an error when the client is used in a standard manner', vcr: 'patching_spec/standard_usage' do
response = patched_client.new.chat(
parameters: {
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: 'How is the weather today in New York?' }]
}
)

expect(response).to be_a(Hash)
expect(response.dig('choices', 0, 'message', 'content')).to be_a(String)
end
end
end
97 changes: 97 additions & 0 deletions spec/vcr_cassettes/patching_spec/standard_usage.yml

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

0 comments on commit b3bf0c6

Please sign in to comment.