-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm.py
148 lines (115 loc) · 4.65 KB
/
llm.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from langchain_pinecone import PineconeVectorStore
from dotenv import load_dotenv
from langchain_openai import OpenAIEmbeddings
from langchain_openai import ChatOpenAI
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
from langchain.chains import create_history_aware_retriever
from langchain_core.prompts import MessagesPlaceholder
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from config import answer_examples
load_dotenv()
store = {}
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = ChatMessageHistory()
return store[session_id]
def get_llm(model='gpt-4o-mini'):
llm = ChatOpenAI(model=model)
return llm
def get_retriever():
embedding = OpenAIEmbeddings(model='text-embedding-3-large')
index_name="tax-md"
database = PineconeVectorStore.from_existing_index(index_name=index_name, embedding=embedding)
retriever = database.as_retriever(search_kwargs={'k': 4})
return retriever
def get_dictionary_chain():
llm = get_llm()
dictionary = ["사람을 나타내는 표현 -> 거주자"]
prompt = ChatPromptTemplate.from_template(f"""
사용자의 질문을 보고, 우리의 사전을 참고해서 사용자의 질문을 변경해주세요.
변경할 필요가 없다고 판단되면, 사용자의 질문을 변경하지 않아도 됩니다.
사전: {dictionary}
질문: {{question}}
""")
dictionary_chain = prompt | llm | StrOutputParser()
return dictionary_chain
def get_history_retriever():
llm = get_llm()
retriever = get_retriever()
contextualize_q_system_prompt = (
"Given a chat history and the latest user question "
"which might reference context in the chat history, "
"formulate a standalone question which can be understood "
"without the chat history. Do NOT answer the question, "
"just reformulate it if needed and otherwise return it as is."
)
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
history_aware_retriever = create_history_aware_retriever(
llm, retriever, contextualize_q_prompt
)
return history_aware_retriever
def get_rag_chain():
llm = get_llm()
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{input}"),
("ai", "{answer}"),
]
)
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=answer_examples,
)
history_aware_retriever = get_history_retriever()
system_prompt = (
"당신은 소득세법 전문가입니다. 사용자가 입력한 소득세법에 관한 질문에 답변해주세요."
"아래에 제공된 문서를 활용해서 답변해주시고"
"답변을 알 수 없다면 모른다고 답변하면 됩니다"
"답변을 제공할 때는 소득세법 (??조)에 따르면 이라고 시작해주시고"
"핵심내용과 리스트 형태를 갖는 답변을 원하고"
"2-3줄 이하의 짧은 답변을 원합니다."
"\n\n"
"{context}"
)
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
few_shot_prompt,
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
conversational_rag_chain = RunnableWithMessageHistory(
rag_chain,
get_session_history,
input_messages_key='input',
history_messages_key='chat_history',
output_messages_key='answer',
).pick('answer')
return conversational_rag_chain
def get_ai_response(user_message):
rag_chain = get_rag_chain()
dictionary_chain = get_dictionary_chain()
tax_chain = {'input': dictionary_chain} | rag_chain
ai_response = tax_chain.stream(
{
'question': user_message
},
config={
"configurable": {'session_id': 'abc123'}
}
)
return ai_response