omniai 1.5.0 → 1.5.1
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/response/choice.rb +40 -0
- data/lib/omniai/chat/response/chunk.rb +15 -0
- data/lib/omniai/chat/response/completion.rb +15 -0
- data/lib/omniai/chat/response/delta.rb +11 -0
- data/lib/omniai/chat/response/delta_choice.rb +25 -0
- data/lib/omniai/chat/response/function.rb +25 -0
- data/lib/omniai/chat/response/message.rb +11 -0
- data/lib/omniai/chat/response/message_choice.rb +25 -0
- data/lib/omniai/chat/response/part.rb +38 -0
- data/lib/omniai/chat/response/payload.rb +77 -0
- data/lib/omniai/chat/response/resource.rb +22 -0
- data/lib/omniai/chat/response/stream.rb +27 -0
- data/lib/omniai/chat/response/tool_call.rb +30 -0
- data/lib/omniai/chat/response/usage.rb +35 -0
- data/lib/omniai/chat.rb +8 -6
- data/lib/omniai/config.rb +3 -1
- data/lib/omniai/version.rb +1 -1
- metadata +16 -12
- data/lib/omniai/chat/chunk.rb +0 -46
- data/lib/omniai/chat/completion.rb +0 -70
- data/lib/omniai/chat/delta.rb +0 -28
- data/lib/omniai/chat/delta_choice.rb +0 -28
- data/lib/omniai/chat/function.rb +0 -28
- data/lib/omniai/chat/message.rb +0 -37
- data/lib/omniai/chat/message_choice.rb +0 -28
- data/lib/omniai/chat/stream.rb +0 -25
- data/lib/omniai/chat/tool_call.rb +0 -33
- data/lib/omniai/chat/usage.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a989cab03afca85385b23f4684d377b8d1a09533a73899fa79eebcce4f4a3260
|
4
|
+
data.tar.gz: bb39575e251b0dd6cf6e0e3b24ba86765fd550feeb3a222ce5f785ca654e8741
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e20f0ee5ab32ceb95e33e38888c81e28f936294cfcd9a3dad80a8d89a9fd3241477455e706d70fb949502b01cdf968547104e55931138512b4e617dd9688fe02
|
7
|
+
data.tar.gz: 58aa9a8a43b2e8a7580beac909b7ddd34fcaaa3d9c60c9105d3dba0cd18923f7361473fdb863890fbfa17013a1339b71008381e105bd961de44dec67db8eac8b
|
@@ -0,0 +1,40 @@
|
|
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 [OmniAI::Chat::Response::Part]
|
14
|
+
def part
|
15
|
+
raise NotImplementedError, "#{self.class.name}#part undefined"
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [OmniAI::Chat::Response::ToolCallList]
|
19
|
+
def tool_call_list
|
20
|
+
part.tool_call_list
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [Boolean]
|
24
|
+
def tool_call_list?
|
25
|
+
tool_call_list.any?
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [String, nil]
|
29
|
+
def content
|
30
|
+
part.content
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Boolean]
|
34
|
+
def content?
|
35
|
+
!content.nil?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
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<OmniAI::Chat::Response::DeltaChoice>]
|
9
|
+
def choices
|
10
|
+
@choices ||= @data['choices'].map { |data| DeltaChoice.new(data:) }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
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<OmniAI::Chat:Response:::MessageChoice>]
|
9
|
+
def choices
|
10
|
+
@choices ||= @data['choices'].map { |data| MessageChoice.new(data:) }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
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 [OmniAI::Chat::Response::Delta]
|
14
|
+
def delta
|
15
|
+
@delta ||= Delta.new(data: @data['delta'])
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [OmniAI::Chat::Response::Delta]
|
19
|
+
def part
|
20
|
+
delta
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
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
|
@@ -0,0 +1,25 @@
|
|
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 [OmniAI::Chat::Response::Message]
|
14
|
+
def message
|
15
|
+
@message ||= Message.new(data: @data['message'])
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [OmniAI::Chat::Response::Message]
|
19
|
+
def part
|
20
|
+
message
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,38 @@
|
|
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<OmniAI::Chat::Response::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 [OmniAI::Chat::Response::ToolCall, nil]
|
32
|
+
def tool_call(index: 0)
|
33
|
+
tool_call_list[index]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,77 @@
|
|
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<OmniAI::Chat::Response::Choice>]
|
34
|
+
def choices
|
35
|
+
raise NotImplementedError, "#{self.class.name}#choices undefined"
|
36
|
+
end
|
37
|
+
|
38
|
+
# @param index [Integer]
|
39
|
+
# @return [OmniAI::Chat::Response::DeltaChoice]
|
40
|
+
def choice(index: 0)
|
41
|
+
choices[index]
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param index [Integer]
|
45
|
+
# @return [OmniAI::Chat::Response::Part]
|
46
|
+
def part(index: 0)
|
47
|
+
choice(index:).part
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [OmniAI::Chat::Response::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<OmniAI::Chat::Response:ToolCall>]
|
66
|
+
def tool_call_list
|
67
|
+
choice.tool_call_list
|
68
|
+
end
|
69
|
+
|
70
|
+
# @return [Boolean]
|
71
|
+
def tool_call_list?
|
72
|
+
choice.tool_call_list?
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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
|
@@ -0,0 +1,27 @@
|
|
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
|
@@ -0,0 +1,30 @@
|
|
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 [OmniAI::Chat::Response::Function]
|
24
|
+
def function
|
25
|
+
@function ||= Function.new(data: @data['function']) if @data['function']
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
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
|
data/lib/omniai/chat.rb
CHANGED
@@ -98,10 +98,10 @@ module OmniAI
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
# @param response [
|
102
|
-
# @return [OmniAI::Chat::Completion]
|
101
|
+
# @param response [HTTP::Response]
|
102
|
+
# @return [OmniAI::Chat::Response::Completion]
|
103
103
|
def complete!(response:)
|
104
|
-
completion = self.class::Completion.new(data: response.parse)
|
104
|
+
completion = self.class::Response::Completion.new(data: response.parse)
|
105
105
|
|
106
106
|
if @tools && completion.tool_call_required?
|
107
107
|
@messages = [
|
@@ -120,11 +120,13 @@ module OmniAI
|
|
120
120
|
def stream!(response:)
|
121
121
|
raise Error, "#{self.class.name}#stream! unstreamable" unless @stream
|
122
122
|
|
123
|
-
self.class::Stream.new(response:).stream! do |chunk|
|
123
|
+
self.class::Response::Stream.new(response:).stream! do |chunk|
|
124
124
|
case @stream
|
125
125
|
when IO, StringIO
|
126
|
-
|
127
|
-
|
126
|
+
if chunk.content?
|
127
|
+
@stream << chunk.content
|
128
|
+
@stream.flush
|
129
|
+
end
|
128
130
|
else @stream.call(chunk)
|
129
131
|
end
|
130
132
|
end
|
data/lib/omniai/config.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module OmniAI
|
4
|
-
# A configuration for each agent w/ `api_key` / `host` / `logger`.
|
4
|
+
# A configuration for each agent w/ `api_key` / `host` / `logger`.
|
5
|
+
#
|
6
|
+
# Usage:
|
5
7
|
#
|
6
8
|
# OmniAI::OpenAI.config do |config|
|
7
9
|
# config.api_key = '...'
|
data/lib/omniai/version.rb
CHANGED
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.5.
|
4
|
+
version: 1.5.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-07-
|
11
|
+
date: 2024-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: event_stream_parser
|
@@ -68,20 +68,24 @@ files:
|
|
68
68
|
- exe/omniai
|
69
69
|
- lib/omniai.rb
|
70
70
|
- lib/omniai/chat.rb
|
71
|
-
- lib/omniai/chat/chunk.rb
|
72
|
-
- lib/omniai/chat/completion.rb
|
73
71
|
- lib/omniai/chat/content/file.rb
|
74
72
|
- lib/omniai/chat/content/media.rb
|
75
73
|
- lib/omniai/chat/content/text.rb
|
76
74
|
- lib/omniai/chat/content/url.rb
|
77
|
-
- lib/omniai/chat/
|
78
|
-
- lib/omniai/chat/
|
79
|
-
- lib/omniai/chat/
|
80
|
-
- lib/omniai/chat/
|
81
|
-
- lib/omniai/chat/
|
82
|
-
- lib/omniai/chat/
|
83
|
-
- lib/omniai/chat/
|
84
|
-
- lib/omniai/chat/
|
75
|
+
- lib/omniai/chat/response/choice.rb
|
76
|
+
- lib/omniai/chat/response/chunk.rb
|
77
|
+
- lib/omniai/chat/response/completion.rb
|
78
|
+
- lib/omniai/chat/response/delta.rb
|
79
|
+
- lib/omniai/chat/response/delta_choice.rb
|
80
|
+
- lib/omniai/chat/response/function.rb
|
81
|
+
- lib/omniai/chat/response/message.rb
|
82
|
+
- lib/omniai/chat/response/message_choice.rb
|
83
|
+
- lib/omniai/chat/response/part.rb
|
84
|
+
- lib/omniai/chat/response/payload.rb
|
85
|
+
- lib/omniai/chat/response/resource.rb
|
86
|
+
- lib/omniai/chat/response/stream.rb
|
87
|
+
- lib/omniai/chat/response/tool_call.rb
|
88
|
+
- lib/omniai/chat/response/usage.rb
|
85
89
|
- lib/omniai/cli.rb
|
86
90
|
- lib/omniai/cli/base_handler.rb
|
87
91
|
- lib/omniai/cli/chat_handler.rb
|
data/lib/omniai/chat/chunk.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A chunk returned by the API.
|
6
|
-
class Chunk
|
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 [Array<OmniAI::Chat::DeltaChoice>]
|
35
|
-
def choices
|
36
|
-
@choices ||= @data['choices'].map { |data| DeltaChoice.new(data:) }
|
37
|
-
end
|
38
|
-
|
39
|
-
# @param index [Integer]
|
40
|
-
# @return [OmniAI::Chat::Delta]
|
41
|
-
def choice(index: 0)
|
42
|
-
choices[index]
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A completion returned by the API.
|
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 inspect
|
16
|
-
"#<#{self.class.name} id=#{id.inspect} choices=#{choices.inspect}"
|
17
|
-
end
|
18
|
-
|
19
|
-
# @return [String]
|
20
|
-
def id
|
21
|
-
@data['id']
|
22
|
-
end
|
23
|
-
|
24
|
-
# @return [Time]
|
25
|
-
def created
|
26
|
-
Time.at(@data['created']) if @data['created']
|
27
|
-
end
|
28
|
-
|
29
|
-
# @return [Time]
|
30
|
-
def updated
|
31
|
-
Time.at(@data['updated']) if @data['updated']
|
32
|
-
end
|
33
|
-
|
34
|
-
# @return [String]
|
35
|
-
def model
|
36
|
-
@data['model']
|
37
|
-
end
|
38
|
-
|
39
|
-
# @return [OmniAI::Chat::Usage]
|
40
|
-
def usage
|
41
|
-
@usage ||= Usage.new(data: @data['usage']) if @data['usage']
|
42
|
-
end
|
43
|
-
|
44
|
-
# @return [Array<OmniAI::Chat::MessageChoice>]
|
45
|
-
def choices
|
46
|
-
@choices ||= @data['choices'].map { |data| MessageChoice.new(data:) }
|
47
|
-
end
|
48
|
-
|
49
|
-
# @param index [Integer] optional - default is 0
|
50
|
-
# @return [OmniAI::Chat::MessageChoice]
|
51
|
-
def choice(index: 0)
|
52
|
-
choices[index]
|
53
|
-
end
|
54
|
-
|
55
|
-
# @return [Boolean]
|
56
|
-
def tool_call_required?
|
57
|
-
choices.any? { |choice| choice.message.tool_call_list.any? }
|
58
|
-
end
|
59
|
-
|
60
|
-
# @return [Array<OmniAI::Chat::ToolCall>]
|
61
|
-
def tool_call_list
|
62
|
-
list = []
|
63
|
-
choices.each do |choice|
|
64
|
-
list += choice.message.tool_call_list
|
65
|
-
end
|
66
|
-
list
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
data/lib/omniai/chat/delta.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A delta returned by the API.
|
6
|
-
class Delta
|
7
|
-
# @param data [Hash]
|
8
|
-
def initialize(data:)
|
9
|
-
@data = data
|
10
|
-
end
|
11
|
-
|
12
|
-
# @return [String]
|
13
|
-
def inspect
|
14
|
-
"#<#{self.class.name} role=#{role.inspect} content=#{content.inspect}>"
|
15
|
-
end
|
16
|
-
|
17
|
-
# @return [String, nil]
|
18
|
-
def content
|
19
|
-
@data['content']
|
20
|
-
end
|
21
|
-
|
22
|
-
# @return [String]
|
23
|
-
def role
|
24
|
-
@data['role'] || Role::USER
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A delta choice returned by the API.
|
6
|
-
class DeltaChoice
|
7
|
-
# @param data [Hash]
|
8
|
-
def initialize(data:)
|
9
|
-
@data = data
|
10
|
-
end
|
11
|
-
|
12
|
-
# @return [Integer]
|
13
|
-
def index
|
14
|
-
@data['index']
|
15
|
-
end
|
16
|
-
|
17
|
-
# @return [OmniAI::Chat::Delta]
|
18
|
-
def delta
|
19
|
-
@delta ||= Delta.new(data: @data['delta'])
|
20
|
-
end
|
21
|
-
|
22
|
-
# @return [String]
|
23
|
-
def inspect
|
24
|
-
"#<#{self.class.name} index=#{index} delta=#{delta.inspect}>"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/lib/omniai/chat/function.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A function returned by the API.
|
6
|
-
class Function
|
7
|
-
# @param data [Hash]
|
8
|
-
def initialize(data:)
|
9
|
-
@data = data
|
10
|
-
end
|
11
|
-
|
12
|
-
# @return [String]
|
13
|
-
def name
|
14
|
-
@data['name']
|
15
|
-
end
|
16
|
-
|
17
|
-
# @return [Hash, nil]
|
18
|
-
def arguments
|
19
|
-
JSON.parse(@data['arguments']) if @data['arguments']
|
20
|
-
end
|
21
|
-
|
22
|
-
# @return [String]
|
23
|
-
def inspect
|
24
|
-
"#<#{self.class.name} name=#{name.inspect} arguments=#{arguments.inspect}>"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/lib/omniai/chat/message.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A message returned by the API.
|
6
|
-
class Message
|
7
|
-
# @return [Hash]
|
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} role=#{role.inspect} content=#{content.inspect}>"
|
18
|
-
end
|
19
|
-
|
20
|
-
# @return [String]
|
21
|
-
def role
|
22
|
-
@data['role'] || Role::USER
|
23
|
-
end
|
24
|
-
|
25
|
-
# @return [String, nil]
|
26
|
-
def content
|
27
|
-
@data['content']
|
28
|
-
end
|
29
|
-
|
30
|
-
# @return [Array<OmniAI::Chat::ToolCall>]
|
31
|
-
def tool_call_list
|
32
|
-
@tool_call_list ||=
|
33
|
-
@data['tool_calls'] ? @data['tool_calls'].map { |tool_call_data| ToolCall.new(data: tool_call_data) } : []
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A choice returned by the API.
|
6
|
-
class MessageChoice
|
7
|
-
# @param data [Hash]
|
8
|
-
def initialize(data:)
|
9
|
-
@data = data
|
10
|
-
end
|
11
|
-
|
12
|
-
# @return [Integer]
|
13
|
-
def index
|
14
|
-
@data['index']
|
15
|
-
end
|
16
|
-
|
17
|
-
# @return [OmniAI::Chat::Message]
|
18
|
-
def message
|
19
|
-
@message ||= Message.new(data: @data['message'])
|
20
|
-
end
|
21
|
-
|
22
|
-
# @return [String]
|
23
|
-
def inspect
|
24
|
-
"#<#{self.class.name} index=#{index} message=#{message.inspect}>"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/lib/omniai/chat/stream.rb
DELETED
@@ -1,25 +0,0 @@
|
|
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
|
-
def initialize(response:)
|
9
|
-
@response = response
|
10
|
-
@parser = EventStreamParser::Parser.new
|
11
|
-
end
|
12
|
-
|
13
|
-
# @yield [OmniAI::Chat::Chunk]
|
14
|
-
def stream!
|
15
|
-
@response.body.each do |chunk|
|
16
|
-
@parser.feed(chunk) do |_, data|
|
17
|
-
next if data.eql?('[DONE]')
|
18
|
-
|
19
|
-
yield(Chunk.new(data: JSON.parse(data)))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A tool-call returned by the API.
|
6
|
-
class ToolCall
|
7
|
-
# @param data [Hash]
|
8
|
-
def initialize(data:)
|
9
|
-
@data = data
|
10
|
-
end
|
11
|
-
|
12
|
-
# @return [String]
|
13
|
-
def id
|
14
|
-
@data['id']
|
15
|
-
end
|
16
|
-
|
17
|
-
# @return [String]
|
18
|
-
def type
|
19
|
-
@data['type']
|
20
|
-
end
|
21
|
-
|
22
|
-
# @return [OmniAI::Chat::Function]
|
23
|
-
def function
|
24
|
-
@function ||= Function.new(data: @data['function']) if @data['function']
|
25
|
-
end
|
26
|
-
|
27
|
-
# @return [String]
|
28
|
-
def inspect
|
29
|
-
"#<#{self.class.name} id=#{id.inspect} type=#{type.inspect}>"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
data/lib/omniai/chat/usage.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module OmniAI
|
4
|
-
class Chat
|
5
|
-
# A usage returned by the API.
|
6
|
-
class Usage
|
7
|
-
attr_accessor :data
|
8
|
-
|
9
|
-
# @param data [Hash]
|
10
|
-
def initialize(data:)
|
11
|
-
@data = data
|
12
|
-
end
|
13
|
-
|
14
|
-
# @return [Integer]
|
15
|
-
def input_tokens
|
16
|
-
@data['input_tokens'] || @data['prompt_tokens']
|
17
|
-
end
|
18
|
-
|
19
|
-
# @return [Integer]
|
20
|
-
def output_tokens
|
21
|
-
@data['output_tokens'] || @data['completion_tokens']
|
22
|
-
end
|
23
|
-
|
24
|
-
def total_tokens
|
25
|
-
@data['total_tokens'] || (input_tokens + output_tokens)
|
26
|
-
end
|
27
|
-
|
28
|
-
# @return [String]
|
29
|
-
def inspect
|
30
|
-
"#<#{self.class.name} input_tokens=#{input_tokens} output_tokens=#{output_tokens} total_tokens=#{total_tokens}>"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|