File size: 6,350 Bytes
aa52b0f
 
 
 
78a142e
aa52b0f
 
 
 
 
 
 
78a142e
aa52b0f
 
 
 
 
 
9eae003
5601f03
 
9eae003
5601f03
 
aa52b0f
 
 
 
 
78a142e
 
aa52b0f
 
 
 
 
 
 
 
 
78a142e
 
 
 
d555058
aa52b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c529944
aa52b0f
89b981b
 
78a142e
89b981b
 
78a142e
89b981b
 
78a142e
89b981b
 
aa52b0f
3f463c6
aa52b0f
781979e
5601f03
3f463c6
5601f03
b7cf0a0
5323034
3f463c6
6f48121
 
5323034
aa52b0f
 
 
 
 
 
 
b7cf0a0
3176981
 
 
3a7ba26
78a142e
cecb5a1
 
 
3176981
 
 
3a7ba26
3176981
cecb5a1
 
 
 
 
 
3a7ba26
cecb5a1
3a7ba26
78a142e
 
aa52b0f
 
3176981
aa52b0f
 
 
 
 
 
 
 
 
b7cf0a0
aa52b0f
 
78a142e
b7cf0a0
aa52b0f
ae8b05d
 
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
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-

import os
import openai
import json
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.utilities import GoogleSearchAPIWrapper
from langchain.agents.tools import Tool
from bs4 import BeautifulSoup
import asyncio
from datetime import timedelta

# APIキーと検索エンジンIDの設定
openai.api_key = os.getenv("OPENAI_API_KEY")
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
CUSTOM_SEARCH_ENGINE_ID = os.getenv("CUSTOM_SEARCH_ENGINE_ID")

# 実行された指示を追跡するリスト
executed_instructions = []

# 調査結果を保存するリスト
research_results = []

async def main(editable_output2, keyword_id):
    search = GoogleSearchAPIWrapper(google_cse_id=CUSTOM_SEARCH_ENGINE_ID, google_api_key=GOOGLE_API_KEY)

    tools = [
        Tool(
            name = "Search",
            func=search.run,
            description="useful for when you need to answer questions about current events"
        ),
    ]

    # 「Planner」,「Executor」, および Agentの定義
    model_name = "gpt-3.5-turbo-16k"
    llm = ChatOpenAI(model_name=model_name, temperature=0, max_tokens=1000)
    planner = load_chat_planner(llm)
    executor = load_agent_executor(llm, tools, verbose=True)

    # ここでexecutorの内容を出力して確認
    print("Executor:", executor)

    agent = PlanAndExecute(planner=planner, executor=executor, verbose=True, 
    suffix='Answer should be in Japanese.')

    # editable_output2 is the text of the article structure (h1, h2, h3...).
    soup = BeautifulSoup(editable_output2, 'html.parser')

    # Use the text of the h1 tag as the purpose.
    h1_text = soup.find('h1').get_text()

    # Use the text of the h2 tags as part of the purpose.
    h2_texts = [h2.get_text() for h2 in soup.find_all('h2')]

    # Use the text of the h3 tags as part of the purpose.
    h3_texts = [h3.get_text() for h3 in soup.find_all('h3')]

    # Generate the purpose.
    purpose = f"about {h1_text}, focusing particularly on {' and '.join(h2_texts)} and {' and '.join(h3_texts)}, to investigate the latest information and details"

    # Specify the type of information you want to research.
    if "人物" in h1_text or any("人物" in h2_text for h2_text in h2_texts) or any("人物" in h3_text for h3_text in h3_texts):
        # If the topic is about a person, specify that you want to research their name and career.
        purpose += " including the person's name and career"
    elif "商品" in h1_text or any("商品" in h2_text for h2_text in h2_texts) or any("商品" in h3_text for h3_text in h3_texts):
        # If the topic is about a product, specify that you want to research the brand name, product name, and price.
        purpose += " including the brand name, product name, and price"
    elif "イベント" in h1_text or any("イベント" in h2_text for h2_text in h2_texts) or any("イベント" in h3_text for h3_text in h3_texts):
        # If the topic is about an event, specify that you want to research the event's content, schedule, and venue.
        purpose += " including the event's content, schedule, and venue"

    # Convert the purpose into an instruction in the form of a question.
    instruction = f"Can you research {purpose} and include specific details in your response?"

    # Run the instruction with a clear expectation of the output format
    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]

    # Prepare the system message
    system_message = {
        "role": "system",
        "content": "あなたはプロのライターです。"
    }

    # Prepare the user message
    research_summary = "\n".join(research_results)
    instructions = []

    # 記事タイトルに関する指示
    instructions.append(f'1. "{h1_text}"に関する導入文を作成してください。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。')

    # research_summaryを文単位に分割
    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'{idx+2}. "{h2_text}"に関する導入文を作成してください。この導入文は、以下の小見出しの内容を考慮してください:{"、".join(h3_for_this_h2)}。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。')
        for h3 in h3_for_this_h2:
            # h3のテキストをキーワードとして使用し、関連する文を探す
            related_sentences = [sentence for sentence in sentences if h3 in sentence]
            
            # 関連する文を基に、<h3>の内容に関する記事本文を生成
            if related_sentences:
                content_for_h3 = "。".join(related_sentences) + "。"
                instructions.append(f'次に、"{h3}"に関する詳細な内容として、以下の情報を記述してください:{content_for_h3} ここでも、オリジナルな内容を心がけてください。')
            else:
                instructions.append(f'次に、"{h3}"に関する詳細な内容を記述してください。オリジナルな内容を心がけてください。')


    user_message = {
        "role": "user",
        "content": "\n".join(instructions)
    }

    # Generate a new text using the ChatCompletion API
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-16k",
        messages=[system_message, user_message],
        temperature=0.7,
    )
    result = response.choices[0]["message"]["content"]

    # Save the generated message to output3.txt
    with open('output3.txt', 'w', encoding='utf-8') as f:
        f.write(result)

    # Print the generated message
    print(result)