omniai 0.0.7 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -7
- data/lib/omniai/chat/choice.rb +19 -7
- data/lib/omniai/chat/chunk.rb +41 -0
- data/lib/omniai/chat/completion.rb +51 -0
- data/lib/omniai/chat/delta.rb +26 -0
- data/lib/omniai/chat/message.rb +13 -4
- data/lib/omniai/chat/request.rb +27 -11
- data/lib/omniai/chat/usage.rb +30 -0
- data/lib/omniai/version.rb +1 -1
- metadata +20 -3
- data/lib/omniai/chat/response.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bab309e1be1b9acc7671c341b8e50cd0ec257ac22b254899b3661e428432f290
|
4
|
+
data.tar.gz: 7968c811a16cf77172b087a03693641dbca83f2ba683e5eff30eda05ad4817e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b22be38a54e83cb76c45e94d739290ee2530e51d81fd892e8a749deba3a54f88d9f7ec99a159df2688dadd7f5be9ce0a6ad03acfc7d0344e7ae4910e910f3bce
|
7
|
+
data.tar.gz: a5a11dd87da47b2f5ee4a49d9a37bbfe55ccb1ff3b0a6da48544d076eb850e925343775ea90cbdf6a30f309bcb2d3e783be21798d7deecfc2762dca80a886393
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@ gem install omniai-openai
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
###
|
25
|
+
### Client
|
26
26
|
|
27
27
|
#### Anthropic (Claude)
|
28
28
|
|
@@ -30,8 +30,6 @@ gem install omniai-openai
|
|
30
30
|
require 'omniai-anthropic'
|
31
31
|
|
32
32
|
client = OmniAI::Anthropic::Client.new
|
33
|
-
completion = client.chat.completion('Tell me a joke.')
|
34
|
-
completion.choice.message.content # '...'
|
35
33
|
```
|
36
34
|
|
37
35
|
#### Google (Gemini)
|
@@ -40,8 +38,6 @@ completion.choice.message.content # '...'
|
|
40
38
|
require 'omniai-google'
|
41
39
|
|
42
40
|
client = OmniAI::Google::Client.new
|
43
|
-
completion = client.chat.completion('Tell me a joke.')
|
44
|
-
completion.choice.message.content # '...'
|
45
41
|
```
|
46
42
|
|
47
43
|
#### Mistral (LeChat)
|
@@ -50,8 +46,6 @@ completion.choice.message.content # '...'
|
|
50
46
|
require 'omniai-mistral'
|
51
47
|
|
52
48
|
client = OmniAI::Mistral::Client.new
|
53
|
-
completion = client.chat.completion('Tell me a joke.')
|
54
|
-
completion.choice.message.content # '...'
|
55
49
|
```
|
56
50
|
|
57
51
|
#### OpenAI (ChatGPT)
|
@@ -63,3 +57,20 @@ client = OmniAI::OpenAI::Client.new
|
|
63
57
|
completion = client.chat.completion('Tell me a joke.')
|
64
58
|
completion.choice.message.content # '...'
|
65
59
|
```
|
60
|
+
|
61
|
+
### Chat
|
62
|
+
|
63
|
+
#### Basic
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
completion = client.chat.completion('Tell me a joke.')
|
67
|
+
puts(completion.choice.message.content) # '...'
|
68
|
+
```
|
69
|
+
|
70
|
+
#### Streaming
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
stream = proc do |chunk|
|
74
|
+
print(chunk.choice.delta.content) # '...'
|
75
|
+
end
|
76
|
+
```
|
data/lib/omniai/chat/choice.rb
CHANGED
@@ -4,14 +4,26 @@ module OmniAI
|
|
4
4
|
class Chat
|
5
5
|
# A choice returned by the API.
|
6
6
|
class Choice
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :data
|
8
8
|
|
9
|
-
# @param
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
# @param data [Hash]
|
10
|
+
def initialize(data:)
|
11
|
+
@data = data
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [Integer]
|
15
|
+
def index
|
16
|
+
@data['index']
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [OmniAI::Chat::Delta]
|
20
|
+
def delta
|
21
|
+
Delta.new(data: @data['delta']) if @data['delta']
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [OmniAI::Chat::Message]
|
25
|
+
def message
|
26
|
+
Message.new(data: @data['message']) if @data['message']
|
15
27
|
end
|
16
28
|
end
|
17
29
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
class Chat
|
5
|
+
# A delta returned by the API.
|
6
|
+
class CompletionChunk
|
7
|
+
attr_accessor :data
|
8
|
+
|
9
|
+
# @param data [Hash]
|
10
|
+
def initialize(data:)
|
11
|
+
@data = data
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
def id
|
16
|
+
@data['id']
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Time]
|
20
|
+
def created
|
21
|
+
Time.at(@data['created']) if @data['created']
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Time]
|
25
|
+
def updated
|
26
|
+
Time.at(@data['updated']) if @data['updated']
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [String]
|
30
|
+
def model
|
31
|
+
@data['model']
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param [index] [Integer]
|
35
|
+
# @return [OmniAI::Chat::Delta]
|
36
|
+
def choice(index: 0)
|
37
|
+
choices[index]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
class Chat
|
5
|
+
# An class wrapping the response completion. A vendor may override if custom behavior is required.
|
6
|
+
class Completion
|
7
|
+
attr_accessor :data
|
8
|
+
|
9
|
+
# @param data [Hash]
|
10
|
+
def initialize(data:)
|
11
|
+
@data = data
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
def id
|
16
|
+
@data['id']
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Time]
|
20
|
+
def created
|
21
|
+
Time.at(@data['created']) if @data['created']
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Time]
|
25
|
+
def updated
|
26
|
+
Time.at(@data['updated']) if @data['updated']
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [String]
|
30
|
+
def model
|
31
|
+
@data['model']
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [OmniAI::Chat::Usage]
|
35
|
+
def usage
|
36
|
+
@usage ||= Usage.new(data: @data['usage']) if @data['usage']
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Array<OmniAI::Chat::Choice>]
|
40
|
+
def choices
|
41
|
+
@choices ||= @data['choices'].map { |choice| Choice.new(data: choice) }
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param [index] [Integer] optional - default is 0
|
45
|
+
# @return [OmniAI::Chat::Choice]
|
46
|
+
def choice(index: 0)
|
47
|
+
choices[index]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
class Chat
|
5
|
+
# A delta returned by the API.
|
6
|
+
class Delta
|
7
|
+
attr_accessor :data
|
8
|
+
|
9
|
+
# @param content [Integer]
|
10
|
+
# @param role [String]
|
11
|
+
def initialize(data:)
|
12
|
+
@data = data
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [String, nil]
|
16
|
+
def role
|
17
|
+
@data['role']
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [String, nil]
|
21
|
+
def content
|
22
|
+
@data['content']
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/omniai/chat/message.rb
CHANGED
@@ -4,13 +4,22 @@ module OmniAI
|
|
4
4
|
class Chat
|
5
5
|
# A message returned by the API.
|
6
6
|
class Message
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :data
|
8
8
|
|
9
9
|
# @param content [Integer]
|
10
10
|
# @param role [String]
|
11
|
-
def initialize(
|
12
|
-
@
|
13
|
-
|
11
|
+
def initialize(data:)
|
12
|
+
@data = data
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [String]
|
16
|
+
def role
|
17
|
+
@data['role']
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [String]
|
21
|
+
def content
|
22
|
+
@data['content']
|
14
23
|
end
|
15
24
|
end
|
16
25
|
end
|
data/lib/omniai/chat/request.rb
CHANGED
@@ -19,23 +19,20 @@ module OmniAI
|
|
19
19
|
# client.chat.completion(messages, model: "...", temperature: 0.0, format: :text)
|
20
20
|
class Request
|
21
21
|
# @param client [OmniAI::Client] the client
|
22
|
-
# @param messages [String]
|
23
|
-
# @param model [String]
|
24
|
-
# @param temperature [Float]
|
25
|
-
# @param
|
26
|
-
|
22
|
+
# @param messages [String] required
|
23
|
+
# @param model [String] required
|
24
|
+
# @param temperature [Float, nil] optional
|
25
|
+
# @param stream [Proc, nil] optional
|
26
|
+
# @param format [Symbol, nil] optional - :text or :json
|
27
|
+
def initialize(client:, messages:, model:, temperature: nil, stream: nil, format: nil)
|
27
28
|
@client = client
|
28
29
|
@messages = messages
|
29
30
|
@model = model
|
30
31
|
@temperature = temperature
|
32
|
+
@stream = stream
|
31
33
|
@format = format
|
32
34
|
end
|
33
35
|
|
34
|
-
# @param prompt [String, Message]
|
35
|
-
# @param model [String]
|
36
|
-
# @param format [Symbol] either :text or :json
|
37
|
-
# @param temperature [Float]
|
38
|
-
# @return [Hash]
|
39
36
|
# @raise [ExecutionError]
|
40
37
|
def process!
|
41
38
|
response = request!
|
@@ -49,7 +46,26 @@ module OmniAI
|
|
49
46
|
# @param response [HTTP::Response]
|
50
47
|
# @return [OmniAI::Chat::Completion::Response]
|
51
48
|
def parse!(response:)
|
52
|
-
|
49
|
+
if @stream
|
50
|
+
stream!(response:)
|
51
|
+
else
|
52
|
+
OmniAI::OpenAI::Chat::Completion.new(data: response.parse)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# @param response [HTTP::Response]
|
57
|
+
# @return [OmniAI::Chat::Chunk]
|
58
|
+
def stream!(response:)
|
59
|
+
parser = EventStreamParser::Parser.new
|
60
|
+
|
61
|
+
response.body.each do |chunk|
|
62
|
+
parser.feed(chunk) do |_, data|
|
63
|
+
break if data.eql?('[DONE]')
|
64
|
+
|
65
|
+
chunk = OmniAI::OpenAI::Chat::Chunk.new(data: data.parse)
|
66
|
+
@stream.call(chunk)
|
67
|
+
end
|
68
|
+
end
|
53
69
|
end
|
54
70
|
|
55
71
|
# @return [Hash]
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
class Chat
|
5
|
+
# An class wrapping the response usage. A vendor may override if custom behavior is required.
|
6
|
+
class Completion
|
7
|
+
attr_accessor :data
|
8
|
+
|
9
|
+
# @param data [Hash]
|
10
|
+
def initialize(data:)
|
11
|
+
@data = data
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [Integer]
|
15
|
+
def completion_tokens
|
16
|
+
@data['completion_tokens']
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Integer]
|
20
|
+
def prompt_tokens
|
21
|
+
@data['prompt_tokens']
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Integer]
|
25
|
+
def total_tokens
|
26
|
+
@data['total_tokens']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/omniai/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omniai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: event_stream_parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: http
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,10 +67,13 @@ files:
|
|
53
67
|
- lib/omniai.rb
|
54
68
|
- lib/omniai/chat.rb
|
55
69
|
- lib/omniai/chat/choice.rb
|
70
|
+
- lib/omniai/chat/chunk.rb
|
71
|
+
- lib/omniai/chat/completion.rb
|
72
|
+
- lib/omniai/chat/delta.rb
|
56
73
|
- lib/omniai/chat/message.rb
|
57
74
|
- lib/omniai/chat/request.rb
|
58
|
-
- lib/omniai/chat/response.rb
|
59
75
|
- lib/omniai/chat/stream.rb
|
76
|
+
- lib/omniai/chat/usage.rb
|
60
77
|
- lib/omniai/client.rb
|
61
78
|
- lib/omniai/version.rb
|
62
79
|
homepage: https://github.com/ksylvest/omniai
|
data/lib/omniai/chat/response.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# An abstract class that provides a consistent interface for processing chat responses.
|
6
|
-
#
|
7
|
-
# Usage:
|
8
|
-
#
|
9
|
-
# class OmniAI::OpenAI::Chat::Response < OmniAI::Chat::Response
|
10
|
-
# def choices
|
11
|
-
# # TODO: implement
|
12
|
-
# end
|
13
|
-
# end
|
14
|
-
class Response
|
15
|
-
attr_accessor :data
|
16
|
-
|
17
|
-
# @param data [Hash]
|
18
|
-
def initialize(data:)
|
19
|
-
@data = data
|
20
|
-
end
|
21
|
-
|
22
|
-
# @param [index] [Integer]
|
23
|
-
# @return [OmniAI::Chat::Choice]
|
24
|
-
def choice(index: 0)
|
25
|
-
choices[index]
|
26
|
-
end
|
27
|
-
|
28
|
-
# @return [Array<OmniAI::Chat::Choice>]
|
29
|
-
def choices
|
30
|
-
raise NotImplementedError, "#{self.class.name}#choices undefined"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|