import csv import time from langchain.callbacks import get_openai_callback from ai_agent import main from systex_app import save_history def agent(question): print(question) start = time.time() with get_openai_callback() as cb: result = main(question) answer = result["generation"] processing_time = time.time() - start save_history(question, answer, cb, processing_time) if "test@systex.com" in answer: answer = "很抱歉,目前我無法回答您的問題,請將您的詢問發送至 test@systex.com 以便獲得更進一步的幫助,謝謝。" print(answer) return result places = { '高雄': ["高雄廠", "高雄總部及運通廠", "運通廠", "總部", "高雄總部", "高雄"], '台北': ["台北廠", "台北辦事處", "台北", "臺北"], '廣興': ["廣興廠", "昆山廠", "廣興"], '北海': ["北海建準廠", "北海廠", "立準廠", "立準", "北海"], '菲律賓': ["菲律賓建準廠", "菲律賓"], '印度': ["India", "印度"], '美國': ["Inc", "美國"], '法國': ["SAS", "法國"], } items = ['直接排放', '能源間接排放', '運輸間接排放', '組織使用產品間接排放', '使用來自組織產品間接排放', '類別1', '類別2', '類別3', '類別4', '類別5', '類別6', '固定燃燒', '移動燃燒', '製程排放', '逸散排放', '土地利用', '外購電力', '外購能源', '上游運輸', '下游運輸', '員工通勤', '商務旅行', '訪客運輸', '購買產品', '外購燃料及能資源', '資本貨物', '上游租賃', '廢棄物處理', '廢棄物清運', '其他委外業務', '產品加工', '產品使用', '產品最終處理', '下游租賃', '投資排放', '其他間接排放'] items2 = ['綠電', "灰電", "自產電力", "外購電力"] times = ["2022", "2023", "去年", "前年"] import random import math question_list = [] for place in places.keys(): print(place) loc_random = random.sample(places[place], k=math.ceil(len(places[place])/2)) print(loc_random) for loc in loc_random: # item_random = random.sample(items, k=10) item_random = items2 for item in item_random: year_time = random.choice(times) # questions_format = [f"建準{place}{year_time}的{item}是多少", f"{place}{year_time}的{item}排放量", f"{year_time}{place}的{item}是多少", # f"請問{year_time}的{place}{item}排放量是多少?", f"{year_time}建準{place}{item}"] questions_format = [f"建準{place}{year_time}的{item}是多少", f"{place}{year_time}的{item}使用量", f"{year_time}{place}的{item}是多少", f"請問{year_time}的{place}{item}使用量是多少?", f"{year_time}建準{place}{item}"] questions = random.sample(questions_format, k=2) # question_list.extend(questions) for question in questions: print(question) result = agent(question) labels = result.keys() with open("results2.csv", "a") as f: writer = csv.DictWriter(f, fieldnames=labels) writer.writeheader() writer.writerow(result)