import os import openai import json import requests import gradio as gr from bs4 import BeautifulSoup import openai 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 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 EnhancedTavilySearchTool: def search(self, query): params = { 'api_key': tavily_api_key, 'query': query, 'max_results': 10, 'detail_level': 'high' } response = requests.post('https://api.tavily.com/search', json=params) if response.status_code == 200: return response.json()['results'] else: raise Exception("Failed to fetch data from Tavily API") # 実行された指示を追跡するリスト executed_instructions = [] # 調査結果を保存するリスト research_results = [] def generate_article(editable_output2): tavily_search_tool = Tool( name="TavilySearch", func=EnhancedTavilySearchTool().search, description="Enhanced 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"""