Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| import os | |
| import openai | |
| import json | |
| from langchain.chat_models import ChatOpenAI | |
| from langchain.experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner | |
| from langchain.llms import OpenAI | |
| from langchain.utilities import GoogleSearchAPIWrapper | |
| from langchain.agents.tools import Tool | |
| from bs4 import BeautifulSoup | |
| import asyncio | |
| from datetime import timedelta | |
| # APIキーと検索エンジンIDの設定 | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") | |
| CUSTOM_SEARCH_ENGINE_ID = os.getenv("CUSTOM_SEARCH_ENGINE_ID") | |
| # 実行された指示を追跡するリスト | |
| executed_instructions = [] | |
| # 調査結果を保存するリスト | |
| research_results = [] | |
| async def main(editable_output2, keyword_id): | |
| search = GoogleSearchAPIWrapper(google_cse_id=CUSTOM_SEARCH_ENGINE_ID, google_api_key=GOOGLE_API_KEY) | |
| tools = [ | |
| Tool( | |
| name = "Search", | |
| func=search.run, | |
| description="useful for when you need to answer questions about current events" | |
| ), | |
| ] | |
| # 「Planner」,「Executor」, および Agentの定義 | |
| model_name = "gpt-3.5-turbo-16k" | |
| llm = ChatOpenAI(model_name=model_name, temperature=0, max_tokens=1000) | |
| planner = load_chat_planner(llm) | |
| executor = load_agent_executor(llm, tools, verbose=True) | |
| # ここでexecutorの内容を出力して確認 | |
| print("Executor:", executor) | |
| agent = PlanAndExecute(planner=planner, executor=executor, verbose=True, | |
| suffix='Answer should be in Japanese.') | |
| # editable_output2 is the text of the article structure (h1, h2, h3...). | |
| soup = BeautifulSoup(editable_output2, 'html.parser') | |
| # Use the text of the h1 tag as the purpose. | |
| h1_text = soup.find('h1').get_text() | |
| # Use the text of the h2 tags as part of the purpose. | |
| h2_texts = [h2.get_text() for h2 in soup.find_all('h2')] | |
| # Use the text of the h3 tags as part of the purpose. | |
| h3_texts = [h3.get_text() for h3 in soup.find_all('h3')] | |
| # Generate the purpose. | |
| purpose = f"about {h1_text}, focusing particularly on {' and '.join(h2_texts)} and {' and '.join(h3_texts)}, to investigate the latest information and details" | |
| # Specify the type of information you want to research. | |
| 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): | |
| # If the topic is about a person, specify that you want to research their name and career. | |
| purpose += " including the person's name and career" | |
| 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): | |
| # If the topic is about a product, specify that you want to research the brand name, product name, and price. | |
| purpose += " including the brand name, product name, and price" | |
| 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): | |
| # If the topic is about an event, specify that you want to research the event's content, schedule, and venue. | |
| purpose += " including the event's content, schedule, and venue" | |
| # Convert the purpose into an instruction in the form of a question. | |
| instruction = f"Can you research {purpose} and include specific details in your response?" | |
| # Run the instruction with a clear expectation of the output format | |
| if instruction not in executed_instructions: | |
| raw_output = agent.run(instruction) | |
| executed_instructions.append(instruction) | |
| response_content = raw_output | |
| research_results.append(response_content) | |
| else: | |
| index = executed_instructions.index(instruction) | |
| response_content = research_results[index] | |
| # Prepare the system message | |
| system_message = { | |
| "role": "system", | |
| "content": "あなたはプロのライターです。" | |
| } | |
| # Prepare the user message | |
| research_summary = "\n".join(research_results) | |
| instructions = [] | |
| # 記事タイトルに関する指示 | |
| instructions.append(f'1. "{h1_text}"に関する導入文を作成してください。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。') | |
| # research_summaryを文単位に分割 | |
| sentences = research_summary.split('。') | |
| # 各見出しと小見出しに関する指示 | |
| for idx, h2_text in enumerate(h2_texts): | |
| h3_for_this_h2 = [h3 for h3 in h3_texts if h3.startswith(f"{idx+1}-")] | |
| instructions.append(f'{idx+2}. "{h2_text}"に関する導入文を作成してください。この導入文は、以下の小見出しの内容を考慮してください:{"、".join(h3_for_this_h2)}。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。') | |
| for h3 in h3_for_this_h2: | |
| # h3のテキストをキーワードとして使用し、関連する文を探す | |
| related_sentences = [sentence for sentence in sentences if h3 in sentence] | |
| # 関連する文を基に、<h3>の内容に関する記事本文を生成 | |
| if related_sentences: | |
| content_for_h3 = "。".join(related_sentences) + "。" | |
| instructions.append(f'次に、"{h3}"に関する詳細な内容として、以下の情報を記述してください:{content_for_h3} ここでも、オリジナルな内容を心がけてください。') | |
| else: | |
| instructions.append(f'次に、"{h3}"に関する詳細な内容を記述してください。オリジナルな内容を心がけてください。') | |
| user_message = { | |
| "role": "user", | |
| "content": "\n".join(instructions) | |
| } | |
| # Generate a new text using the ChatCompletion API | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo-16k", | |
| messages=[system_message, user_message], | |
| temperature=0.7, | |
| ) | |
| result = response.choices[0]["message"]["content"] | |
| # Save the generated message to output3.txt | |
| with open('output3.txt', 'w', encoding='utf-8') as f: | |
| f.write(result) | |
| # Print the generated message | |
| print(result) | |