immosquare-translate 0.1.3 → 0.1.5

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: 2cf94a139c881e315445fb0df7f24699166c1833b2b5f0746b86c91f5025ba5c
4
- data.tar.gz: b518012e6deafacb308f178c1c21ab0191c2e491d47f0bda97e976605155a37f
3
+ metadata.gz: 9daf467e21100501ea1833692e80742d341950fb05f1a451b25134a83e7ba7e1
4
+ data.tar.gz: 7fe077c183fb8beee8d917abad7d014eafd811ef300574cfc776427efad51f0e
5
5
  SHA512:
6
- metadata.gz: 1f96fdafd54a457e86ee7d8b0475807e4f8308ff5e8080a08ca6df369a1c4d515dc45ec90a240d4a4143a531bdaaaf51881532313252054e133b5fd0d33281ae
7
- data.tar.gz: 715d03787f074bff87e69039ddeb466f9cbe449e1e57021ea9a3587fc272cc71d8f431e6f74aa75c33cb9accab2976e559d4ad885989b6d6c2ceab21637208e3
6
+ metadata.gz: 5758dc40a24ab4721d4ec72184d181567e96949b03aeb71e8bbdd14ffc107f0def5197719bf069ab65486fb65876223029d67f5f82de12a5ee96391c5dd61b97
7
+ data.tar.gz: 8ade76c458bb26cec34a1c369703b6e1141a10fc99fbb56774e423ea3ccb95658c82863c7c86cb9169d3fd82e2434cafeb200390f2b7d283a391de44c574e868
@@ -20,27 +20,34 @@ module ImmosquareTranslate
20
20
  model = OPEN_AI_MODELS.find {|m| m[:name] == model_name }
21
21
  model = OPEN_AI_MODELS.find {|m| m[:name] == "gpt-4-0125-preview" } if model.nil?
22
22
  from_iso = ISO_639.find_by_code(from).english_name.split(";").first
23
- to_iso = to.map {|l| ISO_639.find_by_code(l).english_name.split(";").first }
23
+ to_iso = to.map {|iso| [iso, ISO_639.find_by_code(iso).english_name.split(";").first] }
24
24
  headers = {
25
25
  "Content-Type" => "application/json",
26
26
  "Authorization" => "Bearer #{ImmosquareTranslate.configuration.openai_api_key}"
27
27
  }
28
28
 
29
- prompt_system = "As a sophisticated translation AI, your role is to translate sentences from a specified source language to multiple target languages. " \
30
- "It is imperative that you return the translations in a single, pure JSON string format. Use ISO 639-1 language codes for specifying languages. " \
31
- "if string is html, you should return the translated html." \
29
+ prompt_system = "As a sophisticated translation AI, your role is to translate sentences from a specified source language to multiple target languages.\n" \
30
+ "It is imperative that you return the translations in a single, pure JSON string format. Use ISO 639-1 language codes for specifying languages.\n" \
31
+ "If string is html, you should return the translated html.\n" \
32
32
  "Ensure that the output does not include markdown (```json) or any other formatting characters. Adhere to the JSON structure meticulously."
33
33
 
34
34
 
35
- prompt = "Translate the following sentences from '#{from_iso}' into the languages #{to_iso.join(", ")}, and format the output as a single, pure JSON string. " \
36
- "Follow the structure: {\"datas\":[{\"en\":\"English Translation\",\"es\":\"Spanish Translation\",\"it\":\"Italian Translation\"}]}, using the correct ISO 639-1 language codes for each translation. " \
37
- "Your response should strictly conform to this JSON structure without any additional characters or formatting. Sentences to translate are:"
35
+
36
+ prompt = "Translate the #{text.size} #{text.size == 1 ? "sentence" : "sentences"} provided below from the source language (ISO 639-1 code: #{from}) to the target languages specified: #{to_iso.map {|iso, language| "#{language} (ISO 639-1 code: #{iso})" }.join(", ")}. " \
37
+ "Before translating, correct any spelling or grammatical errors in each sentence. " \
38
+ "If the source language is also a target language, include the corrected version of the sentence for that language as well. " \
39
+ "Format the translation output as a JSON string adhering to the following structure: " \
40
+ "{\"datas\":[{\"locale_iso\": \"Translated Text\"}]}, where 'locale_iso' is replaced with the appropriate ISO 639-1 code for each translation. " \
41
+ "Ensure the output strictly follows this JSON format, without any extraneous characters or formatting. " \
42
+ "Sentences to translate:"
43
+
38
44
 
39
45
  text.each_with_index do |sentence, index|
40
46
  prompt += "\n#{index + 1}: #{sentence}"
41
47
  end
42
48
 
43
49
 
50
+
44
51
  body = {
45
52
  :model => model[:name],
46
53
  :messages => [
@@ -74,8 +81,15 @@ module ImmosquareTranslate
74
81
  puts("Estimate price => #{input_price.round(3)} + #{output_price.round(3)} = #{price.round(3)} USD")
75
82
 
76
83
 
77
- p = JSON.parse(choice["message"]["content"])
78
- p["datas"]
84
+ ##============================================================##
85
+ ## On s'assure de ne renvoyer que les locales demandées
86
+ ## car l'API peut renvoyer des locales non demandées...
87
+ ##============================================================##
88
+ content = JSON.parse(choice["message"]["content"])
89
+ datas = content["datas"]
90
+ datas.map do |hash|
91
+ hash.select {|key, _| to.include?(key) }
92
+ end.reject(&:empty?)
79
93
  rescue StandardError => e
80
94
  puts(e.message)
81
95
  puts(e.backtrace)
@@ -1,3 +1,3 @@
1
1
  module ImmosquareTranslate
2
- VERSION = "0.1.3".freeze
2
+ VERSION = "0.1.5".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - IMMO SQUARE
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-31 00:00:00.000000000 Z
11
+ date: 2024-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty