Spaces:
Sleeping
Sleeping
import json | |
import os | |
# import yaml | |
from dotenv import load_dotenv | |
from utils_groupclassification.agent import submit_tool_outputs | |
from utils_groupclassification.assistants_utils import ( | |
create_assistant, | |
create_message, | |
create_run, | |
create_thread, | |
wait_for_run_completion, | |
) | |
from utils_groupclassification.gspread_utils import write_gspread | |
from src.myLogger import set_logger | |
from utils_groupclassification.check_openai import co | |
logger = set_logger("my_app", level="INFO") | |
load_dotenv() | |
model = 'gpt-4o' | |
def search(query) -> list: | |
logger.info("create thread") | |
thread = create_thread() | |
logger.info("created thread") | |
with open("prompts/ph1.txt", "r") as fp: # search prompt | |
assistant_prompt = fp.read() | |
logger.info(f"assistant prompt: {assistant_prompt}") | |
with open("tools/ph1.json", "r") as f: # tool prompt | |
tools = json.load(f) | |
logger.info("create assistant") | |
assistant = create_assistant( # create assistant | |
assistant_prompt, | |
model=model, | |
tools=tools, | |
) | |
logger.info(f"User:{query}") | |
create_message(thread.id, "user", query) | |
run = create_run(thread.id, assistant.id) | |
run = wait_for_run_completion(thread.id, run.id) | |
logger.info(f"status: {run.status}") | |
if run.status == "failed": | |
logger.error(f"Run_failed: {run.last_error}, Run ID: {run.id}") | |
return "Group 5", ['途中でエラー'], [], [] | |
# sys.exit(1) | |
elif run.status == "requires_action": | |
logger.info( | |
f"Run requires action: {run.required_action.submit_tool_outputs.tool_calls}" | |
) | |
run = submit_tool_outputs( | |
thread.id, | |
run.id, | |
run.required_action.submit_tool_outputs.tool_calls, | |
) | |
if run is None: | |
return "Group 5", ['途中でエラー'], [], [] | |
run = wait_for_run_completion(thread.id, run.id) | |
logger.info(f"Run status: {run.status}") | |
with open("prompts/ph2.txt", "r") as fp: # search prompt | |
assistant_prompt = fp.read() | |
logger.info(f"assistant prompt: {assistant_prompt}") | |
with open("tools/ph2.json", "r") as f: # tool prompt | |
tools = json.load(f) | |
assistant1 = create_assistant( # create assistant | |
assistant_prompt, | |
model="gpt-3.5-turbo", | |
tools=tools, | |
) | |
run = create_run(thread.id, assistant1.id) | |
logger.info(f"run.id: {run.id}") | |
run = wait_for_run_completion(thread.id, run.id) | |
logger.info(f"status: {run.status}") | |
if run.status == "failed": | |
logger.error(f"Run_failed: {run.error}, Run ID: {run.id}") | |
return "Group 5", ['途中でエラー'], [], [] | |
# sys.exit(1) | |
elif run.status == "requires_action": | |
logger.info( | |
f"Run requires action: {run.required_action.submit_tool_outputs.tool_calls}" | |
) | |
run = submit_tool_outputs( | |
thread.id, | |
run.id, | |
run.required_action.submit_tool_outputs.tool_calls, | |
) | |
if run is None: | |
return "Group 5", ['途中でエラー'], [], [] | |
run = wait_for_run_completion(thread.id, run.id) | |
logger.info(f"Run status: {run.status}") | |
group, related_url, unrelated_url, other_url = write_gspread(query) | |
if len(related_url) == 0: | |
related_url_list = '関連URLなし' | |
else: | |
related_url_list = ', '.join(related_url) | |
unrelated_url_list = ', '.join(unrelated_url) | |
other_url_list = ', '.join(other_url) | |
return group, related_url_list, unrelated_url_list, other_url_list | |
def group2_classification(company_name): | |
group, related_url_list, unrelated_url_list, other_url_list = search(company_name) | |
if group == 'Group 3': | |
group = 'Group 2' | |
else: | |
group = 'Group 1-2' | |
return group, related_url_list, unrelated_url_list, other_url_list | |