foobara-anthropic-api 0.0.4 → 0.0.6

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: 6f6a67c657c9b5459dfdb50e330cd25c35a3a48f63a49692c2eea441e96ec466
4
- data.tar.gz: c8ec1d575c6dcb6856e5bb99aa04b5a0ddb94fd3e83db2d266e0e7d9c0466630
3
+ metadata.gz: daf62838415e58692f48048970c2306fb4c4432c9be1036e73c2d1c93f5f488a
4
+ data.tar.gz: d40457b63a22ee95e691a7070ad6cb0b971284575b897961d6885644f50f8519
5
5
  SHA512:
6
- metadata.gz: f22fc5a3ec4436f162afe6b36701741f8d2c4c05616cc9e44e7d0862cdcc8eac7f1a2c7ada1430941492d2b5364fdc913f44e846147bdb5fa357907aea550cbd
7
- data.tar.gz: 169e96d8314445850b7baeb20c3e5c7f342ad8d494baab08d710dda32b2f234fab55d8201419208c793bd6118e6c1635ad914f718066ca3b413963b5e376fafa
6
+ metadata.gz: a8b7e11af929dd6b675cdc8b20bb9a6a07ab93d30f4868acb60f9e62d9e4760904015f5a88349cfe7dbc8cc17cd534e105539dca6a3c0c9f1b276f74c05e18c1
7
+ data.tar.gz: '08ebc138fd1cbe48dd1429971c2654b483f29e88d8b7b5b11f920c3ca481aef653ddc55fd2408417a14ccedebe803a743d57e27c4d9efa4d4cc44cc1f0450d6e'
@@ -0,0 +1,42 @@
1
+ require "foobara/http_api_command"
2
+
3
+ module Foobara
4
+ module Ai
5
+ module AnthropicApi
6
+ class BaseCommand < Foobara::Command
7
+ class << self
8
+ attr_accessor :api_token
9
+ end
10
+
11
+ include HttpApiCommand
12
+
13
+ base_url "https://api.anthropic.com/v1"
14
+
15
+ inputs do
16
+ api_token :string
17
+ anthropic_version :string
18
+ end
19
+
20
+ def build_request_headers
21
+ self.request_headers = {
22
+ "Content-Type" => "application/json",
23
+ "X-Api-Key" => api_token,
24
+ "Anthropic-Version" => anthropic_version
25
+ }
26
+ end
27
+
28
+ def api_token
29
+ inputs[:api_token] || BaseCommand.api_token || api_token_from_env
30
+ end
31
+
32
+ def api_token_from_env
33
+ ENV.fetch("ANTHROPIC_API_KEY", nil)
34
+ end
35
+
36
+ def anthropic_version
37
+ inputs[:anthropic_version] || ENV.fetch("ANTHROPIC_VERSION", "2023-06-01")
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,13 +1,13 @@
1
+ require_relative "base_command"
2
+
1
3
  module Foobara
2
4
  module Ai
3
5
  module AnthropicApi
4
- class CreateMessage < Foobara::Command
5
- URL = "https://api.anthropic.com/v1/messages".freeze
6
+ class CreateMessage < BaseCommand
7
+ path "/messages"
8
+ http_method :post
6
9
 
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
10
+ add_inputs do
11
11
  system :string
12
12
  max_tokens :integer, default: 1024
13
13
  model :model, default: Types::ModelEnum::CLAUDE_3_5_SONNET
@@ -16,54 +16,12 @@ module Foobara
16
16
 
17
17
  result Types::MessageResult
18
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
19
  def build_request_body
32
20
  self.request_body = { model:, max_tokens:, messages: }
33
21
  end
34
22
 
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, ignore_unexpected_attributes: true)
23
+ def build_result
24
+ Types::MessageResult.new(response_body, ignore_unexpected_attributes: true)
67
25
  end
68
26
  end
69
27
  end
