mistral_translator 0.4.0 → 0.6.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 +4 -4
- data/CHANGELOG.md +52 -1
- data/README.md +15 -1
- data/docs/api-reference/configuration.md +27 -4
- data/docs/api-reference/errors.md +6 -6
- data/docs/api-reference/methods.md +2 -2
- data/docs/getting-started.md +11 -5
- data/docs/installation.md +0 -2
- data/docs/rails-integration/setup.md +0 -1
- data/lib/mistral_translator/client_helpers.rb +2 -2
- data/lib/mistral_translator/configuration.rb +24 -4
- data/lib/mistral_translator/data/languages.json +2367 -0
- data/lib/mistral_translator/data/regions.json +1170 -0
- data/lib/mistral_translator/data/scripts.json +182 -0
- data/lib/mistral_translator/deprecated_options.rb +45 -0
- data/lib/mistral_translator/helpers.rb +5 -10
- data/lib/mistral_translator/helpers_extensions.rb +0 -58
- data/lib/mistral_translator/locale.rb +84 -0
- data/lib/mistral_translator/locale_helper.rb +14 -29
- data/lib/mistral_translator/prompt_builder.rb +0 -94
- data/lib/mistral_translator/prompt_helpers.rb +0 -23
- data/lib/mistral_translator/prompt_metadata_helpers.rb +0 -5
- data/lib/mistral_translator/response_parser.rb +53 -110
- data/lib/mistral_translator/retries.rb +50 -0
- data/lib/mistral_translator/summarizer.rb +13 -50
- data/lib/mistral_translator/translator.rb +9 -23
- data/lib/mistral_translator/translator_helpers.rb +4 -92
- data/lib/mistral_translator/version.rb +1 -1
- data/lib/mistral_translator.rb +1 -0
- metadata +6 -1
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Adlm" : {
|
|
3
|
+
"en" : "Adlam",
|
|
4
|
+
"fr" : "adlam"
|
|
5
|
+
},
|
|
6
|
+
"Arab" : {
|
|
7
|
+
"en" : "Arabic",
|
|
8
|
+
"fr" : "arabe"
|
|
9
|
+
},
|
|
10
|
+
"Aran" : {
|
|
11
|
+
"en" : "Nastaliq",
|
|
12
|
+
"fr" : "nastaliq"
|
|
13
|
+
},
|
|
14
|
+
"Armn" : {
|
|
15
|
+
"en" : "Armenian",
|
|
16
|
+
"fr" : "arménien"
|
|
17
|
+
},
|
|
18
|
+
"Beng" : {
|
|
19
|
+
"en" : "Bangla",
|
|
20
|
+
"fr" : "bengali"
|
|
21
|
+
},
|
|
22
|
+
"Cakm" : {
|
|
23
|
+
"en" : "Chakma",
|
|
24
|
+
"fr" : "chakma"
|
|
25
|
+
},
|
|
26
|
+
"Cans" : {
|
|
27
|
+
"en" : "Syllabics",
|
|
28
|
+
"fr" : "syllabaire"
|
|
29
|
+
},
|
|
30
|
+
"Cher" : {
|
|
31
|
+
"en" : "Cherokee",
|
|
32
|
+
"fr" : "cherokee"
|
|
33
|
+
},
|
|
34
|
+
"Cyrl" : {
|
|
35
|
+
"en" : "Cyrillic",
|
|
36
|
+
"fr" : "cyrillique"
|
|
37
|
+
},
|
|
38
|
+
"Deva" : {
|
|
39
|
+
"en" : "Devanagari",
|
|
40
|
+
"fr" : "dévanagari"
|
|
41
|
+
},
|
|
42
|
+
"Ethi" : {
|
|
43
|
+
"en" : "Ethiopic",
|
|
44
|
+
"fr" : "éthiopique"
|
|
45
|
+
},
|
|
46
|
+
"Geor" : {
|
|
47
|
+
"en" : "Georgian",
|
|
48
|
+
"fr" : "géorgien"
|
|
49
|
+
},
|
|
50
|
+
"Grek" : {
|
|
51
|
+
"en" : "Greek",
|
|
52
|
+
"fr" : "grec"
|
|
53
|
+
},
|
|
54
|
+
"Gujr" : {
|
|
55
|
+
"en" : "Gujarati",
|
|
56
|
+
"fr" : "goudjarâtî"
|
|
57
|
+
},
|
|
58
|
+
"Guru" : {
|
|
59
|
+
"en" : "Gurmukhi",
|
|
60
|
+
"fr" : "gourmoukhî"
|
|
61
|
+
},
|
|
62
|
+
"Hans" : {
|
|
63
|
+
"en" : "Simplified Han",
|
|
64
|
+
"fr" : "sinogrammes simplifiés"
|
|
65
|
+
},
|
|
66
|
+
"Hant" : {
|
|
67
|
+
"en" : "Traditional Han",
|
|
68
|
+
"fr" : "sinogrammes traditionnels"
|
|
69
|
+
},
|
|
70
|
+
"Hebr" : {
|
|
71
|
+
"en" : "Hebrew",
|
|
72
|
+
"fr" : "hébreu"
|
|
73
|
+
},
|
|
74
|
+
"Hmng" : {
|
|
75
|
+
"en" : "Pahawh Hmong",
|
|
76
|
+
"fr" : "pahawh hmong"
|
|
77
|
+
},
|
|
78
|
+
"Jpan" : {
|
|
79
|
+
"en" : "Japanese",
|
|
80
|
+
"fr" : "japonais"
|
|
81
|
+
},
|
|
82
|
+
"Kana" : {
|
|
83
|
+
"en" : "Katakana",
|
|
84
|
+
"fr" : "katakana"
|
|
85
|
+
},
|
|
86
|
+
"Khmr" : {
|
|
87
|
+
"en" : "Khmer",
|
|
88
|
+
"fr" : "khmer"
|
|
89
|
+
},
|
|
90
|
+
"Knda" : {
|
|
91
|
+
"en" : "Kannada",
|
|
92
|
+
"fr" : "kannara"
|
|
93
|
+
},
|
|
94
|
+
"Kore" : {
|
|
95
|
+
"en" : "Korean",
|
|
96
|
+
"fr" : "coréen"
|
|
97
|
+
},
|
|
98
|
+
"Laoo" : {
|
|
99
|
+
"en" : "Lao",
|
|
100
|
+
"fr" : "lao"
|
|
101
|
+
},
|
|
102
|
+
"Latn" : {
|
|
103
|
+
"en" : "Latin",
|
|
104
|
+
"fr" : "latin"
|
|
105
|
+
},
|
|
106
|
+
"Mand" : {
|
|
107
|
+
"en" : "Mandaean",
|
|
108
|
+
"fr" : "mandéen"
|
|
109
|
+
},
|
|
110
|
+
"Mlym" : {
|
|
111
|
+
"en" : "Malayalam",
|
|
112
|
+
"fr" : "malayalam"
|
|
113
|
+
},
|
|
114
|
+
"Mtei" : {
|
|
115
|
+
"en" : "Meitei Mayek",
|
|
116
|
+
"fr" : "meitei mayek"
|
|
117
|
+
},
|
|
118
|
+
"Mymr" : {
|
|
119
|
+
"en" : "Myanmar",
|
|
120
|
+
"fr" : "birman"
|
|
121
|
+
},
|
|
122
|
+
"Nkoo" : {
|
|
123
|
+
"en" : "N’Ko",
|
|
124
|
+
"fr" : "n’ko"
|
|
125
|
+
},
|
|
126
|
+
"Olck" : {
|
|
127
|
+
"en" : "Ol Chiki",
|
|
128
|
+
"fr" : "ol-chiki"
|
|
129
|
+
},
|
|
130
|
+
"Orya" : {
|
|
131
|
+
"en" : "Odia",
|
|
132
|
+
"fr" : "odia"
|
|
133
|
+
},
|
|
134
|
+
"Rjng" : {
|
|
135
|
+
"en" : "Rejang",
|
|
136
|
+
"fr" : "rejang"
|
|
137
|
+
},
|
|
138
|
+
"Rohg" : {
|
|
139
|
+
"en" : "Hanifi Rohingya",
|
|
140
|
+
"fr" : "hanifi"
|
|
141
|
+
},
|
|
142
|
+
"Sinh" : {
|
|
143
|
+
"en" : "Sinhala",
|
|
144
|
+
"fr" : "cingalais"
|
|
145
|
+
},
|
|
146
|
+
"Syrc" : {
|
|
147
|
+
"en" : "Syriac",
|
|
148
|
+
"fr" : "syriaque"
|
|
149
|
+
},
|
|
150
|
+
"Taml" : {
|
|
151
|
+
"en" : "Tamil",
|
|
152
|
+
"fr" : "tamoul"
|
|
153
|
+
},
|
|
154
|
+
"Telu" : {
|
|
155
|
+
"en" : "Telugu",
|
|
156
|
+
"fr" : "télougou"
|
|
157
|
+
},
|
|
158
|
+
"Tfng" : {
|
|
159
|
+
"en" : "Tifinagh",
|
|
160
|
+
"fr" : "tifinagh"
|
|
161
|
+
},
|
|
162
|
+
"Thaa" : {
|
|
163
|
+
"en" : "Thaana",
|
|
164
|
+
"fr" : "thâna"
|
|
165
|
+
},
|
|
166
|
+
"Tibt" : {
|
|
167
|
+
"en" : "Tibetan",
|
|
168
|
+
"fr" : "tibétain"
|
|
169
|
+
},
|
|
170
|
+
"Vaii" : {
|
|
171
|
+
"en" : "Vai",
|
|
172
|
+
"fr" : "vaï"
|
|
173
|
+
},
|
|
174
|
+
"Wcho" : {
|
|
175
|
+
"en" : "Wancho",
|
|
176
|
+
"fr" : "wantcho"
|
|
177
|
+
},
|
|
178
|
+
"Yiii" : {
|
|
179
|
+
"en" : "Yi",
|
|
180
|
+
"fr" : "yi"
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MistralTranslator
|
|
4
|
+
# Options de configuration obsolètes, retirées en 1.0 : ssl_timeout a toujours réglé
|
|
5
|
+
# l'ouverture de la connexion, default_max_tokens et default_temperature n'ont jamais été lus.
|
|
6
|
+
module DeprecatedOptions
|
|
7
|
+
def ssl_timeout
|
|
8
|
+
deprecated(:ssl_timeout, "use open_timeout")
|
|
9
|
+
open_timeout
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def ssl_timeout=(seconds)
|
|
13
|
+
deprecated(:ssl_timeout, "use open_timeout")
|
|
14
|
+
self.open_timeout = seconds
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def default_max_tokens
|
|
18
|
+
deprecated(:default_max_tokens, "it has no effect")
|
|
19
|
+
@default_max_tokens
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def default_max_tokens=(value)
|
|
23
|
+
deprecated(:default_max_tokens, "it has no effect")
|
|
24
|
+
@default_max_tokens = value
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def default_temperature
|
|
28
|
+
deprecated(:default_temperature, "it has no effect")
|
|
29
|
+
@default_temperature
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def default_temperature=(value)
|
|
33
|
+
deprecated(:default_temperature, "it has no effect")
|
|
34
|
+
@default_temperature = value
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
# L'avertissement désigne la ligne de l'appelant, pas celle de la gem
|
|
40
|
+
def deprecated(name, advice)
|
|
41
|
+
Kernel.warn("[MistralTranslator] config.#{name} is deprecated and will be removed in 1.0: #{advice}",
|
|
42
|
+
uplevel: 2)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -229,15 +229,15 @@ module MistralTranslator
|
|
|
229
229
|
end
|
|
230
230
|
end
|
|
231
231
|
|
|
232
|
-
# Avec
|
|
233
|
-
# on suggère à partir d'un nom de langue (exact
|
|
234
|
-
# à trois lettres (ISO 639-2 comme "fre"
|
|
232
|
+
# Avec des centaines de codes de deux et trois lettres, presque toute chaîne est
|
|
233
|
+
# « proche » d'un code : on suggère à partir d'un nom de langue (exact, ambigu ou
|
|
234
|
+
# mal orthographié) ou d'un code à trois lettres (ISO 639-2 comme "fre").
|
|
235
235
|
def find_locale_suggestions(invalid_locale)
|
|
236
236
|
return [] unless invalid_locale.is_a?(String)
|
|
237
237
|
|
|
238
238
|
input = invalid_locale.strip.downcase
|
|
239
|
-
|
|
240
|
-
return [
|
|
239
|
+
locale = Locale.find(input)
|
|
240
|
+
return [locale.to_s] if locale
|
|
241
241
|
|
|
242
242
|
by_code = input.length == 3 ? LocaleHelper.supported_locales.select { |code| input.start_with?(code) } : []
|
|
243
243
|
by_name = LocaleHelper::LANGUAGES.select do |_code, names|
|
|
@@ -252,11 +252,6 @@ module MistralTranslator
|
|
|
252
252
|
|
|
253
253
|
LevenshteinHelpers.levenshtein_distance(input, name) <= (name.length >= 8 ? 2 : 1)
|
|
254
254
|
end
|
|
255
|
-
|
|
256
|
-
# Exposer la distance de Levenshtein en privé via délégation pour les tests
|
|
257
|
-
def levenshtein_distance(source_string, target_string)
|
|
258
|
-
LevenshteinHelpers.levenshtein_distance(source_string, target_string)
|
|
259
|
-
end
|
|
260
255
|
end
|
|
261
256
|
|
|
262
257
|
# Module pour inclure dans les classes Rails si souhaité
|
|
@@ -25,43 +25,6 @@ module MistralTranslator
|
|
|
25
25
|
opts = { context: options[:context], glossary: options[:glossary] }
|
|
26
26
|
MistralTranslator.translate(text, from: from, to: to, preserve_html: true, **opts)
|
|
27
27
|
end
|
|
28
|
-
|
|
29
|
-
def translate_with_progress(items, from:, to:, **options)
|
|
30
|
-
results = {}
|
|
31
|
-
total = items.size
|
|
32
|
-
processed = 0
|
|
33
|
-
|
|
34
|
-
items.each do |key, text|
|
|
35
|
-
results[key] = MistralTranslator.translate(text, from: from, to: to, **options)
|
|
36
|
-
processed += 1
|
|
37
|
-
yield(processed, total, key) if block_given?
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
results
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def translate_multi_style(text, from:, to:, **options)
|
|
44
|
-
results = {}
|
|
45
|
-
|
|
46
|
-
styles = options[:styles] || %i[formal casual]
|
|
47
|
-
styles.each do |style|
|
|
48
|
-
style_context = options[:context] ? "#{options[:context]} (Style: #{style})" : "Style: #{style}"
|
|
49
|
-
|
|
50
|
-
begin
|
|
51
|
-
results[style] = MistralTranslator.translate(
|
|
52
|
-
text,
|
|
53
|
-
from: from,
|
|
54
|
-
to: to,
|
|
55
|
-
context: style_context,
|
|
56
|
-
glossary: options[:glossary]
|
|
57
|
-
)
|
|
58
|
-
rescue StandardError => e
|
|
59
|
-
results[style] = { error: e.message }
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
results
|
|
64
|
-
end
|
|
65
28
|
end
|
|
66
29
|
|
|
67
30
|
# Extensions pour les helpers d'analyse
|
|
@@ -103,27 +66,6 @@ module MistralTranslator
|
|
|
103
66
|
compression_ratio: (optimal_words.to_f / word_count * 100).round(1)
|
|
104
67
|
}
|
|
105
68
|
end
|
|
106
|
-
|
|
107
|
-
def find_locale_suggestions(invalid_locale)
|
|
108
|
-
return [] unless invalid_locale.is_a?(String)
|
|
109
|
-
|
|
110
|
-
supported = LocaleHelper.supported_locales.map(&:to_s)
|
|
111
|
-
|
|
112
|
-
# Recherche par similarité basique
|
|
113
|
-
suggestions = supported.select do |locale|
|
|
114
|
-
locale.start_with?(invalid_locale.downcase) ||
|
|
115
|
-
invalid_locale.downcase.start_with?(locale)
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
# Si pas de suggestions par préfixe, chercher par distance
|
|
119
|
-
if suggestions.empty?
|
|
120
|
-
suggestions = supported.select do |locale|
|
|
121
|
-
levenshtein_distance(invalid_locale.downcase, locale) <= 2
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
suggestions.first(3) # Limiter à 3 suggestions
|
|
126
|
-
end
|
|
127
69
|
end
|
|
128
70
|
|
|
129
71
|
# Extensions pour les helpers de coût
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module MistralTranslator
|
|
6
|
+
# Une locale BCP 47 : une langue, et éventuellement une écriture et une région
|
|
7
|
+
# ("fr", "pt-BR", "zh-Hant-TW", "haw"). Objet valeur : deux locales aux mêmes
|
|
8
|
+
# sous-étiquettes sont égales. Chaque sous-étiquette est vérifiée contre les
|
|
9
|
+
# données CLDR de data/, générées par bin/generate_languages.
|
|
10
|
+
Locale = Data.define(:language, :script, :region)
|
|
11
|
+
|
|
12
|
+
class Locale
|
|
13
|
+
load_data = ->(name) { JSON.parse(File.read(File.expand_path("data/#{name}.json", __dir__)), freeze: true) }
|
|
14
|
+
|
|
15
|
+
LANGUAGES = load_data.call("languages")
|
|
16
|
+
SCRIPTS = load_data.call("scripts")
|
|
17
|
+
REGIONS = load_data.call("regions")
|
|
18
|
+
|
|
19
|
+
# Code ISO 639-3 des langues qui ont un code à deux lettres : "hat" → "ht"
|
|
20
|
+
ALPHA3 = LANGUAGES.filter_map { |code, names| [names["alpha3"], code] if names["alpha3"] }.to_h.freeze
|
|
21
|
+
|
|
22
|
+
# Noms français, anglais et natifs acceptés à la place du code. Un nom porté
|
|
23
|
+
# par plusieurs langues (« konkani ») est écarté : il faut alors le code.
|
|
24
|
+
NAMES = LANGUAGES
|
|
25
|
+
.flat_map { |code, names| names.values_at("fr", "en", "native").map { |name| [name.downcase, code] } }
|
|
26
|
+
.uniq
|
|
27
|
+
.group_by(&:first)
|
|
28
|
+
.filter_map { |name, entries| [name, entries.first.last] if entries.one? }
|
|
29
|
+
.to_h.freeze
|
|
30
|
+
|
|
31
|
+
# langue[-écriture][-région], séparées par "-" ou "_". Les variantes et les
|
|
32
|
+
# extensions ("ca-ES-valencia", "en-US-u-ca-gregory") ne sont pas acceptées.
|
|
33
|
+
TAG = /\A(?<language>[a-z]{2,3})(?:[-_](?<script>[a-z]{4}))?(?:[-_](?<region>[a-z]{2}|\d{3}))?\z/i
|
|
34
|
+
|
|
35
|
+
class << self
|
|
36
|
+
# Locale désignée par un code ou un nom de langue ; UnsupportedLanguageError sinon.
|
|
37
|
+
def parse(input)
|
|
38
|
+
find(input) || raise(UnsupportedLanguageError, input.to_s)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Comme parse, mais renvoie nil pour une entrée inconnue.
|
|
42
|
+
def find(input)
|
|
43
|
+
return input if input.is_a?(Locale)
|
|
44
|
+
|
|
45
|
+
text = input.to_s.strip
|
|
46
|
+
from_tag(text) || from_name(text)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def from_tag(text)
|
|
52
|
+
return unless (match = TAG.match(text))
|
|
53
|
+
|
|
54
|
+
code = match[:language].downcase
|
|
55
|
+
language = LANGUAGES.key?(code) ? code : ALPHA3[code]
|
|
56
|
+
script = match[:script]&.capitalize
|
|
57
|
+
region = match[:region]&.upcase
|
|
58
|
+
new(language:, script:, region:) if language && known?(SCRIPTS, script) && known?(REGIONS, region)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def known?(table, code) = code.nil? || table.key?(code)
|
|
62
|
+
|
|
63
|
+
def from_name(text)
|
|
64
|
+
language = NAMES[text.downcase]
|
|
65
|
+
new(language:) if language
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def initialize(language:, script: nil, region: nil)
|
|
70
|
+
super
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Forme canonique BCP 47 : "pt-BR", "zh-Hant-TW"
|
|
74
|
+
def to_s = [language, script, region].compact.join("-")
|
|
75
|
+
|
|
76
|
+
# Nom français suivi du code, pour les prompts : « portugais (Brésil) (pt-BR) »
|
|
77
|
+
def prompt_name
|
|
78
|
+
name = LANGUAGES.dig(language, "fr")
|
|
79
|
+
details = [SCRIPTS.dig(script, "fr"), REGIONS.dig(region, "fr")].compact
|
|
80
|
+
name = "#{name} (#{details.join(", ")})" if details.any?
|
|
81
|
+
"#{name} (#{self})"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -1,45 +1,31 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "json"
|
|
4
|
-
|
|
5
3
|
module MistralTranslator
|
|
4
|
+
# Fonctions historiques sur les locales, qui délèguent à Locale.
|
|
6
5
|
module LocaleHelper
|
|
7
|
-
#
|
|
8
|
-
LANGUAGES =
|
|
6
|
+
# Langues CLDR, générées par bin/generate_languages
|
|
7
|
+
LANGUAGES = Locale::LANGUAGES
|
|
9
8
|
|
|
10
9
|
# Nom de chaque langue dans sa propre langue
|
|
11
10
|
SUPPORTED_LANGUAGES = LANGUAGES.transform_values { |names| names["native"].downcase }.freeze
|
|
12
11
|
|
|
13
|
-
# Noms
|
|
14
|
-
LANGUAGE_ALIASES =
|
|
15
|
-
[[names["en"].downcase, code], [names["fr"].downcase, code]]
|
|
16
|
-
end.to_h.freeze
|
|
12
|
+
# Noms acceptés à la place du code
|
|
13
|
+
LANGUAGE_ALIASES = Locale::NAMES
|
|
17
14
|
|
|
18
15
|
class << self
|
|
19
16
|
def locale_to_language(locale)
|
|
20
|
-
|
|
21
|
-
SUPPORTED_LANGUAGES[
|
|
17
|
+
found = Locale.find(locale)
|
|
18
|
+
found ? SUPPORTED_LANGUAGES[found.language] : locale.to_s
|
|
22
19
|
end
|
|
23
20
|
|
|
24
|
-
# Nom français suivi du code, pour les prompts : «
|
|
21
|
+
# Nom français suivi du code, pour les prompts : « portugais (Brésil) (pt-BR) »
|
|
25
22
|
def prompt_language_name(locale)
|
|
26
|
-
|
|
27
|
-
name = LANGUAGES.dig(locale_code, "fr")
|
|
28
|
-
name ? "#{name} (#{locale_code})" : locale_code
|
|
23
|
+
Locale.find(locale)&.prompt_name || locale.to_s
|
|
29
24
|
end
|
|
30
25
|
|
|
31
26
|
def language_to_locale(language)
|
|
32
27
|
normalized_language = language.to_s.downcase.strip
|
|
33
|
-
|
|
34
|
-
# Vérifier d'abord les alias
|
|
35
|
-
return LANGUAGE_ALIASES[normalized_language] if LANGUAGE_ALIASES.key?(normalized_language)
|
|
36
|
-
|
|
37
|
-
# Recherche directe par valeur
|
|
38
|
-
found_locale = SUPPORTED_LANGUAGES.find { |_, lang| lang.downcase == normalized_language }
|
|
39
|
-
return found_locale[0] if found_locale
|
|
40
|
-
|
|
41
|
-
# Retourne la langue telle quelle si pas trouvée
|
|
42
|
-
normalized_language
|
|
28
|
+
LANGUAGE_ALIASES.fetch(normalized_language, normalized_language)
|
|
43
29
|
end
|
|
44
30
|
|
|
45
31
|
def supported_locales
|
|
@@ -51,16 +37,15 @@ module MistralTranslator
|
|
|
51
37
|
end
|
|
52
38
|
|
|
53
39
|
def locale_supported?(locale)
|
|
54
|
-
|
|
40
|
+
!Locale.find(locale).nil?
|
|
55
41
|
end
|
|
56
42
|
|
|
43
|
+
# Forme canonique de la locale ("pt-BR") ; UnsupportedLanguageError si elle est inconnue
|
|
57
44
|
def validate_locale!(locale)
|
|
58
|
-
|
|
59
|
-
raise UnsupportedLanguageError, normalized unless locale_supported?(normalized)
|
|
60
|
-
|
|
61
|
-
normalized
|
|
45
|
+
Locale.parse(locale).to_s
|
|
62
46
|
end
|
|
63
47
|
|
|
48
|
+
# Sous-étiquette de langue seule, sans vérification : "fr-FR" → "fr"
|
|
64
49
|
def normalize_locale(locale)
|
|
65
50
|
return "" if locale.nil? || locale.to_s.empty?
|
|
66
51
|
|
|
@@ -9,7 +9,6 @@ module MistralTranslator
|
|
|
9
9
|
module PromptBuilder
|
|
10
10
|
extend PromptHelpers::ContextBuilder
|
|
11
11
|
extend PromptHelpers::HtmlInstructions
|
|
12
|
-
extend PromptHelpers::FormatInstructions
|
|
13
12
|
extend PromptMetadataHelpers
|
|
14
13
|
|
|
15
14
|
class << self
|
|
@@ -49,55 +48,6 @@ module MistralTranslator
|
|
|
49
48
|
PROMPT
|
|
50
49
|
end
|
|
51
50
|
|
|
52
|
-
# rubocop:disable Metrics/MethodLength
|
|
53
|
-
def bulk_translation_prompt(texts, source_language, target_language, **options)
|
|
54
|
-
source_name = LocaleHelper.prompt_language_name(source_language)
|
|
55
|
-
target_name = LocaleHelper.prompt_language_name(target_language)
|
|
56
|
-
|
|
57
|
-
context_section = build_context_section(options[:context], options[:glossary])
|
|
58
|
-
html_instruction = options[:preserve_html] ? build_html_preservation_instruction : ""
|
|
59
|
-
|
|
60
|
-
# Extraire les valeurs pour les métadonnées
|
|
61
|
-
context = options[:context]
|
|
62
|
-
glossary = options[:glossary]
|
|
63
|
-
preserve_html = options[:preserve_html]
|
|
64
|
-
|
|
65
|
-
Prompt.new(instructions: <<~PROMPT, text: numbered_texts(texts))
|
|
66
|
-
Tu es un traducteur professionnel. Traduis les textes fournis, numérotés.
|
|
67
|
-
Langue source : #{source_name}. Langue cible : #{target_name}.
|
|
68
|
-
#{context_section}
|
|
69
|
-
RÈGLES :
|
|
70
|
-
- Traduis fidèlement chaque texte sans ajouter d'informations
|
|
71
|
-
- Conserve le style, ton et format originaux#{html_instruction}
|
|
72
|
-
- Maintiens la cohérence terminologique entre tous les textes
|
|
73
|
-
- #{Prompt::DATA_RULE}
|
|
74
|
-
- Réponds UNIQUEMENT en JSON valide
|
|
75
|
-
|
|
76
|
-
FORMAT OBLIGATOIRE :
|
|
77
|
-
{
|
|
78
|
-
"translations": [
|
|
79
|
-
{
|
|
80
|
-
"index": 1,
|
|
81
|
-
"source": "texte original 1",
|
|
82
|
-
"target": "texte traduit 1"
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
"index": 2,
|
|
86
|
-
"source": "texte original 2",
|
|
87
|
-
"target": "texte traduit 2"
|
|
88
|
-
}
|
|
89
|
-
],
|
|
90
|
-
"metadata": {
|
|
91
|
-
"source_language": "#{source_language}",
|
|
92
|
-
"target_language": "#{target_language}",
|
|
93
|
-
"count": #{texts.length},
|
|
94
|
-
"operation": "bulk_translation"#{build_metadata_additions(context, glossary, preserve_html)}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
PROMPT
|
|
98
|
-
end
|
|
99
|
-
# rubocop:enable Metrics/MethodLength
|
|
100
|
-
|
|
101
51
|
def summary_prompt(text, max_words, target_language = "fr", context: nil, style: nil)
|
|
102
52
|
target_name = LocaleHelper.prompt_language_name(target_language)
|
|
103
53
|
|
|
@@ -168,50 +118,6 @@ module MistralTranslator
|
|
|
168
118
|
PROMPT
|
|
169
119
|
end
|
|
170
120
|
|
|
171
|
-
def tiered_summary_prompt(text, target_language, **options)
|
|
172
|
-
target_name = LocaleHelper.prompt_language_name(target_language)
|
|
173
|
-
|
|
174
|
-
context_section = build_summary_context_section(options[:context], options[:style])
|
|
175
|
-
style_instruction = build_style_instruction(options[:style])
|
|
176
|
-
|
|
177
|
-
# Extraire les valeurs pour les métadonnées
|
|
178
|
-
context = options[:context]
|
|
179
|
-
style = options[:style]
|
|
180
|
-
|
|
181
|
-
Prompt.new(instructions: <<~PROMPT, text: text)
|
|
182
|
-
Tu es un rédacteur professionnel. Crée trois résumés du texte fourni en #{target_name}.
|
|
183
|
-
#{context_section}
|
|
184
|
-
RÈGLES :
|
|
185
|
-
- Résume fidèlement sans ajouter d'informations
|
|
186
|
-
- Respecte strictement les longueurs demandées
|
|
187
|
-
- Court: #{options[:short]} mots, Moyen: #{options[:medium]} mots, Long: #{options[:long]} mots#{style_instruction}
|
|
188
|
-
- #{Prompt::DATA_RULE}
|
|
189
|
-
- Réponds UNIQUEMENT en JSON valide
|
|
190
|
-
|
|
191
|
-
FORMAT OBLIGATOIRE :
|
|
192
|
-
{
|
|
193
|
-
"content": {
|
|
194
|
-
"source": "texte original",
|
|
195
|
-
"summaries": {
|
|
196
|
-
"short": "résumé court (#{options[:short]} mots)",
|
|
197
|
-
"medium": "résumé moyen (#{options[:medium]} mots)",
|
|
198
|
-
"long": "résumé long (#{options[:long]} mots)"
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
|
-
"metadata": {
|
|
202
|
-
"source_language": "original",
|
|
203
|
-
"target_language": "#{target_language}",
|
|
204
|
-
"summaries": {
|
|
205
|
-
"short": #{options[:short]},
|
|
206
|
-
"medium": #{options[:medium]},
|
|
207
|
-
"long": #{options[:long]}
|
|
208
|
-
},
|
|
209
|
-
"operation": "tiered_summarization"#{build_summary_metadata_additions(context, style)}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
PROMPT
|
|
213
|
-
end
|
|
214
|
-
|
|
215
121
|
def language_detection_prompt(text, confidence_score: false)
|
|
216
122
|
confidence_instruction = if confidence_score
|
|
217
123
|
', "confidence": score_de_confiance_entre_0_et_1'
|
|
@@ -69,28 +69,5 @@ module MistralTranslator
|
|
|
69
69
|
HTML_INSTRUCTION
|
|
70
70
|
end
|
|
71
71
|
end
|
|
72
|
-
|
|
73
|
-
# Helper pour les instructions de formatage
|
|
74
|
-
module FormatInstructions
|
|
75
|
-
def build_json_format_instruction
|
|
76
|
-
<<~JSON_INSTRUCTION
|
|
77
|
-
|
|
78
|
-
FORMAT DE RÉPONSE : Réponds UNIQUEMENT avec un objet JSON valide.
|
|
79
|
-
Pas de texte avant ou après le JSON.
|
|
80
|
-
JSON_INSTRUCTION
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def numbered_texts(texts)
|
|
84
|
-
texts.map.with_index { |text, i| "#{i + 1}. #{text}" }.join("\n")
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def build_batch_format_instruction
|
|
88
|
-
<<~BATCH_INSTRUCTION
|
|
89
|
-
|
|
90
|
-
FORMAT DE RÉPONSE : Réponds avec un tableau JSON contenant les traductions dans l'ordre.
|
|
91
|
-
Chaque élément doit être la traduction correspondante.
|
|
92
|
-
BATCH_INSTRUCTION
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
72
|
end
|
|
96
73
|
end
|
|
@@ -32,10 +32,5 @@ module MistralTranslator
|
|
|
32
32
|
|
|
33
33
|
additions.any? ? ",\n #{additions.join(",\n ")}" : ""
|
|
34
34
|
end
|
|
35
|
-
|
|
36
|
-
def log_prompt_generation(prompt_type, source_locale, target_locale)
|
|
37
|
-
message = "Generated #{prompt_type} prompt for #{source_locale} -> #{target_locale}"
|
|
38
|
-
Logger.debug_if_verbose(message, sensitive: false)
|
|
39
|
-
end
|
|
40
35
|
end
|
|
41
36
|
end
|