Class: BetterTranslate::Providers::ChatGPTProvider

Inherits:
BaseHttpProvider show all
Defined in:
lib/better_translate/providers/chatgpt_provider.rb

Overview

OpenAI ChatGPT translation provider

Uses GPT-5-nano model with temperature=1.0 for natural translations.

Examples:

Basic usage

config = Configuration.new
config.openai_key = ENV['OPENAI_API_KEY']
provider = ChatGPTProvider.new(config)
result = provider.translate_text("Hello", "it", "Italian")
#=> "Ciao"

Constant Summary collapse

API_URL =

OpenAI API endpoint

"https://api.openai.com/v1/chat/completions"
MODEL =

Model to use for translations

"gpt-5-nano"
TEMPERATURE =

Temperature setting for creativity

1.0

Instance Attribute Summary

Attributes inherited from BaseHttpProvider

#cache, #config, #rate_limiter

Instance Method Summary collapse

Methods inherited from BaseHttpProvider

#build_cache_key, #calculate_backoff, #handle_response, #http_client, #initialize, #log_retry, #make_request, #with_cache

Constructor Details

This class inherits a constructor from BetterTranslate::Providers::BaseHttpProvider

Instance Method Details

#build_messages(text, target_lang_name) ⇒ Array<Hash> (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build messages array for ChatGPT API



81
82
83
84
85
86
87
88
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 81

def build_messages(text, target_lang_name)
  system_message = build_system_message(target_lang_name)

  [
    { role: "system", content: system_message },
    { role: "user", content: text }
  ]
end

#build_system_message(target_lang_name) ⇒ String (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build system message with optional context



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 96

def build_system_message(target_lang_name)
  base_message = "You are a professional translator. Translate the following text to #{target_lang_name}. " \
                 "Return ONLY the translated text, without any explanations or additional text. " \
                 "Words like VARIABLE_0, VARIABLE_1, etc. are placeholders and must be kept unchanged in the translation."

  if config.translation_context && !config.translation_context.empty?
    base_message += "\n\nContext: #{config.translation_context}"
  end

  base_message
end

#extract_translation(response) ⇒ String (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract translation from API response

Raises:



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 136

def extract_translation(response)
  parsed = JSON.parse(response.body)
  translation = parsed.dig("choices", 0, "message", "content")

  raise TranslationError, "No translation in response" if translation.nil? || translation.empty?

  translation.strip
rescue JSON::ParserError => e
  raise TranslationError.new(
    "Failed to parse ChatGPT response",
    context: { error: e.message, body: response.body }
  )
end

#make_chat_completion_request(messages) ⇒ Faraday::Response (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Make chat completion request to OpenAI API



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 114

def make_chat_completion_request(messages)
  body = {
    model: MODEL,
    messages: messages,
    temperature: TEMPERATURE
  }

  headers = {
    "Content-Type" => "application/json",
    "Authorization" => "Bearer #{config.openai_key}"
  }

  make_request(:post, API_URL, body: body, headers: headers)
end

#translate_batch(texts, target_lang_code, target_lang_name) ⇒ Array<String>

Translate multiple texts in a batch

Examples:

provider.translate_batch(["Hello", "World"], "it", "Italian")
#=> ["Ciao", "Mondo"]


68
69
70
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 68

def translate_batch(texts, target_lang_code, target_lang_name)
  texts.map { |text| translate_text(text, target_lang_code, target_lang_name) }
end

#translate_text(text, target_lang_code, target_lang_name) ⇒ String

Translate a single text

Examples:

provider.translate_text("Hello world", "it", "Italian")
#=> "Ciao mondo"

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/better_translate/providers/chatgpt_provider.rb', line 39

def translate_text(text, target_lang_code, target_lang_name)
  Validator.validate_text!(text)
  Validator.validate_language_code!(target_lang_code)

  cache_key = build_cache_key(text, target_lang_code)

  with_cache(cache_key) do
    messages = build_messages(text, target_lang_name)
    response = make_chat_completion_request(messages)
    extract_translation(response)
  end
rescue ApiError => e
  raise TranslationError.new(
    "Failed to translate text with ChatGPT: #{e.message}",
    context: { text: text, target_lang: target_lang_code, original_error: e }
  )
end