Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,4 @@
|
|
| 1 |
### Import Section ###
|
| 2 |
-
"""
|
| 3 |
-
IMPORTS HERE
|
| 4 |
-
"""
|
| 5 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_community.document_loaders import PyMuPDFLoader
|
| 7 |
from qdrant_client import QdrantClient
|
|
@@ -23,9 +20,6 @@ import chainlit as cl
|
|
| 23 |
|
| 24 |
|
| 25 |
### Global Section ###
|
| 26 |
-
"""
|
| 27 |
-
GLOBAL CODE HERE
|
| 28 |
-
"""
|
| 29 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 30 |
Loader = PyMuPDFLoader
|
| 31 |
|
|
@@ -56,10 +50,10 @@ chat_model = ChatOpenAI(model="gpt-4o-mini")
|
|
| 56 |
set_llm_cache(InMemoryCache())
|
| 57 |
chat_openai = ChatOpenAI()
|
| 58 |
|
|
|
|
| 59 |
### On Chat Start (Session Start) Section ###
|
| 60 |
@cl.on_chat_start
|
| 61 |
async def on_chat_start():
|
| 62 |
-
""" SESSION SPECIFIC CODE HERE """
|
| 63 |
files = None
|
| 64 |
|
| 65 |
# Wait for the user to upload a file
|
|
@@ -111,21 +105,21 @@ async def on_chat_start():
|
|
| 111 |
retriever = vectorstore.as_retriever(search_type="mmr", search_kwargs={"k": 3})
|
| 112 |
|
| 113 |
# Create a chain
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
# Let the user know that the system is ready
|
| 120 |
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
| 121 |
await msg.update()
|
| 122 |
-
cl.user_session.set("chain",
|
| 123 |
|
| 124 |
|
| 125 |
### Rename Chains ###
|
| 126 |
@cl.author_rename
|
| 127 |
def rename(orig_author: str):
|
| 128 |
-
""" RENAME CODE HERE """
|
| 129 |
rename_dict = {"LLMMathChain": "Albert Einstein", "Chatbot": "Assistant"}
|
| 130 |
return rename_dict.get(orig_author, orig_author)
|
| 131 |
|
|
@@ -133,9 +127,6 @@ def rename(orig_author: str):
|
|
| 133 |
### On Message Section ###
|
| 134 |
@cl.on_message
|
| 135 |
async def main(message: cl.Message):
|
| 136 |
-
"""
|
| 137 |
-
MESSAGE CODE HERE
|
| 138 |
-
"""
|
| 139 |
chain = cl.user_session.get("chain")
|
| 140 |
|
| 141 |
msg = cl.Message(content="")
|
|
|
|
| 1 |
### Import Section ###
|
|
|
|
|
|
|
|
|
|
| 2 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 3 |
from langchain_community.document_loaders import PyMuPDFLoader
|
| 4 |
from qdrant_client import QdrantClient
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
### Global Section ###
|
|
|
|
|
|
|
|
|
|
| 23 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 24 |
Loader = PyMuPDFLoader
|
| 25 |
|
|
|
|
| 50 |
set_llm_cache(InMemoryCache())
|
| 51 |
chat_openai = ChatOpenAI()
|
| 52 |
|
| 53 |
+
|
| 54 |
### On Chat Start (Session Start) Section ###
|
| 55 |
@cl.on_chat_start
|
| 56 |
async def on_chat_start():
|
|
|
|
| 57 |
files = None
|
| 58 |
|
| 59 |
# Wait for the user to upload a file
|
|
|
|
| 105 |
retriever = vectorstore.as_retriever(search_type="mmr", search_kwargs={"k": 3})
|
| 106 |
|
| 107 |
# Create a chain
|
| 108 |
+
retrieval_augmented_qa_chain = (
|
| 109 |
+
{"context": itemgetter("question") | retriever, "question": itemgetter("question")}
|
| 110 |
+
| RunnablePassthrough.assign(context=itemgetter("context"))
|
| 111 |
+
| chat_prompt | chat_model
|
| 112 |
)
|
| 113 |
|
| 114 |
# Let the user know that the system is ready
|
| 115 |
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
| 116 |
await msg.update()
|
| 117 |
+
cl.user_session.set("chain", retrieval_augmented_qa_chain)
|
| 118 |
|
| 119 |
|
| 120 |
### Rename Chains ###
|
| 121 |
@cl.author_rename
|
| 122 |
def rename(orig_author: str):
|
|
|
|
| 123 |
rename_dict = {"LLMMathChain": "Albert Einstein", "Chatbot": "Assistant"}
|
| 124 |
return rename_dict.get(orig_author, orig_author)
|
| 125 |
|
|
|
|
| 127 |
### On Message Section ###
|
| 128 |
@cl.on_message
|
| 129 |
async def main(message: cl.Message):
|
|
|
|
|
|
|
|
|
|
| 130 |
chain = cl.user_session.get("chain")
|
| 131 |
|
| 132 |
msg = cl.Message(content="")
|