-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
334 lines (279 loc) · 10.9 KB
/
app.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import os
import uuid
import datasets
from langchain_huggingface import HuggingFaceEndpointEmbeddings
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain.agents import create_react_agent, AgentExecutor
from langchain.tools.retriever import create_retriever_tool
from langchain_core.messages import SystemMessage
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document
from langchain_core.prompts import PromptTemplate
from langchain_community.vectorstores.chroma import Chroma
import chromadb
from chromadb.config import Settings
from chromadb.utils.embedding_functions import HuggingFaceEmbeddingServer
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
from urllib3.exceptions import ProtocolError
from langchain.retrievers import ContextualCompressionRetriever
from transformers import AutoTokenizer
from tools import get_tools
from tei_rerank import TEIRerank
import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml.loader import SafeLoader
from langchain.globals import set_verbose, set_debug
set_verbose(True)
set_debug(True)
st.set_page_config(layout="wide", page_title="InSightful")
def authenticate():
with open(".streamlit/config.yaml") as file:
config = yaml.load(file, Loader=SafeLoader)
authenticator = stauth.Authenticate(
config["credentials"],
config["cookie"]["name"],
config["cookie"]["key"],
config["cookie"]["expiry_days"],
config["pre-authorized"],
)
name, authentication_status, username = authenticator.login()
st.session_state["authentication_status"] = authentication_status
st.session_state["username"] = username
return authenticator
# Set up Chroma DB client
@st.cache_resource
def setup_chroma_client():
client = chromadb.HttpClient(
host="http://{host}:{port}".format(
host=os.getenv("VECTORDB_HOST", "localhost"),
port=os.getenv("VECTORDB_PORT", "8000"),
),
settings=Settings(allow_reset=True, anonymized_telemetry=False),
)
return client
# Set up Chroma embedding function
@st.cache_resource
def hf_embedding_server():
_embedding_function = HuggingFaceEmbeddingServer(
url="http://{host}:{port}/embed".format(
host=os.getenv("TEI_HOST", "localhost"), port=os.getenv("TEI_PORT", "8081")
)
)
return _embedding_function
# Set up HuggingFaceEndpoint model
@st.cache_resource
def setup_chat_endpoint():
model = ChatOpenAI(
base_url="http://{host}:{port}/v1".format(
host=os.getenv("TGI_HOST", "localhost"), port=os.getenv("TGI_PORT", "8080")
),
max_tokens=os.getenv("MAX_TOKENS", 1024),
temperature=0.7,
api_key="dummy",
)
return model
# Set up Portkey integrated model
@st.cache_resource
def setup_portkey_integrated_model():
from portkey_ai import createHeaders, PORTKEY_GATEWAY_URL
from langchain_openai import ChatOpenAI
portkey_headers = createHeaders(
api_key=os.getenv("PORTKEY_API_KEY"),
custom_host=os.getenv("PORTKEY_CUSTOM_HOST"),
provider=os.getenv("PORTKEY_PROVIDER"),
)
model = ChatOpenAI(
api_key="None",
base_url=PORTKEY_GATEWAY_URL,
model="qwen2", # Verify the exact model name
default_headers=portkey_headers,
)
return model
# Set up HuggingFaceEndpointEmbeddings embedder
@st.cache_resource
def setup_huggingface_embeddings():
embedder = HuggingFaceEndpointEmbeddings(
model="http://{host}:{port}".format(
host=os.getenv("TEI_HOST", "localhost"), port=os.getenv("TEI_PORT", "8081")
),
task="feature-extraction",
)
return embedder
@st.cache_resource
def load_prompt_and_system_ins(
template_file_path="templates/prompt_template.tmpl", template=None
):
# prompt = hub.pull("hwchase17/react-chat")
prompt = PromptTemplate.from_file(template_file_path)
# Set up prompt template
template = """
Based on the retrieved context, respond with an accurate answer. Use the provided tools to support your response.
Be concise and always provide accurate, specific, and relevant information.
"""
system_instructions = SystemMessage(
content=template,
metadata={"role": "system"},
)
return prompt, system_instructions
class RAG:
def __init__(self, collection_name, db_client):
self.collection_name = collection_name
self.db_client = db_client
@retry(
retry=retry_if_exception_type(ProtocolError),
stop=stop_after_attempt(5),
wait=wait_fixed(2),
)
def load_documents(self, doc, num_docs=250):
documents = []
for data in datasets.load_dataset(
doc, split=f"train[:{num_docs}]", num_proc=10
).to_list():
documents.append(
Document(
page_content=data["text"],
metadata=dict(user=data["user"], workspace=data["workspace"]),
)
)
print("Document loaded")
return documents
def chunk_doc(self, pages, chunk_size=512, chunk_overlap=30):
tokenizer = AutoTokenizer.from_pretrained("BAAI/bge-large-en-v1.5")
text_splitter = RecursiveCharacterTextSplitter.from_huggingface_tokenizer(
tokenizer, chunk_size=chunk_size, chunk_overlap=chunk_overlap
)
chunks = text_splitter.split_documents(pages)
print("Document chunked")
return chunks
def insert_embeddings(self, chunks, chroma_embedding_function, batch_size=32):
print(
"Inserting embeddings into collection: {collection_name}".format(
collection_name=self.collection_name
)
)
collection = self.db_client.get_or_create_collection(
self.collection_name, embedding_function=chroma_embedding_function
)
for i in range(0, len(chunks), batch_size):
batch = chunks[i : i + batch_size]
chunk_ids = [str(uuid.uuid1()) for _ in batch]
metadatas = [chunk.metadata for chunk in batch]
documents = [chunk.page_content for chunk in batch]
collection.add(ids=chunk_ids, metadatas=metadatas, documents=documents)
print("Embeddings inserted\n")
def get_retriever(self, vector_store, use_reranker=False):
retriever = vector_store.as_retriever(
search_type="similarity", search_kwargs={"k": 10}
)
if use_reranker:
compressor = TEIRerank(
url="http://{host}:{port}".format(
host=os.getenv("RERANKER_HOST", "localhost"),
port=os.getenv("RERANKER_PORT", "8082"),
),
top_n=4,
batch_size=10,
)
retriever = ContextualCompressionRetriever(
base_compressor=compressor, base_retriever=retriever
)
return retriever
def query_docs(
self, model, question, vector_store, prompt, chat_history, use_reranker=False
):
retriever = self.get_retriever(vector_store, use_reranker)
pass_question = lambda input: input["question"]
rag_chain = (
RunnablePassthrough.assign(context=pass_question | retriever | format_docs)
| prompt
| model
| StrOutputParser()
)
return rag_chain.stream({"question": question, "chat_history": chat_history})
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
def create_retriever(
name, description, client, chroma_embedding_function, embedding_svc, reranker=False
):
collection_name = "software-slacks"
rag = RAG(collection_name=collection_name, db_client=client)
pages = rag.load_documents("spencer/software_slacks", num_docs=100)
chunks = rag.chunk_doc(pages)
rag.insert_embeddings(chunks, chroma_embedding_function)
vector_store = Chroma(
embedding_function=embedding_svc,
collection_name=collection_name,
client=client,
)
retriever = rag.get_retriever(vector_store, use_reranker=reranker)
retriever = vector_store.as_retriever(
search_type="similarity", search_kwargs={"k": 10}
)
return create_retriever_tool(retriever, name, description)
@st.cache_resource
def setup_agent(_model, _prompt, _tools):
agent = create_react_agent(
llm=_model,
prompt=_prompt,
tools=_tools,
)
agent_executor = AgentExecutor(
agent=agent, verbose=True, tools=_tools, handle_parsing_errors=True
)
return agent_executor
def main():
client = setup_chroma_client()
chroma_embedding_function = hf_embedding_server()
prompt, system_instructions = load_prompt_and_system_ins()
if os.getenv("ENABLE_PORTKEY", "False") == "True":
model = setup_portkey_integrated_model()
else:
model = setup_chat_endpoint()
embedder = setup_huggingface_embeddings()
use_reranker = os.getenv("USE_RERANKER", "False") == "True"
retriever_tool = create_retriever(
"slack_conversations_retriever",
"Useful for when you need to answer from Slack conversations.",
client,
chroma_embedding_function,
embedder,
reranker=use_reranker,
)
_tools = get_tools()
_tools.append(retriever_tool)
agent_executor = setup_agent(model, prompt, _tools)
st.title("InSightful: Your AI Assistant for community questions")
st.text("Made with ❤️ by InfraCloud Technologies")
st.markdown(
"""
InSightful is an AI assistant that helps you with your questions.
- It can browse past conversations with your colleagues/teammates and can search StackOverflow for technical questions.
- With access to the web, InSightful can also conduct its own research for you."""
)
chat_history = st.session_state.get(
"chat_history", [{"role": "system", "content": system_instructions.content}]
)
for message in chat_history[1:]:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if question := st.chat_input("Enter your question here"):
st.chat_message("user").markdown(question)
chat_history.append({"role": "user", "content": question})
with st.spinner():
response = agent_executor.invoke(
{
"input": question,
"chat_history": chat_history,
}
)["output"]
st.chat_message("assistant").markdown(response)
chat_history.append({"role": "assistant", "content": response})
st.session_state["chat_history"] = chat_history
if __name__ == "__main__":
# authenticator = authenticate()
# if st.session_state['authentication_status']:
# authenticator.logout()
main()