anthropic 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5ebc9a3f0188078f1a6bd06fc9bd603f22b054bab8228df0882112a223793210
4
+ data.tar.gz: bbbce42b980f930f7f34e283528926d6013dc136d8246bfe6b0aa7802db959ca
5
+ SHA512:
6
+ metadata.gz: b45be07f0fdebad2f46b7701cee689a7abbde61c2b20b594f809dfc22523b1a314c11a2556bcdb7862e0fc22dcdf575e5e037f3f5738e28069688d8a8689e16b
7
+ data.tar.gz: 5cdc5d601c49bf76ace7f4daa54ae4c41f27ec866c28e1ea9c3ebaba64bd6ac06fe4c3413ade2ee41b38f6487f37ef4487fcc551cdc065f2418c3bf983dc4e7c
@@ -0,0 +1,45 @@
1
+ version: 2.1 # Use 2.1 to enable using orbs and other features.
2
+
3
+ # Declare the orbs that we'll use in our config.
4
+ orbs:
5
+ ruby: circleci/ruby@1.0
6
+
7
+ jobs:
8
+ rubocop:
9
+ parallelism: 1
10
+ docker:
11
+ - image: cimg/ruby:3.1-node
12
+ steps:
13
+ - checkout
14
+ - ruby/install-deps
15
+ - run:
16
+ name: Run Rubocop
17
+ command: bundle exec rubocop
18
+ test:
19
+ parameters:
20
+ ruby-image:
21
+ type: string
22
+ parallelism: 1
23
+ docker:
24
+ - image: << parameters.ruby-image >>
25
+ steps:
26
+ - checkout
27
+ - ruby/install-deps
28
+ - run:
29
+ name: Run tests
30
+ command: bundle exec rspec -fd
31
+
32
+ workflows:
33
+ version: 2
34
+ checks:
35
+ jobs:
36
+ - rubocop
37
+ - test:
38
+ matrix:
39
+ parameters:
40
+ ruby-image:
41
+ - cimg/ruby:2.6-node
42
+ - cimg/ruby:2.7-node
43
+ - cimg/ruby:3.0-node
44
+ - cimg/ruby:3.1-node
45
+ - cimg/ruby:3.2-node
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. iOS]
28
+ - Browser [e.g. chrome, safari]
29
+ - Version [e.g. 22]
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - Browser [e.g. stock browser, safari]
35
+ - Version [e.g. 22]
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,15 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ open-pull-requests-limit: 10
8
+ ignore:
9
+ - dependency-name: webmock
10
+ versions:
11
+ - 3.11.1
12
+ - 3.11.3
13
+ - dependency-name: rspec
14
+ versions:
15
+ - 3.10.0
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .byebug_history
14
+ .env
15
+
16
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ Style/Documentation:
7
+ # Skips checking to make sure top level modules / classes have a comment.
8
+ Enabled: false
9
+
10
+ Layout/LineLength:
11
+ Max: 100
12
+ Exclude:
13
+ - "**/*.gemspec"
14
+
15
+ Metrics/AbcSize:
16
+ Max: 20
17
+
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - "spec/**/*"
21
+
22
+ Style/StringLiterals:
23
+ EnforcedStyle: double_quotes
24
+
25
+ Style/FrozenStringLiteralComment:
26
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.0.0] - 2023-07-12
9
+
10
+ ### Added
11
+
12
+ - Initialise repository.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alexrudall@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,3 @@
1
+ ## Contributing
2
+
3
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/anthropic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alexrudall/anthropic/blob/main/CODE_OF_CONDUCT.md).
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Include gem dependencies from anthropic.gemspec
4
+ gemspec
5
+
6
+ gem "byebug", "~> 11.1.3"
7
+ gem "dotenv", "~> 2.8.1"
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.12"
10
+ gem "rubocop", "~> 1.50.2"
11
+ gem "vcr", "~> 6.1.0"
12
+ gem "webmock", "~> 3.18.1"
data/Gemfile.lock ADDED
@@ -0,0 +1,84 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ anthropic (0.0.0)
5
+ faraday (>= 1)
6
+ faraday-multipart (>= 1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.1)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ byebug (11.1.3)
15
+ crack (0.4.5)
16
+ rexml
17
+ diff-lcs (1.5.0)
18
+ dotenv (2.8.1)
19
+ faraday (2.7.7)
20
+ faraday-net_http (>= 2.0, < 3.1)
21
+ ruby2_keywords (>= 0.0.4)
22
+ faraday-multipart (1.0.4)
23
+ multipart-post (~> 2)
24
+ faraday-net_http (3.0.2)
25
+ hashdiff (1.0.1)
26
+ json (2.6.3)
27
+ multipart-post (2.3.0)
28
+ parallel (1.22.1)
29
+ parser (3.2.2.0)
30
+ ast (~> 2.4.1)
31
+ public_suffix (5.0.1)
32
+ rainbow (3.1.1)
33
+ rake (13.0.6)
34
+ regexp_parser (2.8.0)
35
+ rexml (3.2.5)
36
+ rspec (3.12.0)
37
+ rspec-core (~> 3.12.0)
38
+ rspec-expectations (~> 3.12.0)
39
+ rspec-mocks (~> 3.12.0)
40
+ rspec-core (3.12.0)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-expectations (3.12.2)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-mocks (3.12.3)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-support (3.12.0)
49
+ rubocop (1.50.2)
50
+ json (~> 2.3)
51
+ parallel (~> 1.10)
52
+ parser (>= 3.2.0.0)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ regexp_parser (>= 1.8, < 3.0)
55
+ rexml (>= 3.2.5, < 4.0)
56
+ rubocop-ast (>= 1.28.0, < 2.0)
57
+ ruby-progressbar (~> 1.7)
58
+ unicode-display_width (>= 2.4.0, < 3.0)
59
+ rubocop-ast (1.28.0)
60
+ parser (>= 3.2.1.0)
61
+ ruby-progressbar (1.13.0)
62
+ ruby2_keywords (0.0.5)
63
+ unicode-display_width (2.4.2)
64
+ vcr (6.1.0)
65
+ webmock (3.18.1)
66
+ addressable (>= 2.8.0)
67
+ crack (>= 0.3.2)
68
+ hashdiff (>= 0.4.0, < 2.0.0)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ anthropic!
75
+ byebug (~> 11.1.3)
76
+ dotenv (~> 2.8.1)
77
+ rake (~> 13.0)
78
+ rspec (~> 3.12)
79
+ rubocop (~> 1.50.2)
80
+ vcr (~> 6.1.0)
81
+ webmock (~> 3.18.1)
82
+
83
+ BUNDLED WITH
84
+ 2.4.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Alex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # Anthropic (WIP)
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/anthropic.svg)](https://badge.fury.io/rb/anthropic)
4
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/alexrudall/anthropic/blob/main/LICENSE.txt)
5
+ [![CircleCI Build Status](https://circleci.com/gh/alexrudall/anthropic.svg?style=shield)](https://circleci.com/gh/alexrudall/anthropic)
6
+
7
+ Use the [Anthropic API](https://anthropic.com/blog/anthropic-api/) with Ruby! 🤖❤️
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
10
+
11
+ [Ruby AI Builders Discord](https://discord.gg/k4Uc224xVD)
12
+
13
+ [Rails AI Guides](https://railsai.com)
14
+
15
+ Follow me on [Twitter](https://twitter.com/alexrudall) for more Ruby / AI content!
16
+
17
+ ### Bundler
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem "anthropic"
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle install
28
+
29
+ ### Gem install
30
+
31
+ Or install with:
32
+
33
+ $ gem install anthropic
34
+
35
+ and require with:
36
+
37
+ ```ruby
38
+ require "anthropic"
39
+ ```
40
+
41
+ ## Usage
42
+
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)
45
+
46
+ ### Quickstart
47
+
48
+ For a quick test you can pass your token directly to a new client:
49
+
50
+ ```ruby
51
+ client = Anthropic::Client.new(access_token: "access_token_goes_here")
52
+ ```
53
+
54
+ ### With Config
55
+
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.
57
+
58
+ ```ruby
59
+ Anthropic.configure do |config|
60
+ config.access_token = ENV.fetch("ANTHROPIC_API_KEY")
61
+ end
62
+ ```
63
+
64
+ Then you can create a client like this:
65
+
66
+ ```ruby
67
+ client = Anthropic::Client.new
68
+ ```
69
+
70
+ #### Custom timeout or base URI
71
+
72
+ 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
+
74
+ ```ruby
75
+ client = Anthropic::Client.new(
76
+ access_token: "access_token_goes_here",
77
+ request_timeout: 240
78
+ )
79
+ ```
80
+
81
+ or when configuring the gem:
82
+
83
+ ```ruby
84
+ 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
90
+ end
91
+ ```
92
+
93
+ ### Completions
94
+
95
+ Hit the Anthropic API for a completion:
96
+
97
+ ```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"]
106
+ ```
107
+
108
+ ## Development
109
+
110
+ 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.
111
+
112
+ To install this gem onto your local machine, run `bundle exec rake install`.
113
+
114
+ ### Warning
115
+
116
+ 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.
117
+
118
+ ## Release
119
+
120
+ First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set ANTHROPIC_API_KEY in your environment or pass it in like this:
121
+
122
+ ```
123
+ ANTHROPIC_API_KEY=123abc bundle exec rspec
124
+ ```
125
+
126
+ Then update the version number in `version.rb`, update `CHANGELOG.md`, run `bundle install` to update Gemfile.lock, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
127
+
128
+ ## Contributing
129
+
130
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/alexrudall/anthropic>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alexrudall/anthropic/blob/main/CODE_OF_CONDUCT.md).
131
+
132
+ ## License
133
+
134
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
135
+
136
+ ## Code of Conduct
137
+
138
+ Everyone interacting in the Ruby Anthropic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alexrudall/anthropic/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/anthropic.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ require_relative "lib/anthropic/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "anthropic"
5
+ spec.version = Anthropic::VERSION
6
+ spec.authors = ["Alex"]
7
+ spec.email = ["alexrudall@users.noreply.github.com"]
8
+
9
+ spec.summary = "Anthropic API + Ruby! 🤖❤️"
10
+ spec.homepage = "https://github.com/alexrudall/anthropic"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/alexrudall/anthropic"
16
+ spec.metadata["changelog_uri"] = "https://github.com/alexrudall/anthropic/blob/main/CHANGELOG.md"
17
+ spec.metadata["rubygems_mfa_required"] = "true"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "faraday", ">= 1"
29
+ spec.add_dependency "faraday-multipart", ">= 1"
30
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "anthropic"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ module Anthropic
2
+ class Client
3
+ extend Anthropic::HTTP
4
+
5
+ def initialize(access_token: nil, organization_id: nil, uri_base: nil, request_timeout: nil,
6
+ extra_headers: {})
7
+ Anthropic.configuration.access_token = access_token if access_token
8
+ Anthropic.configuration.organization_id = organization_id if organization_id
9
+ Anthropic.configuration.uri_base = uri_base if uri_base
10
+ Anthropic.configuration.request_timeout = request_timeout if request_timeout
11
+ Anthropic.configuration.extra_headers = extra_headers
12
+ end
13
+
14
+ def completions(parameters: {})
15
+ Anthropic::Client.json_post(path: "/completions", parameters: parameters)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module Ruby
2
+ module Anthropic
3
+ VERSION = ::Anthropic::VERSION
4
+
5
+ Error = ::Anthropic::Error
6
+ ConfigurationError = ::Anthropic::ConfigurationError
7
+ Configuration = ::Anthropic::Configuration
8
+ end
9
+ end
@@ -0,0 +1,93 @@
1
+ module Anthropic
2
+ module HTTP
3
+ def get(path:)
4
+ to_json(conn.get(uri(path: path)) do |req|
5
+ req.headers = headers
6
+ end&.body)
7
+ end
8
+
9
+ def json_post(path:, parameters:)
10
+ to_json(conn.post(uri(path: path)) do |req|
11
+ if parameters[:stream].is_a?(Proc)
12
+ req.options.on_data = to_json_stream(user_proc: parameters[:stream])
13
+ parameters[:stream] = true # Necessary to tell Anthropic to stream.
14
+ end
15
+
16
+ req.headers = headers
17
+ req.body = parameters.to_json
18
+ end&.body)
19
+ end
20
+
21
+ def multipart_post(path:, parameters: nil)
22
+ to_json(conn(multipart: true).post(uri(path: path)) do |req|
23
+ req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
24
+ req.body = multipart_parameters(parameters)
25
+ end&.body)
26
+ end
27
+
28
+ def delete(path:)
29
+ to_json(conn.delete(uri(path: path)) do |req|
30
+ req.headers = headers
31
+ end&.body)
32
+ end
33
+
34
+ private
35
+
36
+ def to_json(string)
37
+ return unless string
38
+
39
+ JSON.parse(string)
40
+ rescue JSON::ParserError
41
+ # Convert a multiline string of JSON objects to a JSON array.
42
+ JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
43
+ end
44
+
45
+ # Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.
46
+ # For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could
47
+ # be a data object or an error object as described in the Anthropic API documentation.
48
+ #
49
+ # If the JSON object for a given data or error message is invalid, it is ignored.
50
+ #
51
+ # @param user_proc [Proc] The inner proc to call for each JSON object in the chunk.
52
+ # @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON.
53
+ def to_json_stream(user_proc:)
54
+ proc do |chunk, _|
55
+ chunk.scan(/(?:data|error): (\{.*\})/i).flatten.each do |data|
56
+ user_proc.call(JSON.parse(data))
57
+ rescue JSON::ParserError
58
+ # Ignore invalid JSON.
59
+ end
60
+ end
61
+ end
62
+
63
+ def conn(multipart: false)
64
+ Faraday.new do |f|
65
+ f.options[:timeout] = Anthropic.configuration.request_timeout
66
+ f.request(:multipart) if multipart
67
+ end
68
+ end
69
+
70
+ def uri(path:)
71
+ Anthropic.configuration.uri_base + Anthropic.configuration.api_version + path
72
+ end
73
+
74
+ def headers
75
+ {
76
+ "Content-Type" => "application/json",
77
+ "Authorization" => "Bearer #{Anthropic.configuration.access_token}",
78
+ "Anthropic-Organization" => Anthropic.configuration.organization_id
79
+ }.merge(Anthropic.configuration.extra_headers)
80
+ end
81
+
82
+ def multipart_parameters(parameters)
83
+ parameters&.transform_values do |value|
84
+ next value unless value.is_a?(File)
85
+
86
+ # Doesn't seem like Anthropic need mime_type yet, so not worth
87
+ # the library to figure this out. Hence the empty string
88
+ # as the second argument.
89
+ Faraday::UploadIO.new(value, "", value.path)
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ module Anthropic
2
+ VERSION = "0.0.0".freeze
3
+ end
data/lib/anthropic.rb ADDED
@@ -0,0 +1,51 @@
1
+ require "faraday"
2
+ require "faraday/multipart"
3
+
4
+ require_relative "anthropic/http"
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
+ require_relative "anthropic/version"
11
+
12
+ module Anthropic
13
+ class Error < StandardError; end
14
+ class ConfigurationError < Error; end
15
+
16
+ class Configuration
17
+ attr_writer :access_token
18
+ attr_accessor :api_version, :organization_id, :uri_base, :request_timeout, :extra_headers
19
+
20
+ DEFAULT_API_VERSION = "v1".freeze
21
+ DEFAULT_URI_BASE = "https://api.anthropic.com/".freeze
22
+ DEFAULT_REQUEST_TIMEOUT = 120
23
+
24
+ def initialize
25
+ @access_token = nil
26
+ @api_version = DEFAULT_API_VERSION
27
+ @organization_id = nil
28
+ @uri_base = DEFAULT_URI_BASE
29
+ @request_timeout = DEFAULT_REQUEST_TIMEOUT
30
+ end
31
+
32
+ def access_token
33
+ return @access_token if @access_token
34
+
35
+ error_text = "Anthropic access token missing! See https://github.com/alexrudall/anthropic#usage"
36
+ raise ConfigurationError, error_text
37
+ end
38
+ end
39
+
40
+ class << self
41
+ attr_writer :configuration
42
+ end
43
+
44
+ def self.configuration
45
+ @configuration ||= Anthropic::Configuration.new
46
+ end
47
+
48
+ def self.configure
49
+ yield(configuration)
50
+ end
51
+ end
@@ -0,0 +1,2 @@
1
+ require_relative "../anthropic"
2
+ require_relative "../anthropic/compatibility"
@@ -0,0 +1,5 @@
1
+ ## All Submissions:
2
+
3
+ * [ ] Have you followed the guidelines in our [Contributing document](../blob/main/CONTRIBUTING.md)?
4
+ * [ ] Have you checked to ensure there aren't other open [Pull Requests](../pulls) for the same update/change?
5
+ * [ ] Have you added an explanation of what your changes do and why you'd like us to include them?
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anthropic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-multipart
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ description:
42
+ email:
43
+ - alexrudall@users.noreply.github.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".circleci/config.yml"
49
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
50
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
51
+ - ".github/dependabot.yml"
52
+ - ".gitignore"
53
+ - ".rspec"
54
+ - ".rubocop.yml"
55
+ - CHANGELOG.md
56
+ - CODE_OF_CONDUCT.md
57
+ - CONTRIBUTING.md
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - anthropic.gemspec
64
+ - bin/console
65
+ - bin/setup
66
+ - lib/anthropic.rb
67
+ - lib/anthropic/client.rb
68
+ - lib/anthropic/compatibility.rb
69
+ - lib/anthropic/http.rb
70
+ - lib/anthropic/version.rb
71
+ - lib/ruby/anthropic.rb
72
+ - pull_request_template.md
73
+ homepage: https://github.com/alexrudall/anthropic
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ homepage_uri: https://github.com/alexrudall/anthropic
78
+ source_code_uri: https://github.com/alexrudall/anthropic
79
+ changelog_uri: https://github.com/alexrudall/anthropic/blob/main/CHANGELOG.md
80
+ rubygems_mfa_required: 'true'
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.6.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.4.12
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: "Anthropic API + Ruby! \U0001F916❤️"
100
+ test_files: []