omniai 0.1.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/omniai/chat/request.rb +31 -16
- data/lib/omniai/chat/stream.rb +26 -0
- data/lib/omniai/chat/usage.rb +13 -3
- data/lib/omniai/chat.rb +8 -0
- data/lib/omniai/client.rb +6 -0
- data/lib/omniai/config.rb +6 -0
- data/lib/omniai/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a227888b197b576509bec7208b79f125e2c55baac317ce32b3fed8e2101f11cc
|
4
|
+
data.tar.gz: 16fe842b15ef266556c9887b1625c32a62edbbdba21dea0a8b21e418d76cd435
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4bc067e862d142036dcc15721fb7f3cf5084112145676b3884977e5d3e7025f34637eab3520ad2a1f2d524909f5e8a55b7a2bee35c139062cb481ffc64b3071
|
7
|
+
data.tar.gz: c95602154261e59d7e42fbc94d7afb42f836f54287c4842c0c982a82ced52271ac8c41afbb6cb620865ca948d55971c789c94967f5f18e2af8305271e1c2885b
|
data/lib/omniai/chat/request.rb
CHANGED
@@ -43,39 +43,54 @@ module OmniAI
|
|
43
43
|
|
44
44
|
protected
|
45
45
|
|
46
|
+
# @return [Hash]
|
47
|
+
def payload
|
48
|
+
raise NotImplementedError, "#{self.class.name}#payload undefined"
|
49
|
+
end
|
50
|
+
|
51
|
+
# @return [String]
|
52
|
+
def path
|
53
|
+
raise NotImplementedError, "#{self.class.name}#path undefined"
|
54
|
+
end
|
55
|
+
|
46
56
|
# @param response [HTTP::Response]
|
47
57
|
# @return [OmniAI::Chat::Completion]
|
48
58
|
def parse!(response:)
|
49
59
|
if @stream
|
50
60
|
stream!(response:)
|
51
61
|
else
|
52
|
-
|
62
|
+
complete!(response:)
|
53
63
|
end
|
54
64
|
end
|
55
65
|
|
66
|
+
# @param response [OmniAI::Chat::Completion]
|
67
|
+
def complete!(response:)
|
68
|
+
Completion.new(data: response.parse)
|
69
|
+
end
|
70
|
+
|
56
71
|
# @param response [HTTP::Response]
|
57
|
-
# @return [OmniAI::Chat::
|
72
|
+
# @return [OmniAI::Chat::Stream]
|
58
73
|
def stream!(response:)
|
59
|
-
|
74
|
+
raise Error, "#{self.class.name}#stream! unstreamable" unless @stream
|
60
75
|
|
61
|
-
response.
|
62
|
-
|
63
|
-
break if data.eql?('[DONE]')
|
76
|
+
Stream.new(response:).stream! { |chunk| @stream.call(chunk) }
|
77
|
+
end
|
64
78
|
|
65
|
-
|
66
|
-
|
79
|
+
# @return [Array<Hash>]
|
80
|
+
def messages
|
81
|
+
arrayify(@messages).map do |content|
|
82
|
+
case content
|
83
|
+
when String then { role: OmniAI::Chat::Role::USER, content: }
|
84
|
+
when Hash then content
|
85
|
+
else raise Error, "Unsupported content=#{content.inspect}"
|
67
86
|
end
|
68
87
|
end
|
69
88
|
end
|
70
89
|
|
71
|
-
# @
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
# @return [String]
|
77
|
-
def path
|
78
|
-
raise NotImplementedError, "#{self.class.name}#path undefined"
|
90
|
+
# @param value [Object, Array<Object>]
|
91
|
+
# @return [Array<Object>]
|
92
|
+
def arrayify(value)
|
93
|
+
value.is_a?(Array) ? value : [value]
|
79
94
|
end
|
80
95
|
|
81
96
|
# @return [HTTP::Response]
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OmniAI
|
4
|
+
class Chat
|
5
|
+
# A stream given when streaming.
|
6
|
+
class Stream
|
7
|
+
# @param response [HTTP::Response]
|
8
|
+
# @param block [Proc]
|
9
|
+
def initialize(response:)
|
10
|
+
@response = response
|
11
|
+
@parser = EventStreamParser::Parser.new
|
12
|
+
end
|
13
|
+
|
14
|
+
# @yield [OmniAI::Chat::Chunk]
|
15
|
+
def stream!
|
16
|
+
@response.body.each do |chunk|
|
17
|
+
@parser.feed(chunk) do |_, data|
|
18
|
+
next if data.eql?('[DONE]')
|
19
|
+
|
20
|
+
yield(Chunk.new(data: JSON.parse(data)))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/omniai/chat/usage.rb
CHANGED
@@ -11,20 +11,30 @@ module OmniAI
|
|
11
11
|
@data = data
|
12
12
|
end
|
13
13
|
|
14
|
-
# @return [Integer]
|
14
|
+
# @return [Integer, nil]
|
15
15
|
def completion_tokens
|
16
16
|
@data['completion_tokens']
|
17
17
|
end
|
18
18
|
|
19
|
-
# @return [Integer]
|
19
|
+
# @return [Integer, nil]
|
20
20
|
def prompt_tokens
|
21
21
|
@data['prompt_tokens']
|
22
22
|
end
|
23
23
|
|
24
|
-
# @return [Integer]
|
24
|
+
# @return [Integer, nil]
|
25
25
|
def total_tokens
|
26
26
|
@data['total_tokens']
|
27
27
|
end
|
28
|
+
|
29
|
+
# @return [Integer, nil]
|
30
|
+
def input_tokens
|
31
|
+
@data['input_tokens']
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [Integer, nil]
|
35
|
+
def output_tokens
|
36
|
+
@data['output_tokens']
|
37
|
+
end
|
28
38
|
end
|
29
39
|
end
|
30
40
|
end
|
data/lib/omniai/chat.rb
CHANGED
@@ -17,6 +17,14 @@ module OmniAI
|
|
17
17
|
#
|
18
18
|
# @param client [OmniAI::Client] the client
|
19
19
|
class Chat
|
20
|
+
JSON_PROMPT = 'Respond with valid JSON. Do not include any non-JSON in the response.'
|
21
|
+
|
22
|
+
module Role
|
23
|
+
ASSISTANT = 'assistant'
|
24
|
+
USER = 'user'
|
25
|
+
SYSTEM = 'system'
|
26
|
+
end
|
27
|
+
|
20
28
|
def initialize(client:)
|
21
29
|
@client = client
|
22
30
|
end
|
data/lib/omniai/client.rb
CHANGED
@@ -27,6 +27,12 @@ module OmniAI
|
|
27
27
|
@logger = logger
|
28
28
|
end
|
29
29
|
|
30
|
+
# @return [String]
|
31
|
+
def inspect
|
32
|
+
masked_api_key = "#{api_key[..2]}***" if api_key
|
33
|
+
"#<#{self.class.name} api_key=#{masked_api_key.inspect}>"
|
34
|
+
end
|
35
|
+
|
30
36
|
# @return [HTTP::Client]
|
31
37
|
def connection
|
32
38
|
raise NotImplementedError, "#{self.class.name}#connection undefined"
|
data/lib/omniai/config.rb
CHANGED
@@ -4,5 +4,11 @@ module OmniAI
|
|
4
4
|
# A configuration for each agent w/ `api_key` / `host` / `logger`.
|
5
5
|
class Config
|
6
6
|
attr_accessor :api_key, :host, :logger
|
7
|
+
|
8
|
+
# @return [String]
|
9
|
+
def inspect
|
10
|
+
masked_api_key = "#{api_key[..2]}***" if api_key
|
11
|
+
"#<#{self.class.name} api_key=#{masked_api_key.inspect} host=#{host.inspect}>"
|
12
|
+
end
|
7
13
|
end
|
8
14
|
end
|
data/lib/omniai/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omniai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Sylvestre
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/omniai/chat/delta.rb
|
74
74
|
- lib/omniai/chat/message.rb
|
75
75
|
- lib/omniai/chat/request.rb
|
76
|
+
- lib/omniai/chat/stream.rb
|
76
77
|
- lib/omniai/chat/usage.rb
|
77
78
|
- lib/omniai/client.rb
|
78
79
|
- lib/omniai/config.rb
|