-
Notifications
You must be signed in to change notification settings - Fork 0
/
poc_1.py
90 lines (66 loc) · 2.56 KB
/
poc_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from openai import OpenAI
import os
from langchain.schema import Document
from io import StringIO
from PyPDF2 import PdfReader
def extract_insurance_clauses(uploaded_files):
print("extract_insurance_clauses method invoked!")
# LLM에 질의
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# PDF 파일 열기
# pdf_file = open(pdf_path, 'rb')
# pdf_reader = PyPDF2.PdfReader(pdf_file)
total_contents = ""
if uploaded_files is not None:
print('uploded_files length = ', len(uploaded_files))
pdf_reader = ''
for file in uploaded_files:
print('file name = ', file.name)
pdf_file = open(file.name, 'rb')
pdf_reader = PdfReader(pdf_file)
print('pdf file read finished!')
print('pdf_reader.pages = ', pdf_reader.pages)
text = ""
# len(pdf_reader.pages)
for page in [4,5,6]:
print('page= ', page)
text = pdf_reader.pages[page].extract_text()
prompt = f"""{text}\n
위 텍스트는 보험 계약의 약관입니다. 텍스트에서 보험 계약의 면책조항을 찾아주세요."""
response = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt
}
],
model="gpt-4",
temperature=0.2
)
content = response.choices[0].message.content
if content is None:
continue
total_contents += f"Page : {page+1} - " + content
return parse_insurance_clauses(total_contents)
def parse_insurance_clauses(total_contents):
print("parse_insurance_clauses method invoked!")
# 결과 파싱
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response2 = client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"""{total_contents}\n\n
조항별로 내용을 한줄로 묶어서 페이지, 조항 번호 - 조항 내용으로 파싱해주세요."""
},
{
"role": "assistant",
"content": "[페이지 번호] 페이지, [조항 번호] - [조항내용]\n"
},
],
model="gpt-3.5-turbo",
temperature=0.2
)
responseText = response2.choices[0].message.content
print("extract_insurance_clauses method finished!")
return responseText.split("\n")