From 23a2291824b3673cdf47e0799bcda7d32a8473f9 Mon Sep 17 00:00:00 2001 From: blake johnson Date: Wed, 30 Oct 2024 18:42:41 -0400 Subject: [PATCH 1/2] fix bug, json_post is an instance not class method --- lib/instructor/anthropic/patch.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/instructor/anthropic/patch.rb b/lib/instructor/anthropic/patch.rb index 1b6f064..8d65b78 100644 --- a/lib/instructor/anthropic/patch.rb +++ b/lib/instructor/anthropic/patch.rb @@ -24,7 +24,7 @@ def messages(parameters:, response_model: nil, max_retries: 0, validation_contex 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:) + response = json_post(path: '/messages', parameters:) process_response(response, model) end end From a52470d3e0d0d8ad9dd78ad620cf3cb84392bebd Mon Sep 17 00:00:00 2001 From: blake johnson Date: Wed, 30 Oct 2024 18:51:33 -0400 Subject: [PATCH 2/2] removed anthropic tool use beta header, added tool_choice parameter per anthropic spec which forces the model to call the tool by the provided name. were not submitting multiple tools here so we should always call the tool generated by the schema definition. --- lib/instructor/anthropic/patch.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/instructor/anthropic/patch.rb b/lib/instructor/anthropic/patch.rb index 8d65b78..e3e3167 100644 --- a/lib/instructor/anthropic/patch.rb +++ b/lib/instructor/anthropic/patch.rb @@ -23,7 +23,7 @@ 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' } + parameters.merge!(build_tool_choice(function)) response = json_post(path: '/messages', parameters:) process_response(response, model) end @@ -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.