-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathchains.py
More file actions
36 lines (32 loc) · 1.24 KB
/
Copy pathchains.py
File metadata and controls
36 lines (32 loc) · 1.24 KB
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
from langchain_classic.chains import create_history_aware_retriever, create_retrieval_chain
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
from config import (
OPENAI_MODEL_NAME,
OPENAI_MODEL_TEMPERATURE,
)
from memory import get_session_history
from vectorstore import get_vectorstore
from prompts import contextualize_prompt, qa_prompt
def get_rag_chain():
llm = ChatOpenAI(
model=OPENAI_MODEL_NAME,
temperature=OPENAI_MODEL_TEMPERATURE,
)
retriever = get_vectorstore().as_retriever()
history_aware_retriever = create_history_aware_retriever(llm, retriever, contextualize_prompt)
question_answer_chain = create_stuff_documents_chain(
llm=llm,
prompt=qa_prompt,
)
return create_retrieval_chain(history_aware_retriever, question_answer_chain)
def get_conversational_rag_chain():
rag_chain = get_rag_chain()
return RunnableWithMessageHistory(
runnable=rag_chain,
get_session_history=get_session_history,
input_messages_key='input',
history_messages_key='chat_history',
output_messages_key='answer',
)