Yasu777 commited on
Commit
94e14bd
·
verified ·
1 Parent(s): 284403d

Update article_generator.py

Browse files
Files changed (1) hide show
  1. article_generator.py +16 -35
article_generator.py CHANGED
@@ -203,7 +203,7 @@ def setup_plan_and_execute_agent():
203
  # GPT-4を使用してテキストを生成するヘルパー関数
204
  def generate_text_with_gpt4(prompt):
205
  response = openai.ChatCompletion.create(
206
- model="gpt-4o",
207
  messages=[{"role": "system", "content": "以下についての詳細な情報をまとめ、適宜箇所書き、表もしくはグラフを使って、直接的なコピーまたは近いフレーズを避けてオリジナルの内容にしてください。"},
208
  {"role": "user", "content": prompt}],
209
  temperature=0.7,
@@ -211,46 +211,30 @@ def generate_text_with_gpt4(prompt):
211
  )
212
  return response.choices[0]["message"]["content"].strip()
213
 
 
214
  def perform_initial_tavily_search(h2_texts, h3_texts):
215
  tavily_search_tool = EnhancedTavilySearchTool()
216
  queries = []
217
- references = {} # URL参照を保存するための辞書
218
-
219
  for idx, h2_text in enumerate(h2_texts):
220
  h3_for_this_h2 = [h3 for h3 in h3_texts if h3.startswith(f"{idx+1}-")]
221
- if not h3_for_this_h2 and h2_text.strip() != "まとめ":
 
222
  continue
223
-
224
  query = f"{h2_text} {' '.join(h3_for_this_h2)}"
225
  queries.append(query)
226
 
 
227
  responses = tavily_search_tool.search(queries)
228
  response_dict = {}
229
  for i, query in enumerate(queries):
230
- if i < len(responses):
231
  response_dict[query] = responses[i]
232
- references[query] = responses[i].get('url', 'No URL available')
233
  else:
234
  response_dict[query] = "No response received"
235
 
236
- return response_dict, references
237
-
238
- # 記事の最後に参照URLを追加する関数
239
- def add_references_section(soup, references):
240
- ref_section = soup.new_tag('div', **{'class': 'references'})
241
- ref_title = soup.new_tag('h2')
242
- ref_title.string = "参照"
243
- ref_section.append(ref_title)
244
-
245
- for ref in references.values():
246
- ref_tag = soup.new_tag('p')
247
- ref_link = soup.new_tag('a', href=ref)
248
- ref_link.string = ref
249
- ref_tag.append(ref_link)
250
- ref_section.append(ref_tag)
251
-
252
- soup.append(ref_section)
253
- return soup
254
 
255
  def save_preloaded_tavily_data(data):
256
  with open("preloaded_tavily_data.json", "w", encoding="utf-8") as f:
@@ -284,7 +268,7 @@ def generate_article(editable_output2):
284
 
285
  # 初期のTavily検索
286
  print("Performing initial Tavily search...")
287
- cached_responses, references = perform_initial_tavily_search(h2_texts, h3_texts)
288
  save_preloaded_tavily_data(cached_responses)
289
 
290
  executed_instructions = []
@@ -363,7 +347,7 @@ def generate_article(editable_output2):
363
  try:
364
  print(f"Sending instruction chunk {i+1} of {len(split_instructions)} to GPT-4...")
365
  response = openai.ChatCompletion.create(
366
- model="gpt-4-turbo",
367
  messages=[system_message, user_message],
368
  temperature=0.7,
369
  )
@@ -385,24 +369,21 @@ def generate_article(editable_output2):
385
 
386
  # 初期データをTavily検索で収集する関数
387
  h3_texts = [h3.get_text(strip=True) for h3 in updated_soup.find_all('h3')]
388
- cached_responses, references = perform_initial_tavily_search([], h3_texts)
389
  save_preloaded_tavily_data(cached_responses)
390
 
391
  # h3タグの拡張を行う
392
  expanded_soup = expand_h3_sections(updated_soup, cached_responses)
393
 
394
  final_html = str(expanded_soup)
