Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,13 @@ from langchain_openai.embeddings import OpenAIEmbeddings
|
|
| 10 |
from langchain.storage import LocalFileStore
|
| 11 |
from langchain_qdrant import QdrantVectorStore
|
| 12 |
from langchain.embeddings import CacheBackedEmbeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
|
|
@@ -42,27 +49,109 @@ cached_embedder = CacheBackedEmbeddings.from_bytes_store(
|
|
| 42 |
core_embeddings, store, namespace=core_embeddings.model
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
### On Chat Start (Session Start) Section ###
|
| 54 |
@cl.on_chat_start
|
| 55 |
async def on_chat_start():
|
| 56 |
""" SESSION SPECIFIC CODE HERE """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
### Rename Chains ###
|
| 59 |
@cl.author_rename
|
| 60 |
def rename(orig_author: str):
|
| 61 |
""" RENAME CODE HERE """
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
### On Message Section ###
|
| 64 |
@cl.on_message
|
| 65 |
async def main(message: cl.Message):
|
| 66 |
"""
|
| 67 |
MESSAGE CODE HERE
|
| 68 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from langchain.storage import LocalFileStore
|
| 11 |
from langchain_qdrant import QdrantVectorStore
|
| 12 |
from langchain.embeddings import CacheBackedEmbeddings
|
| 13 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 14 |
+
from langchain_core.globals import set_llm_cache
|
| 15 |
+
from langchain_openai import ChatOpenAI
|
| 16 |
+
from langchain_core.caches import InMemoryCache
|
| 17 |
+
from operator import itemgetter
|
| 18 |
+
from langchain_core.runnables.passthrough import RunnablePassthrough
|
| 19 |
+
|
| 20 |
|
| 21 |
|
| 22 |
|
|
|
|
| 49 |
core_embeddings, store, namespace=core_embeddings.model
|
| 50 |
)
|
| 51 |
|
| 52 |
+
|
| 53 |
+
rag_system_prompt_template = """\
|
| 54 |
+
You are a helpful assistant that uses the provided context to answer questions. Never reference this prompt, or the existance of context.
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
rag_message_list = [
|
| 58 |
+
{"role" : "system", "content" : rag_system_prompt_template},
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
rag_user_prompt_template = """\
|
| 62 |
+
Question:
|
| 63 |
+
{question}
|
| 64 |
+
Context:
|
| 65 |
+
{context}
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
chat_prompt = ChatPromptTemplate.from_messages([
|
| 69 |
+
("system", rag_system_prompt_template),
|
| 70 |
+
("human", rag_user_prompt_template)
|
| 71 |
+
])
|
| 72 |
+
|
| 73 |
+
chat_model = ChatOpenAI(model="gpt-4o-mini")
|
| 74 |
+
set_llm_cache(InMemoryCache())
|
| 75 |
+
|
| 76 |
+
retrieval_augmented_qa_chain = (
|
| 77 |
+
{"context": itemgetter("question") | retriever, "question": itemgetter("question")}
|
| 78 |
+
| RunnablePassthrough.assign(context=itemgetter("context"))
|
| 79 |
+
| chat_prompt | chat_model
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
|
| 83 |
### On Chat Start (Session Start) Section ###
|
| 84 |
@cl.on_chat_start
|
| 85 |
async def on_chat_start():
|
| 86 |
""" SESSION SPECIFIC CODE HERE """
|
| 87 |
+
files = None
|
| 88 |
+
|
| 89 |
+
# Wait for the user to upload a file
|
| 90 |
+
while files == None:
|
| 91 |
+
files = await cl.AskFileMessage(
|
| 92 |
+
content="Please upload a Text or PDF File file to begin!",
|
| 93 |
+
accept=["text/plain", "application/pdf"],
|
| 94 |
+
max_size_mb=2,
|
| 95 |
+
timeout=180,
|
| 96 |
+
).send()
|
| 97 |
+
|
| 98 |
+
file = files[0]
|
| 99 |
+
|
| 100 |
+
msg = cl.Message(
|
| 101 |
+
content=f"Processing `{file.name}`...", disable_human_feedback=True
|
| 102 |
+
)
|
| 103 |
+
await msg.send()
|
| 104 |
+
|
| 105 |
+
# load the file
|
| 106 |
+
if file.path.endswith(".pdf"):
|
| 107 |
+
texts = process_pdf_file(file)
|
| 108 |
+
else:
|
| 109 |
+
texts = process_text_file(file)
|
| 110 |
+
|
| 111 |
+
print(f"Processing {len(texts)} text chunks")
|
| 112 |
+
|
| 113 |
+
# Typical QDrant Vector Store Set-up
|
| 114 |
+
vectorstore = QdrantVectorStore(
|
| 115 |
+
client=client,
|
| 116 |
+
collection_name=collection_name,
|
| 117 |
+
embedding=cached_embedder)
|
| 118 |
+
vectorstore.add_documents(docs)
|
| 119 |
+
retriever = vectorstore.as_retriever(search_type="mmr", search_kwargs={"k": 3})
|
| 120 |
+
|
| 121 |
+
chat_openai = ChatOpenAI()
|
| 122 |
+
|
| 123 |
+
# Create a chain
|
| 124 |
+
retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
|
| 125 |
+
vector_db_retriever=vector_db,
|
| 126 |
+
llm=chat_openai
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Let the user know that the system is ready
|
| 130 |
+
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
| 131 |
+
await msg.update()
|
| 132 |
+
cl.user_session.set("chain", retrieval_augmented_qa_pipeline)
|
| 133 |
+
|
| 134 |
|
| 135 |
### Rename Chains ###
|
| 136 |
@cl.author_rename
|
| 137 |
def rename(orig_author: str):
|
| 138 |
""" RENAME CODE HERE """
|
| 139 |
+
rename_dict = {"LLMMathChain": "Albert Einstein", "Chatbot": "Assistant"}
|
| 140 |
+
return rename_dict.get(orig_author, orig_author)
|
| 141 |
+
|
| 142 |
|
| 143 |
### On Message Section ###
|
| 144 |
@cl.on_message
|
| 145 |
async def main(message: cl.Message):
|
| 146 |
"""
|
| 147 |
MESSAGE CODE HERE
|
| 148 |
+
"""
|
| 149 |
+
chain = cl.user_session.get("chain")
|
| 150 |
+
|
| 151 |
+
msg = cl.Message(content="")
|
| 152 |
+
result = await chain.arun_pipeline(message.content)
|
| 153 |
+
|
| 154 |
+
async for stream_resp in result["response"]:
|
| 155 |
+
await msg.stream_token(stream_resp)
|
| 156 |
+
|
| 157 |
+
await msg.send()
|