Spaces:
Sleeping
Sleeping
Update third.py
Browse files
third.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
import openai
|
| 5 |
-
import json
|
| 6 |
from langchain.chat_models import ChatOpenAI
|
| 7 |
from langchain.experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
|
| 8 |
from langchain.llms import OpenAI
|
|
@@ -10,24 +9,12 @@ from langchain.utilities import GoogleSearchAPIWrapper
|
|
| 10 |
from langchain.agents.tools import Tool
|
| 11 |
from bs4 import BeautifulSoup
|
| 12 |
import asyncio
|
| 13 |
-
from datetime import timedelta
|
| 14 |
-
from langchain.output_parsers import PydanticOutputParser
|
| 15 |
-
from pydantic import BaseModel, Field, validator
|
| 16 |
-
from typing import List
|
| 17 |
|
| 18 |
# APIキーと検索エンジンIDの設定
|
| 19 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 20 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 21 |
CUSTOM_SEARCH_ENGINE_ID = os.getenv("CUSTOM_SEARCH_ENGINE_ID")
|
| 22 |
|
| 23 |
-
# Pydanticのモデルを定義
|
| 24 |
-
class LLMOutput(BaseModel):
|
| 25 |
-
action: str
|
| 26 |
-
action_input: str
|
| 27 |
-
|
| 28 |
-
# PydanticOutputParserのインスタンスを作成
|
| 29 |
-
output_parser = PydanticOutputParser(pydantic_object=LLMOutput)
|
| 30 |
-
|
| 31 |
# 追加: 実行された指示を追跡するリスト
|
| 32 |
executed_instructions = []
|
| 33 |
|
|
@@ -68,37 +55,20 @@ async def main(editable_output2, keyword_id):
|
|
| 68 |
# Generate the purpose.
|
| 69 |
purpose = f"about {h1_text}, focusing particularly on {' and '.join(h2_texts)} and {' and '.join(h3_texts)}, to investigate the latest information and details"
|
| 70 |
|
| 71 |
-
# Specify the type of information you want to research.
|
| 72 |
-
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):
|
| 73 |
-
# If the topic is about a person, specify that you want to research their name and career.
|
| 74 |
-
purpose += " including the person's name and career"
|
| 75 |
-
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):
|
| 76 |
-
# If the topic is about a product, specify that you want to research the brand name, product name, and price.
|
| 77 |
-
purpose += " including the brand name, product name, and price"
|
| 78 |
-
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):
|
| 79 |
-
# If the topic is about an event, specify that you want to research the event's content, schedule, and venue.
|
| 80 |
-
purpose += " including the event's content, schedule, and venue"
|
| 81 |
-
|
| 82 |
# Convert the purpose into an instruction in the form of a question.
|
| 83 |
instruction = f"Can you research {purpose} and include specific details such as names, ages, careers, product names, service names, store names, locations, times, and any relevant numerical data or statistics in your response?"
|
| 84 |
|
| 85 |
# Run the instruction with a clear expectation of the output format
|
| 86 |
if instruction not in executed_instructions:
|
| 87 |
-
|
| 88 |
-
print(raw_output)
|
| 89 |
executed_instructions.append(instruction)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
output_text = parsed_output.action_input
|
| 95 |
-
except json.decoder.JSONDecodeError:
|
| 96 |
-
# JSON形式でない場合は、raw_outputを直接使用する
|
| 97 |
-
output_text = raw_output
|
| 98 |
|
| 99 |
-
research_results.append(output_text)
|
| 100 |
else:
|
| 101 |
-
|
| 102 |
|
| 103 |
# Prepare the system message
|
| 104 |
system_message = {
|
|
@@ -123,13 +93,7 @@ async def main(editable_output2, keyword_id):
|
|
| 123 |
|
| 124 |
# Save the generated message to output3.txt
|
| 125 |
with open('output3.txt', 'w', encoding='utf-8') as f:
|
| 126 |
-
|
| 127 |
-
f.write(json.dumps(result, ensure_ascii=False, indent=4))
|
| 128 |
-
else:
|
| 129 |
-
f.write(result)
|
| 130 |
|
| 131 |
# Print the generated message
|
| 132 |
print(result)
|
| 133 |
-
|
| 134 |
-
if __name__ == "__main__":
|
| 135 |
-
st.write(raw_output)
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
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.llms import OpenAI
|
|
|
|
| 9 |
from langchain.agents.tools import Tool
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# APIキーと検索エンジンIDの設定
|
| 14 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 16 |
CUSTOM_SEARCH_ENGINE_ID = os.getenv("CUSTOM_SEARCH_ENGINE_ID")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# 追加: 実行された指示を追跡するリスト
|
| 19 |
executed_instructions = []
|
| 20 |
|
|
|
|
| 55 |
# Generate the purpose.
|
| 56 |
purpose = f"about {h1_text}, focusing particularly on {' and '.join(h2_texts)} and {' and '.join(h3_texts)}, to investigate the latest information and details"
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Convert the purpose into an instruction in the form of a question.
|
| 59 |
instruction = f"Can you research {purpose} and include specific details such as names, ages, careers, product names, service names, store names, locations, times, and any relevant numerical data or statistics in your response?"
|
| 60 |
|
| 61 |
# Run the instruction with a clear expectation of the output format
|
| 62 |
if instruction not in executed_instructions:
|
| 63 |
+
agent_output = agent.run(instruction)
|
|
|
|
| 64 |
executed_instructions.append(instruction)
|
| 65 |
|
| 66 |
+
# Extract the response content directly from the agent's output
|
| 67 |
+
response_content = agent_output['action_input']
|
| 68 |
+
research_results.append(response_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
|
|
|
| 70 |
else:
|
| 71 |
+
response_content = "This instruction has already been executed."
|
| 72 |
|
| 73 |
# Prepare the system message
|
| 74 |
system_message = {
|
|
|
|
| 93 |
|
| 94 |
# Save the generated message to output3.txt
|
| 95 |
with open('output3.txt', 'w', encoding='utf-8') as f:
|
| 96 |
+
f.write(result)
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Print the generated message
|
| 99 |
print(result)
|
|
|
|
|
|
|
|
|