395
- final_soup_with_refs = add_references_section(BeautifulSoup(final_html, 'html.parser'), references) # 参照セクションの追加
396
- final_html_with_refs = str(final_soup_with_refs)
397
 
398
  with open("output3.txt", "w", encoding="utf-8") as f:
399
- f.write(final_html_with_refs)
400
-
401
- print("Final generated article content with references:")
402
- print(final_html_with_refs)
403
 
404
  print("Article generation complete. Output saved to output3.txt.")
405
- return final_html_with_refs
 
406
 
407
  # HTMLをMarkdownに変換する関数
408
  def custom_html_to_markdown(html):
 
203
  # GPT-4を使用してテキストを生成するヘルパー関数
204
  def generate_text_with_gpt4(prompt):
205
  response = openai.ChatCompletion.create(
206
+ model="gpt-3.5-turbo-0125",
207
  messages=[{"role": "system", "content": "以下についての詳細な情報をまとめ、適宜箇所書き、表もしくはグラフを使って、直接的なコピーまたは近いフレーズを避けてオリジナルの内容にしてください。"},
208
  {"role": "user", "content": prompt}],
209
  temperature=0.7,
 
211
  )
212
  return response.choices[0]["message"]["content"].strip()
213
 
214
+ # 初期データをTavily検索で収集する関数
215
  def perform_initial_tavily_search(h2_texts, h3_texts):
216
  tavily_search_tool = EnhancedTavilySearchTool()
217
  queries = []
218
+
 
219
  for idx, h2_text in enumerate(h2_texts):
220
  h3_for_this_h2 = [h3 for h3 in h3_texts if h3.startswith(f"{idx+1}-")]
221
+ if not h3_for_this_h2 and h2_text.strip() != "まとめ": # "まとめ" セクションを除外
222
+ print(f"No matching h3 elements found for h2: {h2_text} at index {idx+1}")
223
  continue
224
+
225
  query = f"{h2_text} {' '.join(h3_for_this_h2)}"
226
  queries.append(query)
227
 
228
+ print("Performing Tavily search with queries:", queries)
229
  responses = tavily_search_tool.search(queries)
230
  response_dict = {}
231
  for i, query in enumerate(queries):
232
+ if i < len(responses): # 応答リストの範囲内にあることを確認
233
  response_dict[query] = responses[i]
 
234
  else:
235
  response_dict[query] = "No response received"
236
 
237
+ return response_dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
  def save_preloaded_tavily_data(data):
240
  with open("preloaded_tavily_data.json", "w", encoding="utf-8") as f:
 
268
 
269
  # 初期のTavily検索
270
  print("Performing initial Tavily search...")
271
+ cached_responses = perform_initial_tavily_search(h2_texts, h3_texts)
272
  save_preloaded_tavily_data(cached_responses)
273
 
274
  executed_instructions = []
 
347
  try:
348
  print(f"Sending instruction chunk {i+1} of {len(split_instructions)} to GPT-4...")
349
  response = openai.ChatCompletion.create(
350
+ model="gpt-3.5-turbo-0125",
351
  messages=[system_message, user_message],
352
  temperature=0.7,
353
  )
 
369
 
370
  # 初期データをTavily検索で収集する関数
371
  h3_texts = [h3.get_text(strip=True) for h3 in updated_soup.find_all('h3')]
372
+ cached_responses = perform_initial_tavily_search([], h3_texts)
373
  save_preloaded_tavily_data(cached_responses)
374
 
375
  # h3タグの拡張を行う
376
  expanded_soup = expand_h3_sections(updated_soup, cached_responses)
377
 
378
  final_html = str(expanded_soup)
379
+ final_markdown = custom_html_to_markdown(final_html)
 
380
 
381
  with open("output3.txt", "w", encoding="utf-8") as f:
382
+ f.write(final_html)
 
 
 
383
 
384
  print("Article generation complete. Output saved to output3.txt.")
385
+ return final_markdown, final_html
386
+
387
 
388
  # HTMLをMarkdownに変換する関数
389
  def custom_html_to_markdown(html):