Spaces:
Sleeping
Sleeping
File size: 5,450 Bytes
aa52b0f 78a142e 2901d59 aa52b0f 9d7f3ea aa52b0f 78a142e aa52b0f 2901d59 aa52b0f 2901d59 aa52b0f 9eae003 5601f03 9eae003 5601f03 aa52b0f 2901d59 aa52b0f 2901d59 aa52b0f 2901d59 c0e7523 aa52b0f 78a142e e57372d aa52b0f 2901d59 aa52b0f c529944 aa52b0f 2901d59 89b981b 2901d59 89b981b 2901d59 89b981b e57372d aa52b0f 5601f03 3f463c6 5601f03 b7cf0a0 5323034 3f463c6 6f48121 5323034 aa52b0f b759608 aa52b0f b7cf0a0 3176981 5146f85 78a142e cecb5a1 3176981 5146f85 3176981 5146f85 cecb5a1 5146f85 cecb5a1 5146f85 78a142e aa52b0f 3176981 aa52b0f c0e7523 aa52b0f b7cf0a0 aa52b0f 78a142e b7cf0a0 2901d59 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | # -*- coding: utf-8 -*-
import os
import openai
import json
import requests
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.agents.tools import Tool
from bs4 import BeautifulSoup
import asyncio
from datetime import timedelta
# APIキーの設定
openai.api_key = os.getenv("OPENAI_API_KEY")
TAVILY_API_KEY = os.getenv('TAVILY_API_KEY')
# Tavily APIのツールを定義
class TavilySearchTool:
@staticmethod
def search(query):
response = requests.post('https://api.tavily.com/search', headers={
'Content-Type': 'application/json'
}, json={
'api_key': TAVILY_API_KEY,
'query': query,
'max_results': 10,
'include_answers': True
})
if response.status_code == 200:
return response.json()['results']
else:
raise Exception("Failed to fetch data from Tavily API")
# 実行された指示を追跡するリスト
executed_instructions = []
# 調査結果を保存するリスト
research_results = []
async def main(editable_output2, keyword_id):
tavily_search_tool = Tool(
name="TavilySearch",
func=TavilySearchTool.search,
description="Search tool using Tavily API"
)
tools = [tavily_search_tool]
# PlannerとExecutorの定義
model_name = "gpt-3.5-turbo-1106"
llm = ChatOpenAI(model_name=model_name, temperature=0, max_tokens=1000)
planner = load_chat_planner(llm)
executor = load_agent_executor(llm, tools, verbose=True)
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
# HTML解析
soup = BeautifulSoup(editable_output2, 'html.parser')
h1_text = soup.find('h1').get_text()
h2_texts = [h2.get_text() for h2 in soup.find_all('h2')]
h3_texts = [h3.get_text() for h3 in soup.find_all('h3')]
purpose = f"about {h1_text}, focusing particularly on {' and '.join(h2_texts)} and {' and '.join(h3_texts)}, to investigate the latest information and details"
# 特定情報の指定
if "人物" in h1_text or any("人物" in h2 for h2 in h2_texts) or any("人物" in h3 for h3 in h3_texts):
purpose += " including the person's name and career"
elif "商品" in h1_text or any("商品" in h2 for h2 in h2_texts) or any("商品" in h3 for h3 in h3_texts):
purpose += " including the brand name, product name, and price"
elif "イベント" in h1_text or any("イベント" in h2 for h2 in h2_texts) or any("イベント" in h3 for h3 in h3_texts):
purpose += " including the event's content, schedule, and venue"
instruction = f"Can you research {purpose} and include specific details in your response? Please provide the information in Japanese."
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]
system_message = {
"role": "system",
"content": "あなたはプロのライターです。すべての回答を日本語でお願いします。"
}
research_summary = "\n".join(research_results)
instructions = []
instructions.append(f"""
<h1>{h1_text}</h1>
"{h1_text}"に関する導入文を日本語で作成してください。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。""")
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"""
<h2>{h2_text}</h2>
"{h2_text}"に関する導入文を日本語で作成してください。この導入文は、以下の小見出しの内容を考慮してください:{"、".join(h3_for_this_h2)}。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。""")
for h3 in h3_for_this_h2:
related_sentences = [sentence for sentence in sentences if h3 in sentence]
if related_sentences:
content_for_h3 = "。".join(related_sentences) + "。"
instructions.append(f"""
<h3>{h3}</h3>
"{h3}"に関する詳細な内容として、以下の情報を日本語で記述してください:{content_for_h3} ここでも、オリジナルな内容を心がけてください。""")
else:
instructions.append(f"""
<h3>{h3}</h3>
"{h3}"に関する詳細な内容を日本語で記述してください。オリジナルな内容を心がけてください。""")
user_message = {
"role": "user",
"content": "\n".join(instructions)
}
response = openai.ChatCompletion.create(
model="gpt-4-0125-preview",
messages=[system_message, user_message],
temperature=0.7,
)
result = response.choices[0]["message"]["content"]
with open('output3.txt', 'w', encoding='utf-8') as f:
f.write(result)
print(result) |