anthropic 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ebc9a3f0188078f1a6bd06fc9bd603f22b054bab8228df0882112a223793210
4
- data.tar.gz: bbbce42b980f930f7f34e283528926d6013dc136d8246bfe6b0aa7802db959ca
3
+ metadata.gz: 5591b52e376e9a5915a3011d9919d188aaa441fa65f6a8d665b160c5529b0564
4
+ data.tar.gz: e251bf3dd61569807e722f6fe116498fd6801e07fc2333c2f2f40c03e61b5b9f
5
5
  SHA512:
6
- metadata.gz: b45be07f0fdebad2f46b7701cee689a7abbde61c2b20b594f809dfc22523b1a314c11a2556bcdb7862e0fc22dcdf575e5e037f3f5738e28069688d8a8689e16b
7
- data.tar.gz: 5cdc5d601c49bf76ace7f4daa54ae4c41f27ec866c28e1ea9c3ebaba64bd6ac06fe4c3413ade2ee41b38f6487f37ef4487fcc551cdc065f2418c3bf983dc4e7c
6
+ metadata.gz: 6beac221f3595b6599e37175190a59fc20681aa679d7746f060447850aab0b4ca7362ef5ce99a458a9ce1a5f6acfc5a6491ca5bdb7147efadf14cca068576d08
7
+ data.tar.gz: 39189b30389c23c56956b32135587513f3ae7f2927e323262333c82392b5c24239c3a94486a68b2bd569856fe34334a19eb8a3ad7a0f0d99ef9bd5ec16283580
data/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.2.0] - 2024-04-25
9
+
10
+ ### Added
11
+
12
+ - 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!
13
+
14
+ ## [0.1.0] - 2023-07-18
15
+
16
+ ### Changed
17
+
18
+ - Got the gem working with the API. MVP
19
+
8
20
  ## [0.0.0] - 2023-07-12
9
21
 
10
22
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- anthropic (0.0.0)
4
+ anthropic (0.2.0)
5
5
  faraday (>= 1)
6
6
  faraday-multipart (>= 1)
7
7
 
@@ -16,7 +16,7 @@ GEM
16
16
  rexml
17
17
  diff-lcs (1.5.0)
18
18
  dotenv (2.8.1)
19
- faraday (2.7.7)
19
+ faraday (2.7.10)
20
20
  faraday-net_http (>= 2.0, < 3.1)
21
21
  ruby2_keywords (>= 0.0.4)
22
22
  faraday-multipart (1.0.4)
data/README.md CHANGED
@@ -1,18 +1,16 @@
1
- # Anthropic (WIP)
1
+ # Anthropic
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/anthropic.svg)](https://badge.fury.io/rb/anthropic)
4
4
  [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/alexrudall/anthropic/blob/main/LICENSE.txt)
5
5
  [![CircleCI Build Status](https://circleci.com/gh/alexrudall/anthropic.svg?style=shield)](https://circleci.com/gh/alexrudall/anthropic)
6
6
 
7
- Use the [Anthropic API](https://anthropic.com/blog/anthropic-api/) with Ruby! 🤖❤️
7
+ Use the [Anthropic API](https://docs.anthropic.com/claude/reference/getting-started-with-the-api) with Ruby! 🤖🌌
8
8
 
9
- This is very much a WIP and probably doesn't work as I don't have access to the Anthropic API as yet, but it will get a lot better once I do! Hopefully very soon :D
9
+ You can apply for access to the API [here](https://docs.anthropic.com/claude/docs/getting-access-to-claude).
10
10
 
11
- [Ruby AI Builders Discord](https://discord.gg/k4Uc224xVD)
11
+ 🚢 Need someone to ship critical Rails features for you, fast? I'm taking on a few new clients at an experimental crazy low price, check it out: [railsai.com](https://railsai.com?utm_source=anthropic&utm_medium=readme&utm_id=26072023)
12
12
 
13
- [Rails AI Guides](https://railsai.com)
14
-
15
- Follow me on [Twitter](https://twitter.com/alexrudall) for more Ruby / AI content!
13
+ [🎮 Ruby AI Builders Discord](https://discord.gg/k4Uc224xVD) | [🐦 Twitter](https://twitter.com/alexrudall) | [🤖 OpenAI Gem](https://github.com/alexrudall/ruby-openai) | [🚂 Midjourney Gem](https://github.com/alexrudall/midjourney)
16
14
 
17
15
  ### Bundler
18
16
 
@@ -40,8 +38,7 @@ require "anthropic"
40
38
 
41
39
  ## Usage
42
40
 
43
- - Get your API key from [https://platform.anthropic.com/account/api-keys](https://platform.anthropic.com/account/api-keys)
44
- - If you belong to multiple organizations, you can get your Organization ID from [https://platform.anthropic.com/account/org-settings](https://platform.anthropic.com/account/org-settings)
41
+ - Get your API key from [https://console.anthropic.com/account/keys](https://console.anthropic.com/account/keys)
45
42
 
46
43
  ### Quickstart
47
44
 
@@ -53,11 +50,15 @@ client = Anthropic::Client.new(access_token: "access_token_goes_here")
53
50
 
54
51
  ### With Config
55
52
 
56
- For a more robust setup, you can configure the gem with your API keys, for example in an `anthropic.rb` initializer file. Never hardcode secrets into your codebase - instead use something like [dotenv](https://github.com/motdotla/dotenv) to pass the keys safely into your environments.
53
+ For a more robust setup, you can configure the gem with your API keys, for example in an `anthropic.rb` initializer file. Never hardcode secrets into your codebase - instead use something like [dotenv](https://github.com/motdotla/dotenv) to pass the keys safely into your environments or rails credentials if you are using this in a rails project.
57
54
 
58
55
  ```ruby
59
56
  Anthropic.configure do |config|
60
- config.access_token = ENV.fetch("ANTHROPIC_API_KEY")
57
+ # With dotenv
58
+ config.access_token = ENV.fetch("ANTHROPIC_API_KEY")
59
+ # OR
60
+ # With Rails credentials
61
+ config.access_token = Rails.application.credentials.dig(:anthropic, :api_key)
61
62
  end
62
63
  ```
63
64
 
@@ -67,42 +68,71 @@ Then you can create a client like this:
67
68
  client = Anthropic::Client.new
68
69
  ```
69
70
 
70
- #### Custom timeout or base URI
71
+ #### Change version or timeout
72
+
73
+ You can change to a different dated version (different from the URL version which is just `v1`) of Anthropic's API by passing `anthropic_version` when initializing the client. If you don't the default latest will be used, which is "2023-06-01". [More info](https://docs.anthropic.com/claude/reference/versioning)
71
74
 
72
75
  The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the `request_timeout` when initializing the client.
73
76
 
74
77
  ```ruby
75
78
  client = Anthropic::Client.new(
76
- access_token: "access_token_goes_here",
77
- request_timeout: 240
79
+ access_token: "access_token_goes_here",
80
+ anthropic_version: "2023-01-01", # Optional
81
+ request_timeout: 240 # Optional
78
82
  )
79
83
  ```
80
84
 
81
- or when configuring the gem:
85
+ You can also set these keys when configuring the gem:
82
86
 
83
87
  ```ruby
84
88
  Anthropic.configure do |config|
85
- config.access_token = ENV.fetch("ANTHROPIC_API_KEY")
86
- config.request_timeout = 240 # Optional
87
- config.extra_headers = {
88
- "X-Proxy-Refresh": "true"
89
- } # Optional
89
+ config.access_token = ENV.fetch("ANTHROPIC_API_KEY")
90
+ config.anthropic_version = "2023-01-01" # Optional
91
+ config.request_timeout = 240 # Optional
90
92
  end
91
93
  ```
92
94
 
93
- ### Completions
95
+ ### Models
96
+
97
+ Available Models:
98
+
99
+ | Name | API Name |
100
+ | --------------- | ------------------------ |
101
+ | Claude 3 Opus | claude-3-opus-20240229 |
102
+ | Claude 3 Sonnet | claude-3-sonnet-20240229 |
103
+ | Claude 3 Haiku | claude-3-haiku-20240307 |
104
+
105
+ You can find the latest model names in the [Anthropic API documentation](https://docs.anthropic.com/claude/docs/models-overview#model-recommendations).
106
+
107
+ ### Messages
94
108
 
95
- Hit the Anthropic API for a completion:
109
+ ```
110
+ POST https://api.anthropic.com/v1/messages
111
+ ```
112
+
113
+ Send a sequence of messages (user or assistant) to the API and receive a message in response.
96
114
 
97
115
  ```ruby
98
- response = client.completions(
99
- parameters: {
100
- model: "claude-2",
101
- prompt: "Once upon a time",
102
- max_tokens: 5
103
- })
104
- puts response["choices"].map { |c| c["text"] }
105
- # => [", there lived a great"]
116
+ response = client.messages(
117
+ parameters: {
118
+ model: "claude-3-haiku-20240307", # claude-3-opus-20240229, claude-3-sonnet-20240229
119
+ system: "Respond only in Spanish.",
120
+ messages: [
121
+ {"role": "user", "content": "Hello, Claude!"}
122
+ ],
123
+ max_tokens: 1000
124
+ }
125
+ )
126
+ # => {
127
+ # => "id" => "msg_0123MiRVCgSG2PaQZwCGbgmV",
128
+ # => "type" => "message",
129
+ # => "role" => "assistant",
130
+ # => "content" => [{"type"=>"text", "text"=>"¡Hola! Es un gusto saludarte. ¿En qué puedo ayudarte hoy?"}],
131
+ # => "model" => "claude-3-haiku-20240307",
132
+ # => "stop_reason" => "end_turn",
133
+ # => "stop_sequence" => nil,
134
+ # => "usage" => {"input_tokens"=>17, "output_tokens"=>32}
135
+ # => }
106
136
  ```
107
137
 
108
138
  ## Development
@@ -111,6 +141,11 @@ After checking out the repo, run `bin/setup` to install dependencies. You can ru
111
141
 
112
142
  To install this gem onto your local machine, run `bundle exec rake install`.
113
143
 
144
+ 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.
145
+
146
+ > [!WARNING]
147
+ > 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.
148
+
114
149
  ### Warning
115
150
 
116
151
  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.
data/Rakefile CHANGED
@@ -1,6 +1,19 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
5
6
 
6
- task default: :spec
7
+ task :default do
8
+ Rake::Task["test"].invoke
9
+ Rake::Task["lint"].invoke
10
+ end
11
+
12
+ task :test do
13
+ Rake::Task["spec"].invoke
14
+ end
15
+
16
+ task :lint do
17
+ RuboCop::RakeTask.new(:rubocop)
18
+ Rake::Task["rubocop"].invoke
19
+ end
data/anthropic.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
6
6
  spec.authors = ["Alex"]
7
7
  spec.email = ["alexrudall@users.noreply.github.com"]
8
8
 
9
- spec.summary = "Anthropic API + Ruby! 🤖❤️"
9
+ spec.summary = "Anthropic API + Ruby! 🤖🌌"
10
10
  spec.homepage = "https://github.com/alexrudall/anthropic"
11
11
  spec.license = "MIT"
12
12
  spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
@@ -11,8 +11,23 @@ module Anthropic
11
11
  Anthropic.configuration.extra_headers = extra_headers
12
12
  end
13
13
 
14
- def completions(parameters: {})
15
- Anthropic::Client.json_post(path: "/completions", parameters: parameters)
14
+ def complete(parameters: {})
15
+ parameters[:prompt] = wrap_prompt(prompt: parameters[:prompt])
16
+ Anthropic::Client.json_post(path: "/complete", parameters: parameters)
17
+ end
18
+
19
+ def messages(parameters: {})
20
+ Anthropic::Client.json_post(path: "/messages", parameters: parameters)
21
+ end
22
+
23
+ private
24
+
25
+ def wrap_prompt(prompt:, prefix: "\n\nHuman: ", suffix: "\n\nAssistant:")
26
+ return if prompt.nil?
27
+
28
+ prompt.prepend(prefix) unless prompt.start_with?(prefix)
29
+ prompt.concat(suffix) unless prompt.end_with?(suffix)
30
+ prompt
16
31
  end
17
32
  end
18
33
  end
@@ -74,8 +74,8 @@ module Anthropic
74
74
  def headers
75
75
  {
76
76
  "Content-Type" => "application/json",
77
- "Authorization" => "Bearer #{Anthropic.configuration.access_token}",
78
- "Anthropic-Organization" => Anthropic.configuration.organization_id
77
+ "x-api-key" => Anthropic.configuration.access_token,
78
+ "Anthropic-Version" => Anthropic.configuration.anthropic_version
79
79
  }.merge(Anthropic.configuration.extra_headers)
80
80
  end
81
81
 
@@ -83,7 +83,7 @@ module Anthropic
83
83
  parameters&.transform_values do |value|
84
84
  next value unless value.is_a?(File)
85
85
 
86
- # Doesn't seem like Anthropic need mime_type yet, so not worth
86
+ # Doesn't seem like Anthropic needs mime_type yet, so not worth
87
87
  # the library to figure this out. Hence the empty string
88
88
  # as the second argument.
89
89
  Faraday::UploadIO.new(value, "", value.path)
@@ -1,3 +1,3 @@
1
1
  module Anthropic
2
- VERSION = "0.0.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
data/lib/anthropic.rb CHANGED
@@ -3,10 +3,6 @@ require "faraday/multipart"
3
3
 
4
4
  require_relative "anthropic/http"
5
5
  require_relative "anthropic/client"
6
- require_relative "anthropic/files"
7
- require_relative "anthropic/finetunes"
8
- require_relative "anthropic/images"
9
- require_relative "anthropic/models"
10
6
  require_relative "anthropic/version"
11
7
 
12
8
  module Anthropic
@@ -15,15 +11,18 @@ module Anthropic
15
11
 
16
12
  class Configuration
17
13
  attr_writer :access_token
18
- attr_accessor :api_version, :organization_id, :uri_base, :request_timeout, :extra_headers
14
+ attr_accessor :anthropic_version, :api_version, :extra_headers, :organization_id,
15
+ :request_timeout, :uri_base
19
16
 
20
17
  DEFAULT_API_VERSION = "v1".freeze
18
+ DEFAULT_ANTHROPIC_VERSION = "2023-06-01".freeze
21
19
  DEFAULT_URI_BASE = "https://api.anthropic.com/".freeze
22
20
  DEFAULT_REQUEST_TIMEOUT = 120
23
21
 
24
22
  def initialize
25
23
  @access_token = nil
26
24
  @api_version = DEFAULT_API_VERSION
25
+ @anthropic_version = DEFAULT_ANTHROPIC_VERSION
27
26
  @organization_id = nil
28
27
  @uri_base = DEFAULT_URI_BASE
29
28
  @request_timeout = DEFAULT_REQUEST_TIMEOUT
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anthropic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-12 00:00:00.000000000 Z
11
+ date: 2024-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -93,8 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.4.12
96
+ rubygems_version: 3.4.22
97
97
  signing_key:
98
98
  specification_version: 4
99
- summary: "Anthropic API + Ruby! \U0001F916❤️"
99
+ summary: "Anthropic API + Ruby! \U0001F916\U0001F30C"
100
100
  test_files: []