From d82e7759cdb93d52739c5e2ea50ec58597135090 Mon Sep 17 00:00:00 2001 From: Sylvain Utard Date: Sun, 23 Mar 2025 21:50:50 +0100 Subject: [PATCH 1/4] Support tools with params --- lib/ruby_llm/providers/gemini/tools.rb | 24 +- ...41022_can_use_tools_without_parameters.yml | 161 ++++++++++++ ...flash_can_use_tools_without_parameters.yml | 174 +++++++++++++ ...-mini_can_use_tools_without_parameters.yml | 231 ++++++++++++++++++ spec/ruby_llm/chat_tools_spec.rb | 20 ++ 5 files changed, 598 insertions(+), 12 deletions(-) create mode 100644 spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml create mode 100644 spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml create mode 100644 spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml diff --git a/lib/ruby_llm/providers/gemini/tools.rb b/lib/ruby_llm/providers/gemini/tools.rb index 6ed53e17c..8cb85b1c3 100644 --- a/lib/ruby_llm/providers/gemini/tools.rb +++ b/lib/ruby_llm/providers/gemini/tools.rb @@ -54,22 +54,22 @@ def function_declaration_for(tool) { name: tool.name, description: tool.description, - parameters: { - type: 'OBJECT', - properties: format_parameters(tool.parameters), - required: tool.parameters.select { |_, p| p.required }.keys.map(&:to_s) - } - } + parameters: tool.parameters.any? ? format_parameters(tool.parameters) : nil + }.compact end # Format tool parameters for Gemini API def format_parameters(parameters) - parameters.transform_values do |param| - { - type: param_type_for_gemini(param.type), - description: param.description - }.compact - end + { + type: 'OBJECT', + properties: parameters.transform_values do |param| + { + type: param_type_for_gemini(param.type), + description: param.description + }.compact + end, + required: parameters.select { |_, p| p.required }.keys.map(&:to_s) + } end # Convert RubyLLM param types to Gemini API types diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml new file mode 100644 index 000000000..5e4524dc4 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml @@ -0,0 +1,161 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.anthropic.com/v1/messages + body: + encoding: UTF-8 + string: '{"model":"claude-3-5-haiku-20241022","messages":[{"role":"user","content":"What''s + the time in Wakanda? Answer in ISO 8601 format."}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"current_time","description":"Gets + the current time in Wakanda","input_schema":{"type":"object","properties":{},"required":[]}}]}' + headers: + User-Agent: + - Faraday v2.12.2 + X-Api-Key: + - "" + Anthropic-Version: + - '2023-06-01' + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Sun, 23 Mar 2025 20:49:18 GMT + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Anthropic-Ratelimit-Requests-Limit: + - '50' + Anthropic-Ratelimit-Requests-Remaining: + - '49' + Anthropic-Ratelimit-Requests-Reset: + - '2025-03-23T20:49:12Z' + Anthropic-Ratelimit-Input-Tokens-Limit: + - '50000' + Anthropic-Ratelimit-Input-Tokens-Remaining: + - '50000' + Anthropic-Ratelimit-Input-Tokens-Reset: + - '2025-03-23T20:49:17Z' + Anthropic-Ratelimit-Output-Tokens-Limit: + - '10000' + Anthropic-Ratelimit-Output-Tokens-Remaining: + - '10000' + Anthropic-Ratelimit-Output-Tokens-Reset: + - '2025-03-23T20:49:18Z' + Anthropic-Ratelimit-Tokens-Limit: + - '60000' + Anthropic-Ratelimit-Tokens-Remaining: + - '60000' + Anthropic-Ratelimit-Tokens-Reset: + - '2025-03-23T20:49:17Z' + Request-Id: + - "" + Anthropic-Organization-Id: + - 382da897-d586-4e0c-bd1c-a9407bbe3b7a + Via: + - 1.1 google + Cf-Cache-Status: + - DYNAMIC + X-Robots-Tag: + - none + Server: + - cloudflare + Cf-Ray: + - "" + body: + encoding: ASCII-8BIT + string: '{"id":"msg_019dz4tY2eKXDCNpz5M1m4ep","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"I''ll + help you get the current time in Wakanda by using the current_time function."},{"type":"tool_use","id":"toolu_01BdVhiLCcneRYjgmCVWKrr7","name":"current_time","input":{}}],"stop_reason":"tool_use","stop_sequence":null,"usage":{"input_tokens":335,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":57}}' + recorded_at: Sun, 23 Mar 2025 20:49:18 GMT +- request: + method: post + uri: https://api.anthropic.com/v1/messages + body: + encoding: UTF-8 + string: '{"model":"claude-3-5-haiku-20241022","messages":[{"role":"user","content":"What''s + the time in Wakanda? Answer in ISO 8601 format."},{"role":"assistant","content":[{"type":"text","text":"I''ll + help you get the current time in Wakanda by using the current_time function."},{"type":"tool_use","id":"toolu_01BdVhiLCcneRYjgmCVWKrr7","name":"current_time","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01BdVhiLCcneRYjgmCVWKrr7","content":"2025-03-23T21:42:00+01:00"}]}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"current_time","description":"Gets + the current time in Wakanda","input_schema":{"type":"object","properties":{},"required":[]}}]}' + headers: + User-Agent: + - Faraday v2.12.2 + X-Api-Key: + - "" + Anthropic-Version: + - '2023-06-01' + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Sun, 23 Mar 2025 20:49:23 GMT + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Anthropic-Ratelimit-Requests-Limit: + - '50' + Anthropic-Ratelimit-Requests-Remaining: + - '49' + Anthropic-Ratelimit-Requests-Reset: + - '2025-03-23T20:49:19Z' + Anthropic-Ratelimit-Input-Tokens-Limit: + - '50000' + Anthropic-Ratelimit-Input-Tokens-Remaining: + - '50000' + Anthropic-Ratelimit-Input-Tokens-Reset: + - '2025-03-23T20:49:22Z' + Anthropic-Ratelimit-Output-Tokens-Limit: + - '10000' + Anthropic-Ratelimit-Output-Tokens-Remaining: + - '10000' + Anthropic-Ratelimit-Output-Tokens-Reset: + - '2025-03-23T20:49:23Z' + Anthropic-Ratelimit-Tokens-Limit: + - '60000' + Anthropic-Ratelimit-Tokens-Remaining: + - '60000' + Anthropic-Ratelimit-Tokens-Reset: + - '2025-03-23T20:49:22Z' + Request-Id: + - "" + Anthropic-Organization-Id: + - 382da897-d586-4e0c-bd1c-a9407bbe3b7a + Via: + - 1.1 google + Cf-Cache-Status: + - DYNAMIC + X-Robots-Tag: + - none + Server: + - cloudflare + Cf-Ray: + - "" + body: + encoding: ASCII-8BIT + string: '{"id":"msg_01PEaHQWTo9NRjCRd8EeqTNS","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"The + current time in Wakanda is 2025-03-23T21:42:00+01:00 in ISO 8601 format. This + includes the date, time, and timezone offset.\n\nTo break it down:\n- Date: + 2025-03-23 (March 23, 2025)\n- Time: 21:42:00 (9:42:00 PM)\n- Timezone: +01:00 + (1 hour ahead of UTC)"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":419,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":114}}' + recorded_at: Sun, 23 Mar 2025 20:49:23 GMT +recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml new file mode 100644 index 000000000..4728f6c54 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml @@ -0,0 +1,174 @@ +--- +http_interactions: +- request: + method: post + uri: https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent + body: + encoding: UTF-8 + string: '{"contents":[{"role":"user","parts":[{"text":"What''s the time in Wakanda? + Answer in ISO 8601 format."}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"current_time","description":"Gets + the current time in Wakanda"}]}]}' + headers: + User-Agent: + - Faraday v2.12.2 + X-Goog-Api-Key: + - "" + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=UTF-8 + Vary: + - Origin + - Referer + - X-Origin + Date: + - Sun, 23 Mar 2025 20:45:23 GMT + Server: + - scaffolding on HTTPServer2 + X-Xss-Protection: + - '0' + X-Frame-Options: + - SAMEORIGIN + X-Content-Type-Options: + - nosniff + Server-Timing: + - gfet4t7; dur=432 + Alt-Svc: + - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 + Transfer-Encoding: + - chunked + body: + encoding: ASCII-8BIT + string: | + { + "candidates": [ + { + "content": { + "parts": [ + { + "functionCall": { + "name": "current_time", + "args": {} + } + } + ], + "role": "model" + }, + "finishReason": "STOP", + "avgLogprobs": 3.1600163007775941e-06 + } + ], + "usageMetadata": { + "promptTokenCount": 29, + "candidatesTokenCount": 3, + "totalTokenCount": 32, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 29 + } + ], + "candidatesTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 3 + } + ] + }, + "modelVersion": "gemini-2.0-flash" + } + recorded_at: Sun, 23 Mar 2025 20:45:23 GMT +- request: + method: post + uri: https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent + body: + encoding: UTF-8 + string: '{"contents":[{"role":"user","parts":[{"text":"What''s the time in Wakanda? + Answer in ISO 8601 format."}]},{"role":"model","parts":[{"functionCall":{"name":"current_time","args":{}}}]},{"role":"user","parts":[{"functionResponse":{"name":"a5002c81-fcdc-4efb-9d82-4c749a0c90f4","response":{"name":"a5002c81-fcdc-4efb-9d82-4c749a0c90f4","content":"2025-03-23T21:42:00+01:00"}}}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"current_time","description":"Gets + the current time in Wakanda"}]}]}' + headers: + User-Agent: + - Faraday v2.12.2 + X-Goog-Api-Key: + - "" + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json; charset=UTF-8 + Vary: + - Origin + - Referer + - X-Origin + Date: + - Sun, 23 Mar 2025 20:45:24 GMT + Server: + - scaffolding on HTTPServer2 + X-Xss-Protection: + - '0' + X-Frame-Options: + - SAMEORIGIN + X-Content-Type-Options: + - nosniff + Server-Timing: + - gfet4t7; dur=530 + Alt-Svc: + - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 + Transfer-Encoding: + - chunked + body: + encoding: ASCII-8BIT + string: | + { + "candidates": [ + { + "content": { + "parts": [ + { + "text": "2025-03-23T21:42:00+01:00\n" + } + ], + "role": "model" + }, + "finishReason": "STOP", + "avgLogprobs": -0.0045383764574160939 + } + ], + "usageMetadata": { + "promptTokenCount": 125, + "candidatesTokenCount": 26, + "totalTokenCount": 151, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 125 + } + ], + "candidatesTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 26 + } + ] + }, + "modelVersion": "gemini-2.0-flash" + } + recorded_at: Sun, 23 Mar 2025 20:45:24 GMT +recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml new file mode 100644 index 000000000..a93a79155 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml @@ -0,0 +1,231 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.openai.com/v1/chat/completions + body: + encoding: UTF-8 + string: '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"What''s + the time in Wakanda? Answer in ISO 8601 format."}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"current_time","description":"Gets + the current time in Wakanda","parameters":{"type":"object","properties":{},"required":[]}}}],"tool_choice":"auto"}' + headers: + User-Agent: + - Faraday v2.12.2 + Authorization: + - Bearer + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Sun, 23 Mar 2025 20:45:59 GMT + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Access-Control-Expose-Headers: + - X-Request-ID + Openai-Organization: + - "" + Openai-Processing-Ms: + - '989' + Openai-Version: + - '2020-10-01' + X-Ratelimit-Limit-Requests: + - '10000' + X-Ratelimit-Limit-Tokens: + - '200000' + X-Ratelimit-Remaining-Requests: + - '9999' + X-Ratelimit-Remaining-Tokens: + - '199983' + X-Ratelimit-Reset-Requests: + - 8.64s + X-Ratelimit-Reset-Tokens: + - 4ms + X-Request-Id: + - "" + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Cf-Cache-Status: + - DYNAMIC + Set-Cookie: + - "" + - "" + X-Content-Type-Options: + - nosniff + Server: + - cloudflare + Cf-Ray: + - "" + Alt-Svc: + - h3=":443"; ma=86400 + body: + encoding: ASCII-8BIT + string: | + { + "id": "chatcmpl-BEMUgQrMtwcu7AqBtroHnnVZvINHA", + "object": "chat.completion", + "created": 1742762758, + "model": "gpt-4o-mini-2024-07-18", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": null, + "tool_calls": [ + { + "id": "call_M7vR2E6FeGVnmxm69ygAOVI9", + "type": "function", + "function": { + "name": "current_time", + "arguments": "{}" + } + } + ], + "refusal": null, + "annotations": [] + }, + "logprobs": null, + "finish_reason": "tool_calls" + } + ], + "usage": { + "prompt_tokens": 54, + "completion_tokens": 11, + "total_tokens": 65, + "prompt_tokens_details": { + "cached_tokens": 0, + "audio_tokens": 0 + }, + "completion_tokens_details": { + "reasoning_tokens": 0, + "audio_tokens": 0, + "accepted_prediction_tokens": 0, + "rejected_prediction_tokens": 0 + } + }, + "service_tier": "default", + "system_fingerprint": "fp_b8bc95a0ac" + } + recorded_at: Sun, 23 Mar 2025 20:45:59 GMT +- request: + method: post + uri: https://api.openai.com/v1/chat/completions + body: + encoding: UTF-8 + string: '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"What''s + the time in Wakanda? Answer in ISO 8601 format."},{"role":"assistant","tool_calls":[{"id":"call_M7vR2E6FeGVnmxm69ygAOVI9","type":"function","function":{"name":"current_time","arguments":"{}"}}]},{"role":"tool","content":"2025-03-23T21:42:00+01:00","tool_call_id":"call_M7vR2E6FeGVnmxm69ygAOVI9"}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"current_time","description":"Gets + the current time in Wakanda","parameters":{"type":"object","properties":{},"required":[]}}}],"tool_choice":"auto"}' + headers: + User-Agent: + - Faraday v2.12.2 + Authorization: + - Bearer + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Date: + - Sun, 23 Mar 2025 20:46:00 GMT + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Access-Control-Expose-Headers: + - X-Request-ID + Openai-Organization: + - "" + Openai-Processing-Ms: + - '806' + Openai-Version: + - '2020-10-01' + X-Ratelimit-Limit-Requests: + - '10000' + X-Ratelimit-Limit-Tokens: + - '200000' + X-Ratelimit-Remaining-Requests: + - '9998' + X-Ratelimit-Remaining-Tokens: + - '199976' + X-Ratelimit-Reset-Requests: + - 12.771s + X-Ratelimit-Reset-Tokens: + - 7ms + X-Request-Id: + - "" + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + Cf-Cache-Status: + - DYNAMIC + Set-Cookie: + - "" + - "" + X-Content-Type-Options: + - nosniff + Server: + - cloudflare + Cf-Ray: + - "" + Alt-Svc: + - h3=":443"; ma=86400 + body: + encoding: ASCII-8BIT + string: | + { + "id": "chatcmpl-BEMUhrcIGdRBw7mvbSihoAlOTYknS", + "object": "chat.completion", + "created": 1742762759, + "model": "gpt-4o-mini-2024-07-18", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "The current time in Wakanda is 2025-03-23T21:42:00+01:00.", + "refusal": null, + "annotations": [] + }, + "logprobs": null, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 88, + "completion_tokens": 27, + "total_tokens": 115, + "prompt_tokens_details": { + "cached_tokens": 0, + "audio_tokens": 0 + }, + "completion_tokens_details": { + "reasoning_tokens": 0, + "audio_tokens": 0, + "accepted_prediction_tokens": 0, + "rejected_prediction_tokens": 0 + } + }, + "service_tier": "default", + "system_fingerprint": "fp_b8bc95a0ac" + } + recorded_at: Sun, 23 Mar 2025 20:46:00 GMT +recorded_with: VCR 6.3.1 diff --git a/spec/ruby_llm/chat_tools_spec.rb b/spec/ruby_llm/chat_tools_spec.rb index 0de23b9bb..3843d0bb2 100644 --- a/spec/ruby_llm/chat_tools_spec.rb +++ b/spec/ruby_llm/chat_tools_spec.rb @@ -16,6 +16,18 @@ def execute(latitude:, longitude:) end end + class CurrentTime < RubyLLM::Tool # rubocop:disable Lint/ConstantDefinitionInBlock,RSpec/LeakyConstantDeclaration + description 'Gets the current time in Wakanda' + + def initialize(now) + @now = now + end + + def execute + @now.iso8601 + end + end + describe 'function calling' do [ 'claude-3-5-haiku-20241022', @@ -44,6 +56,14 @@ def execute(latitude:, longitude:) expect(response.content).to include('10') end + it "#{model} can use tools without parameters" do + now = Time.new(2025, 3, 23, 21, 42, 0) + chat = RubyLLM.chat(model: model) + .with_tool(CurrentTime.new(now)) + response = chat.ask("What's the time in Wakanda? Answer in ISO 8601 format.") + expect(response.content).to include(now.iso8601) + end + it "#{model} can use tools with multi-turn streaming conversations" do # rubocop:disable RSpec/ExampleLength,RSpec/MultipleExpectations chat = RubyLLM.chat(model: model) .with_tool(Weather) From 61802bc1cd49c6e2f1f7272f1022bb82f062555e Mon Sep 17 00:00:00 2001 From: Sylvain Utard Date: Mon, 24 Mar 2025 20:34:08 +0100 Subject: [PATCH 2/4] fixed lint --- spec/ruby_llm/chat_tools_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/ruby_llm/chat_tools_spec.rb b/spec/ruby_llm/chat_tools_spec.rb index 3843d0bb2..79f3a0d23 100644 --- a/spec/ruby_llm/chat_tools_spec.rb +++ b/spec/ruby_llm/chat_tools_spec.rb @@ -19,12 +19,14 @@ def execute(latitude:, longitude:) class CurrentTime < RubyLLM::Tool # rubocop:disable Lint/ConstantDefinitionInBlock,RSpec/LeakyConstantDeclaration description 'Gets the current time in Wakanda' + attr_reader :now + def initialize(now) @now = now end def execute - @now.iso8601 + now.iso8601 end end From 4dad99cc3d8e75c494d86f9205588a262ad4a7d3 Mon Sep 17 00:00:00 2001 From: Sylvain Utard Date: Mon, 24 Mar 2025 22:54:53 +0100 Subject: [PATCH 3/4] fixed specs --- ...41022_can_use_tools_without_parameters.yml | 45 ++++++++++--------- ...flash_can_use_tools_without_parameters.yml | 38 ++++++++-------- ...-mini_can_use_tools_without_parameters.yml | 44 +++++++++--------- spec/ruby_llm/chat_tools_spec.rb | 6 +-- 4 files changed, 67 insertions(+), 66 deletions(-) diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml index 5e4524dc4..1757cb196 100644 --- a/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml @@ -6,7 +6,7 @@ http_interactions: body: encoding: UTF-8 string: '{"model":"claude-3-5-haiku-20241022","messages":[{"role":"user","content":"What''s - the time in Wakanda? Answer in ISO 8601 format."}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"current_time","description":"Gets + the time in Wakanda? Answer in ISO 8601 format, UTC timezone."}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"current_time","description":"Gets the current time in Wakanda","input_schema":{"type":"object","properties":{},"required":[]}}]}' headers: User-Agent: @@ -27,7 +27,7 @@ http_interactions: message: OK headers: Date: - - Sun, 23 Mar 2025 20:49:18 GMT + - Mon, 24 Mar 2025 21:53:07 GMT Content-Type: - application/json Transfer-Encoding: @@ -39,25 +39,25 @@ http_interactions: Anthropic-Ratelimit-Requests-Remaining: - '49' Anthropic-Ratelimit-Requests-Reset: - - '2025-03-23T20:49:12Z' + - '2025-03-24T21:53:05Z' Anthropic-Ratelimit-Input-Tokens-Limit: - '50000' Anthropic-Ratelimit-Input-Tokens-Remaining: - '50000' Anthropic-Ratelimit-Input-Tokens-Reset: - - '2025-03-23T20:49:17Z' + - '2025-03-24T21:53:06Z' Anthropic-Ratelimit-Output-Tokens-Limit: - '10000' Anthropic-Ratelimit-Output-Tokens-Remaining: - '10000' Anthropic-Ratelimit-Output-Tokens-Reset: - - '2025-03-23T20:49:18Z' + - '2025-03-24T21:53:07Z' Anthropic-Ratelimit-Tokens-Limit: - '60000' Anthropic-Ratelimit-Tokens-Remaining: - '60000' Anthropic-Ratelimit-Tokens-Reset: - - '2025-03-23T20:49:17Z' + - '2025-03-24T21:53:06Z' Request-Id: - "" Anthropic-Organization-Id: @@ -74,17 +74,19 @@ http_interactions: - "" body: encoding: ASCII-8BIT - string: '{"id":"msg_019dz4tY2eKXDCNpz5M1m4ep","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"I''ll - help you get the current time in Wakanda by using the current_time function."},{"type":"tool_use","id":"toolu_01BdVhiLCcneRYjgmCVWKrr7","name":"current_time","input":{}}],"stop_reason":"tool_use","stop_sequence":null,"usage":{"input_tokens":335,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":57}}' - recorded_at: Sun, 23 Mar 2025 20:49:18 GMT + string: '{"id":"msg_01S6KRyrToAJkHjfnyw98ox4","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"I''ll + help you get the current time in Wakanda. I''ll retrieve the current time + and present it in the ISO 8601 format with UTC timezone."},{"type":"tool_use","id":"toolu_016VWoRj6Pfms3RDGJkaQDPs","name":"current_time","input":{}}],"stop_reason":"tool_use","stop_sequence":null,"usage":{"input_tokens":338,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":71}}' + recorded_at: Mon, 24 Mar 2025 21:53:07 GMT - request: method: post uri: https://api.anthropic.com/v1/messages body: encoding: UTF-8 string: '{"model":"claude-3-5-haiku-20241022","messages":[{"role":"user","content":"What''s - the time in Wakanda? Answer in ISO 8601 format."},{"role":"assistant","content":[{"type":"text","text":"I''ll - help you get the current time in Wakanda by using the current_time function."},{"type":"tool_use","id":"toolu_01BdVhiLCcneRYjgmCVWKrr7","name":"current_time","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01BdVhiLCcneRYjgmCVWKrr7","content":"2025-03-23T21:42:00+01:00"}]}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"current_time","description":"Gets + the time in Wakanda? Answer in ISO 8601 format, UTC timezone."},{"role":"assistant","content":[{"type":"text","text":"I''ll + help you get the current time in Wakanda. I''ll retrieve the current time + and present it in the ISO 8601 format with UTC timezone."},{"type":"tool_use","id":"toolu_016VWoRj6Pfms3RDGJkaQDPs","name":"current_time","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_016VWoRj6Pfms3RDGJkaQDPs","content":"2025-03-23T20:42:00Z"}]}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"current_time","description":"Gets the current time in Wakanda","input_schema":{"type":"object","properties":{},"required":[]}}]}' headers: User-Agent: @@ -105,7 +107,7 @@ http_interactions: message: OK headers: Date: - - Sun, 23 Mar 2025 20:49:23 GMT + - Mon, 24 Mar 2025 21:53:10 GMT Content-Type: - application/json Transfer-Encoding: @@ -117,25 +119,25 @@ http_interactions: Anthropic-Ratelimit-Requests-Remaining: - '49' Anthropic-Ratelimit-Requests-Reset: - - '2025-03-23T20:49:19Z' + - '2025-03-24T21:53:08Z' Anthropic-Ratelimit-Input-Tokens-Limit: - '50000' Anthropic-Ratelimit-Input-Tokens-Remaining: - '50000' Anthropic-Ratelimit-Input-Tokens-Reset: - - '2025-03-23T20:49:22Z' + - '2025-03-24T21:53:09Z' Anthropic-Ratelimit-Output-Tokens-Limit: - '10000' Anthropic-Ratelimit-Output-Tokens-Remaining: - '10000' Anthropic-Ratelimit-Output-Tokens-Reset: - - '2025-03-23T20:49:23Z' + - '2025-03-24T21:53:10Z' Anthropic-Ratelimit-Tokens-Limit: - '60000' Anthropic-Ratelimit-Tokens-Remaining: - '60000' Anthropic-Ratelimit-Tokens-Reset: - - '2025-03-23T20:49:22Z' + - '2025-03-24T21:53:09Z' Request-Id: - "" Anthropic-Organization-Id: @@ -152,10 +154,9 @@ http_interactions: - "" body: encoding: ASCII-8BIT - string: '{"id":"msg_01PEaHQWTo9NRjCRd8EeqTNS","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"The - current time in Wakanda is 2025-03-23T21:42:00+01:00 in ISO 8601 format. This - includes the date, time, and timezone offset.\n\nTo break it down:\n- Date: - 2025-03-23 (March 23, 2025)\n- Time: 21:42:00 (9:42:00 PM)\n- Timezone: +01:00 - (1 hour ahead of UTC)"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":419,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":114}}' - recorded_at: Sun, 23 Mar 2025 20:49:23 GMT + string: '{"id":"msg_01VCSRh7a3NsWCkjWcafqTTo","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"The + current time in Wakanda is 2025-03-23T20:42:00Z, which follows the ISO 8601 + format in the UTC timezone. The ''Z'' at the end indicates that this is in + Coordinated Universal Time (UTC)."}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":433,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":62}}' + recorded_at: Mon, 24 Mar 2025 21:53:10 GMT recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml index 4728f6c54..31a7fa6ee 100644 --- a/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml @@ -6,7 +6,7 @@ http_interactions: body: encoding: UTF-8 string: '{"contents":[{"role":"user","parts":[{"text":"What''s the time in Wakanda? - Answer in ISO 8601 format."}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"current_time","description":"Gets + Answer in ISO 8601 format, UTC timezone."}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"current_time","description":"Gets the current time in Wakanda"}]}]}' headers: User-Agent: @@ -31,7 +31,7 @@ http_interactions: - Referer - X-Origin Date: - - Sun, 23 Mar 2025 20:45:23 GMT + - Mon, 24 Mar 2025 21:53:11 GMT Server: - scaffolding on HTTPServer2 X-Xss-Protection: @@ -41,7 +41,7 @@ http_interactions: X-Content-Type-Options: - nosniff Server-Timing: - - gfet4t7; dur=432 + - gfet4t7; dur=516 Alt-Svc: - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 Transfer-Encoding: @@ -64,17 +64,17 @@ http_interactions: "role": "model" }, "finishReason": "STOP", - "avgLogprobs": 3.1600163007775941e-06 + "avgLogprobs": 2.31900776270777e-06 } ], "usageMetadata": { - "promptTokenCount": 29, + "promptTokenCount": 32, "candidatesTokenCount": 3, - "totalTokenCount": 32, + "totalTokenCount": 35, "promptTokensDetails": [ { "modality": "TEXT", - "tokenCount": 29 + "tokenCount": 32 } ], "candidatesTokensDetails": [ @@ -86,14 +86,14 @@ http_interactions: }, "modelVersion": "gemini-2.0-flash" } - recorded_at: Sun, 23 Mar 2025 20:45:23 GMT + recorded_at: Mon, 24 Mar 2025 21:53:11 GMT - request: method: post uri: https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent body: encoding: UTF-8 string: '{"contents":[{"role":"user","parts":[{"text":"What''s the time in Wakanda? - Answer in ISO 8601 format."}]},{"role":"model","parts":[{"functionCall":{"name":"current_time","args":{}}}]},{"role":"user","parts":[{"functionResponse":{"name":"a5002c81-fcdc-4efb-9d82-4c749a0c90f4","response":{"name":"a5002c81-fcdc-4efb-9d82-4c749a0c90f4","content":"2025-03-23T21:42:00+01:00"}}}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"current_time","description":"Gets + Answer in ISO 8601 format, UTC timezone."}]},{"role":"model","parts":[{"functionCall":{"name":"current_time","args":{}}}]},{"role":"user","parts":[{"functionResponse":{"name":"02e75370-ffed-4b37-a77e-1ccf066e7658","response":{"name":"02e75370-ffed-4b37-a77e-1ccf066e7658","content":"2025-03-23T20:42:00Z"}}}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"current_time","description":"Gets the current time in Wakanda"}]}]}' headers: User-Agent: @@ -118,7 +118,7 @@ http_interactions: - Referer - X-Origin Date: - - Sun, 23 Mar 2025 20:45:24 GMT + - Mon, 24 Mar 2025 21:53:11 GMT Server: - scaffolding on HTTPServer2 X-Xss-Protection: @@ -128,7 +128,7 @@ http_interactions: X-Content-Type-Options: - nosniff Server-Timing: - - gfet4t7; dur=530 + - gfet4t7; dur=450 Alt-Svc: - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 Transfer-Encoding: @@ -142,33 +142,33 @@ http_interactions: "content": { "parts": [ { - "text": "2025-03-23T21:42:00+01:00\n" + "text": "2025-03-23T20:42:00Z\n" } ], "role": "model" }, "finishReason": "STOP", - "avgLogprobs": -0.0045383764574160939 + "avgLogprobs": -0.0044805953900019331 } ], "usageMetadata": { - "promptTokenCount": 125, - "candidatesTokenCount": 26, - "totalTokenCount": 151, + "promptTokenCount": 119, + "candidatesTokenCount": 21, + "totalTokenCount": 140, "promptTokensDetails": [ { "modality": "TEXT", - "tokenCount": 125 + "tokenCount": 119 } ], "candidatesTokensDetails": [ { "modality": "TEXT", - "tokenCount": 26 + "tokenCount": 21 } ] }, "modelVersion": "gemini-2.0-flash" } - recorded_at: Sun, 23 Mar 2025 20:45:24 GMT + recorded_at: Mon, 24 Mar 2025 21:53:11 GMT recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml index a93a79155..604dc9a21 100644 --- a/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml @@ -6,7 +6,7 @@ http_interactions: body: encoding: UTF-8 string: '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"What''s - the time in Wakanda? Answer in ISO 8601 format."}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"current_time","description":"Gets + the time in Wakanda? Answer in ISO 8601 format, UTC timezone."}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"current_time","description":"Gets the current time in Wakanda","parameters":{"type":"object","properties":{},"required":[]}}}],"tool_choice":"auto"}' headers: User-Agent: @@ -25,7 +25,7 @@ http_interactions: message: OK headers: Date: - - Sun, 23 Mar 2025 20:45:59 GMT + - Mon, 24 Mar 2025 21:53:12 GMT Content-Type: - application/json Transfer-Encoding: @@ -37,7 +37,7 @@ http_interactions: Openai-Organization: - "" Openai-Processing-Ms: - - '989' + - '453' Openai-Version: - '2020-10-01' X-Ratelimit-Limit-Requests: @@ -47,11 +47,11 @@ http_interactions: X-Ratelimit-Remaining-Requests: - '9999' X-Ratelimit-Remaining-Tokens: - - '199983' + - '199981' X-Ratelimit-Reset-Requests: - 8.64s X-Ratelimit-Reset-Tokens: - - 4ms + - 5ms X-Request-Id: - "" Strict-Transport-Security: @@ -73,9 +73,9 @@ http_interactions: encoding: ASCII-8BIT string: | { - "id": "chatcmpl-BEMUgQrMtwcu7AqBtroHnnVZvINHA", + "id": "chatcmpl-BEk1HDrCd1JHbCFe17oj3g1YYnMmQ", "object": "chat.completion", - "created": 1742762758, + "created": 1742853191, "model": "gpt-4o-mini-2024-07-18", "choices": [ { @@ -85,7 +85,7 @@ http_interactions: "content": null, "tool_calls": [ { - "id": "call_M7vR2E6FeGVnmxm69ygAOVI9", + "id": "call_i0AEa2OVhnmj59JJFzxA2isr", "type": "function", "function": { "name": "current_time", @@ -101,9 +101,9 @@ http_interactions: } ], "usage": { - "prompt_tokens": 54, + "prompt_tokens": 57, "completion_tokens": 11, - "total_tokens": 65, + "total_tokens": 68, "prompt_tokens_details": { "cached_tokens": 0, "audio_tokens": 0 @@ -118,14 +118,14 @@ http_interactions: "service_tier": "default", "system_fingerprint": "fp_b8bc95a0ac" } - recorded_at: Sun, 23 Mar 2025 20:45:59 GMT + recorded_at: Mon, 24 Mar 2025 21:53:12 GMT - request: method: post uri: https://api.openai.com/v1/chat/completions body: encoding: UTF-8 string: '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"What''s - the time in Wakanda? Answer in ISO 8601 format."},{"role":"assistant","tool_calls":[{"id":"call_M7vR2E6FeGVnmxm69ygAOVI9","type":"function","function":{"name":"current_time","arguments":"{}"}}]},{"role":"tool","content":"2025-03-23T21:42:00+01:00","tool_call_id":"call_M7vR2E6FeGVnmxm69ygAOVI9"}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"current_time","description":"Gets + the time in Wakanda? Answer in ISO 8601 format, UTC timezone."},{"role":"assistant","tool_calls":[{"id":"call_i0AEa2OVhnmj59JJFzxA2isr","type":"function","function":{"name":"current_time","arguments":"{}"}}]},{"role":"tool","content":"2025-03-23T20:42:00Z","tool_call_id":"call_i0AEa2OVhnmj59JJFzxA2isr"}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"current_time","description":"Gets the current time in Wakanda","parameters":{"type":"object","properties":{},"required":[]}}}],"tool_choice":"auto"}' headers: User-Agent: @@ -144,7 +144,7 @@ http_interactions: message: OK headers: Date: - - Sun, 23 Mar 2025 20:46:00 GMT + - Mon, 24 Mar 2025 21:53:13 GMT Content-Type: - application/json Transfer-Encoding: @@ -156,7 +156,7 @@ http_interactions: Openai-Organization: - "" Openai-Processing-Ms: - - '806' + - '514' Openai-Version: - '2020-10-01' X-Ratelimit-Limit-Requests: @@ -166,9 +166,9 @@ http_interactions: X-Ratelimit-Remaining-Requests: - '9998' X-Ratelimit-Remaining-Tokens: - - '199976' + - '199973' X-Ratelimit-Reset-Requests: - - 12.771s + - 16.639s X-Ratelimit-Reset-Tokens: - 7ms X-Request-Id: @@ -192,16 +192,16 @@ http_interactions: encoding: ASCII-8BIT string: | { - "id": "chatcmpl-BEMUhrcIGdRBw7mvbSihoAlOTYknS", + "id": "chatcmpl-BEk1IUzbcTIfMx2LURwD6OPZKKu19", "object": "chat.completion", - "created": 1742762759, + "created": 1742853192, "model": "gpt-4o-mini-2024-07-18", "choices": [ { "index": 0, "message": { "role": "assistant", - "content": "The current time in Wakanda is 2025-03-23T21:42:00+01:00.", + "content": "The current time in Wakanda is 2025-03-23T20:42:00Z (UTC).", "refusal": null, "annotations": [] }, @@ -211,8 +211,8 @@ http_interactions: ], "usage": { "prompt_tokens": 88, - "completion_tokens": 27, - "total_tokens": 115, + "completion_tokens": 26, + "total_tokens": 114, "prompt_tokens_details": { "cached_tokens": 0, "audio_tokens": 0 @@ -227,5 +227,5 @@ http_interactions: "service_tier": "default", "system_fingerprint": "fp_b8bc95a0ac" } - recorded_at: Sun, 23 Mar 2025 20:46:00 GMT + recorded_at: Mon, 24 Mar 2025 21:53:13 GMT recorded_with: VCR 6.3.1 diff --git a/spec/ruby_llm/chat_tools_spec.rb b/spec/ruby_llm/chat_tools_spec.rb index 79f3a0d23..0d210476b 100644 --- a/spec/ruby_llm/chat_tools_spec.rb +++ b/spec/ruby_llm/chat_tools_spec.rb @@ -26,7 +26,7 @@ def initialize(now) end def execute - now.iso8601 + now.utc.iso8601 end end @@ -62,8 +62,8 @@ def execute now = Time.new(2025, 3, 23, 21, 42, 0) chat = RubyLLM.chat(model: model) .with_tool(CurrentTime.new(now)) - response = chat.ask("What's the time in Wakanda? Answer in ISO 8601 format.") - expect(response.content).to include(now.iso8601) + response = chat.ask("What's the time in Wakanda? Answer in ISO 8601 format, UTC timezone.") + expect(response.content).to include(now.utc.iso8601) end it "#{model} can use tools with multi-turn streaming conversations" do # rubocop:disable RSpec/ExampleLength,RSpec/MultipleExpectations From e72b3ca79795934536f5fc4f03ca119a0ef71dc3 Mon Sep 17 00:00:00 2001 From: Sylvain Utard Date: Wed, 26 Mar 2025 13:33:13 +0100 Subject: [PATCH 4/4] fixed specs simplifying tests --- ...41022_can_use_tools_without_parameters.yml | 56 +++++++++-------- ...flash_can_use_tools_without_parameters.yml | 52 ++++++++-------- ...-mini_can_use_tools_without_parameters.yml | 60 +++++++++---------- spec/ruby_llm/chat_tools_spec.rb | 20 ++----- 4 files changed, 93 insertions(+), 95 deletions(-) diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml index 1757cb196..625509fae 100644 --- a/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_claude-3-5-haiku-20241022_can_use_tools_without_parameters.yml @@ -6,8 +6,8 @@ http_interactions: body: encoding: UTF-8 string: '{"model":"claude-3-5-haiku-20241022","messages":[{"role":"user","content":"What''s - the time in Wakanda? Answer in ISO 8601 format, UTC timezone."}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"current_time","description":"Gets - the current time in Wakanda","input_schema":{"type":"object","properties":{},"required":[]}}]}' + the best language to learn?"}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"best_language_to_learn","description":"Gets + the best language to learn","input_schema":{"type":"object","properties":{},"required":[]}}]}' headers: User-Agent: - Faraday v2.12.2 @@ -27,7 +27,7 @@ http_interactions: message: OK headers: Date: - - Mon, 24 Mar 2025 21:53:07 GMT + - Wed, 26 Mar 2025 12:36:24 GMT Content-Type: - application/json Transfer-Encoding: @@ -39,25 +39,25 @@ http_interactions: Anthropic-Ratelimit-Requests-Remaining: - '49' Anthropic-Ratelimit-Requests-Reset: - - '2025-03-24T21:53:05Z' + - '2025-03-26T12:36:22Z' Anthropic-Ratelimit-Input-Tokens-Limit: - '50000' Anthropic-Ratelimit-Input-Tokens-Remaining: - '50000' Anthropic-Ratelimit-Input-Tokens-Reset: - - '2025-03-24T21:53:06Z' + - '2025-03-26T12:36:23Z' Anthropic-Ratelimit-Output-Tokens-Limit: - '10000' Anthropic-Ratelimit-Output-Tokens-Remaining: - '10000' Anthropic-Ratelimit-Output-Tokens-Reset: - - '2025-03-24T21:53:07Z' + - '2025-03-26T12:36:23Z' Anthropic-Ratelimit-Tokens-Limit: - '60000' Anthropic-Ratelimit-Tokens-Remaining: - '60000' Anthropic-Ratelimit-Tokens-Reset: - - '2025-03-24T21:53:06Z' + - '2025-03-26T12:36:23Z' Request-Id: - "" Anthropic-Organization-Id: @@ -74,20 +74,18 @@ http_interactions: - "" body: encoding: ASCII-8BIT - string: '{"id":"msg_01S6KRyrToAJkHjfnyw98ox4","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"I''ll - help you get the current time in Wakanda. I''ll retrieve the current time - and present it in the ISO 8601 format with UTC timezone."},{"type":"tool_use","id":"toolu_016VWoRj6Pfms3RDGJkaQDPs","name":"current_time","input":{}}],"stop_reason":"tool_use","stop_sequence":null,"usage":{"input_tokens":338,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":71}}' - recorded_at: Mon, 24 Mar 2025 21:53:07 GMT + string: '{"id":"msg_016N5q71zpXwLnoYPDYjpi92","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"I''ll + help you find out the best language to learn by using the available tool."},{"type":"tool_use","id":"toolu_01HiFoEeZiQJ6CFwCNzT6Nwu","name":"best_language_to_learn","input":{}}],"stop_reason":"tool_use","stop_sequence":null,"usage":{"input_tokens":327,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":58}}' + recorded_at: Wed, 26 Mar 2025 12:36:24 GMT - request: method: post uri: https://api.anthropic.com/v1/messages body: encoding: UTF-8 string: '{"model":"claude-3-5-haiku-20241022","messages":[{"role":"user","content":"What''s - the time in Wakanda? Answer in ISO 8601 format, UTC timezone."},{"role":"assistant","content":[{"type":"text","text":"I''ll - help you get the current time in Wakanda. I''ll retrieve the current time - and present it in the ISO 8601 format with UTC timezone."},{"type":"tool_use","id":"toolu_016VWoRj6Pfms3RDGJkaQDPs","name":"current_time","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_016VWoRj6Pfms3RDGJkaQDPs","content":"2025-03-23T20:42:00Z"}]}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"current_time","description":"Gets - the current time in Wakanda","input_schema":{"type":"object","properties":{},"required":[]}}]}' + the best language to learn?"},{"role":"assistant","content":[{"type":"text","text":"I''ll + help you find out the best language to learn by using the available tool."},{"type":"tool_use","id":"toolu_01HiFoEeZiQJ6CFwCNzT6Nwu","name":"best_language_to_learn","input":{}}]},{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01HiFoEeZiQJ6CFwCNzT6Nwu","content":"Ruby"}]}],"temperature":0.7,"stream":false,"max_tokens":8192,"tools":[{"name":"best_language_to_learn","description":"Gets + the best language to learn","input_schema":{"type":"object","properties":{},"required":[]}}]}' headers: User-Agent: - Faraday v2.12.2 @@ -107,7 +105,7 @@ http_interactions: message: OK headers: Date: - - Mon, 24 Mar 2025 21:53:10 GMT + - Wed, 26 Mar 2025 12:36:30 GMT Content-Type: - application/json Transfer-Encoding: @@ -119,25 +117,25 @@ http_interactions: Anthropic-Ratelimit-Requests-Remaining: - '49' Anthropic-Ratelimit-Requests-Reset: - - '2025-03-24T21:53:08Z' + - '2025-03-26T12:36:25Z' Anthropic-Ratelimit-Input-Tokens-Limit: - '50000' Anthropic-Ratelimit-Input-Tokens-Remaining: - '50000' Anthropic-Ratelimit-Input-Tokens-Reset: - - '2025-03-24T21:53:09Z' + - '2025-03-26T12:36:27Z' Anthropic-Ratelimit-Output-Tokens-Limit: - '10000' Anthropic-Ratelimit-Output-Tokens-Remaining: - '10000' Anthropic-Ratelimit-Output-Tokens-Reset: - - '2025-03-24T21:53:10Z' + - '2025-03-26T12:36:30Z' Anthropic-Ratelimit-Tokens-Limit: - '60000' Anthropic-Ratelimit-Tokens-Remaining: - '60000' Anthropic-Ratelimit-Tokens-Reset: - - '2025-03-24T21:53:09Z' + - '2025-03-26T12:36:27Z' Request-Id: - "" Anthropic-Organization-Id: @@ -154,9 +152,17 @@ http_interactions: - "" body: encoding: ASCII-8BIT - string: '{"id":"msg_01VCSRh7a3NsWCkjWcafqTTo","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"The - current time in Wakanda is 2025-03-23T20:42:00Z, which follows the ISO 8601 - format in the UTC timezone. The ''Z'' at the end indicates that this is in - Coordinated Universal Time (UTC)."}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":433,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":62}}' - recorded_at: Mon, 24 Mar 2025 21:53:10 GMT + string: '{"id":"msg_01VXzvCFsc6ggwCM6qbGXKTK","type":"message","role":"assistant","model":"claude-3-5-haiku-20241022","content":[{"type":"text","text":"According + to the tool, Ruby is recommended as the best language to learn. Ruby is a + dynamic, object-oriented programming language known for its simplicity and + readability. It''s particularly popular for web development, especially with + the Ruby on Rails framework. \n\nSome reasons why Ruby might be a great language + to learn:\n1. Easy to read and write, with a clean and elegant syntax\n2. + Great for web development and scripting\n3. Strong community support\n4. Used + in many startups and tech companies\n5. Good for beginners due to its intuitive + nature\n\nHowever, the \"best\" language can depend on your personal goals, + such as:\n- Web development\n- Data science\n- Mobile app development\n- Game + development\n- Career opportunities\n\nWould you like to know more about Ruby + or discuss how it might fit your specific learning objectives?"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":397,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":182}}' + recorded_at: Wed, 26 Mar 2025 12:36:30 GMT recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml index 31a7fa6ee..51236722c 100644 --- a/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_gemini-2_0-flash_can_use_tools_without_parameters.yml @@ -5,9 +5,9 @@ http_interactions: uri: https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent body: encoding: UTF-8 - string: '{"contents":[{"role":"user","parts":[{"text":"What''s the time in Wakanda? - Answer in ISO 8601 format, UTC timezone."}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"current_time","description":"Gets - the current time in Wakanda"}]}]}' + string: '{"contents":[{"role":"user","parts":[{"text":"What''s the best language + to learn?"}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"best_language_to_learn","description":"Gets + the best language to learn"}]}]}' headers: User-Agent: - Faraday v2.12.2 @@ -31,7 +31,7 @@ http_interactions: - Referer - X-Origin Date: - - Mon, 24 Mar 2025 21:53:11 GMT + - Wed, 26 Mar 2025 12:36:31 GMT Server: - scaffolding on HTTPServer2 X-Xss-Protection: @@ -41,7 +41,7 @@ http_interactions: X-Content-Type-Options: - nosniff Server-Timing: - - gfet4t7; dur=516 + - gfet4t7; dur=505 Alt-Svc: - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 Transfer-Encoding: @@ -56,7 +56,7 @@ http_interactions: "parts": [ { "functionCall": { - "name": "current_time", + "name": "best_language_to_learn", "args": {} } } @@ -64,37 +64,37 @@ http_interactions: "role": "model" }, "finishReason": "STOP", - "avgLogprobs": 2.31900776270777e-06 + "avgLogprobs": -0.00011707750880824668 } ], "usageMetadata": { - "promptTokenCount": 32, - "candidatesTokenCount": 3, - "totalTokenCount": 35, + "promptTokenCount": 22, + "candidatesTokenCount": 7, + "totalTokenCount": 29, "promptTokensDetails": [ { "modality": "TEXT", - "tokenCount": 32 + "tokenCount": 22 } ], "candidatesTokensDetails": [ { "modality": "TEXT", - "tokenCount": 3 + "tokenCount": 7 } ] }, "modelVersion": "gemini-2.0-flash" } - recorded_at: Mon, 24 Mar 2025 21:53:11 GMT + recorded_at: Wed, 26 Mar 2025 12:36:31 GMT - request: method: post uri: https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent body: encoding: UTF-8 - string: '{"contents":[{"role":"user","parts":[{"text":"What''s the time in Wakanda? - Answer in ISO 8601 format, UTC timezone."}]},{"role":"model","parts":[{"functionCall":{"name":"current_time","args":{}}}]},{"role":"user","parts":[{"functionResponse":{"name":"02e75370-ffed-4b37-a77e-1ccf066e7658","response":{"name":"02e75370-ffed-4b37-a77e-1ccf066e7658","content":"2025-03-23T20:42:00Z"}}}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"current_time","description":"Gets - the current time in Wakanda"}]}]}' + string: '{"contents":[{"role":"user","parts":[{"text":"What''s the best language + to learn?"}]},{"role":"model","parts":[{"functionCall":{"name":"best_language_to_learn","args":{}}}]},{"role":"user","parts":[{"functionResponse":{"name":"59a92e0a-8267-4a36-b42f-6c8f41da2add","response":{"name":"59a92e0a-8267-4a36-b42f-6c8f41da2add","content":"Ruby"}}}]}],"generationConfig":{"temperature":0.7},"tools":[{"functionDeclarations":[{"name":"best_language_to_learn","description":"Gets + the best language to learn"}]}]}' headers: User-Agent: - Faraday v2.12.2 @@ -118,7 +118,7 @@ http_interactions: - Referer - X-Origin Date: - - Mon, 24 Mar 2025 21:53:11 GMT + - Wed, 26 Mar 2025 12:36:31 GMT Server: - scaffolding on HTTPServer2 X-Xss-Protection: @@ -128,7 +128,7 @@ http_interactions: X-Content-Type-Options: - nosniff Server-Timing: - - gfet4t7; dur=450 + - gfet4t7; dur=472 Alt-Svc: - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 Transfer-Encoding: @@ -142,33 +142,33 @@ http_interactions: "content": { "parts": [ { - "text": "2025-03-23T20:42:00Z\n" + "text": "I think Ruby is the best language to learn.\n" } ], "role": "model" }, "finishReason": "STOP", - "avgLogprobs": -0.0044805953900019331 + "avgLogprobs": -0.11496557972647926 } ], "usageMetadata": { - "promptTokenCount": 119, - "candidatesTokenCount": 21, - "totalTokenCount": 140, + "promptTokenCount": 98, + "candidatesTokenCount": 11, + "totalTokenCount": 109, "promptTokensDetails": [ { "modality": "TEXT", - "tokenCount": 119 + "tokenCount": 98 } ], "candidatesTokensDetails": [ { "modality": "TEXT", - "tokenCount": 21 + "tokenCount": 11 } ] }, "modelVersion": "gemini-2.0-flash" } - recorded_at: Mon, 24 Mar 2025 21:53:11 GMT + recorded_at: Wed, 26 Mar 2025 12:36:31 GMT recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml b/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml index 604dc9a21..57e087283 100644 --- a/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml +++ b/spec/fixtures/vcr_cassettes/chat_function_calling_gpt-4o-mini_can_use_tools_without_parameters.yml @@ -6,8 +6,8 @@ http_interactions: body: encoding: UTF-8 string: '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"What''s - the time in Wakanda? Answer in ISO 8601 format, UTC timezone."}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"current_time","description":"Gets - the current time in Wakanda","parameters":{"type":"object","properties":{},"required":[]}}}],"tool_choice":"auto"}' + the best language to learn?"}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"best_language_to_learn","description":"Gets + the best language to learn","parameters":{"type":"object","properties":{},"required":[]}}}],"tool_choice":"auto"}' headers: User-Agent: - Faraday v2.12.2 @@ -25,7 +25,7 @@ http_interactions: message: OK headers: Date: - - Mon, 24 Mar 2025 21:53:12 GMT + - Wed, 26 Mar 2025 12:36:32 GMT Content-Type: - application/json Transfer-Encoding: @@ -37,7 +37,7 @@ http_interactions: Openai-Organization: - "" Openai-Processing-Ms: - - '453' + - '835' Openai-Version: - '2020-10-01' X-Ratelimit-Limit-Requests: @@ -47,11 +47,11 @@ http_interactions: X-Ratelimit-Remaining-Requests: - '9999' X-Ratelimit-Remaining-Tokens: - - '199981' + - '199988' X-Ratelimit-Reset-Requests: - 8.64s X-Ratelimit-Reset-Tokens: - - 5ms + - 3ms X-Request-Id: - "" Strict-Transport-Security: @@ -73,9 +73,9 @@ http_interactions: encoding: ASCII-8BIT string: | { - "id": "chatcmpl-BEk1HDrCd1JHbCFe17oj3g1YYnMmQ", + "id": "chatcmpl-BFKHgIGAwLUS0wTdClrDXbEzwGlhE", "object": "chat.completion", - "created": 1742853191, + "created": 1742992592, "model": "gpt-4o-mini-2024-07-18", "choices": [ { @@ -85,10 +85,10 @@ http_interactions: "content": null, "tool_calls": [ { - "id": "call_i0AEa2OVhnmj59JJFzxA2isr", + "id": "call_rSFV6B5evXpOXNsNQfv379HV", "type": "function", "function": { - "name": "current_time", + "name": "best_language_to_learn", "arguments": "{}" } } @@ -101,9 +101,9 @@ http_interactions: } ], "usage": { - "prompt_tokens": 57, - "completion_tokens": 11, - "total_tokens": 68, + "prompt_tokens": 48, + "completion_tokens": 14, + "total_tokens": 62, "prompt_tokens_details": { "cached_tokens": 0, "audio_tokens": 0 @@ -116,17 +116,17 @@ http_interactions: } }, "service_tier": "default", - "system_fingerprint": "fp_b8bc95a0ac" + "system_fingerprint": "fp_27322b4e16" } - recorded_at: Mon, 24 Mar 2025 21:53:12 GMT + recorded_at: Wed, 26 Mar 2025 12:36:32 GMT - request: method: post uri: https://api.openai.com/v1/chat/completions body: encoding: UTF-8 string: '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"What''s - the time in Wakanda? Answer in ISO 8601 format, UTC timezone."},{"role":"assistant","tool_calls":[{"id":"call_i0AEa2OVhnmj59JJFzxA2isr","type":"function","function":{"name":"current_time","arguments":"{}"}}]},{"role":"tool","content":"2025-03-23T20:42:00Z","tool_call_id":"call_i0AEa2OVhnmj59JJFzxA2isr"}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"current_time","description":"Gets - the current time in Wakanda","parameters":{"type":"object","properties":{},"required":[]}}}],"tool_choice":"auto"}' + the best language to learn?"},{"role":"assistant","tool_calls":[{"id":"call_rSFV6B5evXpOXNsNQfv379HV","type":"function","function":{"name":"best_language_to_learn","arguments":"{}"}}]},{"role":"tool","content":"Ruby","tool_call_id":"call_rSFV6B5evXpOXNsNQfv379HV"}],"temperature":0.7,"stream":false,"tools":[{"type":"function","function":{"name":"best_language_to_learn","description":"Gets + the best language to learn","parameters":{"type":"object","properties":{},"required":[]}}}],"tool_choice":"auto"}' headers: User-Agent: - Faraday v2.12.2 @@ -144,7 +144,7 @@ http_interactions: message: OK headers: Date: - - Mon, 24 Mar 2025 21:53:13 GMT + - Wed, 26 Mar 2025 12:36:33 GMT Content-Type: - application/json Transfer-Encoding: @@ -156,7 +156,7 @@ http_interactions: Openai-Organization: - "" Openai-Processing-Ms: - - '514' + - '261' Openai-Version: - '2020-10-01' X-Ratelimit-Limit-Requests: @@ -166,11 +166,11 @@ http_interactions: X-Ratelimit-Remaining-Requests: - '9998' X-Ratelimit-Remaining-Tokens: - - '199973' + - '199986' X-Ratelimit-Reset-Requests: - - 16.639s + - 16.249s X-Ratelimit-Reset-Tokens: - - 7ms + - 4ms X-Request-Id: - "" Strict-Transport-Security: @@ -192,16 +192,16 @@ http_interactions: encoding: ASCII-8BIT string: | { - "id": "chatcmpl-BEk1IUzbcTIfMx2LURwD6OPZKKu19", + "id": "chatcmpl-BFKHhaUl3iiFyJeMb0nD071b7EYPZ", "object": "chat.completion", - "created": 1742853192, + "created": 1742992593, "model": "gpt-4o-mini-2024-07-18", "choices": [ { "index": 0, "message": { "role": "assistant", - "content": "The current time in Wakanda is 2025-03-23T20:42:00Z (UTC).", + "content": "The best language to learn is Ruby.", "refusal": null, "annotations": [] }, @@ -210,9 +210,9 @@ http_interactions: } ], "usage": { - "prompt_tokens": 88, - "completion_tokens": 26, - "total_tokens": 114, + "prompt_tokens": 73, + "completion_tokens": 10, + "total_tokens": 83, "prompt_tokens_details": { "cached_tokens": 0, "audio_tokens": 0 @@ -225,7 +225,7 @@ http_interactions: } }, "service_tier": "default", - "system_fingerprint": "fp_b8bc95a0ac" + "system_fingerprint": "fp_bbfba58e46" } - recorded_at: Mon, 24 Mar 2025 21:53:13 GMT + recorded_at: Wed, 26 Mar 2025 12:36:33 GMT recorded_with: VCR 6.3.1 diff --git a/spec/ruby_llm/chat_tools_spec.rb b/spec/ruby_llm/chat_tools_spec.rb index 0d210476b..daa0821a5 100644 --- a/spec/ruby_llm/chat_tools_spec.rb +++ b/spec/ruby_llm/chat_tools_spec.rb @@ -16,17 +16,11 @@ def execute(latitude:, longitude:) end end - class CurrentTime < RubyLLM::Tool # rubocop:disable Lint/ConstantDefinitionInBlock,RSpec/LeakyConstantDeclaration - description 'Gets the current time in Wakanda' - - attr_reader :now - - def initialize(now) - @now = now - end + class BestLanguageToLearn < RubyLLM::Tool # rubocop:disable Lint/ConstantDefinitionInBlock,RSpec/LeakyConstantDeclaration + description 'Gets the best language to learn' def execute - now.utc.iso8601 + 'Ruby' end end @@ -59,11 +53,9 @@ def execute end it "#{model} can use tools without parameters" do - now = Time.new(2025, 3, 23, 21, 42, 0) - chat = RubyLLM.chat(model: model) - .with_tool(CurrentTime.new(now)) - response = chat.ask("What's the time in Wakanda? Answer in ISO 8601 format, UTC timezone.") - expect(response.content).to include(now.utc.iso8601) + chat = RubyLLM.chat(model: model).with_tool(BestLanguageToLearn) + response = chat.ask("What's the best language to learn?") + expect(response.content).to include('Ruby') end it "#{model} can use tools with multi-turn streaming conversations" do # rubocop:disable RSpec/ExampleLength,RSpec/MultipleExpectations