sql_qa_test.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import csv
  2. import time
  3. from langchain.callbacks import get_openai_callback
  4. from ai_agent import main
  5. from systex_app import save_history
  6. def agent(question):
  7. print(question)
  8. start = time.time()
  9. with get_openai_callback() as cb:
  10. result = main(question)
  11. answer = result["generation"]
  12. processing_time = time.time() - start
  13. save_history(question, answer, cb, processing_time)
  14. if "test@systex.com" in answer:
  15. answer = "很抱歉,目前我無法回答您的問題,請將您的詢問發送至 test@systex.com 以便獲得更進一步的幫助,謝謝。"
  16. print(answer)
  17. return result
  18. places = {
  19. '高雄': ["高雄廠", "高雄總部及運通廠", "運通廠", "總部", "高雄總部", "高雄"],
  20. '台北': ["台北廠", "台北辦事處", "台北", "臺北"],
  21. '廣興': ["廣興廠", "昆山廠", "廣興"],
  22. '北海': ["北海建準廠", "北海廠", "立準廠", "立準", "北海"],
  23. '菲律賓': ["菲律賓建準廠", "菲律賓"],
  24. '印度': ["India", "印度"],
  25. '美國': ["Inc", "美國"],
  26. '法國': ["SAS", "法國"],
  27. }
  28. items = ['直接排放', '能源間接排放', '運輸間接排放', '組織使用產品間接排放', '使用來自組織產品間接排放',
  29. '類別1', '類別2', '類別3', '類別4', '類別5', '類別6',
  30. '固定燃燒', '移動燃燒', '製程排放', '逸散排放', '土地利用',
  31. '外購電力', '外購能源',
  32. '上游運輸', '下游運輸', '員工通勤', '商務旅行', '訪客運輸',
  33. '購買產品', '外購燃料及能資源', '資本貨物', '上游租賃', '廢棄物處理', '廢棄物清運', '其他委外業務',
  34. '產品加工', '產品使用', '產品最終處理', '下游租賃', '投資排放',
  35. '其他間接排放']
  36. items2 = ['綠電', "灰電", "自產電力", "外購電力"]
  37. times = ["2022", "2023", "去年", "前年"]
  38. import random
  39. import math
  40. question_list = []
  41. for place in places.keys():
  42. print(place)
  43. loc_random = random.sample(places[place], k=math.ceil(len(places[place])/2))
  44. print(loc_random)
  45. for loc in loc_random:
  46. # item_random = random.sample(items, k=10)
  47. item_random = items2
  48. for item in item_random:
  49. year_time = random.choice(times)
  50. # questions_format = [f"建準{place}{year_time}的{item}是多少", f"{place}{year_time}的{item}排放量", f"{year_time}{place}的{item}是多少",
  51. # f"請問{year_time}的{place}{item}排放量是多少?", f"{year_time}建準{place}{item}"]
  52. questions_format = [f"建準{place}{year_time}的{item}是多少", f"{place}{year_time}的{item}使用量", f"{year_time}{place}的{item}是多少",
  53. f"請問{year_time}的{place}{item}使用量是多少?", f"{year_time}建準{place}{item}"]
  54. questions = random.sample(questions_format, k=2)
  55. # question_list.extend(questions)
  56. for question in questions:
  57. print(question)
  58. result = agent(question)
  59. labels = result.keys()
  60. with open("results2.csv", "a") as f:
  61. writer = csv.DictWriter(f, fieldnames=labels)
  62. writer.writeheader()
  63. writer.writerow(result)