jekyll-chatgpt-translate 0.0.41 → 0.0.42

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: 9285d05f0fd8813ffa969d52df39481324035a038f91a4958ec9fe5e875cb3d9
4
- data.tar.gz: 8e17f117460148801d2f8a94a7885a01853ca44e62cb38a898aed1031424c968
3
+ metadata.gz: 22b2e725c02a5de3447605daaad8d41fce5885908f59c8bc86cc676ac8d3a661
4
+ data.tar.gz: 5cb8610b0994bab60d9bf23d3aff7888f01f7ff0520e9dddc4e487e99ca94601
5
5
  SHA512:
6
- metadata.gz: 3c375ccbe265fa4d5c8e33230f4f238de544fe5a7de6fdba18eb5891b96e3b0107af5dc8bbe659c8cf8477d39df32059c031566e7b892344d6bfbef3d5373a5f
7
- data.tar.gz: a5f126c7abe7e84856963f17d24dadd6815fa3c4aa3beef989a96c6381ec5b663af0ee8ec0f93d43aad8ff7c436640bd10a26fb07473a2e99c3ed68fd6912615
6
+ metadata.gz: f1bf2b84b63f9b82ce5bc28771ab52b3571a5b4d5629fe6e10038c968bad2127407a33e9d7f1d08a7b11c247f4748021695fc569794e3f9966cca3ec30f567cf
7
+ data.tar.gz: b6385f234df97f3e6f5ea37e555667a59b68db7c4f7ec82ee26bd6e27a4e7cc3df759b7243f50fdcee424a7ecff785a989898ce0c050596a2c616a36f9d3ef55
data/.rultor.yml CHANGED
@@ -1,10 +1,10 @@
1
1
  docker:
2
- image: yegor256/rultor-image:1.21.0
2
+ image: yegor256/rultor-image:1.22.0
3
3
  assets:
4
4
  rubygems.yml: yegor256/home#assets/rubygems.yml
5
5
  install: |
6
6
  pdd -f /dev/null
7
- sudo bundle install --no-color "--gemfile=$(pwd)/Gemfile"
7
+ bundle install --no-color
8
8
  release:
9
9
  script: |-
10
10
  bundle exec rake
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
29
29
  s.required_ruby_version = '>= 3.0'
30
30
  s.name = 'jekyll-chatgpt-translate'
31
- s.version = '0.0.41'
31
+ s.version = '0.0.42'
32
32
  s.license = 'MIT'
33
33
  s.summary = 'Translate Jekyll Pages Through ChatGPT'
34
34
  s.description = [
@@ -45,12 +45,13 @@ class GptTranslate::ChatGPT
45
45
  # +key+ OpenAI API Key, which can't be nil, but can be empty string, which means dry mode (no calls to OpenAI)
46
46
  # +source+ The language to translate from
47
47
  # +target+ The language to translate into
48
- def initialize(key, model, source, target)
48
+ def initialize(key, model, source, target, client: OpenAI::Client.new(access_token: key))
49
49
  raise 'OpenAI key cannot be nil' if key.nil?
50
50
  @key = key
51
51
  @model = model
52
52
  @source = source
53
53
  @target = target
54
+ @client = client
54
55
  end
55
56
 
56
57
  def translate(markdown, min: 32, window_length: 2000)
@@ -115,9 +116,8 @@ class GptTranslate::ChatGPT
115
116
  end
116
117
 
117
118
  def translate_par(par)
118
- client = OpenAI::Client.new(access_token: @key)
119
119
  if @@models_printed
120
- Jekyll.logger.info("Available ChatGPT models: #{client.models.list['data'].map { |m| m['id'] }.join(', ')}")
120
+ Jekyll.logger.info("Available ChatGPT models: #{@client.models.list['data'].map { |m| m['id'] }.join(', ')}")
121
121
  @@models_printed = true
122
122
  end
123
123
  prompt = GptTranslate::Prompt.new(par, @source, @target).to_s
@@ -125,14 +125,15 @@ class GptTranslate::ChatGPT
125
125
  answer = nil
126
126
  attempt = 0
127
127
  begin
128
- response = client.chat(
128
+ response = @client.chat(
129
129
  parameters: {
130
130
  model: @model,
131
131
  messages: [{ role: 'user', content: prompt }],
132
132
  temperature: 0.7
133
133
  }
134
134
  )
135
- answer = JSON.parse(response).dig('choices', 0, 'message', 'content')
135
+ json = response.is_a?(Hash) ? response : JSON.parse(response)
136
+ answer = json.dig('choices', 0, 'message', 'content')
136
137
  if answer.nil?
137
138
  Jekyll.logger.error("No content returned by ChatGPT: #{response}")
138
139
  raise 'No content returned by ChatGPT'
@@ -23,5 +23,5 @@
23
23
  # SOFTWARE.
24
24
 
25
25
  module GptTranslate
26
- VERSION = '0.0.41'
26
+ VERSION = '0.0.42'
27
27
  end
data/test/test_chatgpt.rb CHANGED
@@ -109,10 +109,26 @@ class GptTranslate::ChatGPTTest < Minitest::Test
109
109
  )
110
110
  end
111
111
 
112
+ def test_with_json
113
+ client = Object.new
114
+ def client.chat(*)
115
+ { 'choices' => [{ 'message' => { 'content' => 'done!' } }] }
116
+ end
117
+ chat = GptTranslate::ChatGPT.new('fake-key', 'gpt-3.5-turbo', 'en', 'ru', client: client)
118
+ assert_equal(
119
+ "done!\n\ndone!",
120
+ chat.translate(
121
+ "This is the first paragraph\n\nThis is second\n\nThis is third",
122
+ min: 1, window_length: 4
123
+ )
124
+ )
125
+ end
126
+
112
127
  private
113
128
 
114
129
  def stub_it!
115
- stub_request(:any, 'https://api.openai.com/v1/chat/completions')
116
- .to_return(body: '{"choices":[{"message":{"content": "done!"}}]}')
130
+ stub_request(:any, 'https://api.openai.com/v1/chat/completions').to_return(
131
+ body: '{"choices":[{"message":{"content": "done!"}}]}'
132
+ )
117
133
  end
118
134
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-chatgpt-translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.41
4
+ version: 0.0.42
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -186,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  - !ruby/object:Gem::Version
187
187
  version: '0'
188
188
  requirements: []
189
- rubygems_version: 3.2.15
189
+ rubygems_version: 3.4.10
190
190
  signing_key:
191
191
  specification_version: 4
192
192
  summary: Translate Jekyll Pages Through ChatGPT