foobara-anthropic-api 1.0.8 → 1.1.1

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: bc00c9fcd2f643b5749fd901144969a4798dae15e7bcadf2683d41fe9210dabb
4
- data.tar.gz: f0b04ea2f80ccfa89e4ad80a83380aaf68f28cb9435cc5beb9ca4a0d5b0f80da
3
+ metadata.gz: a52af4ea8053636f9d23c34c2fde33bd9810460cad12c93164057bff397c4b9a
4
+ data.tar.gz: bef0b74d336a08c0e02d71b5ec3d31af661dd73b4ef5d12f21dc763e0c61dfd4
5
5
  SHA512:
6
- metadata.gz: f60b5578bce421d195ede689011bc7a0b307d46fc266af82804e32d6b0e97faad4f0cb6f97d6f7cafaaca67c38652fadadc1924a45b14837eaa2aa6d337b5755
7
- data.tar.gz: 3b8be65f2488c3fa9dff460b8279c06c7f157756d9560c76502b105fc7fd08248fe2afc21e21d5956e3d7c28f7db0a20964b5042e2e78857c684736702808677
6
+ metadata.gz: 4d655a2a5954e403f8dbec91c75ec1b89ff32dbd76c27fba4f5087b29263b328444c09b19be40223d68bd6a77af02e0fa43fb00ea58323e3cd3d0dd637896e1a
7
+ data.tar.gz: 2c53065060423ed1646427d348ffca75789cca9d6fddeaff3e2f8f23538c09526eb3b7707e8e695c45419cf998ea155ec0e77c4b977d416e915b9cc97ed09832
@@ -1,5 +1,5 @@
1
1
  require "foobara/all"
2
2
 
3
- require_relative "../../src/foobara/ai/anthropic_api"
3
+ require_relative "../../src/anthropic_api"
4
4
 
5
5
  Foobara::Util.require_directory "#{__dir__}/../../src"
@@ -0,0 +1,59 @@
1
+ require_relative "get_model"
2
+
3
+ module Foobara
4
+ module Ai
5
+ module AnthropicApi
6
+ class CreateMessage < BaseCommand
7
+ path "/messages"
8
+ http_method :post
9
+
10
+ add_inputs do
11
+ system :string
12
+ max_tokens :integer, :allow_nil, "nil means whatever the max allowed is.", default: nil
13
+ model :model_enum, default: AnthropicApi.default_llm_model
14
+ messages [Types::Message]
15
+ # TODO: maybe this shouldn't be supported here?? It's not really part of this API.
16
+ # Maybe relocate this to Foobara::Ai instead.
17
+ fail_if_hitting_token_limit :boolean, default: false
18
+ end
19
+
20
+ result Types::MessageResult
21
+
22
+ possible_error :hit_token_limit, context: { model: :model_enum, max_tokens: :integer }
23
+
24
+ depends_on GetModel
25
+
26
+ def build_request_body
27
+ body = { model:, max_tokens:, messages: }
28
+
29
+ sys = inputs[:system]
30
+ if sys
31
+ body[:system] = sys
32
+ end
33
+
34
+ self.request_body = body
35
+ end
36
+
37
+ def max_tokens
38
+ @max_tokens ||= inputs[:max_tokens] ||
39
+ run_subcommand!(GetModel, model:).max_tokens ||
40
+ 64_000
41
+ end
42
+
43
+ def build_result
44
+ message_result = Types::MessageResult.new(
45
+ response_body,
46
+ # Set this to false to discover new attributes
47
+ ignore_unexpected_attributes: true
48
+ )
49
+
50
+ if fail_if_hitting_token_limit && message_result.hit_max_tokens?
51
+ add_runtime_error(:hit_token_limit, model:, max_tokens:)
52
+ end
53
+
54
+ message_result
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
data/src/get_model.rb ADDED
@@ -0,0 +1,31 @@
1
+ require_relative "base_command"
2
+
3
+ module Foobara
4
+ module Ai
5
+ module AnthropicApi
6
+ class GetModel < BaseCommand
7
+ include CachedCommand
8
+
9
+ self.foobara_cache_expiry = 24 * 60 * 60
10
+
11
+ def foobara_cache_key
12
+ @foobara_cache_key ||= [Util.underscore(self.class.name), model].join("-")
13
+ end
14
+
15
+ path { "/models/#{model}" }
16
+
17
+ add_inputs do
18
+ model :model_enum, :required
19
+ end
20
+
21
+ result Types::Model
22
+
23
+ def build_result
24
+ Types::Model.new(response_body,
25
+ # Set this to false to discover new attributes
26
+ ignore_unexpected_attributes: true)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -23,8 +23,10 @@ module Foobara
23
23
  end
