omniai 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53ccc1363252abefa6089ff068b11b3ac15b5817339dc935ae9b78cf1d146b20
4
- data.tar.gz: 1b4fe76443ef245f7198c1d55edab1fa6d985afbef1638705caf3d40a1b88c05
3
+ metadata.gz: bab309e1be1b9acc7671c341b8e50cd0ec257ac22b254899b3661e428432f290
4
+ data.tar.gz: 7968c811a16cf77172b087a03693641dbca83f2ba683e5eff30eda05ad4817e4
5
5
  SHA512:
6
- metadata.gz: f687cd00e0cbed7994d8a172c657d135741e8ca5afdc8f4bcff53ffb6ec6746f553f26cfeeddcf08a81a261e72e7dcd0b4aee06e0b9e336016a1843189bb4a29
7
- data.tar.gz: 6d89697cc18301852e6648b2a323634a59df9bf90032146d35837ead4a4746bad13527aeef8c67af8cd0290bdaae3177de50d29c220c8fff86f844f081484970
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
- ### Chat
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
+ ```
@@ -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 :index, :message, :role
7
+ attr_accessor :data
8
8
 
9
- # @param index [Integer]
10
- # @param message [OmniAI::Chat::Message]
11
- # @param role [String]
12
- def initialize(index:, message:)
13
- @index = index
14
- @message = message
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
@@ -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 :content, :role
7
+ attr_accessor :data
8
8
 
9
9
  # @param content [Integer]
10
10
  # @param role [String]
11
- def initialize(content:, role:)
12
- @content = content
13
- @role = role
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
@@ -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 format [Symbol] either :text or :json
26
- def initialize(client:, messages:, model:, temperature: 0.0, format: :text)
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
- raise NotImplementedError, "#{self.class.name}#parse! undefined"
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OmniAI
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.9'
5
5
  end
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.7
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 00:00:00.000000000 Z
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
@@ -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