omniai-openai 1.3.2 → 1.5.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: 7cb12222b6c13b368f2f25d55c66ff57e98114830a32bdd54ee82947aaf38674
4
- data.tar.gz: 7600584957b977be573321a25a995f720fde9e9112fdd3e1b8676ce3d75461e0
3
+ metadata.gz: 9541bdbe54c24be244b7bacd347eb4457d2f8686151cec1d4474889bf23bf6d0
4
+ data.tar.gz: 5fabc408441ca53da67d05f221d14cb74f6c178e30696ec1a76598adb18ec4c8
5
5
  SHA512:
6
- metadata.gz: 75f8bdbfff09e789058e15bf962efcbbe0146b3e522e5efc701ca1303ef734b68f6d19094502c2e8fd89f5db213687b6c5001967e3dfd1b46eaab060b4c4e4c0
7
- data.tar.gz: 5b206397432e32277bda4767bad41707699607cf93e9a396570cbe78ab668f42b5c34c75f99d26d93cccd042b9c08359a899a4f375fabae7fc6c55a587515550
6
+ metadata.gz: 48b237f8d606fff3d8db2aecac46633d6bebbae3c403ce2f68d9d87ecac0fe7beb101ada9be12d15e2d1bf734ef86dab92ff7cbf56298311634e44584b9b7cce
7
+ data.tar.gz: 4627a44685b895c4b2616de3a88ba8b4bb63b038efc7f0536aacc7b9108e373f78d690acdc5adc99635dc9c8e10c6b1e59161643fdf7c3db283a012f1166a9bb
data/Gemfile CHANGED
@@ -5,7 +5,6 @@ source 'https://rubygems.org'
5
5
  gemspec
6
6
 
7
7
  gem 'rake'
8
-
9
8
  gem 'rspec'
10
9
  gem 'rspec_junit_formatter'
11
10
  gem 'rubocop'
@@ -13,3 +12,4 @@ gem 'rubocop-rake'
13
12
  gem 'rubocop-rspec'
14
13
  gem 'simplecov'
15
14
  gem 'webmock'
15
+ gem 'yard'
@@ -25,6 +25,7 @@ module OmniAI
25
25
  stream: @stream.nil? ? nil : !@stream.nil?,
26
26
  temperature: @temperature,
27
27
  response_format: (JSON_RESPONSE_FORMAT if @format.eql?(:json)),
28
+ tools: @tools&.map(&:prepare),
28
29
  }).compact
29
30
  end
30
31
 
@@ -67,10 +67,11 @@ module OmniAI
67
67
  # @param format [Symbol] optional :text or :json
68
68
  # @param temperature [Float, nil] optional
69
69
  # @param stream [Proc, nil] optional
70
+ # @param tools [Array<OmniAI::Tool>, nil] optional
70
71
  #
71
72
  # @return [OmniAI::Chat::Completion]
72
- def chat(messages, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil)
73
- Chat.process!(messages, model:, temperature:, format:, stream:, client: self)
73
+ def chat(messages, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil, tools: nil)
74
+ Chat.process!(messages, model:, temperature:, format:, stream:, tools:, client: self)
74
75
  end
75
76
 
76
77
  # @raise [OmniAI::Error]
@@ -82,7 +83,7 @@ module OmniAI
82
83
  # @param temperature [Float, nil] optional
83
84
  # @param format [Symbol] :text, :srt, :vtt, or :json (default)
84
85
  #
85
- # @return text [OmniAI::Transcribe::Transcription]
86
+ # @return [OmniAI::Transcribe]
86
87
  def transcribe(path, model: Transcribe::Model::WHISPER, language: nil, prompt: nil, temperature: nil, format: nil)
87
88
  Transcribe.process!(path, model:, language:, prompt:, temperature:, format:, client: self)
