Yasu777 commited on
Commit
8f2b805
·
verified ·
1 Parent(s): 7c895f9

Update article_generator.py

Browse files
Files changed (1) hide show
  1. article_generator.py +60 -16
article_generator.py CHANGED
@@ -1,18 +1,62 @@
 
 
 
1
  from langchain.llms import OpenAI
 
2
  from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
3
- import openai
4
- import os
5
-
6
- def generate_article(headline, api_key):
7
- """見出しを基にしてLangchainのPlanAndExecuteモデルにより記事本文を生成する"""
8
- openai.api_key = api_key
9
- openai_llm = OpenAI(api_key=api_key)
10
-
11
- # PlanAndExecuteの設定
12
- agent_executor = load_agent_executor("simple", llm=openai_llm)
13
- chat_planner = load_chat_planner("headline_to_article", llm=openai_llm)
14
- plan_and_execute = PlanAndExecute(planner=chat_planner, executor=agent_executor)
15
-
16
- # 記事生成
17
- output = plan_and_execute.execute({"headline": headline})
18
- return output["text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
  from langchain.llms 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.agents.tools import Tool
8
+
9
+ # 環境変数からAPIキーを設定
10
+ openai.api_key = os.getenv("OPENAI_API_KEY")
11
+ tavily_api_key = os.getenv('TAVILY_API_KEY')
12
+
13
+ class EnhancedTavilySearchTool:
14
+ def search(self, query):
15
+ params = {
16
+ 'api_key': tavily_api_key,
17
+ 'query': query,
18
+ 'max_results': 10,
19
+ 'detail_level': 'high'
20
+ }
21
+ response = requests.post('https://api.tavily.com/search', json=params)
22
+ if response.status_code == 200:
23
+ return response.json()['results']
24
+ else:
25
+ raise Exception("Failed to fetch data from Tavily API")
26
+
27
+ def main(editable_output2, keyword_id):
28
+ tavily_search_tool = Tool(
29
+ name="TavilySearch",
30
+ func=EnhancedTavilySearchTool().search,
31
+ description="Enhanced search tool using Tavily API"
32
+ )
33
+
34
+ tools = [tavily_search_tool]
35
+
36
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo-1106", temperature=0, max_tokens=1000)
37
+ planner = load_chat_planner(llm)
38
+ executor = load_agent_executor(llm, tools, verbose=True)
39
+ agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
40
+
41
+ soup = BeautifulSoup(editable_output2, 'html.parser')
42
+ h1_text = soup.find('h1').get_text()
43
+
44
+ # 実行と結果の取得
45
+ result = agent.run(f"Research about {h1_text}")
46
+ return result
47
+
48
+ # Gradioインターフェースの設定
49
+ with gr.Blocks() as app:
50
+ with gr.Column():
51
+ input_html = gr.Textbox(label="Editable HTML Output", placeholder="HTML content here...", lines=10)
52
+ keyword_id_input = gr.Textbox(label="Keyword ID", placeholder="Enter Keyword ID...")
53
+ generate_button = gr.Button("Generate")
54
+ output_article = gr.Textbox(label="Generated Article", lines=20, readonly=True)
55
+
56
+ generate_button.click(
57
+ fn=main,
58
+ inputs=[input_html, keyword_id_input],
59
+ outputs=output_article
60
+ )
61
+
62
+ app.launch()