Skip to content

Commit

Permalink
Merge pull request #14 from alexrudall/0.2.0
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
alexrudall committed Apr 25, 2024
2 parents 97d5302 + ef842f7 commit 30da1bf
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 62 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ 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.0.0] - 2023-07-12
## [0.2.0] - 2024-04-25

### Added

- Initialise repository.
- Add new Messages endpoint - thanks [@deepakmahakale](https://github.com/deepakmahakale) for the PR, [@obie](https://github.com/obie) for the first pass, and many others for requesting and contributions!

## [0.1.0] - 2023-07-18

### Changed

- Got the gem working with the API. MVP

## [0.0.0] - 2023-07-12

### Added

- Initialise repository.
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.1.0)
anthropic (0.2.0)
faraday (>= 1)
faraday-multipart (>= 1)

Expand Down
76 changes: 23 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ end

### Models

As of March 2024, the following models are available:
Available Models:

Model | Latest API model name
--------------- | ------------------------
Claude 3 Opus | claude-3-opus-20240229
Claude 3 Sonnet | claude-3-sonnet-20240229
Claude 3 Haiku | claude-3-haiku-20240307
| Name | API Name |
| --------------- | ------------------------ |
| Claude 3 Opus | claude-3-opus-20240229 |
| Claude 3 Sonnet | claude-3-sonnet-20240229 |
| Claude 3 Haiku | claude-3-haiku-20240307 |

You can find the latest model names in the [Anthropic API documentation](https://docs.anthropic.com/claude/docs/models-overview#model-recommendations).

Expand All @@ -110,72 +110,42 @@ You can find the latest model names in the [Anthropic API documentation](https:/
POST https://api.anthropic.com/v1/messages
```

Hit the Anthropic API for a messages:
Send a sequence of messages (user or assistant) to the API and receive a message in response.

```ruby
response = client.messages(
parameters: {
model: 'claude-3-haiku-20240307', # claude-3-opus-20240229, claude-3-sonnet-20240229
model: "claude-3-haiku-20240307", # claude-3-opus-20240229, claude-3-sonnet-20240229
system: "Respond only in Spanish.",
messages: [
{"role": "user", "content": "Hello, Claude!"}
],
max_tokens: 1000
}
)
# => {"id"=>"msg_0123MiRVCgSG2PaQZwCGbgmV",
# => "type"=>"message",
# => "role"=>"assistant",
# => "content"=>[{"type"=>"text", "text"=>"¡Hola! Es un gusto saludarte. ¿En qué puedo ayudarte hoy?"}],
# => "model"=>"claude-3-haiku-20240307",
# => "stop_reason"=>"end_turn",
# => "stop_sequence"=>nil,
# => "usage"=>{"input_tokens"=>17, "output_tokens"=>32}}
# => {
# => "id" => "msg_0123MiRVCgSG2PaQZwCGbgmV",
# => "type" => "message",
# => "role" => "assistant",
# => "content" => [{"type"=>"text", "text"=>"¡Hola! Es un gusto saludarte. ¿En qué puedo ayudarte hoy?"}],
# => "model" => "claude-3-haiku-20240307",
# => "stop_reason" => "end_turn",
# => "stop_sequence" => nil,
# => "usage" => {"input_tokens"=>17, "output_tokens"=>32}
# => }
```

### Text Completions [Legacy]

```
POST https://api.anthropic.com/v1/complete
```

> [!WARNING]
> The Text Completions API is a legacy API.
> Anthropic [recommends](https://docs.anthropic.com/claude/reference/complete_post) using the [Messages API](#messages) going forward.
Hit the Anthropic API for a completion:

```ruby
response = client.complete(
parameters: {
model: "claude-2",
prompt: "How high is the sky?",
max_tokens_to_sample: 5
})
puts response["completion"]
# => " The sky has no definitive"
```

Note that all requests are prepended by this library with

`\n\nHuman: `

and appended with

`\n\nAssistant:`

so whatever prompt you pass will be sent to the API as

`\n\nHuman: How high is the sky?\n\nAssistant:`

This is a requirement of [the API](https://docs.anthropic.com/claude/reference/complete_post).

## Development

After checking out the repo, run `bin/setup` to install dependencies. You can run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`.

To run all tests, execute the command `bundle exec rake`, which will also run the linter (Rubocop). This repository uses [VCR](https://github.com/vcr/vcr) to log API requests.

> [!WARNING]
> If you have an `ANTHROPIC_API_KEY` in your `ENV`, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with `unset` or similar if you just want to run the specs against the stored VCR responses.
### Warning

If you have an `ANTHROPIC_API_KEY` in your `ENV`, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with `unset` or similar if you just want to run the specs against the stored VCR responses.
Expand Down
15 changes: 14 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rubocop/rake_task"

RSpec::Core::RakeTask.new(:spec)

task default: :spec
task :default do
Rake::Task["test"].invoke
Rake::Task["lint"].invoke
end

task :test do
Rake::Task["spec"].invoke
end

task :lint do
RuboCop::RakeTask.new(:rubocop)
Rake::Task["rubocop"].invoke
end
4 changes: 2 additions & 2 deletions lib/anthropic/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def complete(parameters: {})
parameters[:prompt] = wrap_prompt(prompt: parameters[:prompt])
Anthropic::Client.json_post(path: "/complete", parameters: parameters)
end
def messages(parameters: {}) # rubocop:disable Metrics/MethodLength

def messages(parameters: {})
Anthropic::Client.json_post(path: "/messages", parameters: parameters)
end

Expand Down
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.1.0".freeze
VERSION = "0.2.0".freeze
end
4 changes: 2 additions & 2 deletions spec/anthropic/client/messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:max_tokens) { 5 }

let(:response) do
Anthropic::Client.new(access_token: ENV['ANTHROPIC_API_KEY']).messages(
Anthropic::Client.new(access_token: ENV.fetch("ANTHROPIC_API_KEY", nil)).messages(
parameters: {
model: model,
messages: [
Expand All @@ -14,7 +14,7 @@
content: "How High is the Sky?"
}
],
max_tokens: max_tokens,
max_tokens: max_tokens
}
)
end
Expand Down

0 comments on commit 30da1bf

Please sign in to comment.