88
89
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module OpenAI
5
+ class Thread
6
+ # An OpenAI content w/ annotations.
7
+ class Annotation
8
+ # @!attribute [rw] data
9
+ # @return [Hash, nil]
10
+ attr_accessor :data
11
+
12
+ # @param data [Hash] required
13
+ # @param client [OmniAI::OpenAI::Client] optional
14
+ def initialize(data:, client: Client.new)
15
+ @data = data
16
+ @client = client
17
+ end
18
+
19
+ # @return [String] "file_citation" or "file_path"
20
+ def type
21
+ @data['type']
22
+ end
23
+
24
+ # @return [String]
25
+ def text
26
+ @data['text']
27
+ end
28
+
29
+ # @return [Integer]
30
+ def start_index
31
+ @data['start_index']
32
+ end
33
+
34
+ # @return [Integer]
35
+ def end_index
36
+ @data['end_index']
37
+ end
38
+
39
+ # @return [Range<Integer>]
40
+ def range
41
+ start_index..end_index
42
+ end
43
+
44
+ # @return [String]
45
+ def file_id
46
+ @file_id ||= (@data['file_citation'] || @data['file_path'])['file_id']
47
+ end
48
+
49
+ # Present if type is "file_citation" or "file_path".
50
+ #
51
+ # @return [OmniAI::OpenAI::File, nil]
52
+ def file!
53
+ @file ||= @client.files.find(id: file_id)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module OpenAI
5
+ class Thread
6
+ # An OpenAI attachment.
7
+ class Attachment
8
+ # @!attribute [rw] data
9
+ # @return [Hash, nil]
10
+ attr_accessor :data
11
+
12
+ # @param data [Array]
13
+ # @param client [OmniAI::OpenAI::Client]
14
+ #
15
+ # @return [Array<OmniAI::OpenAI::Thread::Content>, String, nil]
16
+ def self.for(data:, client: Client.new)
17
+ return data unless data.is_a?(Enumerable)
18
+
19
+ data.map { |attachment| new(data: attachment, client:) }
20
+ end
21
+
22
+ # @param data [Hash]
23
+ # @param client [OmniAI::OpenAI::Client]
24
+ def initialize(data:, client: Client.new)
25
+ @data = data
26
+ @client = client
27
+ end
28
+
29
+ # @return [String] e.g. "text"
30
+ def file_id
31
+ @file_id ||= @data['file_id']
32
+ end
33
+
34
+ # @return [Array<Hash>]
35
+ def tools
36
+ @tools ||= @data['tools']
37
+ end
38
+
39
+ # @return [OmniAI::OpenAI::File]
40
+ def file!
41
+ @file ||= @client.files.find(id: file_id)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module OpenAI
5
+ class Thread
6
+ # An OpenAI content w/ annotations.
7
+ class Content
8
+ module Type
9
+ TEXT = 'text'
10
+ end
11
+
12
+ # @param data [Array]
13
+ # @param client [OmniAI::OpenAI::Client]
14
+ #
15
+ # @return [Array<OmniAI::OpenAI::Thread::Content>, String, nil]
16
+ def self.for(data:, client: Client.new)
17
+ return data unless data.is_a?(Enumerable)
18
+
19
+ data.map { |attachment| new(data: attachment, client:) }
20
+ end
21
+
22
+ # @!attribute [rw] data
23
+ # @return [Hash, nil]
24
+ attr_accessor :data
25
+
26
+ # @param data [Hash]
27
+ def initialize(data:, client:)
28
+ @data = data
29
+ @client = client
30
+ end
31
+
32
+ # @return [String] e.g. "text"
33
+ def type
34
+ @type ||= @data['type']
35
+ end
36
+
37
+ # @return [Boolean]
38
+ def text?
39
+ type.eql?(Type::TEXT)
40
+ end
41
+
42
+ # @return [OmniAI::OpenAI::Thread::Text]
43
+ def text
44
+ @text ||= Text.new(data: @data['text'], client: @client) if @data['text']
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -168,8 +168,8 @@ module OmniAI
168
168
  thread_id: data['thread_id'],
169
169
  run_id: data['run_id'],
170
170
  role: data['role'],
171
- content: data['content'],
172
- attachments: data['attachments'],
171
+ content: Content.for(data: data['content'], client:),
172
+ attachments: Attachment.for(data: data['attachments'], client:),
173
173
  metadata: data['metadata']
174
174
  )
175
175
  end
@@ -181,9 +181,9 @@ module OmniAI
181
181
  @assistant_id = data['assistant_id']
182
182
  @thread_id = data['thread_id']
183
183
  @run_id = data['run_id']
184
- @role = data['role']
185
- @content = data['content']
186
- @attachments = data['attachments']
184
+ @role = data['role']
185
+ @content = Content.for(data: data['content'], client: @client)
186
+ @attachments = Attachment.for(data: data['content'], client: @client)
187
187
  @metadata = data['metadata']
