foobara-open-ai-api 0.0.7 → 0.0.9

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: ef8553ce176e7f0b8d4059b06c7606ff4670381d404f51db536812dec5eb6469
4
- data.tar.gz: 6736eb9ce73a9b6f1f6b7e56a09e347d80cec7c3dfe81f69f757c21415837657
3
+ metadata.gz: 5e6cb9bdbfee03bc629c3c4b2641363f846dec53e14317860cda02c7be0faa22
4
+ data.tar.gz: 60f99581e848a20defcbfbc5f9ba5b01fa4b30a61a49bb8399c557eb8cfd2921
5
5
  SHA512:
6
- metadata.gz: 0c73216f70e9cbf0ef4ef224c2c8db9be55a39102f19bd799edbd86d5a583d43dd450a5d36b6fd761eaaa4267a22144094ecbcda564feea1c82390f90e587a44
7
- data.tar.gz: b80ee1a7820d1ced6bf690d1b6b5bb517a14c789f76aaab0cf75573913ca12003a3c5395c893f28af620580130acc77fe3e9e280248ce9743246c3203bea54eb
6
+ metadata.gz: f79050ea578166386b43af65ed4efe7249e92aba4d3c87eced62d5e8859a65634c2e852fd18a304fccfd8d904a4a7a40d7c4a092ab030b89fe9c7cd0c684cb2f
7
+ data.tar.gz: 3ae087e54109610caa2ce6d065d8658587b751612bf7ddc26e0a7b00b4f69a4ce5db72b1a85073979ca2f47d9ae951d6ce57e4e4c4a453e5eccb207961fb8d91
data/CHANGELOG.md CHANGED
@@ -1,8 +1,10 @@
1
- ## [0.0.7] - 2025-02-17
1
+ ## [0.0.8] - 2025-02-17
2
2
 
3
3
  - Add support for some newly added fields
4
4
  - Make sure our models don't break when OpenAI adds new attributes to their responses
5
5
  before we have a chance to update them
6
+ - Implement ListModels
7
+ - Provide a way to set api token on project module
6
8
 
7
9
  ## [0.0.6] - 2025-02-16
8
10
 
@@ -0,0 +1,33 @@
1
+ require "foobara/http_api_command"
2
+
3
+ module Foobara
4
+ module Ai
5
+ module OpenAiApi
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.openai.com/v1"
14
+
15
+ inputs do
16
+ api_token :string
17
+ end
18
+
19
+ def api_token
20
+ inputs[:api_token] || BaseCommand.api_token || api_token_from_env
21
+ end
22
+
23
+ def api_token_from_env
24
+ ENV.fetch("OPENAI_API_KEY", nil)
25
+ end
26
+
27
+ def build_request_headers
28
+ self.request_headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{api_token}" }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,13 +1,13 @@
1
+ require_relative "base_command"
2
+
1
3
  module Foobara
2
4
  module Ai
3
5
  module OpenAiApi
4
- class CreateChatCompletion < Foobara::Command
5
- URL = "https://api.openai.com/v1/chat/completions".freeze
6
+ class CreateChatCompletion < BaseCommand
7
+ path "/chat/completions"
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
  model :string, default: "gpt-3.5-turbo", one_of: %w[gpt-3.5-turbo gpt-4]
12
12
  messages :array do
13
13
  role :string, one_of: %w[system user assistant]
@@ -17,50 +17,12 @@ module Foobara
17
17
 
18
18
  result Types::ChatCompletion
19
19
 
20
- def execute
21
- build_request_body
22
- build_request_headers
23
- issue_http_request
24
- parse_response
25
- build_completion
26
-
27
- completion
28
- end
29
-
30
- attr_accessor :request_body, :request_headers, :response, :response_body, :completion
31
-
32
20
  def build_request_body
33
21
  self.request_body = { model:, messages: }
34
22
  end
35
23
 
36
- def build_request_headers
37
- self.request_headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{api_token}" }
38
- end
39
-
40
- def api_token
41
- inputs[:api_token] || ENV.fetch("OPENAI_API_KEY", nil)
42
- end
43
-
44
- def issue_http_request
45
- url = URI.parse(URL)
46
- self.response = Net::HTTP.post(url, JSON.generate(request_body), request_headers)
47
- end
48
-
49
- def parse_response
50
- completion_json = if response.is_a?(Net::HTTPSuccess)
51
- response.body
52
- else
53
- # :nocov:
54
- raise "Could not get completion from #{URL}: " \
55
- "#{response.code} #{response.message}"
56
- # :nocov:
57
- end
58
-
59
- self.response_body = JSON.parse(completion_json)
60
- end
61
-
62
- def build_completion
63
- self.completion = Types::ChatCompletion.new(response_body)
24
+ def build_result
25
+ Types::ChatCompletion.new(response_body, ignore_unexpected_attributes: true)
64
26
  end
65
27
  end
66
28
  end
@@ -0,0 +1,19 @@
1
+ module Foobara
2
+ module Ai
3
+ module OpenAiApi
4
+ class ListModels < BaseCommand
5
+ path "/models"
6
+
7
+ inputs do
8
+ api_token :string
9
+ end
10
+
11
+ result [Types::Model]
12
+
13
+ def build_result
14
+ response_body["data"]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ module Foobara
2
+ module Ai
3
+ module OpenAiApi
4
+ module Types
5
+ class Model < Foobara::Model
6
+ attributes do
7
+ id :string
8
+ created :datetime
9
+ object :string
10
+ owned_by :string
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -5,6 +5,16 @@ module Foobara
5
5
  module Ai
6
6
  module OpenAiApi
7
7
  foobara_domain!
8
+
9
+ class << self
10
+ def api_token=(token)
11
+ BaseCommand.api_token = token
12
+ end
13
+
14
+ def api_token
15
+ BaseCommand.api_token
16
+ end
17
+ end
8
18
  end
9
19
  end
10
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-open-ai-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
@@ -10,7 +10,7 @@ cert_chain: []
10
10
  date: 2025-02-17 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
  - - ">="
@@ -36,11 +36,14 @@ files:
36
36
  - README.md
37
37
  - lib/foobara/open_ai_api.rb
38
38
  - src/foobara/ai/open_ai_api.rb
39
+ - src/foobara/ai/open_ai_api/base_command.rb
39
40
  - src/foobara/ai/open_ai_api/create_chat_completion.rb
41
+ - src/foobara/ai/open_ai_api/list_models.rb
40
42
  - src/foobara/ai/open_ai_api/types/chat_completion.rb
41
43
  - src/foobara/ai/open_ai_api/types/chat_completion/choice.rb
42
44
  - src/foobara/ai/open_ai_api/types/chat_completion/message.rb
43
45
  - src/foobara/ai/open_ai_api/types/chat_completion/usage.rb
46
+ - src/foobara/ai/open_ai_api/types/model.rb
44
47
  homepage: https://github.com/foobara/open-ai-api
45
48
  licenses:
46
49
  - Apache-2.0