Update rag_system.py
Browse files- rag_system.py +23 -2
rag_system.py
CHANGED
@@ -12,6 +12,7 @@ from langchain.retrievers import ContextualCompressionRetriever
|
|
12 |
from langchain.retrievers.document_compressors import LLMChainExtractor
|
13 |
from langgraph.graph import Graph
|
14 |
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
|
|
|
15 |
|
16 |
# Load environment variables
|
17 |
load_dotenv()
|
@@ -41,11 +42,31 @@ def load_retrieval_qa_chain():
|
|
41 |
base_retriever=vectorstore.as_retriever()
|
42 |
)
|
43 |
|
44 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
46 |
llm,
|
47 |
retriever=compression_retriever,
|
48 |
-
return_source_documents=True
|
|
|
49 |
)
|
50 |
|
51 |
return qa_chain
|
|
|
12 |
from langchain.retrievers.document_compressors import LLMChainExtractor
|
13 |
from langgraph.graph import Graph
|
14 |
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
|
15 |
+
from langchain.prompts import PromptTemplate
|
16 |
|
17 |
# Load environment variables
|
18 |
load_dotenv()
|
|
|
42 |
base_retriever=vectorstore.as_retriever()
|
43 |
)
|
44 |
|
45 |
+
# Define your instruction/prompt
|
46 |
+
instruction = """๋น์ ์ RAG(Retrieval-Augmented Generation) ๊ธฐ๋ฐ AI ์ด์์คํดํธ์
๋๋ค. ๋ค์ ์ง์นจ์ ๋ฐ๋ผ ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ตํ์ธ์:
|
47 |
+
|
48 |
+
1. ๊ฒ์ ๊ฒฐ๊ณผ ํ์ฉ: ์ ๊ณต๋ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ถ์ํ๊ณ ๊ด๋ จ ์ ๋ณด๋ฅผ ์ฌ์ฉํด ๋ต๋ณํ์ธ์.
|
49 |
+
2. ์ ํ์ฑ ์ ์ง: ์ ๋ณด์ ์ ํ์ฑ์ ํ์ธํ๊ณ , ๋ถํ์คํ ๊ฒฝ์ฐ ์ด๋ฅผ ๋ช
์ํ์ธ์.
|
50 |
+
3. ๊ฐ๊ฒฐํ ์๋ต: ์ง๋ฌธ์ ์ง์ ๋ตํ๊ณ ํต์ฌ ๋ด์ฉ์ ์ง์คํ์ธ์.
|
51 |
+
4. ์ถ๊ฐ ์ ๋ณด ์ ์: ๊ด๋ จ๋ ์ถ๊ฐ ์ ๋ณด๊ฐ ์๋ค๋ฉด ์ธ๊ธํ์ธ์.
|
52 |
+
5. ์ค๋ฆฌ์ฑ ๊ณ ๋ ค: ๊ฐ๊ด์ ์ด๊ณ ์ค๋ฆฝ์ ์ธ ํ๋๋ฅผ ์ ์งํ์ธ์.
|
53 |
+
6. ํ๊ณ ์ธ์ : ๋ต๋ณํ ์ ์๋ ๊ฒฝ์ฐ ์์งํ ์ธ์ ํ์ธ์.
|
54 |
+
7. ๋ํ ์ ์ง: ์์ฐ์ค๋ฝ๊ฒ ๋ํ๋ฅผ ์ด์ด๊ฐ๊ณ , ํ์์ ํ์ ์ง๋ฌธ์ ์ ์ํ์ธ์.
|
55 |
+
|
56 |
+
ํญ์ ์ ํํ๊ณ ์ ์ฉํ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ๊ฒ์ ๋ชฉํ๋ก ํ์ธ์."""
|
57 |
+
|
58 |
+
# Create a prompt template
|
59 |
+
prompt_template = PromptTemplate(
|
60 |
+
input_variables=["context", "question"],
|
61 |
+
template=instruction + "\n\nContext: {context}\n\nQuestion: {question}\n\nAnswer:"
|
62 |
+
)
|
63 |
+
|
64 |
+
# Create ConversationalRetrievalChain with the new retriever and prompt
|
65 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
66 |
llm,
|
67 |
retriever=compression_retriever,
|
68 |
+
return_source_documents=True,
|
69 |
+
combine_docs_chain_kwargs={"prompt": prompt_template}
|
70 |
)
|
71 |
|
72 |
return qa_chain
|