Spaces:
Sleeping
Sleeping
Create article_generator.py
Browse files- article_generator.py +18 -0
article_generator.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"]
|