Spaces:
Sleeping
Sleeping
Add initial project files
Browse files- .env.sample +5 -0
- .gitignore +6 -0
- Dockerfile +11 -0
- app.py +151 -0
- chainlit.md +1 -0
- data/airbnb_10k.pdf +0 -0
- requirements.txt +101 -0
- solution_app.py +155 -0
.env.sample
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# !!! DO NOT UPDATE THIS FILE DIRECTLY. MAKE A COPY AND RENAME IT `.env` TO PROCEED !!! #
|
2 |
+
HF_LLM_ENDPOINT="YOUR_LLM_ENDPOINT_URL_HERE"
|
3 |
+
HF_EMBED_ENDPOINT="YOUR_EMBED_MODEL_ENDPOINT_URL_HERE"
|
4 |
+
HF_TOKEN="YOUR_HF_TOKEN_HERE"
|
5 |
+
# !!! DO NOT UPDATE THIS FILE DIRECTLY. MAKE A COPY AND RENAME IT `.env` TO PROCEED !!! #
|
.gitignore
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
__pycache__/
|
3 |
+
.chainlit
|
4 |
+
*.faiss
|
5 |
+
*.pkl
|
6 |
+
.files
|
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
RUN useradd -m -u 1000 user
|
3 |
+
USER user
|
4 |
+
ENV HOME=/home/user \
|
5 |
+
PATH=/home/user/.local/bin:$PATH
|
6 |
+
WORKDIR $HOME/app
|
7 |
+
COPY --chown=user . $HOME/app
|
8 |
+
COPY ./requirements.txt ~/app/requirements.txt
|
9 |
+
RUN pip install -r requirements.txt
|
10 |
+
COPY . .
|
11 |
+
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import chainlit as cl
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from operator import itemgetter
|
5 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
6 |
+
from langchain_community.document_loaders import PyMuPDFLoader
|
7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
+
from langchain_community.vectorstores import Qdrant
|
9 |
+
from langchain_huggingface import HuggingFaceEndpointEmbeddings
|
10 |
+
from langchain_core.prompts import PromptTemplate
|
11 |
+
from langchain.schema.output_parser import StrOutputParser
|
12 |
+
from langchain.schema.runnable import RunnablePassthrough
|
13 |
+
from langchain.schema.runnable.config import RunnableConfig
|
14 |
+
|
15 |
+
# Load environment variables from .env file
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
# Load HuggingFace environment variables
|
19 |
+
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
20 |
+
HF_EMBED_ENDPOINT = os.environ["HF_EMBED_ENDPOINT"]
|
21 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
22 |
+
|
23 |
+
print("HF_LLM_ENDPOINT", HF_LLM_ENDPOINT)
|
24 |
+
|
25 |
+
# Load HuggingFace Embeddings
|
26 |
+
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
27 |
+
model=HF_EMBED_ENDPOINT,
|
28 |
+
task="feature-extraction",
|
29 |
+
huggingfacehub_api_token=HF_TOKEN,
|
30 |
+
)
|
31 |
+
|
32 |
+
# Load the PDF document
|
33 |
+
documents = PyMuPDFLoader("./data/airbnb_10k.pdf").load()
|
34 |
+
|
35 |
+
|
36 |
+
### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
|
37 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
38 |
+
split_documents = text_splitter.split_documents(documents)
|
39 |
+
|
40 |
+
### 3. LOAD HUGGINGFACE EMBEDDINGS
|
41 |
+
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
42 |
+
model=HF_EMBED_ENDPOINT,
|
43 |
+
task="feature-extraction",
|
44 |
+
huggingfacehub_api_token=HF_TOKEN,
|
45 |
+
)
|
46 |
+
|
47 |
+
|
48 |
+
# Create a Qdrant vector store from the split documents
|
49 |
+
qdrant_vectorstore = Qdrant.from_documents(
|
50 |
+
split_documents,
|
51 |
+
hf_embeddings,
|
52 |
+
location=":memory:",
|
53 |
+
collection_name="Airbnb 10k filings",
|
54 |
+
batch_size=32
|
55 |
+
)
|
56 |
+
|
57 |
+
# Create a retriever from the vector store
|
58 |
+
qdrant_retriever = qdrant_vectorstore.as_retriever()
|
59 |
+
|
60 |
+
|
61 |
+
# -- AUGMENTED -- #
|
62 |
+
"""
|
63 |
+
1. Define a String Template
|
64 |
+
2. Create a Prompt Template from the String Template
|
65 |
+
"""
|
66 |
+
### 1. DEFINE STRING TEMPLATE
|
67 |
+
RAG_PROMPT_TEMPLATE = """\
|
68 |
+
<|start_header_id|>system<|end_header_id|>
|
69 |
+
You are a helpful assistant. Yo are a financial expert . you understand 10k fillings very well. 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|>
|
70 |
+
|
71 |
+
<|start_header_id|>user<|end_header_id|>
|
72 |
+
User Query:
|
73 |
+
{query}
|
74 |
+
|
75 |
+
Context:
|
76 |
+
{context}<|eot_id|>
|
77 |
+
|
78 |
+
<|start_header_id|>assistant<|end_header_id|>
|
79 |
+
"""
|
80 |
+
|
81 |
+
### 2. CREATE PROMPT TEMPLATE
|
82 |
+
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
83 |
+
|
84 |
+
# -- GENERATION -- #
|
85 |
+
"""
|
86 |
+
1. Create a HuggingFaceEndpoint for the LLM
|
87 |
+
"""
|
88 |
+
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
|
89 |
+
hf_llm = HuggingFaceEndpoint(
|
90 |
+
endpoint_url=HF_LLM_ENDPOINT,
|
91 |
+
max_new_tokens=512,
|
92 |
+
top_k=10,
|
93 |
+
top_p=0.95,
|
94 |
+
temperature=0.3,
|
95 |
+
repetition_penalty=1.15,
|
96 |
+
huggingfacehub_api_token=HF_TOKEN,
|
97 |
+
)
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
@cl.author_rename
|
102 |
+
def rename(original_author: str):
|
103 |
+
"""
|
104 |
+
This function can be used to rename the 'author' of a message.
|
105 |
+
|
106 |
+
In this case, we're overriding the 'Assistant' author to be 'Paul Graham Essay Bot'.
|
107 |
+
"""
|
108 |
+
rename_dict = {
|
109 |
+
"Assistant" : "AirBNB 10K Bot"
|
110 |
+
}
|
111 |
+
return rename_dict.get(original_author, original_author)
|
112 |
+
|
113 |
+
@cl.on_chat_start
|
114 |
+
async def start_chat():
|
115 |
+
"""
|
116 |
+
This function will be called at the start of every user session.
|
117 |
+
|
118 |
+
We will build our LCEL RAG chain here, and store it in the user session.
|
119 |
+
|
120 |
+
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
121 |
+
"""
|
122 |
+
|
123 |
+
### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
|
124 |
+
cl.user_session.set("welcome_message", "Wonderful folks, Welcome to the chat! Hope all your questions are answered ")
|
125 |
+
|
126 |
+
lcel_rag_chain = (
|
127 |
+
{"context": itemgetter("query") | qdrant_retriever, "query": itemgetter("query")}
|
128 |
+
| rag_prompt | hf_llm
|
129 |
+
)
|
130 |
+
|
131 |
+
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
132 |
+
|
133 |
+
@cl.on_message
|
134 |
+
async def main(message: cl.Message):
|
135 |
+
"""
|
136 |
+
This function will be called every time a message is recieved from a session.
|
137 |
+
We will use the LCEL RAG chain to generate a response to the user query.
|
138 |
+
|
139 |
+
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.
|
140 |
+
"""
|
141 |
+
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
142 |
+
|
143 |
+
msg = cl.Message(content="")
|
144 |
+
|
145 |
+
async for chunk in lcel_rag_chain.astream(
|
146 |
+
{"query": message.content},
|
147 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
148 |
+
):
|
149 |
+
await msg.stream_token(chunk)
|
150 |
+
|
151 |
+
await msg.send()
|
chainlit.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Welcome to Worlds best interactive chat bot on Airbnb 10k fillings
|
data/airbnb_10k.pdf
ADDED
Binary file (596 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
aiohttp==3.9.5
|
3 |
+
aiosignal==1.3.1
|
4 |
+
annotated-types==0.7.0
|
5 |
+
anyio==3.7.1
|
6 |
+
asyncer==0.0.2
|
7 |
+
attrs==23.2.0
|
8 |
+
bidict==0.23.1
|
9 |
+
certifi==2024.6.2
|
10 |
+
chainlit==1.1.302
|
11 |
+
charset-normalizer==3.3.2
|
12 |
+
chevron==0.14.0
|
13 |
+
click==8.1.7
|
14 |
+
dataclasses-json==0.5.14
|
15 |
+
Deprecated==1.2.14
|
16 |
+
distro==1.9.0
|
17 |
+
fastapi==0.110.3
|
18 |
+
fastapi-socketio==0.0.10
|
19 |
+
filetype==1.2.0
|
20 |
+
frozenlist==1.4.1
|
21 |
+
googleapis-common-protos==1.63.1
|
22 |
+
greenlet==3.0.3
|
23 |
+
groq==0.9.0
|
24 |
+
grpcio==1.64.1
|
25 |
+
grpcio-tools==1.62.2
|
26 |
+
h11==0.14.0
|
27 |
+
h2==4.1.0
|
28 |
+
hpack==4.0.0
|
29 |
+
httpcore==0.17.3
|
30 |
+
httpx==0.24.1
|
31 |
+
hyperframe==6.0.1
|
32 |
+
idna==3.7
|
33 |
+
importlib_metadata==7.1.0
|
34 |
+
jsonpatch==1.33
|
35 |
+
jsonpointer==3.0.0
|
36 |
+
langchain==0.2.5
|
37 |
+
langchain-core==0.2.9
|
38 |
+
langchain-groq==0.1.5
|
39 |
+
langchain-openai==0.1.8
|
40 |
+
langchain-qdrant==0.1.1
|
41 |
+
langchain-text-splitters==0.2.1
|
42 |
+
langchainhub==0.1.20
|
43 |
+
langchain_community==0.2.5
|
44 |
+
langchain_huggingface==0.0.3
|
45 |
+
langchain_text_splitters==0.2.1
|
46 |
+
langsmith==0.1.81
|
47 |
+
Lazify==0.4.0
|
48 |
+
literalai==0.0.604
|
49 |
+
marshmallow==3.21.3
|
50 |
+
multidict==6.0.5
|
51 |
+
mypy-extensions==1.0.0
|
52 |
+
nest-asyncio==1.6.0
|
53 |
+
numpy==1.26.4
|
54 |
+
openai==1.34.0
|
55 |
+
opentelemetry-api==1.25.0
|
56 |
+
opentelemetry-exporter-otlp==1.25.0
|
57 |
+
opentelemetry-exporter-otlp-proto-common==1.25.0
|
58 |
+
opentelemetry-exporter-otlp-proto-grpc==1.25.0
|
59 |
+
opentelemetry-exporter-otlp-proto-http==1.25.0
|
60 |
+
opentelemetry-instrumentation==0.46b0
|
61 |
+
opentelemetry-proto==1.25.0
|
62 |
+
opentelemetry-sdk==1.25.0
|
63 |
+
opentelemetry-semantic-conventions==0.46b0
|
64 |
+
orjson==3.10.5
|
65 |
+
packaging==23.2
|
66 |
+
portalocker==2.8.2
|
67 |
+
protobuf==4.25.3
|
68 |
+
pydantic==2.7.4
|
69 |
+
pydantic_core==2.18.4
|
70 |
+
PyJWT==2.8.0
|
71 |
+
python-dotenv==1.0.1
|
72 |
+
python-engineio==4.9.1
|
73 |
+
python-graphql-client==0.4.3
|
74 |
+
python-multipart==0.0.9
|
75 |
+
python-socketio==5.11.3
|
76 |
+
PyYAML==6.0.1
|
77 |
+
pymupdf==1.24.6
|
78 |
+
qdrant-client==1.9.1
|
79 |
+
regex==2024.5.15
|
80 |
+
requests==2.32.3
|
81 |
+
simple-websocket==1.0.0
|
82 |
+
sniffio==1.3.1
|
83 |
+
SQLAlchemy==2.0.31
|
84 |
+
starlette==0.37.2
|
85 |
+
syncer==2.0.3
|
86 |
+
tenacity==8.4.1
|
87 |
+
tiktoken==0.7.0
|
88 |
+
tomli==2.0.1
|
89 |
+
tqdm==4.66.4
|
90 |
+
types-requests==2.32.0.20240602
|
91 |
+
typing-inspect==0.9.0
|
92 |
+
typing_extensions==4.12.2
|
93 |
+
uptrace==1.24.0
|
94 |
+
urllib3==2.2.2
|
95 |
+
uvicorn==0.25.0
|
96 |
+
watchfiles==0.20.0
|
97 |
+
websockets==12.0
|
98 |
+
wrapt==1.16.0
|
99 |
+
wsproto==1.2.0
|
100 |
+
yarl==1.9.4
|
101 |
+
zipp==3.19.2
|
solution_app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import chainlit as cl
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from operator import itemgetter
|
5 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
6 |
+
from langchain_community.document_loaders import TextLoader
|
7 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
8 |
+
from langchain_community.vectorstores import FAISS
|
9 |
+
from langchain_huggingface import HuggingFaceEndpointEmbeddings
|
10 |
+
from langchain_core.prompts import PromptTemplate
|
11 |
+
from langchain.schema.output_parser import StrOutputParser
|
12 |
+
from langchain.schema.runnable import RunnablePassthrough
|
13 |
+
from langchain.schema.runnable.config import RunnableConfig
|
14 |
+
|
15 |
+
# GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
|
16 |
+
# ---- ENV VARIABLES ---- #
|
17 |
+
"""
|
18 |
+
This function will load our environment file (.env) if it is present.
|
19 |
+
|
20 |
+
NOTE: Make sure that .env is in your .gitignore file - it is by default, but please ensure it remains there.
|
21 |
+
"""
|
22 |
+
load_dotenv()
|
23 |
+
|
24 |
+
"""
|
25 |
+
We will load our environment variables here.
|
26 |
+
"""
|
27 |
+
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
28 |
+
HF_EMBED_ENDPOINT = os.environ["HF_EMBED_ENDPOINT"]
|
29 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
30 |
+
|
31 |
+
# ---- GLOBAL DECLARATIONS ---- #
|
32 |
+
|
33 |
+
# -- RETRIEVAL -- #
|
34 |
+
"""
|
35 |
+
1. Load Documents from Text File
|
36 |
+
2. Split Documents into Chunks
|
37 |
+
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
38 |
+
4. Index Files if they do not exist, otherwise load the vectorstore
|
39 |
+
"""
|
40 |
+
document_loader = TextLoader("./data/paul_graham_essays.txt")
|
41 |
+
documents = document_loader.load()
|
42 |
+
|
43 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
44 |
+
split_documents = text_splitter.split_documents(documents)
|
45 |
+
|
46 |
+
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
47 |
+
model=HF_EMBED_ENDPOINT,
|
48 |
+
task="feature-extraction",
|
49 |
+
huggingfacehub_api_token=HF_TOKEN,
|
50 |
+
)
|
51 |
+
|
52 |
+
if os.path.exists("./data/vectorstore"):
|
53 |
+
vectorstore = FAISS.load_local(
|
54 |
+
"./data/vectorstore",
|
55 |
+
hf_embeddings,
|
56 |
+
allow_dangerous_deserialization=True # this is necessary to load the vectorstore from disk as it's stored as a `.pkl` file.
|
57 |
+
)
|
58 |
+
hf_retriever = vectorstore.as_retriever()
|
59 |
+
print("Loaded Vectorstore")
|
60 |
+
else:
|
61 |
+
print("Indexing Files")
|
62 |
+
os.makedirs("./data/vectorstore", exist_ok=True)
|
63 |
+
for i in range(0, len(split_documents), 32):
|
64 |
+
if i == 0:
|
65 |
+
vectorstore = FAISS.from_documents(split_documents[i:i+32], hf_embeddings)
|
66 |
+
continue
|
67 |
+
vectorstore.add_documents(split_documents[i:i+32])
|
68 |
+
vectorstore.save_local("./data/vectorstore")
|
69 |
+
|
70 |
+
hf_retriever = vectorstore.as_retriever()
|
71 |
+
|
72 |
+
# -- AUGMENTED -- #
|
73 |
+
"""
|
74 |
+
1. Define a String Template
|
75 |
+
2. Create a Prompt Template from the String Template
|
76 |
+
"""
|
77 |
+
RAG_PROMPT_TEMPLATE = """\
|
78 |
+
<|start_header_id|>system<|end_header_id|>
|
79 |
+
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|>
|
80 |
+
|
81 |
+
<|start_header_id|>user<|end_header_id|>
|
82 |
+
User Query:
|
83 |
+
{query}
|
84 |
+
|
85 |
+
Context:
|
86 |
+
{context}<|eot_id|>
|
87 |
+
|
88 |
+
<|start_header_id|>assistant<|end_header_id|>
|
89 |
+
"""
|
90 |
+
|
91 |
+
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
92 |
+
|
93 |
+
# -- GENERATION -- #
|
94 |
+
"""
|
95 |
+
1. Create a HuggingFaceEndpoint for the LLM
|
96 |
+
"""
|
97 |
+
hf_llm = HuggingFaceEndpoint(
|
98 |
+
endpoint_url=HF_LLM_ENDPOINT,
|
99 |
+
max_new_tokens=512,
|
100 |
+
top_k=10,
|
101 |
+
top_p=0.95,
|
102 |
+
temperature=0.3,
|
103 |
+
repetition_penalty=1.15,
|
104 |
+
huggingfacehub_api_token=HF_TOKEN,
|
105 |
+
)
|
106 |
+
|
107 |
+
@cl.author_rename
|
108 |
+
def rename(original_author: str):
|
109 |
+
"""
|
110 |
+
This function can be used to rename the 'author' of a message.
|
111 |
+
|
112 |
+
In this case, we're overriding the 'Assistant' author to be 'Paul Graham Essay Bot'.
|
113 |
+
"""
|
114 |
+
rename_dict = {
|
115 |
+
"Assistant" : "Paul Graham Essay Bot"
|
116 |
+
}
|
117 |
+
return rename_dict.get(original_author, original_author)
|
118 |
+
|
119 |
+
@cl.on_chat_start
|
120 |
+
async def start_chat():
|
121 |
+
"""
|
122 |
+
This function will be called at the start of every user session.
|
123 |
+
|
124 |
+
We will build our LCEL RAG chain here, and store it in the user session.
|
125 |
+
|
126 |
+
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
127 |
+
"""
|
128 |
+
|
129 |
+
lcel_rag_chain = (
|
130 |
+
{"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
131 |
+
| rag_prompt | hf_llm
|
132 |
+
)
|
133 |
+
|
134 |
+
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
135 |
+
|
136 |
+
@cl.on_message
|
137 |
+
async def main(message: cl.Message):
|
138 |
+
"""
|
139 |
+
This function will be called every time a message is recieved from a session.
|
140 |
+
|
141 |
+
We will use the LCEL RAG chain to generate a response to the user query.
|
142 |
+
|
143 |
+
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.
|
144 |
+
"""
|
145 |
+
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
146 |
+
|
147 |
+
msg = cl.Message(content="")
|
148 |
+
|
149 |
+
for chunk in await cl.make_async(lcel_rag_chain.stream)(
|
150 |
+
{"query": message.content},
|
151 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
152 |
+
):
|
153 |
+
await msg.stream_token(chunk)
|
154 |
+
|
155 |
+
await msg.send()
|