omniai 1.7.0 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/README.md +5 -12
  4. data/lib/omniai/chat/choice.rb +68 -0
  5. data/lib/omniai/chat/content.rb +10 -2
  6. data/lib/omniai/chat/file.rb +3 -3
  7. data/lib/omniai/chat/function.rb +57 -0
  8. data/lib/omniai/chat/message/builder.rb +67 -0
  9. data/lib/omniai/chat/message.rb +64 -45
  10. data/lib/omniai/chat/payload.rb +85 -0
  11. data/lib/omniai/chat/prompt.rb +30 -16
  12. data/lib/omniai/chat/response.rb +70 -0
  13. data/lib/omniai/chat/stream.rb +61 -0
  14. data/lib/omniai/chat/text.rb +2 -2
  15. data/lib/omniai/chat/tool_call.rb +54 -0
  16. data/lib/omniai/chat/tool_call_message.rb +61 -0
  17. data/lib/omniai/chat/tool_call_result.rb +51 -0
  18. data/lib/omniai/chat/url.rb +2 -2
  19. data/lib/omniai/chat/usage.rb +60 -0
  20. data/lib/omniai/chat.rb +61 -34
  21. data/lib/omniai/context.rb +12 -0
  22. data/lib/omniai/embed/response.rb +2 -2
  23. data/lib/omniai/tool/parameters.rb +2 -2
  24. data/lib/omniai/tool/property.rb +2 -2
  25. data/lib/omniai/tool.rb +12 -5
  26. data/lib/omniai/version.rb +1 -1
  27. metadata +12 -16
  28. data/lib/omniai/chat/response/choice.rb +0 -35
  29. data/lib/omniai/chat/response/chunk.rb +0 -15
  30. data/lib/omniai/chat/response/completion.rb +0 -15
  31. data/lib/omniai/chat/response/delta.rb +0 -11
  32. data/lib/omniai/chat/response/delta_choice.rb +0 -25
  33. data/lib/omniai/chat/response/function.rb +0 -25
  34. data/lib/omniai/chat/response/message.rb +0 -11
  35. data/lib/omniai/chat/response/message_choice.rb +0 -25
  36. data/lib/omniai/chat/response/part.rb +0 -38
  37. data/lib/omniai/chat/response/payload.rb +0 -72
  38. data/lib/omniai/chat/response/resource.rb +0 -22
  39. data/lib/omniai/chat/response/stream.rb +0 -27
  40. data/lib/omniai/chat/response/tool_call.rb +0 -30
  41. data/lib/omniai/chat/response/usage.rb +0 -35
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.8.1
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-08-02 00:00:00.000000000 Z
11
+ date: 2024-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: event_stream_parser
@@ -68,27 +68,23 @@ files:
68
68
  - exe/omniai
69
69
  - lib/omniai.rb
70
70
  - lib/omniai/chat.rb
71
+ - lib/omniai/chat/choice.rb
71
72
  - lib/omniai/chat/content.rb
72
73
  - lib/omniai/chat/file.rb
74
+ - lib/omniai/chat/function.rb
73
75
  - lib/omniai/chat/media.rb
74
76
  - lib/omniai/chat/message.rb
77
+ - lib/omniai/chat/message/builder.rb
78
+ - lib/omniai/chat/payload.rb
75
79
  - lib/omniai/chat/prompt.rb
76
- - lib/omniai/chat/response/choice.rb
77
- - lib/omniai/chat/response/chunk.rb
78
- - lib/omniai/chat/response/completion.rb
79
- - lib/omniai/chat/response/delta.rb
80
- - lib/omniai/chat/response/delta_choice.rb
81
- - lib/omniai/chat/response/function.rb
82
- - lib/omniai/chat/response/message.rb
83
- - lib/omniai/chat/response/message_choice.rb
84
- - lib/omniai/chat/response/part.rb
85
- - lib/omniai/chat/response/payload.rb
86
- - lib/omniai/chat/response/resource.rb
87
- - lib/omniai/chat/response/stream.rb
88
- - lib/omniai/chat/response/tool_call.rb
89
- - lib/omniai/chat/response/usage.rb
80
+ - lib/omniai/chat/response.rb
81
+ - lib/omniai/chat/stream.rb
90
82
  - lib/omniai/chat/text.rb
83
+ - lib/omniai/chat/tool_call.rb
84
+ - lib/omniai/chat/tool_call_message.rb
85
+ - lib/omniai/chat/tool_call_result.rb
91
86
  - lib/omniai/chat/url.rb
87
+ - lib/omniai/chat/usage.rb
92
88
  - lib/omniai/cli.rb
93
89
  - lib/omniai/cli/base_handler.rb
