Skip to content

Commit

Permalink
Merge pull request #19 from alexrudall/0.3.0
Browse files Browse the repository at this point in the history
Bump version
  • Loading branch information
alexrudall authored Jun 10, 2024
2 parents 5c17417 + 2f60e90 commit b9a3a6f
Show file tree
Hide file tree
Showing 10 changed files with 734 additions and 95 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 8 additions & 0 deletions lib/anthropic/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "http_headers"

# rubocop:disable Metrics/ModuleLength
module Anthropic
module HTTP
include HTTPHeaders
Expand All @@ -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|
Expand All @@ -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)
Expand Down Expand Up @@ -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 = ""
Expand All @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -187,3 +194,4 @@ def try_parse_json(maybe_json)
end
end
end
# rubocop:enable Metrics/ModuleLength
2 changes: 1 addition & 1 deletion lib/anthropic/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Anthropic
VERSION = "0.2.0".freeze
VERSION = "0.3.0".freeze
end
8 changes: 5 additions & 3 deletions spec/anthropic/client/messages_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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" }
Expand Down Expand Up @@ -94,7 +96,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
Expand Down Expand Up @@ -131,7 +133,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
Expand Down Expand Up @@ -170,7 +172,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
Expand Down

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

Loading

0 comments on commit b9a3a6f

Please sign in to comment.