omniai 1.0.9 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -2
- data/lib/omniai/chat.rb +4 -0
- data/lib/omniai/client.rb +14 -0
- data/lib/omniai/transcribe/transcription.rb +26 -0
- data/lib/omniai/transcribe.rb +152 -0
- data/lib/omniai/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c22e433e659a447047be5c560093b288f71619b65c30d28c1f0b772a731f02b9
|
4
|
+
data.tar.gz: c654b22f69ec301f4858e2ec82d9999e14f4b1aeb99b71edc4511410b8c77064
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1068c486faab118bb1171da1ec53a57a4f3b45625b2d5d74de3dc49df4d40c552cc99bab4f640d1cf4eed5693715002a49a14e487b1f75008962c85a158d157
|
7
|
+
data.tar.gz: 3900c46ed2929cae7409b8b3048ef5339738414adfacee83a3b388a8ab93cea6e88d7031513647adafd136e6d1ddf0f9a0e553ea2a42d4de2013602f56919241
|
data/README.md
CHANGED
@@ -80,7 +80,7 @@ messages = [
|
|
80
80
|
'What is the capital of Canada?'
|
81
81
|
]
|
82
82
|
completion = client.chat(messages, model: '...', temperature: 0.7, format: :json)
|
83
|
-
completion.choice.message.content
|
83
|
+
completion.choice.message.content # '...'
|
84
84
|
```
|
85
85
|
|
86
86
|
#### w/ a Collection of Files
|
@@ -100,7 +100,7 @@ message = {
|
|
100
100
|
}
|
101
101
|
|
102
102
|
completion = client.chat(message)
|
103
|
-
completion.choice.message.content
|
103
|
+
completion.choice.message.content # '...'
|
104
104
|
```
|
105
105
|
|
106
106
|
#### Streaming
|
@@ -111,3 +111,12 @@ 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
|
+
transcription = client.transcribe(file.path)
|
121
|
+
transcription.text # '...'
|
122
|
+
```
|
data/lib/omniai/chat.rb
CHANGED
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: nil)
|
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.nil? || @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
|
data/lib/omniai/version.rb
CHANGED
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.
|
4
|
+
version: 1.1.1
|
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: []
|