soohyeon02 commited on
Commit
a5930e3
Β·
1 Parent(s): c722e8c

refactor: get function

Browse files
Files changed (1) hide show
  1. app.py +32 -10
app.py CHANGED
@@ -28,17 +28,39 @@ def get_pdf_text(pdf_docs):
28
  # 과제
29
  # μ•„λž˜ ν…μŠ€νŠΈ μΆ”μΆœ ν•¨μˆ˜λ₯Ό μž‘μ„±
30
 
31
- def get_text_file(docs):
32
- pass
33
-
34
-
35
- def get_csv_file(docs):
36
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- def get_json_file(docs):
39
- pass
40
 
41
-
42
  # λ¬Έμ„œλ“€μ„ μ²˜λ¦¬ν•˜μ—¬ ν…μŠ€νŠΈ 청크둜 λ‚˜λˆ„λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
43
  def get_text_chunks(documents):
44
  text_splitter = RecursiveCharacterTextSplitter(
@@ -64,7 +86,7 @@ def get_vectorstore(text_chunks):
64
  def get_conversation_chain(vectorstore):
65
  gpt_model_name = 'gpt-3.5-turbo'
66
  llm = ChatOpenAI(model_name = gpt_model_name) #gpt-3.5 λͺ¨λΈ λ‘œλ“œ
67
-
68
  # λŒ€ν™” 기둝을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”λͺ¨λ¦¬λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
69
  memory = ConversationBufferMemory(
70
  memory_key='chat_history', return_messages=True)
 
28
  # 과제
29
  # μ•„λž˜ ν…μŠ€νŠΈ μΆ”μΆœ ν•¨μˆ˜λ₯Ό μž‘μ„±
30
 
31
+ def get_text_file(text_docs):
32
+ temp_dir = tempfile.TemporaryDirectory()
33
+ temp_filepath = os.path.join(temp_dir.name, text_docs.name)
34
+ with open(temp_filepath, "wb") as f:
35
+ f.write(text_docs.getvalue())
36
+ text_loader = TextLoader(temp_filepath)
37
+ text_doc = text_loader.load()
38
+ return text_doc
39
+
40
+
41
+ def get_csv_file(csv_docs):
42
+ temp_dir = tempfile.TemporaryDirectory()
43
+ temp_filepath = os.path.join(temp_dir.name, csv_docs.name)
44
+ with open(temp_filepath, "wb") as f:
45
+ f.write(csv_docs.getvalue())
46
+ csv_loader = CSVLoader(temp_filepath)
47
+ csv_doc = csv_loader.load()
48
+ return csv_doc
49
+
50
+ def get_json_file(json_docs):
51
+ temp_dir = tempfile.TemporaryDirectory()
52
+ temp_filepath = os.path.join(temp_dir.name, json_docs.name)
53
+ with open(temp_filepath, "wb") as f:
54
+ f.write(json_docs.getvalue())
55
+ json_loader = JSONLoader(
56
+ file_path=temp_filepath,
57
+ jq_schema='.messages[].content',
58
+ text_content=False
59
+ )
60
+ json_doc = json_loader.load()
61
+ return json_doc
62
 
 
 
63
 
 
64
  # λ¬Έμ„œλ“€μ„ μ²˜λ¦¬ν•˜μ—¬ ν…μŠ€νŠΈ 청크둜 λ‚˜λˆ„λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
65
  def get_text_chunks(documents):
66
  text_splitter = RecursiveCharacterTextSplitter(
 
86
  def get_conversation_chain(vectorstore):
87
  gpt_model_name = 'gpt-3.5-turbo'
88
  llm = ChatOpenAI(model_name = gpt_model_name) #gpt-3.5 λͺ¨λΈ λ‘œλ“œ
89
+
90
  # λŒ€ν™” 기둝을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”λͺ¨λ¦¬λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
91
  memory = ConversationBufferMemory(
92
  memory_key='chat_history', return_messages=True)