@@ -0,0 +1,33 @@
1
+ require_relative "base_command"
2
+
3
+ module Foobara
4
+ module Ai
5
+ module AnthropicApi
6
+ class GetPageOfModels < BaseCommand
7
+ path "/models"
8
+
9
+ add_inputs do
10
+ before_id :string
11
+ after_id :string
12
+ limit :integer, max: 2000, min: 1
13
+ end
14
+
15
+ result Types::PageOfModels
16
+
17
+ def build_request_body
18
+ body = {}
19
+
20
+ body[:before_id] = before_id if before_id
21
+ body[:after_id] = after_id if after_id
22
+ body[:limit] = limit if limit
23
+
24
+ self.request_body = body
25
+ end
26
+
27
+ def build_result
28
+ Types::PageOfModels.new(response_body, ignore_unexpected_attributes: true)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,68 @@
1
+ require_relative "base_command"
2
+
3
+ module Foobara
4
+ module Ai
5
+ module AnthropicApi
6
+ class ListModels < BaseCommand
7
+ description "A convenience command that will repeatedly fetch pages of models until all are fetched"
8
+
9
+ inputs do
10
+ page_size :integer, max: 2000, min: 1, default: 20
11
+ api_token :string, :allow_nil
12
+ end
13
+
14
+ result [Types::Model]
15
+
16
+ depends_on GetPageOfModels
17
+
18
+ def execute
19
+ get_page_of_models
20
+
21
+ extract_models
22
+
23
+ while has_more?
24
+ get_next_page_of_models
25
+ extract_models
26
+ end
27
+
28
+ models
29
+ end
30
+
31
+ attr_accessor :page_of_models
32
+
33
+ def get_page_of_models
34
+ self.page_of_models = run_subcommand!(GetPageOfModels, get_page_of_models_inputs)
35
+ end
36
+
37
+ def extract_models
38
+ models.concat(page_of_models.data)
39
+ end
40
+
41
+ def has_more?
42
+ page_of_models.has_more
43
+ end
44
+
45
+ def get_next_page_of_models
46
+ inputs = get_page_of_models_inputs.merge(after_id: page_of_models.last_id)
47
+ self.page_of_models = run_subcommand!(GetPageOfModels, inputs)
48
+ end
49
+
50
+ def models
51
+ @models ||= []
52
+ end
53
+
54
+ def get_page_of_models_inputs
55
+ return @get_page_of_models_inputs if @get_page_of_models_inputs
56
+
57
+ @get_page_of_models_inputs = {}
58
+
59
+ if page_size
60
+ @get_page_of_models_inputs[:limit] = page_size
61
+ end
62
+
63
+ @get_page_of_models_inputs
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,4 +1,4 @@
1
- require_relative "model"
1
+ require_relative "model_enum"
2
2
  require_relative "role"
3
3
 
4
4
  module Foobara
@@ -2,14 +2,14 @@ module Foobara
2
2
  module Ai
3
3
  module AnthropicApi
4
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
5
+ class Model < Foobara::Model
6
+ attributes do
7
+ id :string, :required
8
+ display_name :string, :required
9
+ type :string, :required
10
+ created_at :datetime, :required
11
+ end
10
12
  end
11
-
12
- Model = AnthropicApi.foobara_register_type(:model, :string, one_of: ModelEnum)
13
13
  end
14
14
  end
15
15
  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
+ AnthropicApi.foobara_register_type(:model, :string, one_of: ModelEnum)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ require_relative "model"
2
+
3
+ module Foobara
4
+ module Ai
5
+ module AnthropicApi
6
+ module Types
7
+ class PageOfModels < Foobara::Model
8
+ attributes do
9
+ data [Types::Model], :required
10
+ first_id :string, :allow_nil, :required
11
+ last_id :string, :allow_nil, :required
12
+ has_more :boolean, :required
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -7,6 +7,16 @@ module Foobara
7
7
 
8
8
  module AnthropicApi
9
9
  foobara_domain!
10
+
11
+ class << self
12
+ def api_token=(token)
13
+ BaseCommand.api_token = token
14
+ end
15
+
16
+ def api_token
17
+ BaseCommand.api_token
18
+ end
19
+ end
10
20
  end
11
21
  end
12
22
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-anthropic-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-17 00:00:00.000000000 Z
10
+ date: 2025-02-18 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
- name: foobara
13
+ name: foobara-http-api-command
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
16
  - - ">="
@@ -32,12 +32,17 @@ files:
32
32
  - LICENSE.txt
33
33
  - lib/foobara/anthropic_api.rb
34
34
  - src/foobara/ai/anthropic_api.rb
35
+ - src/foobara/ai/anthropic_api/base_command.rb
35
36
  - src/foobara/ai/anthropic_api/create_message.rb
37
+ - src/foobara/ai/anthropic_api/get_page_of_models.rb
38
+ - src/foobara/ai/anthropic_api/list_models.rb
36
39
  - src/foobara/ai/anthropic_api/types/message.rb
37
40
  - src/foobara/ai/anthropic_api/types/message_result.rb
38
41
  - src/foobara/ai/anthropic_api/types/message_result/content.rb
39
42
  - src/foobara/ai/anthropic_api/types/message_result/usage.rb
40
43
  - src/foobara/ai/anthropic_api/types/model.rb
44
+ - src/foobara/ai/anthropic_api/types/model_enum.rb
45
+ - src/foobara/ai/anthropic_api/types/page_of_models.rb
41
46
  - src/foobara/ai/anthropic_api/types/role.rb
42
47
  homepage: https://github.com/foobara/anthropic-api
43
48
  licenses: