claude-ruby 0.1.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 +7 -0
- data/.idea/.gitignore +8 -0
- data/.idea/claude-ruby.iml +23 -0
- data/.idea/misc.xml +4 -0
- data/.idea/modules.xml +8 -0
- data/.idea/vcs.xml +6 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +137 -0
- data/Rakefile +17 -0
- data/lib/claude/client.rb +55 -0
- data/lib/claude/ruby/version.rb +7 -0
- data/lib/claude/ruby.rb +9 -0
- data/sig/claude/ruby.rbs +6 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b216c34d33d43142418a12fa080388398044f0552c2146b990a4d78d04f98d82
|
4
|
+
data.tar.gz: 9f82ea13bea11b376aa10b058fba3d45afbc04100fe0fd103e7a6cbd8de5b46a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f774be056a44ab57dd6bab72f0dd7d98e9c25a76f52b7524809beeab003bd14ac400a9b93d8a67323e5902fc91be745131980925f074a47878e61b79727055db
|
7
|
+
data.tar.gz: dcba4a5ff5c4472aa21222d20a37edc7558ae5d5b46abc332b4354844d93f1ff5c121e69766163bbb02f93fb8ca7a26e01da8ec7154fc1b6d533f8081f575f4b
|
data/.idea/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
3
|
+
<component name="ModuleRunConfigurationManager">
|
4
|
+
<shared />
|
5
|
+
</component>
|
6
|
+
<component name="NewModuleRootManager">
|
7
|
+
<content url="file://$MODULE_DIR$">
|
8
|
+
<sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
|
9
|
+
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
10
|
+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
11
|
+
</content>
|
12
|
+
<orderEntry type="inheritedJdk" />
|
13
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
14
|
+
<orderEntry type="library" scope="PROVIDED" name="bundler (v2.1.4, rbenv: 2.6.8) [gem]" level="application" />
|
15
|
+
<orderEntry type="library" scope="PROVIDED" name="minitest (v5.14.4, rbenv: 2.6.8) [gem]" level="application" />
|
16
|
+
<orderEntry type="library" scope="PROVIDED" name="rake (v13.0.6, rbenv: 2.6.8) [gem]" level="application" />
|
17
|
+
</component>
|
18
|
+
<component name="RakeTasksCache">
|
19
|
+
<option name="myRootTask">
|
20
|
+
<RakeTaskImpl id="rake" />
|
21
|
+
</option>
|
22
|
+
</component>
|
23
|
+
</module>
|
data/.idea/misc.xml
ADDED
data/.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectModuleManager">
|
4
|
+
<modules>
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/claude-ruby.iml" filepath="$PROJECT_DIR$/.idea/claude-ruby.iml" />
|
6
|
+
</modules>
|
7
|
+
</component>
|
8
|
+
</project>
|
data/.idea/vcs.xml
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in claude-ruby.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "minitest", "~> 5.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
|
14
|
+
gem 'rest-client'
|
15
|
+
|
16
|
+
group :test do
|
17
|
+
gem 'simplecov', require: false
|
18
|
+
gem 'simplecov_json_formatter', require: false
|
19
|
+
end
|
20
|
+
|
21
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Web Ventures Ltd (NZ)
|
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,137 @@
|
|
1
|
+
# Claude Ruby
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/claude-ruby) [](https://opensource.org/licenses/MIT) [](https://codeclimate.com/github/webven/claude-ruby/maintainability) [](https://codeclimate.com/github/webven/claude-ruby/test_coverage) [](https://github.com/webven/claude-ruby/actions/workflows/ci.yml)
|
4
|
+
|
5
|
+
`claude-ruby` gem is an unofficial ruby SDK for interacting with the Anthropic API, for generating and streaming messages through Claude
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'claude-ruby'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
$ bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
$ gem install claude-ruby
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
To use this gemyou'll need an API key from Anthropic, which can be obtained from the Anthropic console
|
30
|
+
by following the Get API Access link on the [Anthrpic API page](https://www.anthropic.com/api).
|
31
|
+
|
32
|
+
Set your API key as an environment variable then you can create a new `Claude::Client` instance:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'claude/client'
|
36
|
+
|
37
|
+
api_key = ENV['YOUR_ANTROPIC_API_KEY']
|
38
|
+
claude_client = Claude::Client.new(api_key)
|
39
|
+
```
|
40
|
+
|
41
|
+
## Messages
|
42
|
+
|
43
|
+
The anthropic messages endpoint allows you to:
|
44
|
+
```
|
45
|
+
Send a structured list of input messages with text and/or image content,
|
46
|
+
and the model will generate the next message in the conversation.
|
47
|
+
The Messages API can be used for for either single queries or stateless multi-turn conversations.
|
48
|
+
```
|
49
|
+
|
50
|
+
Using the claude-ruby gem you can call the Anthropic messages API by passing in an array of messages
|
51
|
+
where each element is a hash containing `role` and `content` properties.
|
52
|
+
|
53
|
+
The `messages` method allows converse with the Claude model in chat form.
|
54
|
+
It requires an array of messages where each message is a hash with two properties: `role` and `content`.
|
55
|
+
|
56
|
+
`role` can be:
|
57
|
+
- `'user'`: This represents the user's input.
|
58
|
+
- `'assistant'`: This optional role represents the model's output.
|
59
|
+
|
60
|
+
Simple example with a single user message:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
messages = [
|
64
|
+
{
|
65
|
+
role: "user",
|
66
|
+
content: "Who was the first team to win the rugby world cup?"
|
67
|
+
}
|
68
|
+
]
|
69
|
+
|
70
|
+
response = claude_client.messages(messages)
|
71
|
+
```
|
72
|
+
|
73
|
+
The response contains a bunch of metadata and the model's message response.
|
74
|
+
To extract the message text you can you code like this:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
response['content'][0]['text']
|
78
|
+
```
|
79
|
+
|
80
|
+
You can continue the conversation by calling the `messages` method again with an expanded messages array:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
|
84
|
+
messages << {role: "assistant", content: "New Zealand won the first Rugby World Cup in 1987"}
|
85
|
+
messages << {role: "user", content: "Who came third and fourth in that competition?"}
|
86
|
+
|
87
|
+
response = claude_client.messages(messages)
|
88
|
+
puts response['content'][0]['text'] # This will give you the updated message
|
89
|
+
```
|
90
|
+
|
91
|
+
Example with a more sophisticated message structure:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
system = "Only reply in Spanish."
|
95
|
+
|
96
|
+
messages = [
|
97
|
+
{
|
98
|
+
role: "user",
|
99
|
+
content: "Hi there."
|
100
|
+
},
|
101
|
+
{
|
102
|
+
role: "assistant",
|
103
|
+
content: "Hola, como estás?"
|
104
|
+
},
|
105
|
+
{
|
106
|
+
role: "user",
|
107
|
+
content: "How long does it take to fly from Auckland to Buenos Aires?"
|
108
|
+
},
|
109
|
+
]
|
110
|
+
|
111
|
+
response = claude_client.messages(messages, {system: system})
|
112
|
+
```
|
113
|
+
|
114
|
+
For further details of the API visit https://docs.anthropic.com/claude/reference/messages_post
|
115
|
+
|
116
|
+
## Changelog
|
117
|
+
|
118
|
+
For a detailed list of changes for each version of this project, please see the [CHANGELOG](CHANGELOG.md).
|
119
|
+
|
120
|
+
## Development
|
121
|
+
|
122
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
123
|
+
|
124
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
125
|
+
|
126
|
+
## Contributing
|
127
|
+
|
128
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/webven/claude-ruby.
|
129
|
+
|
130
|
+
## License
|
131
|
+
|
132
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
133
|
+
|
134
|
+
## Trademarks
|
135
|
+
|
136
|
+
This is an unofficial gem which has not been developed by Anthropic.
|
137
|
+
We make no claims for the trademarks of 'Anthropic' and 'Claude', which are registered by Anthropic PBC.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.pattern = 'test/**/*_test.rb'
|
9
|
+
t.libs << "lib"
|
10
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
11
|
+
end
|
12
|
+
|
13
|
+
require "rubocop/rake_task"
|
14
|
+
|
15
|
+
RuboCop::RakeTask.new
|
16
|
+
|
17
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Claude
|
5
|
+
class Client
|
6
|
+
|
7
|
+
def initialize(api_key)
|
8
|
+
@api_key = api_key
|
9
|
+
@endpoint = 'https://api.anthropic.com/v1'
|
10
|
+
end
|
11
|
+
|
12
|
+
def messages(messages, params = {})
|
13
|
+
model = params[:model] || 'claude-3-opus-20240229'
|
14
|
+
max_tokens = params[:max_tokens] || 1024
|
15
|
+
|
16
|
+
url = "#{@endpoint}/messages"
|
17
|
+
|
18
|
+
data = {
|
19
|
+
model: model,
|
20
|
+
messages: messages,
|
21
|
+
system: params[:system],
|
22
|
+
max_tokens: max_tokens,
|
23
|
+
metadata: params[:metadata],
|
24
|
+
stop_sequences: params[:stop_sequences],
|
25
|
+
stream: params[:stream],
|
26
|
+
temperature: params[:temperature],
|
27
|
+
top_p: params[:top_p],
|
28
|
+
top_k: params[:top_k],
|
29
|
+
}.compact
|
30
|
+
|
31
|
+
request_api(url, data)
|
32
|
+
end
|
33
|
+
|
34
|
+
def headers
|
35
|
+
{
|
36
|
+
'Content-Type' => 'application/json',
|
37
|
+
'anthropic-version' => "2023-06-01",
|
38
|
+
'x-api-key' => "#{@api_key}",
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def request_api(url, data, method = :post)
|
45
|
+
begin
|
46
|
+
response = RestClient::Request.execute(method: method, url: url, payload: data.to_json, headers: headers)
|
47
|
+
JSON.parse(response.body)
|
48
|
+
rescue RestClient::ExceptionWithResponse => e
|
49
|
+
error_msg = JSON.parse(e.response.body)['error']['message']
|
50
|
+
raise RestClient::ExceptionWithResponse.new("#{e.message}: #{error_msg} (#{e.http_code})"), nil, e.backtrace
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
data/lib/claude/ruby.rb
ADDED
data/sig/claude/ruby.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: claude-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Web Ventures Ltd
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Unofficial ruby SDK for interacting with the Anthropic API, for generating
|
28
|
+
and streaming messages through Claude.
|
29
|
+
email:
|
30
|
+
- webven@mailgab.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".idea/.gitignore"
|
36
|
+
- ".idea/claude-ruby.iml"
|
37
|
+
- ".idea/misc.xml"
|
38
|
+
- ".idea/modules.xml"
|
39
|
+
- ".idea/vcs.xml"
|
40
|
+
- ".rubocop.yml"
|
41
|
+
- CHANGELOG.md
|
42
|
+
- Gemfile
|
43
|
+
- LICENSE.txt
|
44
|
+
- README.md
|
45
|
+
- Rakefile
|
46
|
+
- lib/claude/client.rb
|
47
|
+
- lib/claude/ruby.rb
|
48
|
+
- lib/claude/ruby/version.rb
|
49
|
+
- sig/claude/ruby.rbs
|
50
|
+
homepage: https://github.com/webven/claude-ruby.git
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
homepage_uri: https://github.com/webven/claude-ruby.git
|
55
|
+
source_code_uri: https://github.com/webven/claude-ruby.git
|
56
|
+
changelog_uri: https://github.com/webven/claude-ruby/blob/main/CHANGELOG.md
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 2.6.0
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.5.3
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: A Ruby SDK for the Anthropic Claude API
|
76
|
+
test_files: []
|