juheechoi01 commited on
Commit
fb91e06
ยท
verified ยท
1 Parent(s): c323a88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -16
app.py CHANGED
@@ -21,27 +21,20 @@ def reset_chat():
21
  st.session_state.messages = []
22
  st.session_state.context = None
23
 
24
- DB_PATH = './chroma_db'
25
  vectorstore = Chroma(persist_directory=DB_PATH, embedding_function=UpstageEmbeddings(model="solar-embedding-1-large"))
26
-
27
  chat = ChatUpstage(upstage_api_key = 'up_0NlJbP7KsNtBpQm2U0S7uCY3RMnFP')
28
 
29
  from langchain.retrievers.multi_query import MultiQueryRetriever
30
  retriever = MultiQueryRetriever.from_llm(
31
- retriever=vectorstore.as_retriever(k=5), llm=chat)
32
 
33
- # 1) ์ฑ—๋ด‡์— '๊ธฐ์–ต'์„ ์ž…ํžˆ๊ธฐ ์œ„ํ•œ ์ฒซ๋ฒˆ์งธ ๋‹จ๊ณ„
34
 
35
- # ์ด์ „ ๋ฉ”์‹œ์ง€๋“ค๊ณผ ์ตœ์‹  ์‚ฌ์šฉ์ž ์งˆ๋ฌธ์„ ๋ถ„์„ํ•ด, ๋ฌธ๋งฅ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์—†์ด ํ˜ผ์ž์„œ๋งŒ ๋ดค์„ ๋•Œ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ์งˆ๋ฌธ์„ ๋‹ค์‹œ ๊ตฌ์„ฑํ•จ.
36
- # ์ฆ‰ ์ƒˆ๋กœ ๋“ค์–ด์˜จ ๊ทธ ์งˆ๋ฌธ ์ž์ฒด์—๋งŒ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹ค์‹œ ์žฌํŽธ์„ฑ (llm ์˜ˆ์ „ ๋Œ€ํ™”๋ฅผ ๊ธฐ์–ตํ•ด์„œ ๋Œ€ํ™”๋ฅผ ์žฌ๊ตฌ์„ฑ)
37
  from langchain.chains import create_history_aware_retriever
38
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
39
 
40
  contextualize_q_system_prompt = "When there are older conversations and more recent user questions, these questions may be related to previous conversations. In this case, change the question to a question that can be understood independently without needing to know the content of the conversation. You don't have to answer the question, just reformulate it if necessary or leave it as is."
41
 