188
188
  end
189
189
 
@@ -34,7 +34,6 @@ module OmniAI
34
34
  # @param content [String, Array, nil] optional
35
35
  # @param attachments [Array, nil] optional
36
36
  # @param metadata [Hash, nil] optional
37
- # @param client [OmniAI::OpenAI::Client] optional
38
37
  # @return [OmniAI::OpenAI::Thread::Message]
39
38
  def build(role: nil, content: nil, attachments: [], metadata: {})
40
39
  Message.new(role:, content:, attachments:, metadata:, thread_id: @thread.id, client: @client)
@@ -183,7 +183,7 @@ module OmniAI
183
183
  self
184
184
  end
185
185
 
186
- # @param interval [Integer, Float, nil] optional (seconds)
186
+ # @param delay [Numeric, nil] optional (seconds)
187
187
  #
188
188
  # @return [OmniAI::OpenAI::Thread::Run]
189
189
  def poll!(delay: 2)
@@ -31,12 +31,11 @@ module OmniAI
31
31
  end
32
32
 
33
33
  # @param assistant_id [String] required
34
- # @param model [String] optional
35
- # @param temperature [Float] optional
36
- # @param instructions [String] optional
34
+ # @param model [String, nil] optional
35
+ # @param temperature [Float, nil] optional
36
+ # @param instructions [String, nil] optional
37
37
  # @param tools [Array<Hash>, nil] optional
38
38
  # @param metadata [Hash, nil] optional
39
- # @param client [OmniAI::OpenAI::Client] optional
40
39
  # @return [OmniAI::OpenAI::Thread::Message]
41
40
  def build(assistant_id:, model: nil, temperature: nil, instructions: nil, tools: nil, metadata: {})
42
41
  Run.new(
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module OpenAI
5
+ class Thread
6
+ # An OpenAI text w/ annotations.
7
+ class Text
8
+ # @!attribute [rw] data
9
+ # @return [Hash, nil]
10
+ attr_accessor :data
11
+
12
+ # @param data [Hash]
13
+ # @param client [OmniAI::OpenAI::Client]
14
+ def initialize(data:, client:)
15
+ @data = data
16
+ @client = client
17
+ end
18
+
19
+ # @return [String]
20
+ def annotate!
21
+ text = value
22
+
23
+ annotations.each do |annotation|
24
+ file = annotation.file!
25
+ text = text.gsub(annotation.text, "[#{file.filename}:#{annotation.range}]")
26
+ end
27
+
28
+ text
29
+ end
30
+
31
+ # @return [String] e.g. "text"
32
+ def type
33
+ @data['type']
34
+ end
35
+
36
+ # @return [String]
37
+ def value
38
+ @data['value']
39
+ end
40
+
41
+ # @return [Array<OmniAI::OpenAI::Thread::Annotation>]
42
+ def annotations
43
+ @annotations ||= @data['annotations'].map { |data| Annotation.new(data:, client: @client) }
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module OpenAI
5
- VERSION = '1.3.2'
5
+ VERSION = '1.5.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai-openai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.5.0
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-24 00:00:00.000000000 Z
11
+ date: 2024-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: event_stream_parser
@@ -71,10 +71,14 @@ files:
71
71
  - lib/omniai/openai/files.rb
72
72
  - lib/omniai/openai/speak.rb
73
73
  - lib/omniai/openai/thread.rb
74
+ - lib/omniai/openai/thread/annotation.rb
75
+ - lib/omniai/openai/thread/attachment.rb
76
+ - lib/omniai/openai/thread/content.rb
74
77
  - lib/omniai/openai/thread/message.rb
75
78
  - lib/omniai/openai/thread/messages.rb
76
79
  - lib/omniai/openai/thread/run.rb
77
80
  - lib/omniai/openai/thread/runs.rb
81
+ - lib/omniai/openai/thread/text.rb
78
82
  - lib/omniai/openai/threads.rb
79
83
  - lib/omniai/openai/tool.rb
80
84
  - lib/omniai/openai/transcribe.rb
@@ -101,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
105
  - !ruby/object:Gem::Version
102
106
  version: '0'
103
107
  requirements: []
104
- rubygems_version: 3.5.3
108
+ rubygems_version: 3.5.14
105
109
  signing_key:
106
110
  specification_version: 4
107
111
  summary: A generalized framework for interacting with OpenAI