Working with FAISS
Browse files- .gitignore +6 -0
- Dockerfile +12 -0
- Open_Source_RAG_Leveraging_Hugging_Face_Endpoints_through_LangChain.ipynb +634 -0
- README.md +10 -1
- app.py +174 -0
- chainlit.md +3 -0
- data/10Q-AirBnB.pdf +0 -0
- requirements.txt +9 -0
- stuff +0 -0
.gitignore
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
__pycache__/
|
3 |
+
.chainlit
|
4 |
+
*.faiss
|
5 |
+
*.pkl
|
6 |
+
.files
|
Dockerfile
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
RUN mkdir -p $HOME/app/data/vectorstore && chown -R user:user $HOME/app/data
|
8 |
+
COPY --chown=user . $HOME/app
|
9 |
+
COPY ./requirements.txt ~/app/requirements.txt
|
10 |
+
RUN pip install -r requirements.txt
|
11 |
+
COPY . .
|
12 |
+
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
Open_Source_RAG_Leveraging_Hugging_Face_Endpoints_through_LangChain.ipynb
ADDED
@@ -0,0 +1,634 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "lcW6UWldWUMp"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# Open Source RAG - Leveraging Hugging Face Endpoints through LangChain\n",
|
10 |
+
"\n",
|
11 |
+
"In the following notebook we will dive into the world of Open Source models hosted on Hugging Face's [inference endpoints](https://ui.endpoints.huggingface.co/).\n",
|
12 |
+
"\n",
|
13 |
+
"The notebook will be broken into the following parts:\n",
|
14 |
+
"\n",
|
15 |
+
"- 🤝 Breakout Room #2:\n",
|
16 |
+
" 1. Install required libraries\n",
|
17 |
+
" 2. Set Environment Variables\n",
|
18 |
+
" 3. Creating LangChain components powered by the endpoints\n",
|
19 |
+
" 4. Creating a simple RAG pipeline with [LangChain v0.2.0](https://blog.langchain.dev/langchain-v02-leap-to-stability/)"
|
20 |
+
]
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"cell_type": "markdown",
|
24 |
+
"metadata": {
|
25 |
+
"id": "-spIWt2J3Quk"
|
26 |
+
},
|
27 |
+
"source": [
|
28 |
+
"## Task 1: Install required libraries\n",
|
29 |
+
"\n",
|
30 |
+
"Now we've got to get our required libraries!\n",
|
31 |
+
"\n",
|
32 |
+
"We'll start with our `langchain` and `huggingface` dependencies.\n",
|
33 |
+
"\n"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 1,
|
39 |
+
"metadata": {
|
40 |
+
"id": "EwGLnp31jXJj"
|
41 |
+
},
|
42 |
+
"outputs": [],
|
43 |
+
"source": [
|
44 |
+
"!pip install -qU langchain-huggingface langchain-community faiss-cpu"
|
45 |
+
]
|
46 |
+
},
|
47 |
+
{
|
48 |
+
"cell_type": "markdown",
|
49 |
+
"metadata": {
|
50 |
+
"id": "SpZTBLwK3TIz"
|
51 |
+
},
|
52 |
+
"source": [
|
53 |
+
"## Task 2: Set Environment Variables\n",
|
54 |
+
"\n",
|
55 |
+
"We'll need to set our `HF_TOKEN` so that we can send requests to our protected API endpoint.\n",
|
56 |
+
"\n",
|
57 |
+
"We'll also set-up our OpenAI API key, which we'll leverage later.\n",
|
58 |
+
"\n"
|
59 |
+
]
|
60 |
+
},
|
61 |
+
{
|
62 |
+
"cell_type": "code",
|
63 |
+
"execution_count": 2,
|
64 |
+
"metadata": {
|
65 |
+
"colab": {
|
66 |
+
"base_uri": "https://localhost:8080/"
|
67 |
+
},
|
68 |
+
"id": "NspG8I0XlFTt",
|
69 |
+
"outputId": "edbf992c-97c0-46b1-9b69-40651a5e60d1"
|
70 |
+
},
|
71 |
+
"outputs": [],
|
72 |
+
"source": [
|
73 |
+
"import os\n",
|
74 |
+
"import getpass\n",
|
75 |
+
"\n",
|
76 |
+
"os.environ[\"HF_TOKEN\"] = getpass.getpass(\"HuggingFace Write Token: \")"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "markdown",
|
81 |
+
"metadata": {
|
82 |
+
"id": "QMru14VBZAtw"
|
83 |
+
},
|
84 |
+
"source": [
|
85 |
+
"## Task 3: Creating LangChain components powered by the endpoints\n",
|
86 |
+
"\n",
|
87 |
+
"We're going to wrap our endpoints in LangChain components in order to leverage them, thanks to LCEL, as we would any other LCEL component!"
|
88 |
+
]
|
89 |
+
},
|
90 |
+
{
|
91 |
+
"cell_type": "markdown",
|
92 |
+
"metadata": {
|
93 |
+
"id": "TGooehdzcmPb"
|
94 |
+
},
|
95 |
+
"source": [
|
96 |
+
"### HuggingFaceEndpoint for LLM\n",
|
97 |
+
"\n",
|
98 |
+
"We can use the `HuggingFaceEndpoint` found [here](https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/llms/huggingface_endpoint.py) to power our chain - let's look at how we would implement it."
|
99 |
+
]
|
100 |
+
},
|
101 |
+
{
|
102 |
+
"cell_type": "code",
|
103 |
+
"execution_count": 3,
|
104 |
+
"metadata": {
|
105 |
+
"id": "N7u2Tu1FsURh"
|
106 |
+
},
|
107 |
+
"outputs": [],
|
108 |
+
"source": [
|
109 |
+
"YOUR_LLM_ENDPOINT_URL = \"https://ta8oebbz7gg0766a.us-east-1.aws.endpoints.huggingface.cloud\""
|
110 |
+
]
|
111 |
+
},
|
112 |
+
{
|
113 |
+
"cell_type": "code",
|
114 |
+
"execution_count": 4,
|
115 |
+
"metadata": {
|
116 |
+
"colab": {
|
117 |
+
"base_uri": "https://localhost:8080/"
|
118 |
+
},
|
119 |
+
"id": "L3Cz6Mrnt2ku",
|
120 |
+
"outputId": "f23f611f-5f08-4332-a74c-5b8d8311d185"
|
121 |
+
},
|
122 |
+
"outputs": [
|
123 |
+
{
|
124 |
+
"name": "stderr",
|
125 |
+
"output_type": "stream",
|
126 |
+
"text": [
|
127 |
+
"/opt/homebrew/anaconda3/envs/llmops/lib/python3.12/site-packages/langchain_core/_api/deprecation.py:139: LangChainDeprecationWarning: The class `HuggingFaceEndpoint` was deprecated in LangChain 0.0.37 and will be removed in 0.3. An updated version of the class exists in the langchain-huggingface package and should be used instead. To use it run `pip install -U langchain-huggingface` and import as `from langchain_huggingface import HuggingFaceEndpoint`.\n",
|
128 |
+
" warn_deprecated(\n",
|
129 |
+
"/opt/homebrew/anaconda3/envs/llmops/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
130 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
131 |
+
]
|
132 |
+
},
|
133 |
+
{
|
134 |
+
"name": "stdout",
|
135 |
+
"output_type": "stream",
|
136 |
+
"text": [
|
137 |
+
"The token has not been saved to the git credentials helper. Pass `add_to_git_credential=True` in this function directly or `--add-to-git-credential` if using via `huggingface-cli` if you want to set the git credential as well.\n",
|
138 |
+
"Token is valid (permission: write).\n",
|
139 |
+
"Your token has been saved to /Users/maraka/.cache/huggingface/token\n",
|
140 |
+
"Login successful\n"
|
141 |
+
]
|
142 |
+
}
|
143 |
+
],
|
144 |
+
"source": [
|
145 |
+
"from langchain_community.llms import HuggingFaceEndpoint\n",
|
146 |
+
"\n",
|
147 |
+
"hf_llm = HuggingFaceEndpoint(\n",
|
148 |
+
" endpoint_url=f\"{YOUR_LLM_ENDPOINT_URL}\",\n",
|
149 |
+
" max_new_tokens=512,\n",
|
150 |
+
" top_k=10,\n",
|
151 |
+
" top_p=0.95,\n",
|
152 |
+
" typical_p=0.95,\n",
|
153 |
+
" temperature=0.01,\n",
|
154 |
+
" repetition_penalty=1.03,\n",
|
155 |
+
" huggingfacehub_api_token=os.environ[\"HF_TOKEN\"]\n",
|
156 |
+
")"
|
157 |
+
]
|
158 |
+
},
|
159 |
+
{
|
160 |
+
"cell_type": "markdown",
|
161 |
+
"metadata": {
|
162 |
+
"id": "fun4XrRxZK9n"
|
163 |
+
},
|
164 |
+
"source": [
|
165 |
+
"Now we can use our endpoint like we would any other LLM!"
|
166 |
+
]
|
167 |
+
},
|
168 |
+
{
|
169 |
+
"cell_type": "code",
|
170 |
+
"execution_count": 5,
|
171 |
+
"metadata": {
|
172 |
+
"colab": {
|
173 |
+
"base_uri": "https://localhost:8080/",
|
174 |
+
"height": 127
|
175 |
+
},
|
176 |
+
"id": "OFAbFT91Z8QV",
|
177 |
+
"outputId": "588714ad-da28-4330-801b-7121b6f17ccf"
|
178 |
+
},
|
179 |
+
"outputs": [
|
180 |
+
{
|
181 |
+
"data": {
|
182 |
+
"text/plain": [
|
183 |
+
"\" I hope you're having a great day! I just wanted to reach out and say hello. I've been thinking about you lately and wanted to see how you're doing. Is everything going well? Do you have any new updates or news you'd like to share? I'm always here to listen and support you in any way I can. Take care and talk to you soon!\\nI hope this message finds you well. I was just thinking about you and wanted to say hello. How have you been? Have you been up to anything exciting or new? I'd love to hear about it. If you have any time, I'd love to catch up and chat. Let me know if that sounds good to you.\\nI hope you're doing well. I was just thinking about you and wanted to reach out. It's been a while since we last spoke, and I was wondering how you've been. Have you been up to anything new or exciting? I'd love to hear about it. If you have some time, I'd love to catch up and chat. Let me know if that sounds good to you.\\nI hope you're doing well. I was just thinking about you and wanted to say hello. I know it's been a while since we last spoke, but I've been thinking about you and wanted to reach out. How have you been? Have you been up to anything new or exciting? I'd love to hear about it. If you have some time, I'd love to catch up and chat. Let me know if that sounds good to you.\\nI hope you're doing well. I was just thinking about you and wanted to say hello. I know it's been a while since we last spoke, but I've been thinking about you and wanted to reach out. How have you been? Have you been up to anything new or exciting? I'd love to hear about it. If you have some time, I'd love to catch up and chat. Let me know if that sounds good to you.\\nI hope you're doing well. I was just thinking about you and wanted to say hello. I know it's been a while since we last spoke, but I've been thinking about you and wanted to reach out. How have you been? Have you been up to anything new or exciting? I'd love to hear about it. If you have some time, I'd love to catch up and chat. Let me know if that sounds good to you.\\nI hope you're doing well. I was just\""
|
184 |
+
]
|
185 |
+
},
|
186 |
+
"execution_count": 5,
|
187 |
+
"metadata": {},
|
188 |
+
"output_type": "execute_result"
|
189 |
+
}
|
190 |
+
],
|
191 |
+
"source": [
|
192 |
+
"hf_llm.invoke(\"Hello, how are you?\")"
|
193 |
+
]
|
194 |
+
},
|
195 |
+
{
|
196 |
+
"cell_type": "markdown",
|
197 |
+
"metadata": {
|
198 |
+
"id": "ngH3fhw4aQ8T"
|
199 |
+
},
|
200 |
+
"source": [
|
201 |
+
"Now we can add a RAG-style prompt using Llama 3 Instruct's prompt templating!"
|
202 |
+
]
|
203 |
+
},
|
204 |
+
{
|
205 |
+
"cell_type": "code",
|
206 |
+
"execution_count": 6,
|
207 |
+
"metadata": {
|
208 |
+
"id": "zdvv4JmkzEtj"
|
209 |
+
},
|
210 |
+
"outputs": [],
|
211 |
+
"source": [
|
212 |
+
"from langchain_core.prompts import PromptTemplate\n",
|
213 |
+
"\n",
|
214 |
+
"RAG_PROMPT_TEMPLATE = \"\"\"\\\n",
|
215 |
+
"<|start_header_id|>system<|end_header_id|>\n",
|
216 |
+
"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|>\n",
|
217 |
+
"\n",
|
218 |
+
"<|start_header_id|>user<|end_header_id|>\n",
|
219 |
+
"User Query:\n",
|
220 |
+
"{query}\n",
|
221 |
+
"\n",
|
222 |
+
"Context:\n",
|
223 |
+
"{context}<|eot_id|>\n",
|
224 |
+
"\n",
|
225 |
+
"<|start_header_id|>assistant<|end_header_id|>\n",
|
226 |
+
"\"\"\"\n",
|
227 |
+
"\n",
|
228 |
+
"rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)"
|
229 |
+
]
|
230 |
+
},
|
231 |
+
{
|
232 |
+
"cell_type": "markdown",
|
233 |
+
"metadata": {
|
234 |
+
"id": "Oe0Qrzn4adzh"
|
235 |
+
},
|
236 |
+
"source": [
|
237 |
+
"Let's create a simple LCEL chain using our prompt template Runnable and our LLM Runnable."
|
238 |
+
]
|
239 |
+
},
|
240 |
+
{
|
241 |
+
"cell_type": "code",
|
242 |
+
"execution_count": 7,
|
243 |
+
"metadata": {
|
244 |
+
"id": "CE4djpxM0-Fg"
|
245 |
+
},
|
246 |
+
"outputs": [],
|
247 |
+
"source": [
|
248 |
+
"rag_chain = rag_prompt | hf_llm"
|
249 |
+
]
|
250 |
+
},
|
251 |
+
{
|
252 |
+
"cell_type": "code",
|
253 |
+
"execution_count": 8,
|
254 |
+
"metadata": {
|
255 |
+
"colab": {
|
256 |
+
"base_uri": "https://localhost:8080/",
|
257 |
+
"height": 36
|
258 |
+
},
|
259 |
+
"id": "PNwrLXqDxHDY",
|
260 |
+
"outputId": "f6803286-1aa5-488a-eea9-8bece68da7f5"
|
261 |
+
},
|
262 |
+
"outputs": [
|
263 |
+
{
|
264 |
+
"data": {
|
265 |
+
"text/plain": [
|
266 |
+
"'According to the context, Carl is 40 years old.'"
|
267 |
+
]
|
268 |
+
},
|
269 |
+
"execution_count": 8,
|
270 |
+
"metadata": {},
|
271 |
+
"output_type": "execute_result"
|
272 |
+
}
|
273 |
+
],
|
274 |
+
"source": [
|
275 |
+
"rag_chain.invoke({\"query\" : \"Who old is Carl?\", \"context\" : \"Carl is a sweet dude, he's 40.\"})"
|
276 |
+
]
|
277 |
+
},
|
278 |
+
{
|
279 |
+
"cell_type": "markdown",
|
280 |
+
"metadata": {
|
281 |
+
"id": "emGw4-66aBfa"
|
282 |
+
},
|
283 |
+
"source": [
|
284 |
+
"### HuggingFaceInferenceAPIEmbeddings\n",
|
285 |
+
"\n",
|
286 |
+
"Now we can leverage the `HuggingFaceInferenceAPIEmbeddings` module in LangChain to connect to our Hugging Face Inference Endpoint hosted embedding model."
|
287 |
+
]
|
288 |
+
},
|
289 |
+
{
|
290 |
+
"cell_type": "code",
|
291 |
+
"execution_count": 9,
|
292 |
+
"metadata": {
|
293 |
+
"id": "n9Q7e4Gnwe_C"
|
294 |
+
},
|
295 |
+
"outputs": [],
|
296 |
+
"source": [
|
297 |
+
"from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings\n",
|
298 |
+
"\n",
|
299 |
+
"YOUR_EMBED_MODEL_URL = \"https://ropgl65i14toxmih.us-east-1.aws.endpoints.huggingface.cloud\"\n",
|
300 |
+
"\n",
|
301 |
+
"hf_embeddings = HuggingFaceEndpointEmbeddings(\n",
|
302 |
+
" model=f\"{YOUR_EMBED_MODEL_URL}\",\n",
|
303 |
+
" task=\"feature-extraction\",\n",
|
304 |
+
" huggingfacehub_api_token=os.environ[\"HF_TOKEN\"],\n",
|
305 |
+
")"
|
306 |
+
]
|
307 |
+
},
|
308 |
+
{
|
309 |
+
"cell_type": "markdown",
|
310 |
+
"metadata": {
|
311 |
+
"id": "YXYRBqbBayWb"
|
312 |
+
},
|
313 |
+
"source": [
|
314 |
+
"Let's build a simple cosine-similarity function to verify our endpoint is working as expected."
|
315 |
+
]
|
316 |
+
},
|
317 |
+
{
|
318 |
+
"cell_type": "code",
|
319 |
+
"execution_count": null,
|
320 |
+
"metadata": {
|
321 |
+
"id": "lOP6LKr74RG8"
|
322 |
+
},
|
323 |
+
"outputs": [],
|
324 |
+
"source": [
|
325 |
+
"import numpy as np\n",
|
326 |
+
"from numpy.linalg import norm\n",
|
327 |
+
"\n",
|
328 |
+
"def cosine_similarity(phrase_1, phrase_2):\n",
|
329 |
+
" vec_1 = hf_embeddings.embed_documents([phrase_1])[0]\n",
|
330 |
+
" vec2_2 = hf_embeddings.embed_documents([phrase_2])[0]\n",
|
331 |
+
" return np.dot(vec_1, vec2_2) / (norm(vec_1) * norm(vec2_2))"
|
332 |
+
]
|
333 |
+
},
|
334 |
+
{
|
335 |
+
"cell_type": "markdown",
|
336 |
+
"metadata": {
|
337 |
+
"id": "uGZNhxF2bVIr"
|
338 |
+
},
|
339 |
+
"source": [
|
340 |
+
"Let's try a few examples below!"
|
341 |
+
]
|
342 |
+
},
|
343 |
+
{
|
344 |
+
"cell_type": "code",
|
345 |
+
"execution_count": null,
|
346 |
+
"metadata": {
|
347 |
+
"colab": {
|
348 |
+
"base_uri": "https://localhost:8080/"
|
349 |
+
},
|
350 |
+
"id": "5o_cqEZ34f15",
|
351 |
+
"outputId": "d3eb4933-8842-4278-fe48-2dc15e430b60"
|
352 |
+
},
|
353 |
+
"outputs": [
|
354 |
+
{
|
355 |
+
"data": {
|
356 |
+
"text/plain": [
|
357 |
+
"0.8903063446222079"
|
358 |
+
]
|
359 |
+
},
|
360 |
+
"execution_count": 44,
|
361 |
+
"metadata": {},
|
362 |
+
"output_type": "execute_result"
|
363 |
+
}
|
364 |
+
],
|
365 |
+
"source": [
|
366 |
+
"cosine_similarity(\"I love my fluffy dog!\", \"I adore this furry puppy!\")"
|
367 |
+
]
|
368 |
+
},
|
369 |
+
{
|
370 |
+
"cell_type": "code",
|
371 |
+
"execution_count": null,
|
372 |
+
"metadata": {
|
373 |
+
"colab": {
|
374 |
+
"base_uri": "https://localhost:8080/"
|
375 |
+
},
|
376 |
+
"id": "R1nsAV1n4w4a",
|
377 |
+
"outputId": "db53d783-4c87-404f-de67-fc1d01583e68"
|
378 |
+
},
|
379 |
+
"outputs": [
|
380 |
+
{
|
381 |
+
"data": {
|
382 |
+
"text/plain": [
|
383 |
+
"0.743020791930313"
|
384 |
+
]
|
385 |
+
},
|
386 |
+
"execution_count": 48,
|
387 |
+
"metadata": {},
|
388 |
+
"output_type": "execute_result"
|
389 |
+
}
|
390 |
+
],
|
391 |
+
"source": [
|
392 |
+
"cosine_similarity(\"I love my fluffy dog!\", \"Eating pizza is the worst! Yuck!\")"
|
393 |
+
]
|
394 |
+
},
|
395 |
+
{
|
396 |
+
"cell_type": "markdown",
|
397 |
+
"metadata": {
|
398 |
+
"id": "iiz6vKMlbbP4"
|
399 |
+
},
|
400 |
+
"source": [
|
401 |
+
"## Task 4: Preparing Data!\n",
|
402 |
+
"\n",
|
403 |
+
"We'll start by loading some data from GitHub (Paul Graham's Essays) and then move to chunking them into manageable pieces!\n",
|
404 |
+
"\n",
|
405 |
+
"First - let's grab the repository where the files live."
|
406 |
+
]
|
407 |
+
},
|
408 |
+
{
|
409 |
+
"cell_type": "code",
|
410 |
+
"execution_count": null,
|
411 |
+
"metadata": {
|
412 |
+
"colab": {
|
413 |
+
"base_uri": "https://localhost:8080/"
|
414 |
+
},
|
415 |
+
"id": "AkuzZben5Eqp",
|
416 |
+
"outputId": "eb8d39ae-fd70-4691-ddaa-1f8aa15f1c19"
|
417 |
+
},
|
418 |
+
"outputs": [
|
419 |
+
{
|
420 |
+
"name": "stdout",
|
421 |
+
"output_type": "stream",
|
422 |
+
"text": [
|
423 |
+
"Cloning into 'paul-graham-to-kindle'...\n",
|
424 |
+
"remote: Enumerating objects: 36, done.\u001b[K\n",
|
425 |
+
"remote: Counting objects: 100% (36/36), done.\u001b[K\n",
|
426 |
+
"remote: Compressing objects: 100% (33/33), done.\u001b[K\n",
|
427 |
+
"remote: Total 36 (delta 3), reused 31 (delta 1), pack-reused 0\u001b[K\n",
|
428 |
+
"Receiving objects: 100% (36/36), 2.35 MiB | 7.13 MiB/s, done.\n",
|
429 |
+
"Resolving deltas: 100% (3/3), done.\n"
|
430 |
+
]
|
431 |
+
}
|
432 |
+
],
|
433 |
+
"source": [
|
434 |
+
"!git clone https://github.com/dbredvick/paul-graham-to-kindle.git"
|
435 |
+
]
|
436 |
+
},
|
437 |
+
{
|
438 |
+
"cell_type": "markdown",
|
439 |
+
"metadata": {
|
440 |
+
"id": "8prMk6R0bsYd"
|
441 |
+
},
|
442 |
+
"source": [
|
443 |
+
"Next - we can load them using LangChain!"
|
444 |
+
]
|
445 |
+
},
|
446 |
+
{
|
447 |
+
"cell_type": "code",
|
448 |
+
"execution_count": 106,
|
449 |
+
"metadata": {
|
450 |
+
"id": "K155zM7e53lt"
|
451 |
+
},
|
452 |
+
"outputs": [],
|
453 |
+
"source": [
|
454 |
+
"from langchain_community.document_loaders import TextLoader\n",
|
455 |
+
"\n",
|
456 |
+
"document_loader = TextLoader(\"./paul-graham-to-kindle/paul_graham_essays.txt\")\n",
|
457 |
+
"documents = document_loader.load()"
|
458 |
+
]
|
459 |
+
},
|
460 |
+
{
|
461 |
+
"cell_type": "markdown",
|
462 |
+
"metadata": {
|
463 |
+
"id": "5wYfo6_0bwVc"
|
464 |
+
},
|
465 |
+
"source": [
|
466 |
+
"Now, let's split them into 1000 character pieces."
|
467 |
+
]
|
468 |
+
},
|
469 |
+
{
|
470 |
+
"cell_type": "code",
|
471 |
+
"execution_count": 108,
|
472 |
+
"metadata": {
|
473 |
+
"colab": {
|
474 |
+
"base_uri": "https://localhost:8080/"
|
475 |
+
},
|
476 |
+
"id": "w-Gx_0iL6Ikc",
|
477 |
+
"outputId": "4cd1de4f-8a7d-4727-dc92-0ce3d321a82f"
|
478 |
+
},
|
479 |
+
"outputs": [
|
480 |
+
{
|
481 |
+
"data": {
|
482 |
+
"text/plain": [
|
483 |
+
"4265"
|
484 |
+
]
|
485 |
+
},
|
486 |
+
"execution_count": 108,
|
487 |
+
"metadata": {},
|
488 |
+
"output_type": "execute_result"
|
489 |
+
}
|
490 |
+
],
|
491 |
+
"source": [
|
492 |
+
"from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
|
493 |
+
"\n",
|
494 |
+
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)\n",
|
495 |
+
"split_documents = text_splitter.split_documents(documents)\n",
|
496 |
+
"len(split_documents)"
|
497 |
+
]
|
498 |
+
},
|
499 |
+
{
|
500 |
+
"cell_type": "markdown",
|
501 |
+
"metadata": {
|
502 |
+
"id": "d5HrkDhTb4i_"
|
503 |
+
},
|
504 |
+
"source": [
|
505 |
+
"Just the same as we would with OpenAI's embeddings model - we can instantiate our `FAISS` vector store with our documents and our `HuggingFaceEmbeddings` model!\n",
|
506 |
+
"\n",
|
507 |
+
"We'll need to take a few extra steps, though, due to a few limitations of the endpoint/FAISS.\n",
|
508 |
+
"\n",
|
509 |
+
"We'll start by embeddings our documents in batches of `32`.\n",
|
510 |
+
"\n",
|
511 |
+
"> NOTE: This process might take a while depending on the compute you assigned your embedding endpoint!"
|
512 |
+
]
|
513 |
+
},
|
514 |
+
{
|
515 |
+
"cell_type": "code",
|
516 |
+
"execution_count": 110,
|
517 |
+
"metadata": {
|
518 |
+
"id": "ucghQgRp6YXr"
|
519 |
+
},
|
520 |
+
"outputs": [],
|
521 |
+
"source": [
|
522 |
+
"from langchain_community.vectorstores import FAISS\n",
|
523 |
+
"\n",
|
524 |
+
"for i in range(0, len(split_documents), 32):\n",
|
525 |
+
" if i == 0:\n",
|
526 |
+
" vectorstore = FAISS.from_documents(split_documents[i:i+32], hf_embeddings)\n",
|
527 |
+
" continue\n",
|
528 |
+
" vectorstore.add_documents(split_documents[i:i+32])"
|
529 |
+
]
|
530 |
+
},
|
531 |
+
{
|
532 |
+
"cell_type": "markdown",
|
533 |
+
"metadata": {
|
534 |
+
"id": "q07ZUp6Db_AO"
|
535 |
+
},
|
536 |
+
"source": [
|
537 |
+
"Next, we set up FAISS as a retriever."
|
538 |
+
]
|
539 |
+
},
|
540 |
+
{
|
541 |
+
"cell_type": "code",
|
542 |
+
"execution_count": 111,
|
543 |
+
"metadata": {
|
544 |
+
"id": "fXr-yrAq7h8V"
|
545 |
+
},
|
546 |
+
"outputs": [],
|
547 |
+
"source": [
|
548 |
+
"hf_retriever = vectorstore.as_retriever()"
|
549 |
+
]
|
550 |
+
},
|
551 |
+
{
|
552 |
+
"cell_type": "markdown",
|
553 |
+
"metadata": {
|
554 |
+
"id": "sYrW6FRecO7U"
|
555 |
+
},
|
556 |
+
"source": [
|
557 |
+
"## Task 5: Simple LCEL RAG Chain\n",
|
558 |
+
"\n",
|
559 |
+
"Now we can set up our LCEL RAG chain!\n",
|
560 |
+
"\n",
|
561 |
+
"> NOTE: We're not returning context for this example, and only returning the text output from the LLM."
|
562 |
+
]
|
563 |
+
},
|
564 |
+
{
|
565 |
+
"cell_type": "code",
|
566 |
+
"execution_count": 112,
|
567 |
+
"metadata": {
|
568 |
+
"id": "ffIzIlct8ISb"
|
569 |
+
},
|
570 |
+
"outputs": [],
|
571 |
+
"source": [
|
572 |
+
"from operator import itemgetter\n",
|
573 |
+
"from langchain.schema.output_parser import StrOutputParser\n",
|
574 |
+
"from langchain.schema.runnable import RunnablePassthrough\n",
|
575 |
+
"\n",
|
576 |
+
"lcel_rag_chain = {\"context\": itemgetter(\"query\") | hf_retriever, \"query\": itemgetter(\"query\")}| rag_prompt | hf_llm"
|
577 |
+
]
|
578 |
+
},
|
579 |
+
{
|
580 |
+
"cell_type": "code",
|
581 |
+
"execution_count": 114,
|
582 |
+
"metadata": {
|
583 |
+
"colab": {
|
584 |
+
"base_uri": "https://localhost:8080/",
|
585 |
+
"height": 127
|
586 |
+
},
|
587 |
+
"id": "HOQfkEgb8nPH",
|
588 |
+
"outputId": "92601728-d001-43e2-e543-e714d66f4f4e"
|
589 |
+
},
|
590 |
+
"outputs": [
|
591 |
+
{
|
592 |
+
"data": {
|
593 |
+
"application/vnd.google.colaboratory.intrinsic+json": {
|
594 |
+
"type": "string"
|
595 |
+
},
|
596 |
+
"text/plain": [
|
597 |
+
"\"Based on the provided context, it seems that Paul Graham, the author, is discussing the shortcomings of Silicon Valley and suggesting ways to improve it. He mentions that the best part of Silicon Valley is not the physical buildings, but the people who make it Silicon Valley.\\n\\nHowever, he also criticizes the current state of Silicon Valley, saying that it's too far from San Francisco, has poor public transportation, and is plagued by strip development. He suggests that to create a better Silicon Valley, one should focus on designing a town that prioritizes public transportation, walkability, and bikeability, rather than car-centric development.\\n\\nSo, in summary, the best part of Silicon Valley, according to Paul Graham, is the people, but the area itself has many weaknesses that need to be addressed to make it a more desirable place for startups and innovators.\""
|
598 |
+
]
|
599 |
+
},
|
600 |
+
"execution_count": 114,
|
601 |
+
"metadata": {},
|
602 |
+
"output_type": "execute_result"
|
603 |
+
}
|
604 |
+
],
|
605 |
+
"source": [
|
606 |
+
"lcel_rag_chain.invoke({\"query\" : \"What is the best part of Silicon Valley?\"})"
|
607 |
+
]
|
608 |
+
}
|
609 |
+
],
|
610 |
+
"metadata": {
|
611 |
+
"colab": {
|
612 |
+
"provenance": [],
|
613 |
+
"toc_visible": true
|
614 |
+
},
|
615 |
+
"kernelspec": {
|
616 |
+
"display_name": "Python 3",
|
617 |
+
"name": "python3"
|
618 |
+
},
|
619 |
+
"language_info": {
|
620 |
+
"codemirror_mode": {
|
621 |
+
"name": "ipython",
|
622 |
+
"version": 3
|
623 |
+
},
|
624 |
+
"file_extension": ".py",
|
625 |
+
"mimetype": "text/x-python",
|
626 |
+
"name": "python",
|
627 |
+
"nbconvert_exporter": "python",
|
628 |
+
"pygments_lexer": "ipython3",
|
629 |
+
"version": "3.12.3"
|
630 |
+
}
|
631 |
+
},
|
632 |
+
"nbformat": 4,
|
633 |
+
"nbformat_minor": 0
|
634 |
+
}
|
README.md
CHANGED
@@ -1 +1,10 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: AI 10Q Analyzer Bot
|
3 |
+
emoji: 🚀
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: docker
|
7 |
+
pinned: false
|
8 |
+
---
|
9 |
+
|
10 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_community.document_loaders import PyMuPDFLoader
|
8 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
9 |
+
from langchain_community.vectorstores import FAISS
|
10 |
+
from langchain_huggingface import HuggingFaceEndpointEmbeddings
|
11 |
+
from langchain_core.prompts import PromptTemplate
|
12 |
+
from langchain.schema.output_parser import StrOutputParser
|
13 |
+
from langchain.schema.runnable import RunnablePassthrough
|
14 |
+
from langchain.schema.runnable.config import RunnableConfig
|
15 |
+
from langchain.globals import set_debug
|
16 |
+
|
17 |
+
set_debug(False)
|
18 |
+
|
19 |
+
# GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
|
20 |
+
# ---- ENV VARIABLES ---- #
|
21 |
+
"""
|
22 |
+
This function will load our environment file (.env) if it is present.
|
23 |
+
|
24 |
+
NOTE: Make sure that .env is in your .gitignore file - it is by default, but please ensure it remains there.
|
25 |
+
"""
|
26 |
+
load_dotenv()
|
27 |
+
|
28 |
+
"""
|
29 |
+
We will load our environment variables here.
|
30 |
+
"""
|
31 |
+
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
32 |
+
HF_EMBED_ENDPOINT = os.environ["HF_EMBED_ENDPOINT"]
|
33 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
34 |
+
|
35 |
+
# ---- GLOBAL DECLARATIONS ---- #
|
36 |
+
|
37 |
+
# -- RETRIEVAL -- #
|
38 |
+
"""
|
39 |
+
1. Load Documents from Text File
|
40 |
+
2. Split Documents into Chunks
|
41 |
+
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
42 |
+
4. Index Files if they do not exist, otherwise load the vectorstore
|
43 |
+
"""
|
44 |
+
### 1. CREATE TEXT LOADER AND LOAD DOCUMENTS
|
45 |
+
### NOTE: PAY ATTENTION TO THE PATH THEY ARE IN.
|
46 |
+
pdf_loader = PyMuPDFLoader("./data/10Q-AirBnB.pdf")
|
47 |
+
documents = pdf_loader.load()
|
48 |
+
|
49 |
+
### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
|
50 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=25)
|
51 |
+
split_documents = text_splitter.split_documents(documents)
|
52 |
+
|
53 |
+
### 3. LOAD HUGGINGFACE EMBEDDINGS
|
54 |
+
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
55 |
+
model=HF_EMBED_ENDPOINT,
|
56 |
+
task="feature-extraction",
|
57 |
+
huggingfacehub_api_token=HF_TOKEN,
|
58 |
+
)
|
59 |
+
DATA_DIR = "./data"
|
60 |
+
VECTOR_STORE_DIR = os.path.join(DATA_DIR, "vectorstore")
|
61 |
+
VECTOR_STORE_PATH = os.path.join(VECTOR_STORE_DIR, "index.faiss")
|
62 |
+
|
63 |
+
FAISS_MAX_FETCH_SIZE = 2
|
64 |
+
FAISS_MAX_BATCH_SIZE = 32
|
65 |
+
if os.path.exists(VECTOR_STORE_PATH):
|
66 |
+
vectorstore = FAISS.load_local(
|
67 |
+
VECTOR_STORE_DIR,
|
68 |
+
hf_embeddings,
|
69 |
+
allow_dangerous_deserialization=True # this is necessary to load the vectorstore from disk as it's stored as a `.pkl` file.
|
70 |
+
)
|
71 |
+
hf_retriever = vectorstore.as_retriever(search_kwargs={"k": FAISS_MAX_FETCH_SIZE, "fetch_k": FAISS_MAX_FETCH_SIZE})
|
72 |
+
print("Loaded Vectorstore at " + VECTOR_STORE_DIR)
|
73 |
+
else:
|
74 |
+
print("Indexing Files")
|
75 |
+
os.makedirs(VECTOR_STORE_DIR, exist_ok=True)
|
76 |
+
### 4. INDEX FILES
|
77 |
+
### NOTE: REMEMBER TO BATCH THE DOCUMENTS WITH MAXIMUM BATCH SIZE = 32
|
78 |
+
for i in range(0, len(split_documents), FAISS_MAX_BATCH_SIZE):
|
79 |
+
if i==0:
|
80 |
+
vectorstore = FAISS.from_documents(split_documents[i:i+FAISS_MAX_BATCH_SIZE], hf_embeddings)
|
81 |
+
continue
|
82 |
+
vectorstore.add_documents(split_documents[i:i+FAISS_MAX_BATCH_SIZE])
|
83 |
+
vectorstore.save_local(VECTOR_STORE_DIR)
|
84 |
+
|
85 |
+
hf_retriever = vectorstore.as_retriever(search_kwargs={"k": FAISS_MAX_FETCH_SIZE, "fetch_k": FAISS_MAX_FETCH_SIZE})
|
86 |
+
|
87 |
+
# -- AUGMENTED -- #
|
88 |
+
"""
|
89 |
+
1. Define a String Template
|
90 |
+
2. Create a Prompt Template from the String Template
|
91 |
+
"""
|
92 |
+
### 1. DEFINE STRING TEMPLATE
|
93 |
+
RAG_PROMPT_TEMPLATE = """\
|
94 |
+
<|start_header_id|>system<|end_header_id|>
|
95 |
+
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|>
|
96 |
+
|
97 |
+
<|start_header_id|>user<|end_header_id|>
|
98 |
+
User Query:
|
99 |
+
{query}
|
100 |
+
|
101 |
+
Context:
|
102 |
+
{context}<|eot_id|>
|
103 |
+
|
104 |
+
<|start_header_id|>assistant<|end_header_id|>
|
105 |
+
"""
|
106 |
+
|
107 |
+
### 2. CREATE PROMPT TEMPLATE
|
108 |
+
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
109 |
+
|
110 |
+
# -- GENERATION -- #
|
111 |
+
"""
|
112 |
+
1. Create a HuggingFaceEndpoint for the LLM
|
113 |
+
"""
|
114 |
+
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
|
115 |
+
hf_llm = HuggingFaceEndpoint(
|
116 |
+
endpoint_url=HF_LLM_ENDPOINT,
|
117 |
+
max_new_tokens=512,
|
118 |
+
top_k=10,
|
119 |
+
top_p=0.95,
|
120 |
+
temperature=0.3,
|
121 |
+
repetition_penalty=1.15,
|
122 |
+
huggingfacehub_api_token=HF_TOKEN,
|
123 |
+
)
|
124 |
+
|
125 |
+
@cl.author_rename
|
126 |
+
def rename(original_author: str):
|
127 |
+
"""
|
128 |
+
This function can be used to rename the 'author' of a message.
|
129 |
+
|
130 |
+
In this case, we're overriding the 'Assistant' author to be 'Paul Graham Essay Bot'.
|
131 |
+
"""
|
132 |
+
rename_dict = {
|
133 |
+
"Assistant" : "Paul Graham Essays Bot"
|
134 |
+
}
|
135 |
+
return rename_dict.get(original_author, original_author)
|
136 |
+
|
137 |
+
@cl.on_chat_start
|
138 |
+
async def start_chat():
|
139 |
+
"""
|
140 |
+
This function will be called at the start of every user session.
|
141 |
+
|
142 |
+
We will build our LCEL RAG chain here, and store it in the user session.
|
143 |
+
|
144 |
+
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
145 |
+
"""
|
146 |
+
|
147 |
+
### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
|
148 |
+
lcel_rag_chain = (
|
149 |
+
{"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
150 |
+
| rag_prompt | hf_llm
|
151 |
+
)
|
152 |
+
|
153 |
+
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
154 |
+
|
155 |
+
@cl.on_message
|
156 |
+
async def main(message: cl.Message):
|
157 |
+
"""
|
158 |
+
This function will be called every time a message is recieved from a session.
|
159 |
+
|
160 |
+
We will use the LCEL RAG chain to generate a response to the user query.
|
161 |
+
|
162 |
+
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.
|
163 |
+
"""
|
164 |
+
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
165 |
+
|
166 |
+
msg = cl.Message(content="")
|
167 |
+
|
168 |
+
async for chunk in lcel_rag_chain.astream(
|
169 |
+
{"query": message.content},
|
170 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
171 |
+
):
|
172 |
+
await msg.stream_token(chunk)
|
173 |
+
|
174 |
+
await msg.send()
|
chainlit.md
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# Paul Graham Essays Bot
|
2 |
+
|
3 |
+
### I am your personal assistant that can advise you on investing and silicon valley. I have built my knowledge from essays by Paul Graham.
|
data/10Q-AirBnB.pdf
ADDED
Binary file (596 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
chainlit==1.1.304
|
2 |
+
langchain==0.2.5
|
3 |
+
langchain_community==0.2.5
|
4 |
+
langchain_core==0.2.9
|
5 |
+
langchain_huggingface==0.0.3
|
6 |
+
langchain_text_splitters==0.2.1
|
7 |
+
python-dotenv==1.0.1
|
8 |
+
faiss-cpu
|
9 |
+
pymupdf
|
stuff
ADDED
File without changes
|