24
24
 
25
25
  def text
26
- content.rfind { it.type == "text" }.text
26
+ content.reverse.find { it.type == "text" }&.text
27
27
  end
28
+
29
+ def hit_max_tokens? = stop_reason == "max_tokens"
28
30
  end
29
31
  end
30
32
  end
@@ -7,6 +7,7 @@ module Foobara
7
7
  model_list = ListModels.run!.map(&:id).sort
8
8
  ModelEnum = Foobara::Enumerated.make_module(model_list)
9
9
 
10
+ # TODO: change all 3 projects to use :symbol instead of :string
10
11
  AnthropicApi.foobara_register_type(:model_enum, :string, one_of: ModelEnum)
11
12
  end
12
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-anthropic-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
@@ -65,19 +65,20 @@ extra_rdoc_files: []
65
65
  files:
66
66
  - LICENSE.txt
67
67
  - lib/foobara/anthropic_api.rb
68
- - src/foobara/ai/anthropic_api.rb
69
- - src/foobara/ai/anthropic_api/base_command.rb
70
- - src/foobara/ai/anthropic_api/create_message.rb
71
- - src/foobara/ai/anthropic_api/get_page_of_models.rb
72
- - src/foobara/ai/anthropic_api/list_models.rb
73
- - src/foobara/ai/anthropic_api/types/message.rb
74
- - src/foobara/ai/anthropic_api/types/message_result.rb
75
- - src/foobara/ai/anthropic_api/types/message_result/content.rb
76
- - src/foobara/ai/anthropic_api/types/message_result/usage.rb
77
- - src/foobara/ai/anthropic_api/types/model.rb
78
- - src/foobara/ai/anthropic_api/types/model_enum.rb
79
- - src/foobara/ai/anthropic_api/types/page_of_models.rb
80
- - src/foobara/ai/anthropic_api/types/role.rb
68
+ - src/anthropic_api.rb
69
+ - src/base_command.rb
70
+ - src/create_message.rb
71
+ - src/get_model.rb
72
+ - src/get_page_of_models.rb
73
+ - src/list_models.rb
74
+ - src/types/message.rb
75
+ - src/types/message_result.rb
76
+ - src/types/message_result/content.rb
77
+ - src/types/message_result/usage.rb
78
+ - src/types/model.rb
79
+ - src/types/model_enum.rb
80
+ - src/types/page_of_models.rb
81
+ - src/types/role.rb
81
82
  homepage: https://github.com/foobara/anthropic-api
82
83
  licenses:
83
84
  - None specified yet
@@ -1,40 +0,0 @@
1
- require_relative "base_command"
2
-
3
- module Foobara
4
- module Ai
5
- module AnthropicApi
6
- class CreateMessage < BaseCommand
7
- path "/messages"
8
- http_method :post
9
-
10
- add_inputs do
11
- system :string
12
- max_tokens :integer, default: 1024
13
- model :model_enum, default: AnthropicApi.default_llm_model
14
- messages [Types::Message]
15
- end
16
-
17
- result Types::MessageResult
18
-
19
- def build_request_body
20
- body = { model:, max_tokens:, messages: }
21
-
22
- sys = inputs[:system]
23
- if sys
24
- body[:system] = sys
25
- end
26
-
27
- self.request_body = body
28
- end
29
-
30
- def build_result
31
- Types::MessageResult.new(
32
- response_body,
33
- # Set this to false to discover new attributes
34
- ignore_unexpected_attributes: true
35
- )
36
- end
37
- end
38
- end
39
- end
40
- end