foobara-open-ai-api 0.0.6 → 0.0.8
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 +4 -4
- data/CHANGELOG.md +7 -0
- data/src/foobara/ai/open_ai_api/base_command.rb +27 -0
- data/src/foobara/ai/open_ai_api/create_chat_completion.rb +8 -46
- data/src/foobara/ai/open_ai_api/list_models.rb +19 -0
- data/src/foobara/ai/open_ai_api/types/chat_completion/message.rb +1 -0
- data/src/foobara/ai/open_ai_api/types/chat_completion/usage.rb +10 -0
- data/src/foobara/ai/open_ai_api/types/chat_completion.rb +1 -0
- data/src/foobara/ai/open_ai_api/types/model.rb +16 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1134ded66f3c63f8f0def005ed37383f5c69522899e51fe3316dce02c1210f8
|
4
|
+
data.tar.gz: 0170e54de38d1b4ac99b8d7c110d67ce67dbb2da53bebca8fdf94dda7c0f0027
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be8f41c655f0a08a2c3b44a2c13ad9f5bf2f39fb055b2119b537a3459e344fb476bb672087e2887ae5e3d779bca81b93b0f9a2c0771a56f98b24a72fe26c8f9f
|
7
|
+
data.tar.gz: 2f091240994a101906585d51cd6915d0153dd7d2eb1f011f63d6cfa8f7e8593144be01ce1877ca47e8096870eef4b91cfa31a8966d617a8d24fce695d0675b39
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## [0.0.7] - 2025-02-17
|
2
|
+
|
3
|
+
- Add support for some newly added fields
|
4
|
+
- Make sure our models don't break when OpenAI adds new attributes to their responses
|
5
|
+
before we have a chance to update them
|
6
|
+
- Implement ListModels
|
7
|
+
|
1
8
|
## [0.0.6] - 2025-02-16
|
2
9
|
|
3
10
|
- Rename Completion to ChatCompletion
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "foobara/http_api_command"
|
2
|
+
|
3
|
+
module Foobara
|
4
|
+
module Ai
|
5
|
+
module OpenAiApi
|
6
|
+
class BaseCommand < Foobara::Command
|
7
|
+
include HttpApiCommand
|
8
|
+
|
9
|
+
base_url "https://api.openai.com/v1"
|
10
|
+
|
11
|
+
inputs do
|
12
|
+
# TODO: come up with some kind of execution context object to wrap all of these calls upon which
|
13
|
+
# data wrapping a "request" can live instead of being passed around?
|
14
|
+
api_token :string
|
15
|
+
end
|
16
|
+
|
17
|
+
def api_token
|
18
|
+
inputs[:api_token] || ENV.fetch("OPENAI_API_KEY", nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_request_headers
|
22
|
+
self.request_headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{api_token}" }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
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 <
|
5
|
-
|
6
|
+
class CreateChatCompletion < BaseCommand
|
7
|
+
path "/chat/completions"
|
8
|
+
http_method :post
|
6
9
|
|
7
|
-
|
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
|
37
|
-
|
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
|
@@ -8,6 +8,16 @@ module Foobara
|
|
8
8
|
prompt_tokens :integer
|
9
9
|
completion_tokens :integer
|
10
10
|
total_tokens :integer
|
11
|
+
prompt_tokens_details do
|
12
|
+
audio_tokens :integer
|
13
|
+
cached_tokens :integer
|
14
|
+
end
|
15
|
+
completion_tokens_details do
|
16
|
+
accepted_prediction_tokens :integer
|
17
|
+
audio_tokens :integer
|
18
|
+
reasoning_tokens :integer
|
19
|
+
rejected_prediction_tokens :integer
|
20
|
+
end
|
11
21
|
end
|
12
22
|
end
|
13
23
|
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foobara-open-ai-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Miles Georgi
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-02-
|
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
|