Yasu777 commited on
Commit
78a142e
·
1 Parent(s): 8f9b005

Update third.py

Browse files
Files changed (1) hide show
  1. third.py +17 -19
third.py CHANGED
@@ -2,6 +2,7 @@
2
 
3
  import os
4
  import openai
 
5
  from langchain.chat_models import ChatOpenAI
6
  from langchain.experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
7
  from langchain.llms import OpenAI
@@ -9,6 +10,7 @@ from langchain.utilities import GoogleSearchAPIWrapper
9
  from langchain.agents.tools import Tool
10
  from bs4 import BeautifulSoup
11
  import asyncio
 
12
 
13
  # APIキーと検索エンジンIDの設定
14
  openai.api_key = os.getenv("OPENAI_API_KEY")
@@ -26,8 +28,8 @@ async def main(editable_output2, keyword_id):
26
 
27
  tools = [
28
  Tool(
29
- name="Search",
30
- func=lambda input_dict: search.run(input_dict) if isinstance(input_dict, str) else search.run(input_dict.get("query", input_dict.get("value"))),
31
  description="useful for when you need to answer questions about current events"
32
  ),
33
  ]
@@ -37,7 +39,11 @@ async def main(editable_output2, keyword_id):
37
  llm = ChatOpenAI(model_name=model_name, temperature=0, max_tokens=1000)
38
  planner = load_chat_planner(llm)
39
  executor = load_agent_executor(llm, tools, verbose=True)
40
- agent = PlanAndExecute(planner=planner, executor=executor, verbose=True,
 
 
 
 
41
  suffix='Answer should be in Japanese.')
42
 
43
  # editable_output2 is the text of the article structure (h1, h2, h3...).
@@ -57,10 +63,13 @@ async def main(editable_output2, keyword_id):
57
 
58
  # Specify the type of information you want to research.
59
  if "人物" in h1_text or any("人物" in h2_text for h2_text in h2_texts) or any("人物" in h3_text for h3_text in h3_texts):
 
60
  purpose += " including the person's name and career"
61
  elif "商品" in h1_text or any("商品" in h2_text for h2_text in h2_texts) or any("商品" in h3_text for h3_text in h3_texts):
 
62
  purpose += " including the brand name, product name, and price"
63
  elif "イベント" in h1_text or any("イベント" in h2_text for h2_text in h2_texts) or any("イベント" in h3_text for h3_text in h3_texts):
 
64
  purpose += " including the event's content, schedule, and venue"
65
 
66
  # Convert the purpose into an instruction in the form of a question.
@@ -85,12 +94,10 @@ async def main(editable_output2, keyword_id):
85
  # Prepare the user message
86
  research_summary = "\n".join(research_results)
87
  instructions = []
88
- formatted_text = []
89
 
90
  # 記事タイトルに関する指示
91
  instructions.append(f'1. "{h1_text}"に関する導入文を作成してください。')
92
- formatted_text.append(f"<h1>{h1_text}</h1>")
93
-
94
  # research_summaryを文単位に分割
95
  sentences = research_summary.split('。')
96
 
@@ -98,7 +105,6 @@ async def main(editable_output2, keyword_id):
98
  for idx, h2_text in enumerate(h2_texts):
99
  h3_for_this_h2 = [h3 for h3 in h3_texts if h3.startswith(f"{idx+1}-")]
100
  instructions.append(f'{idx+2}. "{h2_text}"に関する導入文を作成してください。この導入文は、以下の小見出しの内容を考慮してください:{"、".join(h3_for_this_h2)}。')
101
- formatted_text.append(f"<h2>{h2_text}</h2>")
102
  for h3 in h3_for_this_h2:
103
  # h3のテキストをキーワードとして使用し、関連する文を探す
104
  related_sentences = [sentence for sentence in sentences if h3 in sentence]
@@ -107,11 +113,10 @@ async def main(editable_output2, keyword_id):
107
  if related_sentences:
108
  content_for_h3 = "。".join(related_sentences) + "。"
109
  instructions.append(f'次に、"{h3}"に関する詳細な内容として、以下の情報を記述してください:{content_for_h3}')
110
- formatted_text.append(f"<h3>{h3}</h3>")
111
  else:
112
  instructions.append(f'次に、"{h3}"に関する詳細な内容を記述してください。')
113
- formatted_text.append(f"<h3>{h3}</h3>")
114
-
115
  user_message = {
116
  "role": "user",
117
  "content": "\n".join(instructions)
@@ -125,16 +130,9 @@ async def main(editable_output2, keyword_id):
125
  )
