omniai 1.7.0 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/README.md +3 -10
- data/lib/omniai/chat/choice.rb +68 -0
- data/lib/omniai/chat/content.rb +10 -2
- data/lib/omniai/chat/file.rb +3 -3
- data/lib/omniai/chat/function.rb +57 -0
- data/lib/omniai/chat/message/builder.rb +67 -0
- data/lib/omniai/chat/message.rb +64 -45
- data/lib/omniai/chat/payload.rb +85 -0
- data/lib/omniai/chat/prompt.rb +30 -16
- data/lib/omniai/chat/response.rb +70 -0
- data/lib/omniai/chat/stream.rb +61 -0
- data/lib/omniai/chat/text.rb +2 -2
- data/lib/omniai/chat/tool_call.rb +54 -0
- data/lib/omniai/chat/tool_call_message.rb +61 -0
- data/lib/omniai/chat/tool_call_result.rb +51 -0
- data/lib/omniai/chat/url.rb +2 -2
- data/lib/omniai/chat/usage.rb +60 -0
- data/lib/omniai/chat.rb +61 -34
- data/lib/omniai/context.rb +12 -0
- data/lib/omniai/embed/response.rb +2 -2
- data/lib/omniai/tool.rb +6 -2
- data/lib/omniai/version.rb +1 -1
- metadata +12 -16
- data/lib/omniai/chat/response/choice.rb +0 -35
- data/lib/omniai/chat/response/chunk.rb +0 -15
- data/lib/omniai/chat/response/completion.rb +0 -15
- data/lib/omniai/chat/response/delta.rb +0 -11
- data/lib/omniai/chat/response/delta_choice.rb +0 -25
- data/lib/omniai/chat/response/function.rb +0 -25
- data/lib/omniai/chat/response/message.rb +0 -11
- data/lib/omniai/chat/response/message_choice.rb +0 -25
- data/lib/omniai/chat/response/part.rb +0 -38
- data/lib/omniai/chat/response/payload.rb +0 -72
- data/lib/omniai/chat/response/resource.rb +0 -22
- data/lib/omniai/chat/response/stream.rb +0 -27
- data/lib/omniai/chat/response/tool_call.rb +0 -30
- data/lib/omniai/chat/response/usage.rb +0 -35
@@ -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,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,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
|