i18n-ai 0.1.0 → 0.1.1

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: 06e2c7c1e6b90add5766612403fefc33653d55b98e2a49b6ba0920a736545788
4
- data.tar.gz: 19c739baa0b768bb12496cbb43dc2e58eb78a6afe629ab2db11e53dcfe5e94b0
3
+ metadata.gz: a4b8bb0bd9755345297a83d3c236a95fa9d68c1b432743f08cb48b9bc38fa03b
4
+ data.tar.gz: 62f2a9fc7281367224041c3222191e345c15848757ee6e6cfc4905de2dc4156f
5
5
  SHA512:
6
- metadata.gz: a3abb0c81d6716e37df291fa0ef3b2b3853d97696de0247e0677b453cb781d154751b4dd8c4577596ef2e02ef35630a42956189547df3a046e556c85fc062764
7
- data.tar.gz: 971b820565e02c742653a423e5eef1f8f6b4111cb3d9d2e535b0752fceb4c1996fc5e09338d866d7a19c2a9e9bd95299c110e40f8f6d0d17a2ffd26ef1c3154f
6
+ metadata.gz: 2a72a92c4f61419e2a76e9ad7df7086168128d24c5d6eafe9be395be4db948e6e6d06937d2db8d11356a85f295c53cf46952fb09eb1c7171eacfe4aa4e009d89
7
+ data.tar.gz: 45997aa4a669cc795ee91841a6de94974ba782387ad1e55384700d3a407749b097079abe77aec6c2f505a42bf31adfdb01a145e8cc24dab52b3a7089b860044c
data/README.md CHANGED
@@ -14,19 +14,47 @@ gem "i18n-ai"
14
14
 
15
15
  And do `bundle install`.
16
16
 
17
- ## Usage
17
+ ## Configuration
18
18
 
19
- First you need to set the environment variable `ENV["OPENAI_ACCESS_TOKEN"]`.
19
+ To use I18nAi, you need to set the appropriate environment variables based on the AI service you wish to use.
20
20
 
21
- To configure and enable other locales, create a file `config/initializers/i18n_ai.rb` and add the following:
21
+ ### OpenAi
22
+
23
+ If you are using OpenAI, set `ENV["OPENAI_ACCESS_TOKEN"]`
24
+
25
+ To configure and enable other locales, create a file `config/initializers/i18n_aii.rb` and add the following:
22
26
 
23
27
  ```
24
28
  # config/initializers/i18n_ai.rb
25
29
  I18nAi.configure do |config|
30
+ config.ai_settings = {
31
+ provider: "openai",
32
+ model: "gpt-4o-mini",
33
+ access_token: ENV["OPENAI_ACCESS_TOKEN"]
34
+ }
26
35
  config.generate_locales = [:es, :it] # add your other supported locales to this array
27
36
  end
28
37
  ```
29
38
 
39
+ ### Anthropic's Claude
40
+
41
+ If you prefer to use Anthropic's Claude, set `ENV[ANTHROPIC_API_KEY]`
42
+
43
+ To configure and enable other locales, create a file `config/initializers/i18n_ai.rb` and add the following:
44
+
45
+ ```
46
+ I18nAi.configure do |config|
47
+ config.ai_settings = {
48
+ provider: "anthropic",
49
+ model: "claude-3-haiku-20240307",
50
+ access_token: ENV["ANTHROPIC_ACCESS_TOKEN"]
51
+ }
52
+ config.generate_locales = [:es]
53
+ end
54
+ ```
55
+
56
+ ## Usage
57
+
30
58
  Every page reload, the gem will check if the `en.yml` file changed and if it did, it will automatically generate the configured locale files.
31
59
 
