omniai 1.0.8 → 1.1.0

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: 1971cb590311266b342596c44ff56398999b8657e9f6cf2b8d2afffb8b91257b
4
- data.tar.gz: 7866818c3dd9c1af3b03a351ecdc38c527ef1d08bb877533a9f9159bce8d5ed5
3
+ metadata.gz: 7a31351fb7cc352c108f55c3df8d57020bf2d520b935dce75eb87e62e4a0737c
4
+ data.tar.gz: a8ca504f40836a0072dcf4549767632a19bb6497da53390404fc1399ae264522
5
5
  SHA512:
6
- metadata.gz: 2c93b8a97fc2ca09765e9c2c537c15491a7344cc505e6204addd972dbb2c7ee2219213d2892745df48bdd4c0193bf3d80391142da2462de437290fc3a6d442db
7
- data.tar.gz: 0b7d13dfde39f89c78a57ea3b03549262fb7b9962d127a01e89ca3785402098b6c0e34fc957145615d279a14f4d644769c25a2d3f833aa9c1136397d6b7384b3
6
+ metadata.gz: 76f22ab7edf30b0f5aedbe071d7a1ff0fd5c1b2a5ab1f5a36273b9041c62a27796a510a4c73fd52f27606ff4a5e8d84fbb46cea03a6949da71ada89cdc62daba
7
+ data.tar.gz: 73d4718a9a593e5c520f48f8a1fa6c06560a2f3447c609f1a85ecbbda9c7d55dc45a6bec9d57de5054b1936be1d21266bbcb3324096a8337db1cf05d7359f09a
data/README.md CHANGED
@@ -111,3 +111,11 @@ stream = proc do |chunk|
111
111
  end
112
112
  client.chat('Tell me a joke.', stream:)