94
90
  - lib/omniai/cli/chat_handler.rb
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # For use with MessageChoice or DeltaChoice.
7
- class Choice < Resource
8
- # @return [Integer]
9
- def index
10
- @data['index']
11
- end
12
-
13
- # @return [Part]
14
- def part
15
- raise NotImplementedError, "#{self.class.name}#part undefined"
16
- end
17
-
18
- # @return [ToolCallList]
19
- def tool_call_list
20
- part.tool_call_list
21
- end
22
-
23
- # @return [String, nil]
24
- def content
25
- part.content
26
- end
27
-
28
- # @return [Boolean]
29
- def content?
30
- !content.nil?
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A chunk returned by the API.
7
- class Chunk < Payload
8
- # @return [Array<DeltaChoice>]
9
- def choices
10
- @choices ||= @data['choices'].map { |data| DeltaChoice.new(data:) }
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A completion returned by the API.
7
- class Completion < Payload
8
- # @return [Array<MessageChoice>]
9
- def choices
10
- @choices ||= @data['choices'].map { |data| MessageChoice.new(data:) }
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A delta returned by the API.
7
- class Delta < Part
8
- end
9
- end
10
- end
11
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A delta choice returned by the API.
7
- class DeltaChoice < Choice
8
- # @return [String]
9
- def inspect
10
- "#<#{self.class.name} index=#{index} delta=#{delta.inspect}>"
11
- end
12
-
13
- # @return [Delta]
14
- def delta
15
- @delta ||= Delta.new(data: @data['delta'])
16
- end
17
-
18
- # @return [Delta]
19
- def part
20
- delta
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A function returned by the API.
7
- class Function < Resource
8
- # @return [String]
9
- def inspect
10
- "#<#{self.class.name} name=#{name.inspect} arguments=#{arguments.inspect}>"
11
- end
12
-
13
- # @return [String]
14
- def name
15
- @data['name']
16
- end
17
-
18
- # @return [Hash, nil]
19
- def arguments
20
- JSON.parse(@data['arguments']) if @data['arguments']
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A message returned by the API.
7
- class Message < Part
8
- end
9
- end
10
- end
11
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A choice returned by the API.
7
- class MessageChoice < Choice
8
- # @return [String]
9
- def inspect
10
- "#<#{self.class.name} index=#{index} message=#{message.inspect}>"
11
- end
12
-
13
- # @return [Message]
14
- def message
15
- @message ||= Message.new(data: @data['message'])
16
- end
17
-
18
- # @return [Message]
19
- def part
20
- message
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # Either a delta or message.
7
- class Part < Resource
8
- # @return [String]
9
- def inspect
10
- "#<#{self.class.name} role=#{role.inspect} content=#{content.inspect}>"
11
- end
12
-
13
- # @return [String]
14
- def role
15
- @data['role'] || Role::USER
16
- end
17
-
18
- # @return [String, nil]
19
- def content
20
- @data['content']
21
- end
22
-
23
- # @return [Array<ToolCall>]
24
- def tool_call_list
25
- return [] unless @data['tool_calls']
26
-
27
- @tool_call_list ||= @data['tool_calls'].map { |tool_call_data| ToolCall.new(data: tool_call_data) }
28
- end
29
-
30
- # @param index [Integer]
31
- # @return [ToolCall, nil]
32
- def tool_call(index: 0)
33
- tool_call_list[index]
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A chunk or completion.
7
- class Payload < Resource
8
- # @return [String]
9
- def inspect
10
- "#<#{self.class.name} id=#{id.inspect} choices=#{choices.inspect}>"
11
- end
12
-
13
- # @return [String]
14
- def id
15
- @data['id']
16
- end
17
-
18
- # @return [Time]
19
- def created
20
- Time.at(@data['created']) if @data['created']
21
- end
22
-
23
- # @return [Time]
24
- def updated
25
- Time.at(@data['updated']) if @data['updated']
26
- end
27
-
28
- # @return [String]
29
- def model
30
- @data['model']
31
- end
32
-
33
- # @return [Array<Choice>]
34
- def choices
35
- raise NotImplementedError, "#{self.class.name}#choices undefined"
36
- end
37
-
38
- # @param index [Integer]
39
- # @return [DeltaChoice]
40
- def choice(index: 0)
41
- choices[index]
42
- end
43
-
44
- # @param index [Integer]
45
- # @return [Part]
46
- def part(index: 0)
47
- choice(index:).part
48
- end
49
-
50
- # @return [Usage]
51
- def usage
52
- @usage ||= Usage.new(data: @data['usage']) if @data['usage']
53
- end
54
-
55
- # @return [String, nil]
56
- def content
57
- choice.content
58
- end
59
-
60
- # @return [Boolean]
61
- def content?
62
- choice.content?
63
- end
64
-
65
- # @return [Array<ToolCall>]
66
- def tool_call_list
67
- choice.tool_call_list
68
- end
69
- end
70
- end
71
- end
72
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A generic data to handle response.
7
- class Resource
8
- attr_accessor :data
9
-
10
- # @param data [Hash]
11
- def initialize(data:)
12
- @data = data
13
- end
14
-
15
- # @return [String]
16
- def inspect
17
- "#<#{self.class.name} data=#{@data.inspect}>"
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A stream given when streaming.
7
- class Stream
8
- # @param response [HTTP::Response]
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
27
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A tool-call returned by the API.
7
- class ToolCall < Resource
8
- # @return [String]
9
- def inspect
10
- "#<#{self.class.name} id=#{id.inspect} type=#{type.inspect}>"
11
- end
12
-
13
- # @return [String]
14
- def id
15
- @data['id']
16
- end
17
-
18
- # @return [String]
19
- def type
20
- @data['type']
21
- end
22
-
23
- # @return [Function]
24
- def function
25
- @function ||= Function.new(data: @data['function']) if @data['function']
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OmniAI
4
- class Chat
5
- module Response
6
- # A usage returned by the API.
7
- class Usage < Resource
8
- # @return [String]
9
- def inspect
10
- properties = [
11
- "input_tokens=#{input_tokens}",
12
- "output_tokens=#{output_tokens}",
13
- "total_tokens=#{total_tokens}",
14
- ]
15
- "#<#{self.class.name} #{properties.join(' ')}>"
16
- end
17
-
18
- # @return [Integer]
19
- def input_tokens
20
- @data['input_tokens'] || @data['prompt_tokens']
21
- end
22
-
23
- # @return [Integer]
24
- def output_tokens
25
- @data['output_tokens'] || @data['completion_tokens']
26
- end
27
-
28
- # @return [Integer]
29
- def total_tokens
30
- @data['total_tokens'] || (input_tokens + output_tokens)
31
- end
32
- end
33
- end
34
- end
35
- end