Spaces:
Runtime error
Runtime error
File size: 4,957 Bytes
9708f2a 387f307 9708f2a fd2d856 9708f2a f0cd44d 53b1dd3 f63f886 9708f2a 911e021 9708f2a d154644 387f307 53b1dd3 9708f2a 2459c07 892ebe9 9708f2a 544f61e fd2d856 9708f2a d2e07cf b8d4ae6 f0cd44d 544f61e f0cd44d 9708f2a f0cd44d 9708f2a b8d4ae6 9708f2a f0cd44d 544f61e f0cd44d d2e07cf 9708f2a 2459c07 9708f2a 8d85171 9708f2a 53b1dd3 9708f2a 53b1dd3 9708f2a |
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 |
import os
import chainlit as cl
from dotenv import load_dotenv
from operator import itemgetter
from langchain_huggingface import HuggingFaceEndpoint
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.document_loaders import PyMuPDFLoader
from langchain_core.prompts import PromptTemplate
from langchain_community.embeddings.openai import OpenAIEmbeddings
from langchain.schema.runnable.config import RunnableConfig
from langchain_community.vectorstores import Qdrant
# GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
# ---- ENV VARIABLES ---- #
"""
This function will load our environment file (.env) if it is present.
NOTE: Make sure that .env is in your .gitignore file - it is by default, but please ensure it remains there.
"""
load_dotenv()
"""
We will load our environment variables here.
"""
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
HF_EMBED_ENDPOINT = os.environ["HF_EMBED_ENDPOINT"]
HF_TOKEN = os.environ["HF_TOKEN"]
OPENAPI_KEY = os.environ["OPENAI_API_KEY"]
# ---- GLOBAL DECLARATIONS ---- #
# -- RETRIEVAL -- #
"""
1. Load Documents from Text File
2. Split Documents into Chunks
3. Load HuggingFace Embeddings (remember to use the URL we set above)
4. Index Files if they do not exist, otherwise load the vectorstore
"""
#Load the Pdf Documents from airbnb-10k
documents = PyMuPDFLoader("data/airbnb-10k.pdf").load()
### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=0
)
split_documents = text_splitter.split_documents(documents)
### 3. LOAD open ai EMBEDDINGS
embeddings = OpenAIEmbeddings(OPENAPI_API_KEY=OPENAPI_KEY,model="text-embedding-ada-002")
#Initialize the Vector Store
if os.path.exists("./vectorstore"):
vectorstore = Qdrant.from_existing_collection(
embeddings = embeddings,
path = "./vectorstore",
collection_name = "airbnb-10k",
)
hf_retriever = vectorstore.as_retriever()
else:
os.makedirs("./vectorstore", exist_ok=True)
### 4. INDEX FILES
### NOTE: REMEMBER TO BATCH THE DOCUMENTS WITH MAXIMUM BATCH SIZE = 32
vectorstore = Qdrant.from_documents(
split_documents,
embeddings,
path= "./vectorstore",
collection_name="airbnb-10k",
)
hf_retriever = vectorstore.as_retriever()
# -- AUGMENTED -- #
"""
1. Define a String Template
2. Create a Prompt Template from the String Template
"""
### 1. DEFINE STRING TEMPLATE
RAG_PROMPT_TEMPLATE = """\
<|start_header_id|>system<|end_header_id|>
You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|>
<|start_header_id|>user<|end_header_id|>
User Query:
{query}
Context:
{context}<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""
### 2. CREATE PROMPT TEMPLATE
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
# -- GENERATION -- #
"""
1. Create a HuggingFaceEndpoint for the LLM
"""
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
hf_llm = HuggingFaceEndpoint(
endpoint_url=HF_LLM_ENDPOINT,
max_new_tokens=512,
top_k=10,
top_p=0.95,
typical_p=0.95,
temperature=0.01,
repetition_penalty=1.03,
streaming=True,
huggingfacehub_api_token=os.environ["HF_TOKEN"]
)
@cl.author_rename
def rename(original_author: str):
"""
This function can be used to rename the 'author' of a message.
In this case, we're overriding the 'Assistant' author to be 'AirBnb Stock Analyzer'.
"""
rename_dict = {
"Assistant" : "AirBnB Stock Analyzer"
}
return rename_dict.get(original_author, original_author)
@cl.on_chat_start
async def start_chat():
"""
This function will be called at the start of every user session.
We will build our LCEL RAG chain here, and store it in the user session.
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
"""
### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
lcel_rag_chain = {"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}| rag_prompt | hf_llm
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
@cl.on_message
async def main(message: cl.Message):
"""
This function will be called every time a message is recieved from a session.
We will use the LCEL RAG chain to generate a response to the user query.
The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
"""
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
msg = cl.Message(content="")
async for chunk in lcel_rag_chain.astream(
{"query": message.content},
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
):
await msg.stream_token(chunk)
await msg.send() |