42
- # MessagePlaceHolder: 'chat_history' ์ž…๋ ฅ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ „ ๋ฉ”์„ธ์ง€ ๊ธฐ๋ก๋“ค์„ ํ”„๋กฌํ”„ํŠธ์— ํฌํ•จ์‹œํ‚ด.
43
- # ์ฆ‰, ํ”„๋กฌํ”„ํŠธ, ๋ฉ”์„ธ์ง€ ๊ธฐ๋ก(๋ฌธ๋งฅ ์ •๋ณด), ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์œผ๋กœ ํ”„๋กฌํ”„ํŠธ๊ฐ€ ๊ตฌ์„ฑ๋จ.
44
-
45
  contextualize_q_prompt = ChatPromptTemplate.from_messages(
46
  [
47
  ('system', contextualize_q_system_prompt),
@@ -50,17 +43,15 @@ contextualize_q_prompt = ChatPromptTemplate.from_messages(
50
  ]
51
  )
52
 
53
- # ์ด๋ฅผ ํ† ๋Œ€๋กœ ๋ฉ”์„ธ์ง€ ๊ธฐ๋ก์„ ๊ธฐ์–ตํ•˜๋Š” retriever๋ฅผ ์ƒ์„ฑ.
54
  history_aware_retriever = create_history_aware_retriever(
55
  chat, retriever, contextualize_q_prompt
56
  )
57
 
58
- # 2) ์ฒด์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋Š” retriever ์ฒด์ธ ์ƒ์„ฑ
59
  from langchain.chains import create_retrieval_chain
60
  from langchain.chains.combine_documents import create_stuff_documents_chain
61
 
62
  qa_system_prompt = """
63
- You are an intelligent assistant helping the members of the Korean National Assembly with questions related to law and policy. As you will respond to the Korean National Assembly, you must answer politely. Read the given questions carefully and give the answer in Korean ONLY using the following pieces of the context. WRITE YOUR ANSWER ONLY BASED ON THE CONTEXT.
64
 
65
  DO NOT TRY TO MAKE UP AN ANSWER:
66
  ย - If the answer to the question cannot be determined from the context alone, say "I cannot determine the answer to that.".
@@ -72,19 +63,17 @@ qa_prompt = ChatPromptTemplate.from_messages(
72
  [
73
  ('system', qa_system_prompt),
74
  MessagesPlaceholder('chat_history'),
75
- ('human','{input}'+' ๋‹ต๋ณ€์€ ๊ตฌ์ฒด์ ์œผ๋กœ ์‹œ๊ฐ„์— ํ๋ฆ„์— ๋”ฐ๋ผ ์ž‘์„ฑํ•ด์ค˜. ๊ทธ๋ฆฌ๊ณ  ๋‹ต๋ณ€ํ•  ๋•Œ metadata์— ์žˆ๋Š” source๋„ ํ•จ๊ป˜ ์ œ๊ณตํ•ด์ค˜.'),
76
  ]
77
  )
78
 
79
  question_answer_chain = create_stuff_documents_chain(chat, qa_prompt)
80
 
81
- # ๊ฒฐ๊ณผ๊ฐ’์€ input, chat_history, context, answer ํฌํ•จํ•จ.
82
  rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
83
 
84
 
85
-
86
  # ์›น์‚ฌ์ดํŠธ ์ œ๋ชฉ
87
- st.title("๊ตญํšŒ ํšŒ์˜๋ก ๊ธฐ๋ฐ˜ ์ฑ—๋ด‡ ์„œ๋น„์Šค ๐Ÿš€")
88
 
89
  if 'messages' not in st.session_state:
90
  st.session_state['messages'] = [{'role': 'assistant',
 
21
  st.session_state.messages = []
22
  st.session_state.context = None
23
 
24
+ DB_PATH = './chroma_db_3'
25
  vectorstore = Chroma(persist_directory=DB_PATH, embedding_function=UpstageEmbeddings(model="solar-embedding-1-large"))
 
26
  chat = ChatUpstage(upstage_api_key = 'up_0NlJbP7KsNtBpQm2U0S7uCY3RMnFP')
27
 
28
  from langchain.retrievers.multi_query import MultiQueryRetriever
29
  retriever = MultiQueryRetriever.from_llm(
30
+ retriever=vectorstore.as_retriever(), llm=chat)
31
 
 
32
 
 
 
33
  from langchain.chains import create_history_aware_retriever
34
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
35
 
36
  contextualize_q_system_prompt = "When there are older conversations and more recent user questions, these questions may be related to previous conversations. In this case, change the question to a question that can be understood independently without needing to know the content of the conversation. You don't have to answer the question, just reformulate it if necessary or leave it as is."
37
 
 
 
 
38
  contextualize_q_prompt = ChatPromptTemplate.from_messages(
39
  [
40
  ('system', contextualize_q_system_prompt),
 
43
  ]
44
  )
45
 
 
46
  history_aware_retriever = create_history_aware_retriever(
47
  chat, retriever, contextualize_q_prompt
48
  )
49
 
 
50
  from langchain.chains import create_retrieval_chain
51
  from langchain.chains.combine_documents import create_stuff_documents_chain
52
 
53
  qa_system_prompt = """
54
+ You are an intelligent assistant helping the members of the Korean National Assembly with questions related to law and policy. You must answer politely. Read the given questions carefully and give the answer in Korean ONLY using the following pieces of the context. AGAIN, WRITE YOUR ANSWER ONLY BASED ON THE CONTEXT FROM THE DATABASE AND DON'T SEARCH ON THE INTERNET.
55
 
56
  DO NOT TRY TO MAKE UP AN ANSWER:
57
  ย - If the answer to the question cannot be determined from the context alone, say "I cannot determine the answer to that.".
 
63
  [
64
  ('system', qa_system_prompt),
65
  MessagesPlaceholder('chat_history'),
66
+ ('human','{input}'+' ๋‹ต๋ณ€์€ ๊ตฌ์ฒด์ ์œผ๋กœ ์ตœ์‹  ์ •๋ณด๋ถ€ํ„ฐ ์‹œ๊ฐ„์˜ ํ๋ฆ„์— ๋”ฐ๋ผ ์ž‘์„ฑํ•ด์ค˜. ๊ทธ๋ฆฌ๊ณ  ๋‹ต๋ณ€ํ•  ๋•Œ metadata์— ์žˆ๋Š” source๋ฅผ ํ•จ๊ป˜ ์ œ๊ณตํ•ด์ค˜.'),
67
  ]
68
  )
69
 
70
  question_answer_chain = create_stuff_documents_chain(chat, qa_prompt)
71
 
 
72
  rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
73
 
74
 
 
75
  # ์›น์‚ฌ์ดํŠธ ์ œ๋ชฉ
76
+ st.title("๊ตญํšŒ ํšŒ์˜๋ก ๊ธฐ๋ฐ˜ ์ฑ—๋ด‡ ์„œ๋น„์Šค :orange[NaraRAG] ๐Ÿ“œโš–๏ธ")
77
 
78
  if 'messages' not in st.session_state:
79
  st.session_state['messages'] = [{'role': 'assistant',