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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91c87df3579aa11f253f670664cbcf4721dc4145cb12740ab05f9f81a8fef913
4
- data.tar.gz: 590b0626a6e5dee174d34ddd429154485a7345539bc398815851ca5c4bd50ea3
3
+ metadata.gz: f98f84d08a1cbe5960d00ca2b76c1d1ce7adc930431247971694b4a93e497367
4
+ data.tar.gz: b42bc703a7bc1880a3503a1470b9f51923ce925ace01458f590e836313f80478
5
5
  SHA512:
6
- metadata.gz: '08ecd1e198abe8f8363ca6bc227a36a00335f6cc52024b7c5f6ef2013e6a0205a860bdcbc102c34b7953b0708d7f02bc7379946deb7f0b2e702314ffd3f6a143'
7
- data.tar.gz: 4e3b1a31c6112457ed53f274d32cf98c21bae53bffcc69d8b91e2ec79403790a4090fbb69a37bc4e7cdd2f30ad99a232777160e29c0944750906c4b1d477483b
6
+ metadata.gz: 2500f28ee2e4185e89b433a1265b863647a7fbbdd35c1288ecd33e939261cec5ff328875b96ddb531506462914c77bd7cd8d3db6145ebc324fbc80efa321f1a9
7
+ data.tar.gz: e5f6d3da9c17b2fd58d578aa3bd2037e213d9cf8a175df15f9d791bd24ae96e0be5eeb7432e1401d08fa94863dd6be2c94077e133e04375fd33a92fddb6b9ac6
data/.rubocop.yml CHANGED
@@ -18,3 +18,14 @@ Layout/LineLength:
18
18
  Naming/FileName:
19
19
  Exclude:
20
20
  - 'lib/i18n-ai.rb'
21
+
22
+ Metrics/BlockLength:
23
+ Exclude:
24
+ - 'i18n-ai.gemspec'
25
+
26
+ Style/Documentation:
27
+ Enabled: false
28
+
29
+ Style/BlockDelimiters:
30
+ Exclude:
31
+ - 'spec/**/*_spec.rb'
@@ -10,33 +10,29 @@ module I18nAi
10
10
  def initialize
11
11
  super
12
12
  @client = Anthropic::Client.new(
13
- access_token: @config[: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: @config[:model],
21
- messages: [{ role: "user", content: content(locale, text) }]
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 handle_error(error)
39
- puts "Error: #{error.message}"
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 content(locale, text_to_translate)
12
- # rubocop:disable Layout/LineLength
13
- "Translate the following YAML content to #{locale.to_s.upcase} and make sure to retain the keys in english except the first key which is the 2 letter language code:\n\n#{text_to_translate}"
14
- # rubocop:enable Layout/LineLength
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: @config[: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: content(locale, text) }],
23
- max_tokens: 5000
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
@@ -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
- response = translate_locales(locale, text_to_translate)
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module I18nAi
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
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.3
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-06 00:00:00.000000000 Z
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