113
113
  ```
114
+
115
+ ### Transcribe
116
+
117
+ Clients that support chat (e.g. OpenAI w/ "Whisper", etc) convert recordings to text via the following calls:
118
+
119
+ ```ruby
120
+ client.transcribe(file.path)
121
+ ```
@@ -33,7 +33,7 @@ module OmniAI
33
33
 
34
34
  # @return [Array<OmniAI::Chat::Choice>]
35
35
  def choices
36
- @choices ||= @data['choices'].map { |data| Choice.new(data:) }
36
+ @choices ||= @data['choices'].map { |data| Choice.for(data:) }
37
37
  end
38
38
 
39
39
  # @param [index] [Integer]
data/lib/omniai/chat.rb CHANGED
@@ -33,6 +33,10 @@ module OmniAI
33
33
  SYSTEM = 'system'
34
34
  end
35
35
 
36
+ module Format
37
+ JSON = :json
38
+ end
39
+
36
40
  def self.process!(...)
37
41
  new(...).process!
38
42
  end
data/lib/omniai/client.rb CHANGED
@@ -45,5 +45,19 @@ module OmniAI
45
45
  def chat(messages, model:, temperature: nil, format: nil, stream: nil)
46
46
  raise NotImplementedError, "#{self.class.name}#chat undefined"
47
47
  end
48
+
49
+ # @raise [OmniAI::Error]
50
+ #
51
+ # @param file [IO]
52
+ # @param model [String]
53
+ # @param language [String, nil] optional
54
+ # @param prompt [String, nil] optional
55
+ # @param temperature [Float, nil] optional
56
+ # @param format [Symbol] :text, :srt, :vtt, or :json (default)
57
+ #
58
+ # @return text [OmniAI::Transcribe::Transcription]
59
+ def transcribe(file, model:, language: nil, prompt: nil, temperature: nil, format: Transcription::Format::JSON)
60
+ raise NotImplementedError, "#{self.class.name}#speak undefined"
61
+ end
48
62
  end
49
63
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ class Transcribe
5
+ # A transcription returned by the API.
6
+ class Transcription
7
+ attr_accessor :data, :format
8
+
9
+ # @param data [Hash]
10
+ def initialize(data:, format:)
11
+ @data = data
12
+ @format = format
13
+ end
14
+
15
+ # @return [String]
16
+ def text
17
+ @data['text']
18
+ end
19
+
20
+ # @return [String]
21
+ def inspect
22
+ "#<#{self.class} text=#{text.inspect} format=#{format.inspect}>"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ # An abstract class that provides a consistent interface for processing transcribe requests.
5
+ #
6
+ # Usage:
7
+ #
8
+ # class OmniAI::OpenAI::Transcribe < OmniAI::Transcribe
9
+ # module Model
10
+ # WHISPER_1 = "whisper-1"
11
+ # end
12
+ #
13
+ # protected
14
+ #
15
+ # # @return [Hash]
16
+ # def payload
17
+ # raise NotImplementedError, "#{self.class.name}#payload undefined"
18
+ # end
19
+ #
20
+ # # @return [String]
21
+ # def path
22
+ # raise NotImplementedError, "#{self.class.name}#path undefined"
23
+ # end
24
+ # end
25
+ #
26
+ # client.transcribe(File.open("..."), model: "...", format: :json)
27
+ class Transcribe
28
+ module Language
29
+ AFRIKAANS = 'af'
30
+ ARABIC = 'ar'
31
+ ARMENIAN = 'hy'
32
+ AZERBAIJANI = 'az'
33
+ BELARUSIAN = 'be'
34
+ BOSNIAN = 'bs'
35
+ BULGARIAN = 'bg'
36
+ CATALAN = 'ca'
37
+ CHINESE = 'zh'
38
+ CROATIAN = 'hr'
39
+ CZECH = 'cs'
40
+ DANISH = 'da'
41
+ DUTCH = 'nl'
42
+ ENGLISH = 'en'
43
+ ESTONIAN = 'et'
44
+ FINNISH = 'fi'
45
+ FRENCH = 'fr'
46
+ GALICIAN = 'gl'
47
+ GERMAN = 'de'
48
+ GREEK = 'el'
49
+ HEBREW = 'he'
50
+ HINDI = 'hi'
51
+ HUNGARIAN = 'hu'
52
+ ICELANDIC = 'is'
53
+ INDONESIAN = 'id'
54
+ ITALIAN = 'it'
55
+ JAPANESE = 'ja'
56
+ KANNADA = 'kn'
57
+ KAZAKH = 'kk'
58
+ KOREAN = 'ko'
59
+ LATVIAN = 'lv'
60
+ LITHUANIAN = 'lt'
61
+ MACEDONIAN = 'mk'
62
+ MALAY = 'ms'
63
+ MARATHI = 'mr'
64
+ MAORI = 'mi'
65
+ NEPALI = 'ne'
66
+ NORWEGIAN = 'no'
67
+ PERSIAN = 'fa'
68
+ POLISH = 'pl'
69
+ PORTUGUESE = 'pt'
70
+ ROMANIAN = 'ro'
71
+ RUSSIAN = 'ru'
72
+ SERBIAN = 'sr'
73
+ SLOVAK = 'sk'
74
+ SLOVENIAN = 'sl'
75
+ SPANISH = 'es'
76
+ SWAHILI = 'sw'
77
+ SWEDISH = 'sv'
78
+ TAGALOG = 'tl'
79
+ TAMIL = 'ta'
80
+ THAI = 'th'
81
+ TURKISH = 'tr'
82
+ UKRAINIAN = 'uk'
83
+ URDU = 'ur'
84
+ VIETNAMESE = 'vi'
85
+ WELSH = 'cy'
86
+ end
87
+
88
+ module Format
89
+ JSON = 'json'
90
+ TEXT = 'text'
91
+ VTT = 'vtt'
92
+ SRT = 'srt'
93
+ end
94
+
95
+ def self.process!(...)
96
+ new(...).process!
97
+ end
98
+
99
+ # @param path [String] required
100
+ # @param client [OmniAI::Client] the client
101
+ # @param model [String] required
102
+ # @param language [String, nil] optional
103
+ # @param prompt [String, nil] optional
104
+ # @param temperature [Float, nil] optional
105
+ # @param format [String, nil] optional
106
+ def initialize(path, client:, model:, language: nil, prompt: nil, temperature: nil, format: Format::JSON)
107
+ @path = path
108
+ @model = model
109
+ @language = language
110
+ @prompt = prompt
111
+ @temperature = temperature
112
+ @format = format
113
+ @client = client
114
+ end
115
+
116
+ # @return [OmniAI::Transcribe::Transcription]
117
+ # @raise [ExecutionError]
118
+ def process!
119
+ response = request!
120
+ raise HTTPError, response.flush unless response.status.ok?
121
+
122
+ data = @format.eql?(Format::JSON) ? response.parse : { text: String(response.body) }
123
+ Transcription.new(format: @format, data:)
124
+ end
125
+
126
+ protected
127
+
128
+ # @return [Hash]
129
+ def payload
130
+ {
131
+ file: HTTP::FormData::File.new(@path),
132
+ model: @model,
133
+ language: @language,
134
+ prompt: @prompt,
135
+ temperature: @temperature,
136
+ }.compact
137
+ end
138
+
139
+ # @return [String]
140
+ def path
141
+ raise NotImplementedError, "#{self.class.name}#path undefined"
142
+ end
143
+
144
+ # @return [HTTP::Response]
145
+ def request!
146
+ @client
147
+ .connection
148
+ .accept(@format.eql?(Format::JSON) ? :json : :text)
149
+ .post(path, form: payload)
150
+ end
151
+ end
152
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OmniAI
4
- VERSION = '1.0.8'
4
+ VERSION = '1.1.0'
5
5
  end
data/lib/omniai.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'event_stream_parser'
3
4
  require 'http'
4
5
  require 'zeitwerk'
5
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
@@ -76,6 +76,8 @@ files:
76
76
  - lib/omniai/chat/usage.rb
77
77
  - lib/omniai/client.rb
78
78
  - lib/omniai/config.rb
79
+ - lib/omniai/transcribe.rb
80
+ - lib/omniai/transcribe/transcription.rb
79
81
  - lib/omniai/version.rb
80
82
  homepage: https://github.com/ksylvest/omniai
81
83
  licenses: []