Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 = './
|
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(
|
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.
|
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}'+' ๋ต๋ณ์ ๊ตฌ์ฒด์ ์ผ๋ก
|
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',
|