omniai-openai 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -2
- data/lib/omniai/openai/client.rb +9 -4
- data/lib/omniai/openai/config.rb +3 -1
- data/lib/omniai/openai/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3afb339ec2a0e69d5a268fd134aae4119e3140ccad493f22d7db43b817145a4
|
4
|
+
data.tar.gz: f49c3cfd25228e49555981cc93e05e6702d3ce4edde9fa07277bc68c83312361
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4455db3523b81301793b936528312394cb0f98b8f51ad7a49f5f6fe5811c2969dcaf679649deb0cb90bfec5fdf5838f3ba6444eea645d560ceaaaf96b9c15f5f
|
7
|
+
data.tar.gz: 804f70fd6067cba057aaa7dbbe7d65f5edaa389e5315fb4a107f154fbafa1139d33ad637439e8b838036767c02e4e2cfed0d69c43c0a36cb805bb4dfe21eb907
|
data/README.md
CHANGED
@@ -25,7 +25,7 @@ A client may also be passed the following options:
|
|
25
25
|
- `api_key` (required - default is `ENV['OPENAI_API_KEY']`)
|
26
26
|
- `organization` (optional)
|
27
27
|
- `project` (optional)
|
28
|
-
- `host` (optional)
|
28
|
+
- `host` (optional) useful for usage with Ollama or LocalAI
|
29
29
|
|
30
30
|
### Configuration
|
31
31
|
|
@@ -36,10 +36,30 @@ OmniAI::OpenAI.configure do |config|
|
|
36
36
|
config.api_key = 'sk-...' # default: ENV['OPENAI_API_KEY']
|
37
37
|
config.organization = '...' # default: ENV['OPENAI_ORGANIZATION']
|
38
38
|
config.project = '...' # default: ENV['OPENAI_PROJECT']
|
39
|
-
config.host = '...' # default: 'https://api.openai.com'
|
39
|
+
config.host = '...' # default: 'https://api.openai.com' - override for usage with LocalAI / Ollama
|
40
40
|
end
|
41
41
|
```
|
42
42
|
|
43
|
+
#### Usage with [LocalAI](https://localai.io/)
|
44
|
+
|
45
|
+
LocalAI offers [built in compatability with the OpenAI specification](https://localai.io/). To initialize a client that points to a Ollama change the host accordingly:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
client = OmniAI::OpenAI::Client.new(host: 'http://localhost:8080', api_key: nil)
|
49
|
+
```
|
50
|
+
|
51
|
+
_For details on installation or running LocalAI see the [getting started tutorial](https://localai.io/basics/getting_started/)._
|
52
|
+
|
53
|
+
#### Usage with [Ollama](https://www.ollama.com/)
|
54
|
+
|
55
|
+
Ollama offers [built in compatability with the OpenAI specification](https://ollama.com/blog/openai-compatibility). To initialize a client that points to a Ollama change the host accordingly:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
client = OmniAI::OpenAI::Client.new(host: 'http://localhost:11434', api_key: nil)
|
59
|
+
```
|
60
|
+
|
61
|
+
_For details on installation or running Ollama checkout [the project README](https://github.com/ollama/ollama)._
|
62
|
+
|
43
63
|
### Chat
|
44
64
|
|
45
65
|
A chat completion is generated by passing in prompts using any a variety of formats:
|
data/lib/omniai/openai/client.rb
CHANGED
@@ -33,19 +33,24 @@ module OmniAI
|
|
33
33
|
logger: OmniAI::OpenAI.config.logger,
|
34
34
|
host: OmniAI::OpenAI.config.host
|
35
35
|
)
|
36
|
-
|
36
|
+
if api_key.nil? && host.eql?(Config::DEFAULT_HOST)
|
37
|
+
raise(
|
38
|
+
ArgumentError,
|
39
|
+
%(ENV['OPENAI_API_KEY'] must be defined or `api_key` must be passed when using #{Config::DEFAULT_HOST})
|
40
|
+
)
|
41
|
+
end
|
37
42
|
|
38
|
-
super(api_key:, logger:)
|
43
|
+
super(api_key:, host:, logger:)
|
39
44
|
|
40
45
|
@organization = organization
|
41
46
|
@project = project
|
42
|
-
@host = host
|
43
47
|
end
|
44
48
|
|
45
49
|
# @return [HTTP::Client]
|
46
50
|
def connection
|
47
51
|
@connection ||= begin
|
48
|
-
http = HTTP.
|
52
|
+
http = HTTP.persistent(@host)
|
53
|
+
http = http.auth("Bearer #{@api_key}") if @api_key
|
49
54
|
http = http.headers('OpenAI-Organization': @organization) if @organization
|
50
55
|
http = http.headers('OpenAI-Project': @project) if @project
|
51
56
|
http
|
data/lib/omniai/openai/config.rb
CHANGED
@@ -6,12 +6,14 @@ module OmniAI
|
|
6
6
|
class Config < OmniAI::Config
|
7
7
|
attr_accessor :organization, :project, :chat_options, :transcribe_options, :speak_options
|
8
8
|
|
9
|
+
DEFAULT_HOST = 'https://api.openai.com'
|
10
|
+
|
9
11
|
def initialize
|
10
12
|
super
|
11
13
|
@api_key = ENV.fetch('OPENAI_API_KEY', nil)
|
12
14
|
@organization = ENV.fetch('OPENAI_ORGANIZATION', nil)
|
13
15
|
@project = ENV.fetch('OPENAI_PROJECT', nil)
|
14
|
-
@host = ENV.fetch('OPENAI_HOST',
|
16
|
+
@host = ENV.fetch('OPENAI_HOST', DEFAULT_HOST)
|
15
17
|
@chat_options = {}
|
16
18
|
@transcribe_options = {}
|
17
19
|
@speak_options = {}
|