i18n-ai 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +11 -0
- data/lib/i18n_ai/clients/anthropic_client.rb +6 -10
- data/lib/i18n_ai/clients/base_client.rb +35 -4
- data/lib/i18n_ai/clients/open_ai_client.rb +8 -7
- data/lib/i18n_ai/railtie.rb +3 -13
- data/lib/i18n_ai/version.rb +1 -1
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f98f84d08a1cbe5960d00ca2b76c1d1ce7adc930431247971694b4a93e497367
|
4
|
+
data.tar.gz: b42bc703a7bc1880a3503a1470b9f51923ce925ace01458f590e836313f80478
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2500f28ee2e4185e89b433a1265b863647a7fbbdd35c1288ecd33e939261cec5ff328875b96ddb531506462914c77bd7cd8d3db6145ebc324fbc80efa321f1a9
|
7
|
+
data.tar.gz: e5f6d3da9c17b2fd58d578aa3bd2037e213d9cf8a175df15f9d791bd24ae96e0be5eeb7432e1401d08fa94863dd6be2c94077e133e04375fd33a92fddb6b9ac6
|
data/.rubocop.yml
CHANGED
@@ -10,33 +10,29 @@ module I18nAi
|
|
10
10
|
def initialize
|
11
11
|
super
|
12
12
|
@client = Anthropic::Client.new(
|
13
|
-
access_token:
|
13
|
+
access_token: config[:access_token]
|
14
14
|
)
|
15
15
|
end
|
16
16
|
|
17
17
|
def chat(locale, text)
|
18
18
|
response = @client.messages(
|
19
19
|
parameters: {
|
20
|
-
model:
|
21
|
-
messages: [{ role: "user", content:
|
20
|
+
model: config[:model],
|
21
|
+
messages: [{ role: "user", content: chat_prompt(locale, text) }],
|
22
|
+
max_tokens: 4096
|
22
23
|
}
|
23
24
|
)
|
24
|
-
|
25
25
|
parse_response(response)
|
26
|
-
rescue StandardError => e
|
27
|
-
handle_error(e)
|
28
26
|
end
|
29
27
|
|
30
28
|
private
|
31
29
|
|
32
30
|
def parse_response(response)
|
33
31
|
response.dig("content", 0, "text")
|
34
|
-
rescue TypeError, NoMethodError => e
|
35
|
-
handle_error(e)
|
36
32
|
end
|
37
33
|
|
38
|
-
def
|
39
|
-
|
34
|
+
def extract_translated_content(chat_content)
|
35
|
+
chat_content
|
40
36
|
end
|
41
37
|
end
|
42
38
|
end
|
@@ -4,19 +4,50 @@ module I18nAi
|
|
4
4
|
module Clients
|
5
5
|
# The BaseClient class serves as a base class for all AI client implementations
|
6
6
|
class BaseClient
|
7
|
+
attr_reader :config
|
8
|
+
|
7
9
|
def initialize
|
8
10
|
@config = I18nAi.configuration.ai_settings
|
9
11
|
end
|
10
12
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def translate_content(locale, content)
|
14
|
+
chat_content = chat(locale, content)
|
15
|
+
extract_translated_content(chat_content)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def chat_prompt(locale, text_to_translate)
|
21
|
+
<<~PROMPT
|
22
|
+
Translate the following YAML content to the language of the country using the ISO 639 language code
|
23
|
+
of #{locale}. Make sure to retain the keys in english except the first key which is the 2 letter language code:
|
24
|
+
|
25
|
+
""""
|
26
|
+
#{text_to_translate}"
|
27
|
+
""""
|
28
|
+
|
29
|
+
Return only the YAML content without explanation.
|
30
|
+
|
31
|
+
Example Return YAML:
|
32
|
+
|
33
|
+
"""
|
34
|
+
{{ISO_639 language code}}:
|
35
|
+
key_1: "value1"
|
36
|
+
"""
|
37
|
+
PROMPT
|
38
|
+
end
|
39
|
+
|
40
|
+
def chat(locale, text)
|
41
|
+
raise NotImplementedError, "Subclasses must implement this method"
|
15
42
|
end
|
16
43
|
|
17
44
|
def parse_response(response)
|
18
45
|
raise NotImplementedError, "Subclasses must implement this method"
|
19
46
|
end
|
47
|
+
|
48
|
+
def extract_translated_content(chat_content)
|
49
|
+
raise NotImplementedError, "Subclasses must implement this method"
|
50
|
+
end
|
20
51
|
end
|
21
52
|
end
|
22
53
|
end
|
@@ -5,12 +5,11 @@ require_relative "base_client"
|
|
5
5
|
|
6
6
|
module I18nAi
|
7
7
|
module Clients
|
8
|
-
# The AnthropicClient class is responsible for interacting with the OpenAI API
|
9
8
|
class OpenAiClient < BaseClient
|
10
9
|
def initialize
|
11
10
|
super
|
12
11
|
@client = OpenAI::Client.new(
|
13
|
-
access_token:
|
12
|
+
access_token: config[:access_token],
|
14
13
|
log_errors: true
|
15
14
|
)
|
16
15
|
end
|
@@ -19,14 +18,11 @@ module I18nAi
|
|
19
18
|
response = @client.chat(
|
20
19
|
parameters: {
|
21
20
|
model: @config[:model],
|
22
|
-
messages: [{ role: "user", content:
|
23
|
-
max_tokens:
|
21
|
+
messages: [{ role: "user", content: chat_prompt(locale, text) }],
|
22
|
+
max_tokens: 4096
|
24
23
|
}
|
25
24
|
)
|
26
|
-
|
27
25
|
parse_response(response)
|
28
|
-
rescue StandardError => e
|
29
|
-
handle_error(e)
|
30
26
|
end
|
31
27
|
|
32
28
|
private
|
@@ -40,6 +36,11 @@ module I18nAi
|
|
40
36
|
def handle_error(error)
|
41
37
|
puts "Error: #{error.message}"
|
42
38
|
end
|
39
|
+
|
40
|
+
def extract_translated_content(chat_content)
|
41
|
+
match_data = chat_content.match(/```yaml(.*?)```/m)
|
42
|
+
match_data ? match_data[1].strip : nil
|
43
|
+
end
|
43
44
|
end
|
44
45
|
end
|
45
46
|
end
|
data/lib/i18n_ai/railtie.rb
CHANGED
@@ -11,6 +11,8 @@ module I18nAi
|
|
11
11
|
class Railtie < Rails::Railtie
|
12
12
|
# Initialize the I18nAi middleware
|
13
13
|
class I18nAiMiddleware
|
14
|
+
attr_reader :client
|
15
|
+
|
14
16
|
def initialize(app)
|
15
17
|
@app = app
|
16
18
|
@client = configure_client
|
@@ -83,10 +85,7 @@ module I18nAi
|
|
83
85
|
generate_locales = I18nAi.configuration.generate_locales
|
84
86
|
|
85
87
|
generate_locales.each do |locale|
|
86
|
-
|
87
|
-
next unless response
|
88
|
-
|
89
|
-
translated_content = extract_translated_content(response)
|
88
|
+
translated_content = client.translate_content(locale, text_to_translate)
|
90
89
|
save_translated_locales(locale, translated_content) if translated_content
|
91
90
|
end
|
92
91
|
end
|
@@ -95,15 +94,6 @@ module I18nAi
|
|
95
94
|
YAML.load_file(locales_file)
|
96
95
|
end
|
97
96
|
|
98
|
-
def translate_locales(locale, text_to_translate)
|
99
|
-
@client.chat(locale, text_to_translate)
|
100
|
-
end
|
101
|
-
|
102
|
-
def extract_translated_content(response)
|
103
|
-
match_data = response.match(/```yaml(.*?)```/m)
|
104
|
-
match_data ? match_data[1].strip : nil
|
105
|
-
end
|
106
|
-
|
107
97
|
def save_translated_locales(locale, translated_content)
|
108
98
|
locales_file = Rails.root.join("config", "locales", "#{locale}.yml")
|
109
99
|
File.write(locales_file, translated_content)
|
data/lib/i18n_ai/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n-ai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- William Estoque
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-08-
|
11
|
+
date: 2024-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -58,6 +58,34 @@ dependencies:
|
|
58
58
|
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: vcr
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: webmock
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
61
89
|
description:
|
62
90
|
email:
|
63
91
|
- william.estoque@gmail.com
|