forked from dmayboroda/minima
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_chain.py
More file actions
231 lines (206 loc) · 8.51 KB
/
llm_chain.py
File metadata and controls
231 lines (206 loc) · 8.51 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
import uuid
import torch
import datetime
import logging
from dataclasses import dataclass
from typing import Sequence, Optional
from langchain.schema import Document
from qdrant_client import QdrantClient
from langchain_ollama import ChatOllama
from minima_embed import MinimaEmbeddings
from langgraph.graph import START, StateGraph
from langchain_qdrant import QdrantVectorStore
from langchain_core.messages import BaseMessage
from langgraph.graph.message import add_messages
from typing_extensions import Annotated, TypedDict
from langgraph.checkpoint.memory import MemorySaver
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.messages import AIMessage, HumanMessage
from langchain.chains.retrieval import create_retrieval_chain
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import CrossEncoderReranker
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_community.cross_encoders.huggingface import HuggingFaceCrossEncoder
from langchain.chains.history_aware_retriever import create_history_aware_retriever
logger = logging.getLogger(__name__)
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."
)
SYSTEM_PROMPT = (
"You are an assistant for question-answering tasks. "
"Use the following pieces of retrieved context to answer "
"the question. If you don't know the answer, say that you "
"don't know. Use three sentences maximum and keep the "
"answer concise."
"\n\n"
"{context}"
)
QUERY_ENHANCEMENT_PROMPT = (
"You are an expert at converting user questions into queries."
"You have access to a users files."
"Perform query expansion."
"Just return one expanded query, do not add any other text."
"If there are acronyms or words you are not familiar with, do not try to rephrase them."
"Do not change the original meaning of the question and do not add any additional information."
)
class ParaphrasedQuery(BaseModel):
paraphrased_query: str = Field(
...,
description="A unique paraphrasing of the original question.",
)
@dataclass
class LLMConfig:
"""Configuration settings for the LLM Chain"""
qdrant_collection: str = "mnm_storage"
qdrant_host: str = "qdrant"
ollama_url: str = "http://ollama:11434"
ollama_model: str = os.environ.get("OLLAMA_MODEL")
rerank_model: str = os.environ.get("RERANKER_MODEL")
temperature: float = 0.5
device: torch.device = torch.device(
"mps" if torch.backends.mps.is_available() else
"cuda" if torch.cuda.is_available() else
"cpu"
)
@dataclass
class LocalConfig:
LOCAL_FILES_PATH = os.environ.get("LOCAL_FILES_PATH")
CONTAINER_PATH = os.environ.get("CONTAINER_PATH")
class State(TypedDict):
"""State definition for the LLM Chain"""
input: str
chat_history: Annotated[Sequence[BaseMessage], add_messages]
context: str
answer: str
init_query: str
class LLMChain:
"""A chain for processing LLM queries with context awareness and retrieval capabilities"""
def __init__(self, config: Optional[LLMConfig] = None):
"""Initialize the LLM Chain with optional custom configuration"""
self.localConfig = LocalConfig()
self.config = config or LLMConfig()
self.llm = self._setup_llm()
self.document_store = self._setup_document_store()
self.chain = self._setup_chain()
self.graph = self._create_graph()
def _setup_llm(self) -> ChatOllama:
"""Initialize the LLM model"""
return ChatOllama(
base_url=self.config.ollama_url,
model=self.config.ollama_model,
temperature=self.config.temperature
)
def _setup_document_store(self) -> QdrantVectorStore:
"""Initialize the document store with vector embeddings"""
qdrant = QdrantClient(host=self.config.qdrant_host)
embed_model = MinimaEmbeddings()
return QdrantVectorStore(
client=qdrant,
collection_name=self.config.qdrant_collection,
embedding=embed_model
)
def _setup_chain(self):
"""Set up the retrieval and QA chain"""
# Initialize retriever with reranking
base_retriever = self.document_store.as_retriever()
reranker = HuggingFaceCrossEncoder(
model_name=self.config.rerank_model,
model_kwargs={'device': self.config.device},
)
compression_retriever = ContextualCompressionRetriever(
base_compressor=CrossEncoderReranker(model=reranker, top_n=3),
base_retriever=base_retriever
)
# Create history-aware retriever
contextualize_prompt = ChatPromptTemplate.from_messages([
("system", CONTEXTUALIZE_Q_SYSTEM_PROMPT),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
])
history_aware_retriever = create_history_aware_retriever(
self.llm, compression_retriever, contextualize_prompt
)
# Create QA chain
qa_prompt = ChatPromptTemplate.from_messages([
("system", SYSTEM_PROMPT),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
])
qa_chain = create_stuff_documents_chain(self.llm, qa_prompt)
retrieval_chain = create_retrieval_chain(history_aware_retriever, qa_chain)
return retrieval_chain
def _create_graph(self) -> StateGraph:
"""Create the processing graph"""
workflow = StateGraph(state_schema=State)
workflow.add_node("enhance", self._enhance_query)
workflow.add_node("retrieval", self._call_model)
workflow.add_edge(START, "enhance")
workflow.add_edge("enhance", "retrieval")
return workflow.compile(checkpointer=MemorySaver())
def _enhance_query(self, state: State) -> str:
"""Enhance the query using the LLM"""
prompt_enhancement = ChatPromptTemplate.from_messages([
("system", QUERY_ENHANCEMENT_PROMPT),
("human", "{input}"),
])
query_enhancement = prompt_enhancement | self.llm
enhanced_query = query_enhancement.invoke({
"input": state["input"]
})
logger.info(f"Enhanced query: {enhanced_query}")
state["init_query"] = state["input"]
state["input"] = enhanced_query.content
return state
def _call_model(self, state: State) -> dict:
"""Process the query through the model"""
logger.info(f"Processing query: {state['init_query']}")
logger.info(f"Enhanced query: {state['input']}")
response = self.chain.invoke(state)
logger.info(f"Received response: {response['answer']}")
return {
"chat_history": [
HumanMessage(state["init_query"]),
AIMessage(response["answer"]),
],
"context": response["context"],
"answer": response["answer"],
}
def invoke(self, message: str) -> dict:
"""
Process a user message and return the response
Args:
message: The user's input message
Returns:
dict: Contains the model's response or error information
"""
try:
logger.info(f"Processing query: {message}")
config = {
"configurable": {
"thread_id": uuid.uuid4(),
"thread_ts": datetime.datetime.now().isoformat()
}
}
result = self.graph.invoke(
{"input": message},
config=config
)
logger.info(f"OUTPUT: {result}")
links = set()
for ctx in result["context"]:
doc: Document = ctx
path = doc.metadata["file_path"].replace(
self.localConfig.CONTAINER_PATH,
self.localConfig.LOCAL_FILES_PATH
)
links.add(f"file://{path}")
return {"answer": result["answer"], "links": links}
except Exception as e:
logger.error(f"Error processing query", exc_info=True)
return {"error": str(e), "status": "error"}