nblog_duo 0.0.93 → 0.0.95
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/nblog_duo.rb +0 -240
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f88597202922bff68a8c8fd9f4e0691af3c1f62284f2b780a2923a12d6d75eaa
|
4
|
+
data.tar.gz: 37748fb4d77fb92f93038d3dfcb885d89e0503017fddba7b400182140ebd1e8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05c6f3cf39bda7d39b293b78a001288214db0819072866148060cbe243afd50cb0f062edbdc109da974f6cc172b8153bde416bdb2c542eb74acc2b975d955282
|
7
|
+
data.tar.gz: 3871676d62236637acdddc1000e10710a05c51b85f86a7a6a71bb9677e5f2095b6349fa9b9cccb4e50302c4776d060b1e1ce5f93bf25316d25f37a8cfece9aa0
|
data/lib/nblog_duo.rb
CHANGED
@@ -108,246 +108,6 @@ end
|
|
108
108
|
|
109
109
|
|
110
110
|
|
111
|
-
class Chat_title
|
112
|
-
def initialize(api_key, gpt_title_prompt)
|
113
|
-
@api_key = api_key
|
114
|
-
@gpt_title_prompt = gpt_title_prompt
|
115
|
-
end
|
116
|
-
|
117
|
-
def message(title)
|
118
|
-
puts 'Sending request to GPT...(제목 생성 중...)'
|
119
|
-
# "키워드 기반 글 생성 중..." 메시지를 별도 스레드로 처리
|
120
|
-
thread = Thread.new do
|
121
|
-
while true
|
122
|
-
print "▶"
|
123
|
-
sleep(3)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
url = 'https://api.openai.com/v1/chat/completions'
|
127
|
-
headers = {
|
128
|
-
'Content-Type' => 'application/json',
|
129
|
-
'Authorization' => 'Bearer ' + @api_key
|
130
|
-
}
|
131
|
-
data = {
|
132
|
-
'model' => 'gpt-4',
|
133
|
-
'messages' => [{
|
134
|
-
"role" => "system",
|
135
|
-
"content" => "너는 매우 친절하고 성의 있게 답변하는 AI 어시스턴트야."
|
136
|
-
},
|
137
|
-
{
|
138
|
-
"role" => "user",
|
139
|
-
"content" => "#{@gpt_title_prompt}\n#{title}"
|
140
|
-
}]
|
141
|
-
}
|
142
|
-
|
143
|
-
begin
|
144
|
-
req = HTTP.headers(headers).post(url, json: data)
|
145
|
-
|
146
|
-
response = JSON.parse(req.body.to_s)
|
147
|
-
|
148
|
-
|
149
|
-
if req.status == 429
|
150
|
-
return "API 요청 제한을 초과했습니다. 플랜 및 할당량을 확인하세요."
|
151
|
-
end
|
152
|
-
|
153
|
-
# 응답 데이터에서 안전하게 값 추출
|
154
|
-
answer = response.dig('choices', 0, 'message', 'content')
|
155
|
-
|
156
|
-
# 따옴표 제거
|
157
|
-
answer = answer.gsub('"', '') if answer
|
158
|
-
|
159
|
-
answer ||= title # 응답이 없을 경우 기본 메시지 설정
|
160
|
-
rescue => e
|
161
|
-
puts "Error: #{e.message}"
|
162
|
-
answer = "오류가 발생했습니다."
|
163
|
-
end
|
164
|
-
|
165
|
-
# "생성 중..." 메시지 출력 종료
|
166
|
-
thread.kill
|
167
|
-
|
168
|
-
puts 'API return ==> '
|
169
|
-
puts answer
|
170
|
-
answer
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
|
175
|
-
class Chat_content
|
176
|
-
def initialize(api_key, gpt_content_prompt)
|
177
|
-
@api_key = api_key
|
178
|
-
@gpt_content_prompt = gpt_content_prompt
|
179
|
-
end
|
180
|
-
|
181
|
-
def message(content)
|
182
|
-
puts 'Sending request to GPT...(내용 변형 중...)'
|
183
|
-
puts '주의:GPT 특성상 원고 길이가 공백 포함 4천자를 넘기면 오류가 발생할 수 있습니다.'
|
184
|
-
# "키워드 기반 글 생성 중..." 메시지를 별도 스레드로 처리
|
185
|
-
thread = Thread.new do
|
186
|
-
while true
|
187
|
-
print "▶"
|
188
|
-
sleep(3)
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
url = 'https://api.openai.com/v1/chat/completions'
|
193
|
-
headers = {
|
194
|
-
'Content-Type' => 'application/json',
|
195
|
-
'Authorization' => 'Bearer ' + @api_key
|
196
|
-
}
|
197
|
-
data = {
|
198
|
-
'model' => 'gpt-4',
|
199
|
-
'messages' => [{
|
200
|
-
"role" => "system",
|
201
|
-
"content" => "너는 매우 친절하고 성의 있게 답변하는 AI 어시스턴트야."
|
202
|
-
},
|
203
|
-
{
|
204
|
-
"role" => "user",
|
205
|
-
"content" => "#{@gpt_content_prompt}\n#{content}"
|
206
|
-
|
207
|
-
}]
|
208
|
-
}
|
209
|
-
|
210
|
-
begin
|
211
|
-
req = HTTP.headers(headers).post(url, json: data)
|
212
|
-
|
213
|
-
response = JSON.parse(req.body.to_s)
|
214
|
-
|
215
|
-
|
216
|
-
if req.status == 429
|
217
|
-
return "API 요청 제한을 초과했습니다. 플랜 및 할당량을 확인하세요."
|
218
|
-
end
|
219
|
-
|
220
|
-
# 응답 데이터에서 안전하게 값 추출
|
221
|
-
answer = response.dig('choices', 0, 'message', 'content')
|
222
|
-
answer ||= (content) # 응답이 없을 경우 기본 메시지 설정
|
223
|
-
rescue => e
|
224
|
-
puts "Error: #{e.message}"
|
225
|
-
answer = "오류가 발생했습니다."
|
226
|
-
end
|
227
|
-
|
228
|
-
# "생성 중..." 메시지 출력 종료
|
229
|
-
thread.kill
|
230
|
-
|
231
|
-
puts 'API return ==> '
|
232
|
-
puts answer
|
233
|
-
answer
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
#############################################gpt############################################
|
240
|
-
|
241
|
-
require 'glimmer-dsl-libui'
|
242
|
-
require 'selenium-webdriver'
|
243
|
-
# require 'webdrivers'
|
244
|
-
require 'iconv'
|
245
|
-
require 'nokogiri'
|
246
|
-
require 'http'
|
247
|
-
require 'json'
|
248
|
-
require 'down'
|
249
|
-
require 'rmagick'
|
250
|
-
require 'fileutils'
|
251
|
-
require 'rest-client'
|
252
|
-
require 'open3'
|
253
|
-
require 'clipboard'
|
254
|
-
require 'crack'
|
255
|
-
require 'uri'
|
256
|
-
require 'cgi'
|
257
|
-
require 'digest'
|
258
|
-
require 'auto_click'
|
259
|
-
require 'rainbow/refinement'
|
260
|
-
include AutoClickMethods
|
261
|
-
using Rainbow
|
262
|
-
include Glimmer
|
263
|
-
|
264
|
-
class Chat
|
265
|
-
def initialize(api_key, gpt_keyword_prompt)
|
266
|
-
@api_key = api_key
|
267
|
-
@gpt_keyword_prompt = gpt_keyword_prompt
|
268
|
-
end
|
269
|
-
|
270
|
-
def message(keyword)
|
271
|
-
puts 'Sending request to GPT...(키워드 기반 글 생성 중...)'
|
272
|
-
|
273
|
-
# "키워드 기반 글 생성 중..." 메시지 출력 스레드
|
274
|
-
thread = Thread.new do
|
275
|
-
while true
|
276
|
-
print "▶"
|
277
|
-
sleep 3
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
url = 'https://api.openai.com/v1/chat/completions'
|
282
|
-
headers = {
|
283
|
-
'Content-Type' => 'application/json',
|
284
|
-
'Authorization' => 'Bearer ' + @api_key
|
285
|
-
}
|
286
|
-
|
287
|
-
# 사용자로부터 받은 입력과 GPT 프롬프트의 토큰 수 계산
|
288
|
-
message_tokens = calculate_tokens(keyword) + calculate_tokens(@gpt_keyword_prompt)
|
289
|
-
|
290
|
-
# 8,192 토큰을 초과하지 않도록 최대 토큰 수를 설정
|
291
|
-
max_response_tokens = [8192 - message_tokens, 4000].min # 8,192 - 입력된 토큰 수, 4,000 이하로 설정
|
292
|
-
|
293
|
-
# 요청 데이터 설정
|
294
|
-
data = {
|
295
|
-
'model' => 'gpt-4',
|
296
|
-
'messages' => [
|
297
|
-
{
|
298
|
-
"role" => "assistant",
|
299
|
-
"content" => "#{keyword}\n#{@gpt_keyword_prompt}"
|
300
|
-
}
|
301
|
-
],
|
302
|
-
'max_tokens' => max_response_tokens # 최대 응답 토큰 설정
|
303
|
-
}
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
answer = ''
|
308
|
-
begin
|
309
|
-
req = HTTP.headers(headers).post(url, :json => data)
|
310
|
-
|
311
|
-
# 상태 코드 확인
|
312
|
-
if req.status != 200
|
313
|
-
raise "HTTP Error: #{req.status}, Response Body: #{req.body.to_s}"
|
314
|
-
end
|
315
|
-
|
316
|
-
# 응답 내용 출력 (디버깅용)
|
317
|
-
response = JSON.parse(req.to_s)
|
318
|
-
|
319
|
-
|
320
|
-
# 응답 데이터에서 안전하게 값 추출
|
321
|
-
if response['choices'] && response['choices'][0] && response['choices'][0]['message']
|
322
|
-
answer = response['choices'][0]['message']['content']
|
323
|
-
else
|
324
|
-
raise "Invalid API response format"
|
325
|
-
end
|
326
|
-
rescue => e
|
327
|
-
# 오류 메시지 출력
|
328
|
-
puts "Error occurred: #{e.message}"
|
329
|
-
answer = "오류가 발생했습니다."
|
330
|
-
end
|
331
|
-
|
332
|
-
# "생성 중..." 메시지 출력 종료
|
333
|
-
thread.kill
|
334
|
-
|
335
|
-
# 결과 로그 출력
|
336
|
-
puts "Final API response ==> #{answer}"
|
337
|
-
return answer
|
338
|
-
end
|
339
|
-
|
340
|
-
def calculate_tokens(text)
|
341
|
-
# 간단한 방식으로 텍스트의 토큰 수 계산 (정확도는 다를 수 있음)
|
342
|
-
# OpenAI API는 1토큰이 대략 4글자 정도임
|
343
|
-
text.split(/\s+/).length # 간단한 단어 수로 계산
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
111
|
class Chat_title
|
352
112
|
def initialize(api_key, gpt_title_prompt)
|
353
113
|
@api_key = api_key
|