Class: GptTranslate::ChatGPT

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-chatgpt-translate/chatgpt.rb

Overview

Abstraction of ChatGPT.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2023-2024 Yegor Bugayenko

License

MIT

Constant Summary collapse

@@models_printed =

Is TRUE if we already shown to the user the available models.

false

Instance Method Summary collapse

Constructor Details

#initialize(key, model, source, target, client: OpenAI::Client.new(access_token: key, uri_base: api_base_url)) ⇒ ChatGPT

Ctor. key OpenAI API Key, which can’t be nil, but can be empty string, which means dry mode (no calls to OpenAI) source The language to translate from target The language to translate into



48
49
50
51
52
53
54
55
# File 'lib/jekyll-chatgpt-translate/chatgpt.rb', line 48

def initialize(key, model, source, target, client: OpenAI::Client.new(access_token: key, uri_base: api_base_url))
  raise 'OpenAI key cannot be nil' if key.nil?
  @key = key
  @model = model
  @source = source
  @target = target
  @client = client
end

Instance Method Details

#api_base_urlObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jekyll-chatgpt-translate/chatgpt.rb', line 57

def api_base_url
  url = ENV.fetch('OPENAI_API_BASE', 'https://api.openai.com/')
  Jekyll.logger.info("Current OpenAI API Base URL: #{url.inspect}")
  unless url == 'https://api.openai.com/'
    Jekyll.logger.warn(
      'Warning: You\'re using a custom endpoint for the OpenAI API. ' \
      'The provider of this endpoint may have access to all details ' \
      'of your requests. Only use a custom endpoint if you trust the provider.'
    )
  end
  url
end

#translate(markdown, min: 32, window_length: 2000) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/jekyll-chatgpt-translate/chatgpt.rb', line 70

def translate(markdown, min: 32, window_length: 2000)
  pars = GptTranslate::Pars.new(markdown).to_a
  ready = []
  later = []
  pars.each_with_index do |pa, i|
    par = pa.dup
    par.strip!
    if @source == @target
      Jekyll.logger.debug("No need to translate from #{@source.inspect} to itself: #{par.inspect}")
      ready[i] = par
    elsif par.length < min
      Jekyll.logger.debug("Not translating this, b/c too short: #{par.inspect}")
      ready[i] = par
    elsif par.start_with?('```')
      Jekyll.logger.debug("Not translating this code block: #{par.inspect}")
      ready[i] = par
    elsif @key.empty?
      ready[i] = par
    elsif par.start_with?('> ')
      ready[i] = "> #{translate_par(par[2..])}"
    elsif par.start_with?('* ')
      ready[i] = "* #{translate_par(par[2..])}"
    elsif par =~ /^[0-9]+\. /
      ready[i] = "1. #{translate_par(par.split('.', 2)[1])}"
    elsif par =~ /^[^\p{Alnum}\*'"\[]/
      Jekyll.logger.debug("Not translating this, b/c it's not a plain text: #{par.inspect}")
      ready[i] = par
    else
      later[i] = par
    end
  end
  out = []
  i = 0
  while i < pars.length
    unless ready[i].nil?
      out << ready[i]
      i += 1
      next
    end
    accum = []
    until later[i].nil?
      already = Tiktoken.encoding_for_model('gpt-4').encode(accum.join).length
      if already > window_length
        Jekyll.logger.debug("Already #{already} words, over the window_length of #{window_length}")
        break
      end
      accum << later[i]
      i += 1
    end
    out << translate_pars(accum)
    i += 1
  end
  out.join("\n\n")
end