foobara-anthropic-api 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/lib/foobara/anthropic_api.rb +5 -0
- data/src/foobara/ai/anthropic_api/create_message.rb +71 -0
- data/src/foobara/ai/anthropic_api/types/message.rb +14 -0
- data/src/foobara/ai/anthropic_api/types/message_result/content.rb +16 -0
- data/src/foobara/ai/anthropic_api/types/message_result/usage.rb +16 -0
- data/src/foobara/ai/anthropic_api/types/message_result.rb +23 -0
- data/src/foobara/ai/anthropic_api/types/model.rb +16 -0
- data/src/foobara/ai/anthropic_api/types/role.rb +14 -0
- data/src/foobara/ai/anthropic_api.rb +12 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 460564625ca99478e4533807393b31319cdbb1fbd88ec3bbf3c2fdad2553ade1
|
4
|
+
data.tar.gz: 539d6b7ec69c3f4b8ee858e327927980b112f79b46fb9f841d1415b73a993dc7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50c3d406b8b139e2447051c549bc657defc1a85d13d71faaaffa0ec23443af91d2925fcea58c93950a88679a77108aa8e29ab2154f16e280f79d2a6d3e0185e9
|
7
|
+
data.tar.gz: b8a0972cbd2eac9f490462b119cb7ea316306876c8ebf7e7d65b705af2ebbed0649aabb90bfdac7625707dd66cdbeef328b5940a25799c2acfb3d5ee31f64a87
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Miles Georgi
|
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.
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Foobara
|
2
|
+
module Ai
|
3
|
+
module AnthropicApi
|
4
|
+
class CreateMessage < Foobara::Command
|
5
|
+
URL = "https://api.anthropic.com/v1/messages".freeze
|
6
|
+
|
7
|
+
inputs do
|
8
|
+
# TODO: come up with some kind of execution context object to wrap all of these calls upon which
|
9
|
+
# data wrapping a "request" can live instead of being passed around?
|
10
|
+
api_token :string
|
11
|
+
system :string
|
12
|
+
max_tokens :integer, default: 1024
|
13
|
+
model :model, default: Types::ModelEnum::CLAUDE_3_5_SONNET
|
14
|
+
messages [Types::Message]
|
15
|
+
end
|
16
|
+
|
17
|
+
result Types::MessageResult
|
18
|
+
|
19
|
+
def execute
|
20
|
+
build_request_body
|
21
|
+
build_request_headers
|
22
|
+
issue_http_request
|
23
|
+
parse_response
|
24
|
+
build_message_result
|
25
|
+
|
26
|
+
message_result
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_accessor :request_body, :request_headers, :response, :response_body, :message_result
|
30
|
+
|
31
|
+
def build_request_body
|
32
|
+
self.request_body = { model:, max_tokens:, messages: }
|
33
|
+
end
|
34
|
+
|
35
|
+
def build_request_headers
|
36
|
+
self.request_headers = {
|
37
|
+
"Content-Type" => "application/json",
|
38
|
+
"X-Api-Key" => api_token,
|
39
|
+
"Anthropic-Version" => "2023-06-01"
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
def api_token
|
44
|
+
inputs[:api_token] || ENV.fetch("ANTHROPIC_API_KEY", nil)
|
45
|
+
end
|
46
|
+
|
47
|
+
def issue_http_request
|
48
|
+
url = URI.parse(URL)
|
49
|
+
self.response = Net::HTTP.post(url, JSON.generate(request_body), request_headers)
|
50
|
+
end
|
51
|
+
|
52
|
+
def parse_response
|
53
|
+
json = if response.is_a?(Net::HTTPSuccess)
|
54
|
+
response.body
|
55
|
+
else
|
56
|
+
# :nocov:
|
57
|
+
raise "Could not get message result from #{URL}: " \
|
58
|
+
"#{response.code} #{response.message}"
|
59
|
+
# :nocov:
|
60
|
+
end
|
61
|
+
|
62
|
+
self.response_body = JSON.parse(json)
|
63
|
+
end
|
64
|
+
|
65
|
+
def build_message_result
|
66
|
+
self.message_result = Types::MessageResult.new(response_body)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Foobara
|
2
|
+
module Ai
|
3
|
+
module AnthropicApi
|
4
|
+
module Types
|
5
|
+
class MessageResult < Foobara::Model
|
6
|
+
class Content < Foobara::Model
|
7
|
+
attributes do
|
8
|
+
text :string, :required
|
9
|
+
type :string, :required, one_of: %w[text]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Foobara
|
2
|
+
module Ai
|
3
|
+
module AnthropicApi
|
4
|
+
module Types
|
5
|
+
class MessageResult < Foobara::Model
|
6
|
+
class Usage < Foobara::Model
|
7
|
+
attributes do
|
8
|
+
input_tokens :integer, :required
|
9
|
+
output_tokens :integer, :required
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative "model"
|
2
|
+
require_relative "role"
|
3
|
+
|
4
|
+
module Foobara
|
5
|
+
module Ai
|
6
|
+
module AnthropicApi
|
7
|
+
module Types
|
8
|
+
class MessageResult < Foobara::Model
|
9
|
+
attributes do
|
10
|
+
content [Content]
|
11
|
+
id :string, :required
|
12
|
+
model :model, :required
|
13
|
+
role :role, :required
|
14
|
+
stop_reason :string, :required, one_of: %w[end_turn max_tokens stop_sequence]
|
15
|
+
stop_sequence :string, :allow_nil
|
16
|
+
type :string, :required, one_of: %w[message]
|
17
|
+
usage Usage
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Foobara
|
2
|
+
module Ai
|
3
|
+
module AnthropicApi
|
4
|
+
module Types
|
5
|
+
module ModelEnum
|
6
|
+
CLAUDE_3_5_SONNET = "claude-3-5-sonnet-20240620".freeze
|
7
|
+
CLAUDE_3_OPUS = "claude-3-opus-20240229".freeze
|
8
|
+
CLAUDE_3_SONNET = "claude-3-sonnet-20240229".freeze
|
9
|
+
CLAUDE_3_HAIKU = "claude-3-haiku-20240307".freeze
|
10
|
+
end
|
11
|
+
|
12
|
+
Model = AnthropicApi.foobara_register_type(:model, :string, one_of: ModelEnum)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: foobara-anthropic-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Miles Georgi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: foobara
|
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:
|
28
|
+
email:
|
29
|
+
- azimux@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE.txt
|
35
|
+
- lib/foobara/anthropic_api.rb
|
36
|
+
- src/foobara/ai/anthropic_api.rb
|
37
|
+
- src/foobara/ai/anthropic_api/create_message.rb
|
38
|
+
- src/foobara/ai/anthropic_api/types/message.rb
|
39
|
+
- src/foobara/ai/anthropic_api/types/message_result.rb
|
40
|
+
- src/foobara/ai/anthropic_api/types/message_result/content.rb
|
41
|
+
- src/foobara/ai/anthropic_api/types/message_result/usage.rb
|
42
|
+
- src/foobara/ai/anthropic_api/types/model.rb
|
43
|
+
- src/foobara/ai/anthropic_api/types/role.rb
|
44
|
+
homepage: https://github.com/foobara/anthropic-api
|
45
|
+
licenses:
|
46
|
+
- None specified yet
|
47
|
+
metadata:
|
48
|
+
homepage_uri: https://github.com/foobara/anthropic-api
|
49
|
+
source_code_uri: https://github.com/foobara/anthropic-api
|
50
|
+
changelog_uri: https://github.com/foobara/anthropic-api/blob/main/CHANGELOG.md
|
51
|
+
rubygems_mfa_required: 'true'
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.2.2
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.4.10
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: No description. Add one.
|
71
|
+
test_files: []
|