BabyWriterPRO.v-llama3 / headline_generator.py
Yasu777's picture
Update headline_generator.py
c183d57 verified
import os
from groq import Groq
# Groq APIの設定
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
def generate_headlines_and_subheadings(new_keyword, ngram_text):
try:
response = client.chat.completions.create(
model="llama3-70b-8192",
messages=[
{"role": "system", "content": "You are a writing expert and a professional translator; structure your headings according to the flow of the PREP method (conclusion -> reasons -> specific examples -> conclusion). Create witty sentences without redundancy. Respond in Japanese, and do not add any explanations or supplementary information beyond what is directly requested."},
{"role": "user", "content": f'(キーワード)の感情分析を行った結果に基づいて、興味や関心を引きつける「(キーワード)を含めたタイトル」、興味や関心を引きつける「見出し」をステップバイステップで生成してください。そしてそれぞれの見出しに沿って「小見出し」を適宜生成してください。最後に、記事を締めくくる「<h2>まとめ</h2>」のセクションを追加してください。タイトル、見出し、小見出しはPREP法に基づいて一貫性のあるものとし、記事の前半部分で(キーワード)の検索意図を満たす記事構成内容にし、(テキスト)が(キーワード)に関連している場合、(テキスト)に含まれる語句を使用して「見出し」、「小見出し」を生成してください。(テキスト)は(キーワード)を検索するユーザーの検索意図を深掘りした興味や関心です。情報を次のように整理してください:タイトル、見出しおよび小見出しは順に「<h1>(キーワード)を含めたタイトル</h1>」「<h2>1.見出し</h2>」「<h3>1-1 小見出し とは</h3>、<h3>1-2 小見出し とは</h3>」「<h2>2.見出し</h2>」「<h3>2-1 小見出し とは</h3>、<h3>2-2 小見出し とは</h3>」「<h2>3.見出し</h2>」「<h3>3-1 小見出し とは</h3>、<h3>3-2 小見出し とは</h3>」「<h2>4.見出し</h2>」「<h3>4-1 小見出し とは</h3>、<h3>4-2 小見出し とは</h3>」というようにいくつかの見出しと小見出しで構成し、必ず、見出しは最大4つ、小見出しは各見出しに対して最大2つまでの構成とします。各見出し、小見出し、まとめに説明文は不要です。タイトル、見出し、そして小見出しは検索者が求める情報を整理して記載するようにしてください。見出しと小見出しの冗長性を避けてください。すべてのコンテンツは日本語で書かれている必要があり、検索者の意図を満たすことを目指してください。テキスト:\"{ngram_text}\" キーワード:\"{new_keyword}\"'}
],
temperature=0.7,
max_tokens=2000
)
result = response.choices[0].message.content.strip()
print(result)
return result
except Exception as e:
print(f"An error occurred while generating content: {str(e)}")
return None
if __name__ == "__main__":
import sys
new_keyword = sys.argv[1] if len(sys.argv) > 1 else "default_keyword"
output1_path = os.path.join(os.path.dirname(__file__), "output1.txt")
try:
with open(output1_path, "r", encoding="utf-8") as f:
ngram_text = f.read().strip() or "No Ngram text available"
except Exception as e:
print(f"Error reading output1.txt: {e}")
ngram_text = "No Ngram text available"
output = generate_headlines_and_subheadings(new_keyword, ngram_text)
if output:
with open("output2.txt", "w", encoding="utf-8") as file:
file.write(output)