Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/instructor/anthropic/patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def messages(parameters:, response_model: nil, max_retries: 0, validation_contex
function = build_function(model)
parameters[:max_tokens] = 1024 unless parameters.key?(:max_tokens)
parameters = prepare_parameters(parameters, validation_context, function)
::Anthropic.configuration.extra_headers = { 'anthropic-beta' => 'tools-2024-04-04' }
response = ::Anthropic::Client.json_post(path: '/messages', parameters:)
parameters.merge!(build_tool_choice(function))
response = json_post(path: '/messages', parameters:)
process_response(response, model)
end
end
Expand All @@ -39,6 +39,20 @@ def process_response(response, model)
iterable? ? process_multiple_responses(parsed_response, model) : process_single_response(parsed_response, model)
end

# Builds the tool choice configuration for the API request.
#
# @param function [Hash] The function details.
# @return [Hash] The tool choice configuration.

def build_tool_choice(function)
{
tool_choice: {
type: 'tool',
name: function[:name]
}
}
end

# Builds the function details for the API request.
#
# @param model [Class] The response model class.
Expand Down