Yasu777 commited on
Commit
712ef5a
·
verified ·
1 Parent(s): 28e2062

Update article_generator.py

Browse files
Files changed (1) hide show
  1. article_generator.py +62 -20
article_generator.py CHANGED
@@ -26,43 +26,85 @@ class EnhancedTavilySearchTool:
26
  else:
27
  raise Exception("Failed to fetch data from Tavily API")
28
 
29
- # PlanAndExecute エージェンのセットアップ
30
- def setup_agent():
31
- tavily_search_tool = EnhancedTavilySearchTool()
32
- tools = [Tool(name="TavilySearch", func=tavily_search_tool.search, description="Enhanced search tool using Tavily API")]
33
- llm = ChatOpenAI(model_name="gpt-3.5-turbo-1106", temperature=0, max_tokens=1000)
 
 
 
 
 
 
 
 
 
 
 
 
34
  planner = load_chat_planner(llm)
35
  executor = load_agent_executor(llm, tools, verbose=True)
36
- agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
37
- return agent
38
 
39
- agent = setup_agent()
40
 
41
- # 記事本文の生成
42
- def generate_article(editable_output2):
43
  soup = BeautifulSoup(editable_output2, 'html.parser')
44
  h1_text = soup.find('h1').get_text()
45
  h2_texts = [h2.get_text() for h2 in soup.find_all('h2')]
46
  h3_texts = [h3.get_text() for h3 in soup.find_all('h3')]
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  system_message = {
49
  "role": "system",
50
  "content": "あなたはプロのライターです。すべての回答を日本語でお願いします。"
51
  }
52
 
53
- instructions = [
54
- f"<h1>{h1_text}</h1> \"{h1_text}\"に関する導入文を日本語で作成してください。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。"
55
- ]
 
 
 
 
 
56
 
57
  for idx, h2_text in enumerate(h2_texts):
58
  h3_for_this_h2 = [h3 for h3 in h3_texts if h3.startswith(f"{idx+1}-")]
59
- instructions.append(
60
- f"<h2>{h2_text}</h2> \"{h2_text}\"に関する導入文を日本語で作成してください。この導入文は、以下の小見出しの内容を考慮してください:{'、'.join(h3_for_this_h2)}。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。"
61
- )
62
  for h3 in h3_for_this_h2:
63
- instructions.append(
64
- f"<h3>{h3}</h3> \"{h3}\"に関する詳細な内容を日本語で記述してください。オリジナルな内容を心がけてください。"
65
- )
 
 
 
 
 
 
 
66
 
67
  user_message = {
68
  "role": "user",
@@ -72,7 +114,7 @@ def generate_article(editable_output2):
72
  response = openai.ChatCompletion.create(
73
  model="gpt-4-0125-preview",
74
  messages=[system_message, user_message],
75
- temperature=0.7
76
  )
77
  result = response.choices[0]["message"]["content"]
78
 
 
26
  else:
27
  raise Exception("Failed to fetch data from Tavily API")
28
 
29
+ # 実行された指示を追跡するリス
30
+ executed_instructions = []
31
+ # 調査結果を保存するリスト
32
+ research_results = []
33
+
34
+ async def main(editable_output2, keyword_id):
35
+ tavily_search_tool = Tool(
36
+ name="TavilySearch",
37
+ func=EnhancedTavilySearchTool().search,
38
+ description="Enhanced search tool using Tavily API"
39
+ )
40
+
41
+ tools = [tavily_search_tool]
42
+
43
+ # PlannerとExecutorの拡張定義
44
+ model_name = "gpt-3.5-turbo-1106"
45
+ llm = ChatOpenAI(model_name=model_name, temperature=0, max_tokens=1000)
46
  planner = load_chat_planner(llm)
47
  executor = load_agent_executor(llm, tools, verbose=True)
 
 
48
 
49
+ agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
50
 
51
+ # HTML解析
 
52
  soup = BeautifulSoup(editable_output2, 'html.parser')
53
  h1_text = soup.find('h1').get_text()
54
  h2_texts = [h2.get_text() for h2 in soup.find_all('h2')]
55
  h3_texts = [h3.get_text() for h3 in soup.find_all('h3')]
56
 
57
+ purpose = f"about {h1_text}, focusing particularly on {' and '.join(h2_texts)} and {' and '.join(h3_texts)}, to investigate the latest information and details"
58
+
59
+ # 特定情報の指定
60
+ if "人物" in h1_text or any("人物" in h2 for h2 in h2_texts) or any("人物" in h3 for h3 in h3_texts):
61
+ purpose += " including the person's name and career"
62
+ elif "商品" in h1_text or any("商品" in h2 for h2 in h2_texts) or any("商品" in h3 for h3 in h3_texts):
63
+ purpose += " including the brand name, product name, and price"
64
+ elif "イベント" in h1_text or any("イベント" in h2 for h2 in h2_texts) or any("イベント" in h3 for h3 in h3_texts):
65
+ purpose += " including the event's content, schedule, and venue"
66
+
67
+ instruction = f"Can you research {purpose} and include specific details in your response? Please provide the information in Japanese."
68
+
69
+ if instruction not in executed_instructions:
70
+ raw_output = agent.run(instruction)
71
+ executed_instructions.append(instruction)
72
+ response_content = raw_output
73
+ research_results.append(response_content)
74
+ else:
75
+ index = executed_instructions.index(instruction)
76
+ response_content = research_results[index]
77
+
78
  system_message = {
79
  "role": "system",
80
  "content": "あなたはプロのライターです。すべての回答を日本語でお願いします。"
81
  }
82
 
83
+ research_summary = "\n".join(research_results)
84
+ instructions = []
85
+
86
+ instructions.append(f"""
87
+ <h1>{h1_text}</h1>
88
+ "{h1_text}"に関する導入文を日本語で作成してください。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。""")
89
+
90
+ sentences = research_summary.split('。')
91
 
92
  for idx, h2_text in enumerate(h2_texts):
93
  h3_for_this_h2 = [h3 for h3 in h3_texts if h3.startswith(f"{idx+1}-")]
94
+ instructions.append(f"""
95
+ <h2>{h2_text}</h2>
96
+ "{h2_text}"に関する導入文を日本語で作成してください。この導入文は、以下の小見出しの内容を考慮してください:{"、".join(h3_for_this_h2)}。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。""")
97
  for h3 in h3_for_this_h2:
98
+ related_sentences = [sentence for sentence in sentences if h3 in sentence]
99
+ if related_sentences:
100
+ content_for_h3 = "。".join(related_sentences) + "。"
101
+ instructions.append(f"""
102
+ <h3>{h3}</h3>
103
+ "{h3}"に関する詳細な内容として、以下の情報を日本語で記述してください:{content_for_h3} ここでも、オリジナルな内容を心がけてください。""")
104
+ else:
105
+ instructions.append(f"""
106
+ <h3>{h3}</h3>
107
+ "{h3}"に関する詳細な内容を日本語で記述してください。オリジナルな内容を心がけてください。""")
108
 
109
  user_message = {
110
  "role": "user",
 
114
  response = openai.ChatCompletion.create(
115
  model="gpt-4-0125-preview",
116
  messages=[system_message, user_message],
117
+ temperature=0.7,
118
  )
119
  result = response.choices[0]["message"]["content"]
120