From 26424ab94ed5f96227c7e800bb8c8cb03e00b7c4 Mon Sep 17 00:00:00 2001 From: Alex Rudall Date: Mon, 10 Jun 2024 14:37:02 +0100 Subject: [PATCH 1/4] Bump version --- CHANGELOG.md | 6 ++++++ Gemfile.lock | 2 +- lib/anthropic/version.rb | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd6979a..9d5e628 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.3.0] - 2024-06-10 + +### Added + +- Add chat streaming! Thank you to the inimitable [@swombat](https://github.com/swombat) for adding this vital functionality! + ## [0.2.0] - 2024-04-25 ### Added diff --git a/Gemfile.lock b/Gemfile.lock index 2303fb3..8ff0a44 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - anthropic (0.2.0) + anthropic (0.3.0) event_stream_parser (>= 0.3.0, < 2.0.0) faraday (>= 1) faraday-multipart (>= 1) diff --git a/lib/anthropic/version.rb b/lib/anthropic/version.rb index 7e21078..6e3f456 100644 --- a/lib/anthropic/version.rb +++ b/lib/anthropic/version.rb @@ -1,3 +1,3 @@ module Anthropic - VERSION = "0.2.0".freeze + VERSION = "0.3.0".freeze end From f550df8a0fcd5d2e116e510ad6f5fa26857876ee Mon Sep 17 00:00:00 2001 From: Alex Rudall Date: Mon, 10 Jun 2024 14:43:08 +0100 Subject: [PATCH 2/4] Refactor for Rubocop --- spec/anthropic/client/messages_spec.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/anthropic/client/messages_spec.rb b/spec/anthropic/client/messages_spec.rb index 13a0602..826149c 100644 --- a/spec/anthropic/client/messages_spec.rb +++ b/spec/anthropic/client/messages_spec.rb @@ -4,6 +4,7 @@ let(:model) { "claude-3-haiku-20240307" } let(:messages) { [{ role: "user", content: "How high is the sky?" }] } let(:max_tokens) { 5 } + let(:json_prompt) { "Answer in the provided JSON format. Only include JSON." } let(:response) do Anthropic::Client.new(access_token: ENV.fetch("ANTHROPIC_API_KEY", nil)).messages( @@ -94,7 +95,7 @@ context "streaming JSON" do let(:model) { "claude-3-haiku-20240307" } let(:messages) do - [{ role: "user", content: "Give me the heights of the 3 tallest mountains. Answer in the provided JSON format. Only include JSON." }, + [{ role: "user", content: "Give me the height of the 3 tallest mountains. #{json_prompt}" }, { role: "assistant", content: '[{"name": "Mountain Name", "height": "height in km"}]' }] end @@ -131,7 +132,7 @@ context "streaming preprocessed JSON" do let(:model) { "claude-3-haiku-20240307" } let(:messages) do - [{ role: "user", content: "Give me the heights of the 3 tallest mountains. Answer in the provided JSON format. Only include JSON." }, + [{ role: "user", content: "Give me the height of the 3 tallest mountains. #{json_prompt}" }, { role: "assistant", content: '[{"name": "Mountain Name", "height": "height in km"}]' }] end @@ -170,7 +171,7 @@ context "malformed streaming preprocessed JSON" do let(:model) { "claude-3-haiku-20240307" } let(:messages) do - [{ role: "user", content: "Give me the heights of the 3 tallest mountains. Answer in the provided JSON format. Only include JSON." }, + [{ role: "user", content: "Give me the height of the 3 tallest mountains. #{json_prompt}" }, { role: "assistant", content: '[{"name": "Mountain Name", "height": "height in km"}]' }] end From c5036e1ccc4d393d7a83e2893e40dd934d02aa1e Mon Sep 17 00:00:00 2001 From: Alex Rudall Date: Mon, 10 Jun 2024 14:44:27 +0100 Subject: [PATCH 3/4] Rerun specs --- spec/anthropic/client/messages_spec.rb | 3 +- ...claude-2_complete_how_high_is_the_sky_.yml | 18 +- ...rovided_json_format_only_include_json_.yml | 319 ++++++++++++++++++ ...20240307_messages_how_high_is_the_sky_.yml | 30 +- ...0240307_streaming_how_high_is_the_sky_.yml | 132 ++++---- ...rovided_json_format_only_include_json_.yml | 304 +++++++++++++++++ 6 files changed, 715 insertions(+), 91 deletions(-) create mode 100644 spec/fixtures/cassettes/claude-3-haiku-20240307_malformed_streaming_json_give_me_the_height_of_the_3_tallest_mountains_answer_in_the_provided_json_format_only_include_json_.yml create mode 100644 spec/fixtures/cassettes/claude-3-haiku-20240307_streaming_json_give_me_the_height_of_the_3_tallest_mountains_answer_in_the_provided_json_format_only_include_json_.yml diff --git a/spec/anthropic/client/messages_spec.rb b/spec/anthropic/client/messages_spec.rb index 826149c..5516c2a 100644 --- a/spec/anthropic/client/messages_spec.rb +++ b/spec/anthropic/client/messages_spec.rb @@ -1,10 +1,11 @@ RSpec.describe Anthropic::Client do + let(:json_prompt) { "Answer in the provided JSON format. Only include JSON." } + describe "#messages" do context "with all required parameters (:model, :messages, :max_tokens)" do let(:model) { "claude-3-haiku-20240307" } let(:messages) { [{ role: "user", content: "How high is the sky?" }] } let(:max_tokens) { 5 } - let(:json_prompt) { "Answer in the provided JSON format. Only include JSON." } let(:response) do Anthropic::Client.new(access_token: ENV.fetch("ANTHROPIC_API_KEY", nil)).messages( diff --git a/spec/fixtures/cassettes/claude-2_complete_how_high_is_the_sky_.yml b/spec/fixtures/cassettes/claude-2_complete_how_high_is_the_sky_.yml index aa92399..e7ce068 100644 --- a/spec/fixtures/cassettes/claude-2_complete_how_high_is_the_sky_.yml +++ b/spec/fixtures/cassettes/claude-2_complete_how_high_is_the_sky_.yml @@ -8,12 +8,12 @@ http_interactions: string: '{"model":"claude-2","max_tokens_to_sample":5,"prompt":"\n\nHuman: How high is the sky?\n\nAssistant:"}' headers: - Content-Type: - - application/json 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: @@ -26,7 +26,7 @@ http_interactions: message: OK headers: Date: - - Tue, 07 May 2024 18:29:27 GMT + - Mon, 10 Jun 2024 13:44:11 GMT Content-Type: - application/json Transfer-Encoding: @@ -34,9 +34,9 @@ http_interactions: Connection: - keep-alive Request-Id: - - req_01ErBrETPn8PsPvGWTfagKw8 + - req_013BtiMCAupAuJ1dSKEKuQk7 X-Cloud-Trace-Context: - - ad14367d1d74093a2e30d7b74f2bb2ed + - 8037a5b8e7f934583a4a6272b8a16987 Via: - 1.1 google Cf-Cache-Status: @@ -44,10 +44,10 @@ http_interactions: Server: - cloudflare Cf-Ray: - - 8803468c0b4848b6-LHR + - 8919cb68deee635b-LHR body: encoding: ASCII-8BIT - string: '{"type":"completion","id":"compl_01HgheXepu78x1LrTQ9s8JKF","completion":" - The sky has no definitive","stop_reason":"max_tokens","model":"claude-2.1","stop":null,"log_id":"compl_01HgheXepu78x1LrTQ9s8JKF"}' - recorded_at: Tue, 07 May 2024 18:29:27 GMT + string: '{"type":"completion","id":"compl_01RCNYLKbgpXie5MaBbYFCVj","completion":" + The sky has no definitive","stop_reason":"max_tokens","model":"claude-2.1","stop":null,"log_id":"compl_01RCNYLKbgpXie5MaBbYFCVj"}' + recorded_at: Mon, 10 Jun 2024 13:44:11 GMT recorded_with: VCR 6.1.0 diff --git a/spec/fixtures/cassettes/claude-3-haiku-20240307_malformed_streaming_json_give_me_the_height_of_the_3_tallest_mountains_answer_in_the_provided_json_format_only_include_json_.yml b/spec/fixtures/cassettes/claude-3-haiku-20240307_malformed_streaming_json_give_me_the_height_of_the_3_tallest_mountains_answer_in_the_provided_json_format_only_include_json_.yml new file mode 100644 index 0000000..472fa64 --- /dev/null +++ b/spec/fixtures/cassettes/claude-3-haiku-20240307_malformed_streaming_json_give_me_the_height_of_the_3_tallest_mountains_answer_in_the_provided_json_format_only_include_json_.yml @@ -0,0 +1,319 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.anthropic.com/v1/messages + body: + encoding: UTF-8 + string: '{"model":"claude-3-haiku-20240307","messages":[{"role":"user","content":"Give + me the height of the 3 tallest mountains. Answer in the provided JSON format. + Only include JSON."},{"role":"assistant","content":"[{\"name\": \"Mountain + Name\", \"height\": \"height in km\"}]"}],"max_tokens":200,"stream":true}' + headers: + 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: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Mon, 10 Jun 2024 13:44:17 GMT + Content-Type: + - text/event-stream; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Cache-Control: + - no-cache + Anthropic-Ratelimit-Requests-Limit: + - '50' + Anthropic-Ratelimit-Requests-Remaining: + - '45' + Anthropic-Ratelimit-Requests-Reset: + - '2024-06-10T13:45:07Z' + Anthropic-Ratelimit-Tokens-Limit: + - '50000' + Anthropic-Ratelimit-Tokens-Remaining: + - '49000' + Anthropic-Ratelimit-Tokens-Reset: + - '2024-06-10T13:45:07Z' + Request-Id: + - req_01BH17VfoEqwhi4iP4TV5JrW + Via: + - 1.1 google + Cf-Cache-Status: + - DYNAMIC + Server: + - cloudflare + Cf-Ray: + - 8919cb90398a4195-LHR + body: + encoding: UTF-8 + string: |+ + event: message_start + data: {"type":"message_start","message":{"id":"msg_01Vy6A9rsuYAjYjEZExGoqqM","type":"message","role":"assistant","model":"claude-3-haiku-20240307","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":48,"output_tokens":2}} } + + event: content_block_start + data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}} + + event: ping + data: {"type": "ping"} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n\n```"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"json"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n["} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"{"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"name"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Mount"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Ever"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"est"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\","} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"height"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"8"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"849"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"km"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"},"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"{"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"name"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"K"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"2"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\","} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"height"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"8"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"611"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"km"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"},"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"{"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"name"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Kang"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ch"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"enj"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"u"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"nga"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\","} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"height"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"8"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"586"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"km"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"}"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n]"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n```"} } + + event: content_block_stop + data: {"type":"content_block_stop","index":0 } + + event: message_delta + data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":99} } + + event: message_stop + data: {"type":"message_stop" } + + recorded_at: Mon, 10 Jun 2024 13:44:17 GMT +recorded_with: VCR 6.1.0 +... diff --git a/spec/fixtures/cassettes/claude-3-haiku-20240307_messages_how_high_is_the_sky_.yml b/spec/fixtures/cassettes/claude-3-haiku-20240307_messages_how_high_is_the_sky_.yml index 129d052..afe6b4e 100644 --- a/spec/fixtures/cassettes/claude-3-haiku-20240307_messages_how_high_is_the_sky_.yml +++ b/spec/fixtures/cassettes/claude-3-haiku-20240307_messages_how_high_is_the_sky_.yml @@ -8,12 +8,12 @@ http_interactions: string: '{"model":"claude-3-haiku-20240307","messages":[{"role":"user","content":"How high is the sky?"}],"max_tokens":5}' headers: - Content-Type: - - application/json 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: @@ -26,7 +26,7 @@ http_interactions: message: OK headers: Date: - - Tue, 07 May 2024 18:29:28 GMT + - Mon, 10 Jun 2024 13:44:12 GMT Content-Type: - application/json Transfer-Encoding: @@ -34,21 +34,21 @@ http_interactions: Connection: - keep-alive Anthropic-Ratelimit-Requests-Limit: - - '4000' + - '50' Anthropic-Ratelimit-Requests-Remaining: - - '3999' + - '49' Anthropic-Ratelimit-Requests-Reset: - - '2024-05-07T18:30:33Z' + - '2024-06-10T13:45:07Z' Anthropic-Ratelimit-Tokens-Limit: - - '400000' + - '50000' Anthropic-Ratelimit-Tokens-Remaining: - - '400000' + - '50000' Anthropic-Ratelimit-Tokens-Reset: - - '2024-05-07T18:30:33Z' + - '2024-06-10T13:45:07Z' Request-Id: - - req_01HTBC4gh5swix23Ya1VuM2E + - req_016qnqUnA4XQFhp7h2kEYVKy X-Cloud-Trace-Context: - - 8b8564b8ed5242fb8fdceff423085f94 + - 84ab992884d6b45ee52b851e17ce88c3 Via: - 1.1 google Cf-Cache-Status: @@ -56,10 +56,10 @@ http_interactions: Server: - cloudflare Cf-Ray: - - 8803468f2e7179c4-LHR + - 8919cb6f0fe0774f-LHR body: encoding: ASCII-8BIT - string: '{"id":"msg_01AtAUn3Ke2HVF5qBVxKv9BR","type":"message","role":"assistant","model":"claude-3-haiku-20240307","stop_sequence":null,"usage":{"input_tokens":13,"output_tokens":5},"content":[{"type":"text","text":"The - sky does not have"}],"stop_reason":"max_tokens"}' - recorded_at: Tue, 07 May 2024 18:29:27 GMT + string: '{"id":"msg_01GrMsP31d2nc5FWkcqvyy7L","type":"message","role":"assistant","model":"claude-3-haiku-20240307","content":[{"type":"text","text":"There + is no definitive"}],"stop_reason":"max_tokens","stop_sequence":null,"usage":{"input_tokens":13,"output_tokens":5}}' + recorded_at: Mon, 10 Jun 2024 13:44:12 GMT recorded_with: VCR 6.1.0 diff --git a/spec/fixtures/cassettes/claude-3-haiku-20240307_streaming_how_high_is_the_sky_.yml b/spec/fixtures/cassettes/claude-3-haiku-20240307_streaming_how_high_is_the_sky_.yml index d439657..60ae01d 100644 --- a/spec/fixtures/cassettes/claude-3-haiku-20240307_streaming_how_high_is_the_sky_.yml +++ b/spec/fixtures/cassettes/claude-3-haiku-20240307_streaming_how_high_is_the_sky_.yml @@ -8,12 +8,12 @@ http_interactions: string: '{"model":"claude-3-haiku-20240307","messages":[{"role":"user","content":"How high is the sky?"}],"max_tokens":50,"stream":true}' headers: - Content-Type: - - application/json 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: @@ -26,7 +26,7 @@ http_interactions: message: OK headers: Date: - - Tue, 07 May 2024 18:29:28 GMT + - Mon, 10 Jun 2024 13:44:13 GMT Content-Type: - text/event-stream; charset=utf-8 Transfer-Encoding: @@ -36,19 +36,19 @@ http_interactions: Cache-Control: - no-cache Anthropic-Ratelimit-Requests-Limit: - - '4000' + - '50' Anthropic-Ratelimit-Requests-Remaining: - - '3998' + - '47' Anthropic-Ratelimit-Requests-Reset: - - '2024-05-07T18:30:33Z' + - '2024-06-10T13:45:07Z' Anthropic-Ratelimit-Tokens-Limit: - - '400000' + - '50000' Anthropic-Ratelimit-Tokens-Remaining: - - '400000' + - '50000' Anthropic-Ratelimit-Tokens-Reset: - - '2024-05-07T18:30:33Z' + - '2024-06-10T13:45:07Z' Request-Id: - - req_01A9oBeaLBfph7uLQUX1jRgn + - req_01A6HzQ2aSP2MyX8JLXRiRrm Via: - 1.1 google Cf-Cache-Status: @@ -56,175 +56,175 @@ http_interactions: Server: - cloudflare Cf-Ray: - - 88034692ac2f4152-LHR + - 8919cb7b5f00652b-LHR body: encoding: UTF-8 string: |+ event: message_start - data: {"type":"message_start","message":{"id":"msg_01WMWvcZq5JEMLf6Jja4Bven","type":"message","role":"assistant","model":"claude-3-haiku-20240307","stop_sequence":null,"usage":{"input_tokens":13,"output_tokens":1},"content":[],"stop_reason":null} } + data: {"type":"message_start","message":{"id":"msg_01KuioQpFZnvrV53hzRTziQz","type":"message","role":"assistant","model":"claude-3-haiku-20240307","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":13,"output_tokens":1}} } event: content_block_start - data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}} + data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""} } event: ping data: {"type": "ping"} event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"There"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"The"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" is"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" sky"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" no"}} + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" does"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" single"}} + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" not"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" defin"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" have"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"itive"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" a"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" single"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"height"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" define"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"d height"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" of"}} + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Here"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" sky"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" are"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" some"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" The"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" key"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" sky"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" points"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" refers"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" about"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" to"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" height"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" exp"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" of"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"anse"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" of"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" sky"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" space"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":":"}} event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" above"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n\n-"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" The"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Earth"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" sky"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"'s"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" refers"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" surface"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" to"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Here"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" earth"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" are"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"'s"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" some"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" atmosphere"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" key"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" points"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" which"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" about"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" extends"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" up"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" height"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" to"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" of"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" about"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" sky"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"600"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":":"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n\n-"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"miles"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" The"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" ("} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Earth"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"1"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"'s"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" atmosphere"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"000"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" extends"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" up"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"km"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" to"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":")"} } event: content_block_delta - data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" about"} } + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" above"} } event: content_block_stop - data: {"type":"content_block_stop","index":0 } + data: {"type":"content_block_stop","index":0 } event: message_delta - data: {"type":"message_delta","delta":{"stop_reason":"max_tokens","stop_sequence":null},"usage":{"output_tokens":50} } + data: {"type":"message_delta","delta":{"stop_reason":"max_tokens","stop_sequence":null},"usage":{"output_tokens":50} } event: message_stop - data: {"type":"message_stop" } + data: {"type":"message_stop" } - recorded_at: Tue, 07 May 2024 18:29:28 GMT + recorded_at: Mon, 10 Jun 2024 13:44:14 GMT recorded_with: VCR 6.1.0 ... diff --git a/spec/fixtures/cassettes/claude-3-haiku-20240307_streaming_json_give_me_the_height_of_the_3_tallest_mountains_answer_in_the_provided_json_format_only_include_json_.yml b/spec/fixtures/cassettes/claude-3-haiku-20240307_streaming_json_give_me_the_height_of_the_3_tallest_mountains_answer_in_the_provided_json_format_only_include_json_.yml new file mode 100644 index 0000000..069e076 --- /dev/null +++ b/spec/fixtures/cassettes/claude-3-haiku-20240307_streaming_json_give_me_the_height_of_the_3_tallest_mountains_answer_in_the_provided_json_format_only_include_json_.yml @@ -0,0 +1,304 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.anthropic.com/v1/messages + body: + encoding: UTF-8 + string: '{"model":"claude-3-haiku-20240307","messages":[{"role":"user","content":"Give + me the height of the 3 tallest mountains. Answer in the provided JSON format. + Only include JSON."},{"role":"assistant","content":"[{\"name\": \"Mountain + Name\", \"height\": \"height in km\"}]"}],"max_tokens":200,"stream":true}' + headers: + 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: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Mon, 10 Jun 2024 13:44:15 GMT + Content-Type: + - text/event-stream; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Cache-Control: + - no-cache + Anthropic-Ratelimit-Requests-Limit: + - '50' + Anthropic-Ratelimit-Requests-Remaining: + - '46' + Anthropic-Ratelimit-Requests-Reset: + - '2024-06-10T13:45:07Z' + Anthropic-Ratelimit-Tokens-Limit: + - '50000' + Anthropic-Ratelimit-Tokens-Remaining: + - '50000' + Anthropic-Ratelimit-Tokens-Reset: + - '2024-06-10T13:45:07Z' + Request-Id: + - req_01RtoygMHEPYAQt3gQRxwLdP + Via: + - 1.1 google + Cf-Cache-Status: + - DYNAMIC + Server: + - cloudflare + Cf-Ray: + - 8919cb894b4571da-LHR + body: + encoding: UTF-8 + string: |+ + event: message_start + data: {"type":"message_start","message":{"id":"msg_013erTS2G5nTdMWJ8USSFpVi","type":"message","role":"assistant","model":"claude-3-haiku-20240307","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":48,"output_tokens":2}} } + + event: content_block_start + data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""} } + + event: ping + data: {"type": "ping"} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n\n{"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"mountains"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" ["} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"{"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"name"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Mount"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Ever"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"est"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\","} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"height"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"8"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"848"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"},"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"{"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"name"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"K"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"2"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\","} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"height"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"8"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"611"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"},"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"{"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"name"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" \""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Kang"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ch"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"enj"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"u"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"nga"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\","} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\""} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"height"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\":"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"8"}} + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"."} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"586"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"}"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n "} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"]"} } + + event: content_block_delta + data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"\n}"} } + + event: content_block_stop + data: {"type":"content_block_stop","index":0 } + + event: message_delta + data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":94} } + + event: message_stop + data: {"type":"message_stop" } + + recorded_at: Mon, 10 Jun 2024 13:44:16 GMT +recorded_with: VCR 6.1.0 +... From 2f60e90c6037398d758fb8fccf3366d421bf90b9 Mon Sep 17 00:00:00 2001 From: Alex Rudall Date: Mon, 10 Jun 2024 14:46:22 +0100 Subject: [PATCH 4/4] Add Rubocop exceptions --- lib/anthropic/http.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/anthropic/http.rb b/lib/anthropic/http.rb index 379ca6d..8e24a2c 100644 --- a/lib/anthropic/http.rb +++ b/lib/anthropic/http.rb @@ -2,6 +2,7 @@ require_relative "http_headers" +# rubocop:disable Metrics/ModuleLength module Anthropic module HTTP include HTTPHeaders @@ -14,6 +15,7 @@ def get(path:) end # This is currently the workhorse for all API calls. + # rubocop:disable Metrics/MethodLength def json_post(path:, parameters:) str_resp = {} response = conn.post(uri(path: path)) do |req| @@ -30,6 +32,7 @@ def json_post(path:, parameters:) str_resp.empty? ? response.body : str_resp end + # rubocop:enable Metrics/MethodLength # Unused? def multipart_post(path:, parameters: nil) @@ -64,6 +67,7 @@ def to_json(string) # # @param user_proc [Proc] The inner proc to call for each JSON object in the chunk. # @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON. + # rubocop:disable Metrics/MethodLength def to_json_stream(user_proc:, response:, preprocess: nil) parser = EventStreamParser::Parser.new preprocess_stack = "" @@ -82,7 +86,9 @@ def to_json_stream(user_proc:, response:, preprocess: nil) end end end + # rubocop:enable Metrics/MethodLength + # rubocop:disable Metrics/MethodLength def _handle_message_type(type, parsed_data, response, &block) case type when "message_start" @@ -97,6 +103,7 @@ def _handle_message_type(type, parsed_data, response, &block) block.yield delta end end + # rubocop:enable Metrics/MethodLength # Decides whether to preprocess JSON or text and calls the appropriate method. def preprocess(directive, stack, delta, user_proc) @@ -187,3 +194,4 @@ def try_parse_json(maybe_json) end end end +# rubocop:enable Metrics/ModuleLength