omniai 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d90d000047c670a17478146fa35fca9c993127067b01b11113cc46e739d15e2
4
- data.tar.gz: d025855eb6ea4ae79bc7c126a2853fff7a6a0e21d597fa86b2efb6b8d1c3ddf1
3
+ metadata.gz: 1694fba08fa9ef9bd8dc3496ce1acbc3c321357d082303869df74ee667a83a65
4
+ data.tar.gz: c0d98caa29e034c610d20654cc175e42eeb69cef414ba79a5fda61d883d2d9f3
5
5
  SHA512:
6
- metadata.gz: 77247cd74f88fdaa527b377940f32cfaad187c26f8ae4c6f373c708c866ce5c956c0436194786acb474ae07f423ea83511df6bbd5029c91247aff4b5d8ee2891
7
- data.tar.gz: c7d3a40a6c21fe1827b162e94eae0dd0d2f66014797e4d878fa6c7aacf17c443412fcefa2f4bc0ad008f7b64b41ab4e2d583109990d6418d77451f1ba67b4fbd
6
+ metadata.gz: ddfcd3c15811a63b401290577b36170573e4e170064a8cf64686ceacfc1ddb2c3e5af10ab2a7bb1057db9fe691b8359a94da24c0ce774f71ed5df50063b3925b
7
+ data.tar.gz: 66dcd111279e7de048473789cfae9020a0c6bb9b812bdd10a1bb2c2c12ee246bd9cebf6b7890322fdcc4a72743033746bd9e5a3f8d283b43ae0bbea5315de05e
@@ -6,7 +6,7 @@ module OmniAI
6
6
  #
7
7
  # Usage:
8
8
  #
9
- # class OmniAI::OpenAI::ChatGPT::Completion < OmniAI::Chat::Completion
9
+ # class OmniAI::OpenAI::ChatGPT::Request < OmniAI::Chat::Request
10
10
  # module Model
11
11
  # CHAT = "davinci"
12
12
  # end
@@ -23,7 +23,7 @@ module OmniAI
23
23
  # @param model [String] required
24
24
  # @param temperature [Float, nil] optional
25
25
  # @param stream [Proc, nil] optional
26
- # @param format [Symbol, nil] optional - :text or :json
26
+ # @param format [Symbol, nil] optional - :json
27
27
  def initialize(client:, messages:, model:, temperature: nil, stream: nil, format: nil)
28
28
  @client = client
29
29
  @messages = messages
@@ -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
- # @return [OmniAI::Chat::Completion::Response]
57
+ # @return [OmniAI::Chat::Completion]
48
58
  def parse!(response:)
49
59
  if @stream
50
60
  stream!(response:)
51
61
  else
52
- OmniAI::OpenAI::Chat::Completion.new(data: response.parse)
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
- parser = EventStreamParser::Parser.new
74
+ raise Error, "#{self.class.name}#stream! unstreamable" unless @stream
60
75
 
61
- response.body.each do |chunk|
62
- parser.feed(chunk) do |_, data|
63
- break if data.eql?('[DONE]')
76
+ Stream.new(response:).stream! { |chunk| @stream.call(chunk) }
77
+ end
64
78
 
65
- chunk = OmniAI::OpenAI::Chat::Chunk.new(data: JSON.parse(data))
66
- @stream.call(chunk)
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
- # @return [Hash]
72
- def payload
73
- raise NotImplementedError, "#{self.class.name}#payload undefined"
74
- end
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
@@ -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,12 @@ module OmniAI
17
17
  #
18
18
  # @param client [OmniAI::Client] the client
19
19
  class Chat
20
+ module Role
21
+ ASSISTANT = 'assistant'
22
+ USER = 'user'
23
+ SYSTEM = 'system'
24
+ end
25
+
20
26
  def initialize(client:)
21
27
  @client = client
22
28
  end
data/lib/omniai/config.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OmniAI
2
4
  # A configuration for each agent w/ `api_key` / `host` / `logger`.
3
5
  class Config
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OmniAI
4
- VERSION = '0.1.4'
4
+ VERSION = '0.2.0'
5
5
  end
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.1.4
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