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
176 lines (158 loc) · 6.47 KB
/
llm_chain.py
File metadata and controls
176 lines (158 loc) · 6.47 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
import uuid
import torch
import datetime
import logging
from dataclasses import dataclass
from typing import Sequence, Optional
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.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}"
)
@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 = "qwen2:0.5b"
rerank_model: str = "BAAI/bge-reranker-base"
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"
)
class State(TypedDict):
"""State definition for the LLM Chain"""
input: str
chat_history: Annotated[Sequence[BaseMessage], add_messages]
context: str
answer: 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.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)
return create_retrieval_chain(history_aware_retriever, qa_chain)
def _create_graph(self) -> StateGraph:
"""Create the processing graph"""
workflow = StateGraph(state_schema=State)
workflow.add_edge(START, "model")
workflow.add_node("model", self._call_model)
return workflow.compile(checkpointer=MemorySaver())
def _call_model(self, state: State) -> dict:
"""Process the query through the model"""
logger.info(f"Processing query: {state['input']}")
response = self.chain.invoke(state)
logger.info(f"Received response: {response['answer']}")
return {
"chat_history": [
HumanMessage(state["input"]),
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}")
return result["answer"]
except Exception as e:
logger.error(f"Error processing query", exc_info=True)
return {"error": str(e), "status": "error"}