omniai 0.1.5 → 0.2.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 +30 -15
- data/lib/omniai/chat/stream.rb +26 -0
- data/lib/omniai/chat/usage.rb +13 -3
- data/lib/omniai/chat.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: 1694fba08fa9ef9bd8dc3496ce1acbc3c321357d082303869df74ee667a83a65
|
4
|
+
data.tar.gz: c0d98caa29e034c610d20654cc175e42eeb69cef414ba79a5fda61d883d2d9f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddfcd3c15811a63b401290577b36170573e4e170064a8cf64686ceacfc1ddb2c3e5af10ab2a7bb1057db9fe691b8359a94da24c0ce774f71ed5df50063b3925b
|
7
|
+
data.tar.gz: 66dcd111279e7de048473789cfae9020a0c6bb9b812bdd10a1bb2c2c12ee246bd9cebf6b7890322fdcc4a72743033746bd9e5a3f8d283b43ae0bbea5315de05e
|
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
72
|
# @return [OmniAI::Chat::Chunk]
|
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
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: 0.2.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
|