126
  result = response.choices[0]["message"]["content"]
127
 
128
- # Split the generated text into sections
129
- sections = result.split("\n")
130
-
131
- # Format the generated text with the HTML tags
132
- for i, section in enumerate(sections):
133
- formatted_text[i] += section
134
-
135
  # Save the generated message to output3.txt
136
  with open('output3.txt', 'w', encoding='utf-8') as f:
137
- f.write("\n".join(formatted_text))
138
 
139
  # Print the generated message
140
- print("\n".join(formatted_text))
 
2
 
3
  import os
4
  import openai
5
+ import json
6
  from langchain.chat_models import ChatOpenAI
7
  from langchain.experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
8
  from langchain.llms import OpenAI
 
10
  from langchain.agents.tools import Tool
11
  from bs4 import BeautifulSoup
12
  import asyncio
13
+ from datetime import timedelta
14
 
15
  # APIキーと検索エンジンIDの設定
16
  openai.api_key = os.getenv("OPENAI_API_KEY")
 
28
 
29
  tools = [
30
  Tool(
31
+ name = "Search",
32
+ func=search.run,
33
  description="useful for when you need to answer questions about current events"
34
  ),
35
  ]
 
39
  llm = ChatOpenAI(model_name=model_name, temperature=0, max_tokens=1000)
40
  planner = load_chat_planner(llm)
41
  executor = load_agent_executor(llm, tools, verbose=True)
42
+
43
+ # ここでexecutorの内容を出力して確認
44
+ print("Executor:", executor)
45
+
46
+ agent = PlanAndExecute(planner=planner, executer=executor, verbose=True,
47
  suffix='Answer should be in Japanese.')
48
 
49
  # editable_output2 is the text of the article structure (h1, h2, h3...).
 
63
 
64
  # Specify the type of information you want to research.
65
  if "人物" in h1_text or any("人物" in h2_text for h2_text in h2_texts) or any("人物" in h3_text for h3_text in h3_texts):
66
+ # If the topic is about a person, specify that you want to research their name and career.
67
  purpose += " including the person's name and career"
68
  elif "商品" in h1_text or any("商品" in h2_text for h2_text in h2_texts) or any("商品" in h3_text for h3_text in h3_texts):
69
+ # If the topic is about a product, specify that you want to research the brand name, product name, and price.
70
  purpose += " including the brand name, product name, and price"
71
  elif "イベント" in h1_text or any("イベント" in h2_text for h2_text in h2_texts) or any("イベント" in h3_text for h3_text in h3_texts):
72
+ # If the topic is about an event, specify that you want to research the event's content, schedule, and venue.
73
  purpose += " including the event's content, schedule, and venue"
74
 
75
  # Convert the purpose into an instruction in the form of a question.
 
94
  # Prepare the user message
95
  research_summary = "\n".join(research_results)
96
  instructions = []
 
97
 
98
  # 記事タイトルに関する指示
99
  instructions.append(f'1. "{h1_text}"に関する導入文を作成してください。')
100
+
 
101
  # research_summaryを文単位に分割
102
  sentences = research_summary.split('。')
103
 
 
105
  for idx, h2_text in enumerate(h2_texts):
106
  h3_for_this_h2 = [h3 for h3 in h3_texts if h3.startswith(f"{idx+1}-")]
107
  instructions.append(f'{idx+2}. "{h2_text}"に関する導入文を作成してください。この導入文は、以下の小見出しの内容を考慮してください:{"、".join(h3_for_this_h2)}。')
 
108
  for h3 in h3_for_this_h2:
109
  # h3のテキストをキーワードとして使用し、関連する文を探す
110
  related_sentences = [sentence for sentence in sentences if h3 in sentence]
 
113
  if related_sentences:
114
  content_for_h3 = "。".join(related_sentences) + "。"
115
  instructions.append(f'次に、"{h3}"に関する詳細な内容として、以下の情報を記述してください:{content_for_h3}')
 
116
  else:
117
  instructions.append(f'次に、"{h3}"に関する詳細な内容を記述してください。')
118
+
119
+
120
  user_message = {
121
  "role": "user",
122
  "content": "\n".join(instructions)
 
130
  )
131
  result = response.choices[0]["message"]["content"]
132
 
 
 
 
 
 
 
 
133
  # Save the generated message to output3.txt
134
  with open('output3.txt', 'w', encoding='utf-8') as f:
135
+ f.write(result)
136
 
137
  # Print the generated message
138
+ print(result)