Chatbot giao quyết định
Run to view results
pdfpath
dutoanpath
chunhiem
chutri
loaihopdong
thuongmaihoa
def extractpdf(pdfpath):
#loadfile
data = process_pdf(pdfpath)
with open('file.txt', 'w', encoding='utf-8') as file:
file.write(data)
print(data)
#extract requirement
df = pd.read_excel(pdf_extract_file)
api_key = openaikey
description = extract_description(df)
print(description)
structure = extract_structure(df)
dict_datatype = extract_dict(df,'Parameter','Datatype')
print(dict_datatype)
dict_require = extract_dict(df,'Parameter','Require')
print(dict_require)
structure = extract_structure(df)
context = f'Extract needed data from users input about commercial contracts. Return data with the following structure:{structure}, with the information of data as following: {description}. Fill None for unextractable value. Dont explain. If users input is not relevant to contracts or agreement, dont extract. Correct output and spelling in line with language of input tex'
samplequestion = """
HỢP ĐÔNG DỊCH VỤ TƯ VẤN Số: 15/HĐDV/VPI-PVOIL/2023 về việc Thuê tư vấn cập nhật Kế hoạch theo Đơn hàng số 1293/ĐH-ATMT-ATPC Hôm nay, ngày 20 tháng 7 năm 2023 tại Tổng công Íụ Dâu Việt Nam — CTCP, tầng
14-18 Tòa nhà PetroVietNam Tower, số I-5 Lê Duẩn, P.Bắn Nghé, Quận 1, TP Hà Chí Minh, chúng tôi, đại diện cho các BỀN ký Hợp đẳng, gồm có: .BÊN A: TÓNG CÔNG TY DẢU VIỆT NAM —- CTCP (PVOIL) Địa chỉ: Tầng 14-18, Tòa nhà PetroVietnam Tower, số 1-5 Lê Duẫn, Phường Bến Nghé, Quận 1, TP. Hồ Chí Minh
Điện thoại: (028) 39106990 Fax: (028) 39106980 Mã số thuế: 0305795054 Người đại diện: Ông Võ Khánh Hưng Chức vụ: Phó Tổng Giám đốc (Theo Ủy quyền số 40/UQ-DVN ngày 24/4/2023) BÊN B: VIỆN DẦU KHÍ VIỆT NAM
Địa chỉ: 167 Phố Trung Kính, P. Yên Hòa, Q. Cầu Giấy, TP. Hà Nội Điện thoại: (024) 37843061 Fax: (024) 37844156 Tài khoản số: 0011000015920 tại Sở giao dịch Ngân hàng Ngoại thương Việt Nam Mã số thuế: 0100150295 Đại diện: Ông NGUYỄN ANH ĐỨC Chức vụ: Viện Trưởng ĐIỀU 4: HÌNH THỨC VÀ GIÁ TRỊ HỢP ĐÒNG 4.1. Hình thức Hợp đồng: Hợp đồng trọn gói. 4.2. Giá trị Hợp đồng là: 479.765.000 VNĐ (Bằng chữ: Bốn trăm bảy mươi chín triệu bảy trăm sảu mươi lăm ngàn đông). Giá trên đã bao gồm toàn bộ chỉ phí thực hiện Hợp đồng, thuế, phí, lệ phí và tất cả các chi phí khác để BÊN B thực hiện Hợp đồng này. - Chỉ tiết giá trị Hợp đồng tại Phụ lục 2 “Chỉ tiết giá Hợp đồng” đính kèm.
Loại hợp đồng, đơn hàng: Hợp đồng
Chủ nhiệm: Phí Ngọc Đông
"""
sampleanswer = "{'tenhopdong': 'Thuê tư vấn cập nhật Kế hoạch theo Đơn hàng số 1293/ĐH-ATMT-ATPC', 'sohopdong': '15/HĐDV/VPI-PVOIL/2023', 'thoigianhopdong': 'ngày 20 tháng 7 năm 2023', 'chutri': 'Nguyễn Anh Đức', 'khachhang': 'TÓNG CÔNG TY DẢU VIỆT NAM —- CTCP', 'bangchu': 'Bốn trăm bảy mươi chín triệu bảy trăm sáu mươi lăm nghìn đồng'}"
firstdict = get_openai_response(api_key, data, context, samplequestion, sampleanswer)
firstdict = ast.literal_eval(firstdict)
return firstdict
def cnct_output(chunhiem, chutri):
chunhiemdata, chutridata = query_database(chunhiem, chutri)
return chunhiemdata,chutridata
def dutoan_output(dutoanpath):
output = dutoan_extract(dutoanpath)
return output
Run to view results
def get_openai_response(api_key, query, context, samplequestion, sampleanswer, messages):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# Thêm thông tin ngữ cảnh và câu hỏi mới vào danh sách messages
if not messages:
messages.extend([
{"role": "system", "content": context},
{"role": "user", "content": samplequestion},
{"role": "assistant", "content": sampleanswer}
])
# Thêm câu hỏi mới từ người dùng
messages.append({"role": "user", "content": query})
data = {
"model": "gpt-3.5-turbo",
"messages": messages
}
url = "https://api.openai.com/v1/chat/completions"
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
response_data = response.json()
content = response_data['choices'][0]['message']['content']
# Thêm câu trả lời vào danh sách messages
messages.append({"role": "assistant", "content": content})
return content, messages
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None, messages
# Khởi tạo danh sách messages rỗng để lưu trữ cuộc trò chuyện
messages = []
Run to view results
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
executor.submit(extractpdf,pdfpath),
executor.submit(cnct_output,chunhiem, chutri),
executor.submit(dutoan_output,dutoanpath)
]
#future = concurrent.futures.as_completed(futures)
firstdict = futures[0].result()
seconddict = futures[1].result()[0]
thirddict = futures[1].result()[1]
forthdict = futures[2].result()
dicts = [firstdict,seconddict,thirddict, forthdict]
merged_dict = {}
for dict_str in dicts:
if isinstance(dict_str, str):
dict_obj = ast.literal_eval(dict_str.strip('"'))
elif isinstance(dict_str, dict):
dict_obj = dict_str
merged_dict.update(dict_obj)
else:
print(dict_str)
merged_dict['loaihopdong']=loaihopdong
merged_dict['thuongmaihoa']=thuongmaihoa
print(merged_dict)
autoreport(merged_dict, 'testlan3')
df = pd.DataFrame(merged_dict, index = [0])
writeback_database(df)
Run to view results
Run to view results