This repository was archived by the owner on Nov 26, 2025. It is now read-only.
forked from NVIDIA/GenerativeAIExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchains.py
More file actions
208 lines (170 loc) · 7.79 KB
/
Copy pathchains.py
File metadata and controls
208 lines (170 loc) · 7.79 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
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""LLM Chains for executing Retrival Augmented Generation."""
import os
import logging
import nltk
from pathlib import Path
from typing import Generator, List, Dict, Any
from llama_index.core.prompts.base import Prompt
from llama_index.core.readers import download_loader
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.core.base.response.schema import StreamingResponse
from llama_index.core.node_parser import LangchainNodeParser
from llama_index.llms.langchain import LangChainLLM
from llama_index.embeddings.langchain import LangchainEmbedding
from langchain_core.output_parsers.string import StrOutputParser
from langchain_core.prompts.chat import ChatPromptTemplate
from RetrievalAugmentedGeneration.common.utils import (
LimitRetrievedNodesLength,
get_config,
get_doc_retriever,
get_llm,
get_text_splitter,
get_vector_index,
set_service_context,
get_embedding_model,
get_docs_vectorstore_llamaindex,
del_docs_vectorstore_llamaindex,
)
from RetrievalAugmentedGeneration.common.base import BaseExample
from RetrievalAugmentedGeneration.common.tracing import (
langchain_instrumentation_class_wrapper,
)
# nltk downloader
nltk.download("averaged_perceptron_tagger")
# prestage the embedding model
_ = get_embedding_model()
set_service_context()
logger = logging.getLogger(__name__)
text_splitter = None
@langchain_instrumentation_class_wrapper
class QAChatbot(BaseExample):
def ingest_docs(self, filepath: str, filename: str):
"""Ingest documents to the VectorDB."""
try:
logger.info(f"Ingesting {filename} in vectorDB")
_, ext = os.path.splitext(filename)
if ext.lower() == ".pdf":
PDFReader = download_loader("PDFReader")
loader = PDFReader()
documents = loader.load_data(file=Path(filepath))
else:
unstruct_reader = download_loader("UnstructuredReader")
loader = unstruct_reader()
documents = loader.load_data(file=Path(filepath), split_documents=False)
filename = filename[:-4]
for document in documents:
document.metadata = {"filename": filename, "common_field": "all"}
document.excluded_embed_metadata_keys = ["filename", "page_label"]
index = get_vector_index()
global text_splitter
if not text_splitter:
text_splitter = get_text_splitter()
node_parser = LangchainNodeParser(text_splitter)
nodes = node_parser.get_nodes_from_documents(documents)
index.insert_nodes(nodes)
logger.info(f"Document {filename} ingested successfully")
except Exception as e:
logger.error(f"Failed to ingest document due to exception {e}")
raise ValueError(
"Failed to upload document. Please upload an unstructured text document."
)
def get_documents(self):
"""Retrieves filenames stored in the vector store."""
return get_docs_vectorstore_llamaindex()
def delete_documents(self, filenames: List[str]):
"""Delete documents from the vector index."""
return del_docs_vectorstore_llamaindex(filenames)
def llm_chain(
self, query: str, chat_history: List["Message"], **kwargs
) -> Generator[str, None, None]:
"""Execute a simple LLM chain using the components defined above."""
logger.info("Using llm to generate response directly without knowledge base.")
set_service_context(**kwargs)
# TODO Include chat_history
prompt = get_config().prompts.chat_template.format(
context_str="", query_str=query
)
logger.info(f"Prompt used for response generation: {prompt}")
# stream_complete is returning empty response with NIM
# TODO: Use llama_index llm wrapper to stream response
if get_config().llm.model_engine == "triton-trt-llm":
llm = LangChainLLM(get_llm(**kwargs))
response = llm.stream_complete(
prompt,
tokens=kwargs.get("max_tokens", None),
callbacks=[self.cb_handler],
)
gen_response = (resp.delta for resp in response)
return gen_response
else:
# This is for get_config().llm.model_engine == "nvidia-ai-endpoints-nim"
user_input = [("user", get_config().prompts.chat_template)]
prompt_template = ChatPromptTemplate.from_messages(user_input)
llm = get_llm(**kwargs)
chain = prompt_template | llm | StrOutputParser()
augmented_user_input = "\n\nQuestion: " + query + "\n"
return chain.stream(
{"context_str": "", "query_str": query},
config={"callbacks": [self.cb_handler]},
)
def rag_chain(
self, query: str, chat_history: List["Message"], **kwargs
) -> Generator[str, None, None]:
"""Execute a Retrieval Augmented Generation chain using the components defined above."""
logger.info("Using rag to generate response from document")
set_service_context(**kwargs)
retriever = get_doc_retriever(num_nodes=get_config().retriever.top_k)
qa_template = Prompt(get_config().prompts.rag_template)
logger.info(f"Prompt used for response generation: {qa_template}")
# Handling Retrieval failure
nodes = retriever.retrieve(query)
if not nodes:
logger.warning("Retrieval failed to get any relevant context")
return iter(
[
"No response generated from LLM, make sure your query is relavent to the ingested document."
]
)
# TODO Include chat_history
query_engine = RetrieverQueryEngine.from_args(
retriever,
text_qa_template=qa_template,
node_postprocessors=[LimitRetrievedNodesLength()],
streaming=True,
)
response = query_engine.query(query)
# Properly handle an empty response
if isinstance(response, StreamingResponse):
return response.response_gen
logger.warning(
"No response generated from LLM, make sure you've ingested document."
)
return StreamingResponse(iter(["No response generated from LLM, make sure you have ingested document from the Knowledge Base Tab."])).response_gen # type: ignore
def document_search(self, content: str, num_docs: int) -> List[Dict[str, Any]]:
"""Search for the most relevant documents for the given search parameters."""
try:
retriever = get_doc_retriever(num_nodes=get_config().retriever.top_k)
nodes = retriever.retrieve(content)
output = []
for node in nodes:
file_name = nodes[0].metadata["filename"]
entry = {"score": node.score, "source": file_name, "content": node.text}
output.append(entry)
return output
except Exception as e:
logger.error(f"Error from /documentSearch endpoint. Error details: {e}")
return []