Spaces:
Sleeping
Sleeping
Update article_generator.py
Browse files- article_generator.py +22 -16
article_generator.py
CHANGED
|
@@ -133,6 +133,17 @@ def setup_plan_and_execute_agent():
|
|
| 133 |
print("PlanAndExecute agent setup complete.")
|
| 134 |
return agent
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# 記事のセクションをGPT-4で拡張する関数
|
| 137 |
def expand_section_with_gpt4(h2_text, h3_texts, preloaded_data):
|
| 138 |
prompts = []
|
|
@@ -146,22 +157,17 @@ def expand_section_with_gpt4(h2_text, h3_texts, preloaded_data):
|
|
| 146 |
prompts.append(f"このテーマ「{h3_text}」に関する詳細情報を加えてください。")
|
| 147 |
|
| 148 |
expanded_texts = []
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
expanded_texts.append(expanded_text)
|
| 161 |
-
except Exception as e:
|
| 162 |
-
error_message = f"Error in generating text for {prompt}: {str(e)}"
|
| 163 |
-
print(error_message) # エラーメッセージを詳細に出力
|
| 164 |
-
expanded_texts.append("Error in text generation.")
|
| 165 |
|
| 166 |
return expanded_texts
|
| 167 |
|
|
|
|
| 133 |
print("PlanAndExecute agent setup complete.")
|
| 134 |
return agent
|
| 135 |
|
| 136 |
+
# GPT-4を使用してテキストを生成するヘルパー関数
|
| 137 |
+
def generate_text_with_gpt4(prompt):
|
| 138 |
+
response = openai.ChatCompletion.create(
|
| 139 |
+
model="gpt-4-turbo",
|
| 140 |
+
messages=[{"role": "system", "content": "以下について詳細な情報を生成してください。"},
|
| 141 |
+
{"role": "user", "content": prompt}],
|
| 142 |
+
temperature=0.7,
|
| 143 |
+
max_tokens=2000
|
| 144 |
+
)
|
| 145 |
+
return response.choices[0]["message"]["content"].strip()
|
| 146 |
+
|
| 147 |
# 記事のセクションをGPT-4で拡張する関数
|
| 148 |
def expand_section_with_gpt4(h2_text, h3_texts, preloaded_data):
|
| 149 |
prompts = []
|
|
|
|
| 157 |
prompts.append(f"このテーマ「{h3_text}」に関する詳細情報を加えてください。")
|
| 158 |
|
| 159 |
expanded_texts = []
|
| 160 |
+
with ThreadPoolExecutor(max_workers=len(prompts)) as executor:
|
| 161 |
+
future_to_prompt = {executor.submit(generate_text_with_gpt4, prompt): prompt for prompt in prompts}
|
| 162 |
+
for future in as_completed(future_to_prompt):
|
| 163 |
+
prompt = future_to_prompt[future]
|
| 164 |
+
try:
|
| 165 |
+
expanded_text = future.result()
|
| 166 |
+
expanded_texts.append(expanded_text)
|
| 167 |
+
except Exception as e:
|
| 168 |
+
error_message = f"Error in generating text for {prompt}: {str(e)}"
|
| 169 |
+
print(error_message)
|
| 170 |
+
expanded_texts.append("Error in text generation.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
return expanded_texts
|
| 173 |
|