32
60
  The gem has been setup to generate a `es.yml` file by default.
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "anthropic"
4
+ require_relative "base_client"
5
+
6
+ module I18nAi
7
+ module Clients
8
+ class AnthropicClient < BaseClient
9
+ def initialize
10
+ super
11
+ @client = Anthropic::Client.new(
12
+ access_token: @config[:access_token]
13
+ )
14
+ end
15
+
16
+ def chat(locale, text)
17
+ response = @client.messages(
18
+ parameters: {
19
+ model: @config[:model],
20
+ messages: [
21
+ { role: "user", content: content(locale, text) }
22
+ ]
23
+ }
24
+ )
25
+
26
+ parse_response(response)
27
+ rescue StandardError => e
28
+ handle_error(e)
29
+ end
30
+
31
+ private
32
+
33
+ def parse_response(response)
34
+ response.dig("content", 0, "text")
35
+ rescue TypeError, NoMethodError => e
36
+ handle_error(e)
37
+ end
38
+
39
+ def handle_error(error)
40
+ puts "Error: #{error.message}"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nAi
4
+ module Clients
5
+ class BaseClient
6
+ def initialize
7
+ @config = I18nAi.configuration.ai_settings
8
+ end
9
+
10
+ def content(locale, text_to_translate)
11
+ "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}"
12
+ end
13
+
14
+ def parse_response(response)
15
+ raise NotImplementedError, "Subclasses must implement this method"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openai"
4
+ require_relative "base_client"
5
+
6
+ module I18nAi
7
+ module Clients
8
+ class OpenAiClient < BaseClient
9
+ def initialize
10
+ super
11
+ @client = OpenAI::Client.new(
12
+ access_token: @config[:access_token],
13
+ log_errors: true
14
+ )
15
+ end
16
+
17
+ def chat(locale, text)
18
+ response = @client.chat(
19
+ parameters: {
20
+ model: @config[:model],
21
+ messages: [{ role: "user", content: content(locale, text) }],
22
+ max_tokens: 5000
23
+ }
24
+ )
25
+
26
+ parse_response(response)
27
+ rescue StandardError => e
28
+ handle_error(e)
29
+ end
30
+
31
+ private
32
+
33
+ def parse_response(response)
34
+ response.dig("choices", 0, "message", "content")
35
+ rescue TypeError, NoMethodError => e
36
+ handle_error(e)
37
+ end
38
+
39
+ def handle_error(error)
40
+ puts "Error: #{error.message}"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,8 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module I18nAi
2
4
  class Configuration
3
- attr_accessor :generate_locales
5
+ attr_accessor :ai_settings, :generate_locales
4
6
 
5
7
  def initialize
8
+ @ai_settings = {}
6
9
  @generate_locales = [:es]
7
10
  end
8
11
  end
@@ -1,16 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/railtie"
2
- require "openai"
3
4
  require "digest"
4
5
 
6
+ require_relative "clients/open_ai_client"
7
+ require_relative "clients/anthropic_client"
8
+
5
9
  module I18nAi
6
10
  class Railtie < Rails::Railtie
7
11
  class I18nAiMiddleware
8
12
  def initialize(app)
9
13
  @app = app
10
- @client = OpenAI::Client.new(
11
- access_token: ENV.fetch("OPENAI_ACCESS_TOKEN"),
12
- log_errors: true
13
- )
14
+ @client = configure_client
14
15
  @last_checksum = nil
15
16
  end
16
17
 
@@ -37,6 +38,18 @@ module I18nAi
37
38
 
38
39
  private
39
40
 
41
+ def configure_client
42
+ config = I18nAi.configuration.ai_settings
43
+ case config[:provider]
44
+ when "anthropic"
45
+ I18nAi::Clients::AnthropicClient.new
46
+ when "openai"
47
+ I18nAi::Clients::OpenAiClient.new
48
+ else
49
+ raise "Unknown AI provider: #{config[:provider]}"
50
+ end
51
+ end
52
+
40
53
  def generate_translations(locales_file)
41
54
  locales = YAML.load_file(locales_file)
42
55
  text_to_translate = locales.to_yaml
@@ -45,16 +58,12 @@ module I18nAi
45
58
  generate_locales.each do |locale|
46
59
  # Make a request to OpenAI to translate the locales to the specified locale
47
60
  response = @client.chat(
48
- parameters: {
49
- model: "gpt-4o-mini",
50
- messages: [{ role: "user",
51
- content: "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}" }],
52
- max_tokens: 5000
53
- }
61
+ locale, text_to_translate
54
62
  )
55
63
 
56
- translated_text = response["choices"][0]["message"]["content"]
57
- match_data = translated_text.match(/```yaml(.*?)```/m)
64
+ next unless response
65
+
66
+ match_data = response.match(/```yaml(.*?)```/m)
58
67
  str = match_data ? match_data[1].strip : nil
59
68
 
60
69
  # Save the response to <locale>.yml
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module I18nAi
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/i18n-ai.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "i18n-ai/version"
2
4
  require_relative "i18n-ai/railtie"
3
5
  require_relative "i18n-ai/configuration"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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-01 00:00:00.000000000 Z
11
+ date: 2024-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: anthropic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: ruby-openai
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,9 @@ files:
38
52
  - README.md
39
53
  - Rakefile
40
54
  - lib/i18n-ai.rb
55
+ - lib/i18n-ai/clients/anthropic_client.rb
56
+ - lib/i18n-ai/clients/base_client.rb
57
+ - lib/i18n-ai/clients/open_ai_client.rb
41
58
  - lib/i18n-ai/configuration.rb
42
59
  - lib/i18n-ai/railtie.rb
43
60
  - lib/i18n-ai/version.rb