Spaces:
Running
Running
SimaFarazi
commited on
Commit
•
d95d830
1
Parent(s):
e4e0003
add history prompt app dir
Browse files- Dockerfile +2 -2
- app_format_prompt/app/__pycache__/chains.cpython-312.pyc +0 -0
- app_format_prompt/app/__pycache__/prompts.cpython-312.pyc +0 -0
- app_format_prompt/app/test_components.ipynb +36 -85
- app_history_prompt/app/__pycache__/chains.cpython-312.pyc +0 -0
- app_history_prompt/app/__pycache__/prompts.cpython-312.pyc +0 -0
- app_history_prompt/app/__pycache__/schemas.cpython-312.pyc +0 -0
- app_history_prompt/app/chains.py +25 -0
- app_history_prompt/app/main.py +42 -0
- app_history_prompt/app/prompts.py +37 -0
- app_history_prompt/app/schemas.py +7 -0
- app_history_prompt/app/test_components.ipynb +296 -0
- app_history_prompt/requirements.txt +13 -0
- app_history_prompt/test.py +12 -0
Dockerfile
CHANGED
@@ -9,11 +9,11 @@ ENV PATH="/home/user/.local/bin:$PATH"
|
|
9 |
WORKDIR /app
|
10 |
# Copy the requirements.txt file from the host to the container
|
11 |
# The --chown=user ensures the copied file is owned by our 'user'
|
12 |
-
COPY --chown=user ./
|
13 |
# Install the Python dependencies listed in requirements.txt
|
14 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
15 |
# Copy the rest of the application code from the host to the container
|
16 |
# Again, ensure the copied files are owned by 'user'
|
17 |
-
COPY --chown=user ./
|
18 |
# Specify the command to run when the container starts
|
19 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
9 |
WORKDIR /app
|
10 |
# Copy the requirements.txt file from the host to the container
|
11 |
# The --chown=user ensures the copied file is owned by our 'user'
|
12 |
+
COPY --chown=user ./app_history_prompt/requirements.txt requirements.txt
|
13 |
# Install the Python dependencies listed in requirements.txt
|
14 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
15 |
# Copy the rest of the application code from the host to the container
|
16 |
# Again, ensure the copied files are owned by 'user'
|
17 |
+
COPY --chown=user ./app_history_prompt/app/* /app
|
18 |
# Specify the command to run when the container starts
|
19 |
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app_format_prompt/app/__pycache__/chains.cpython-312.pyc
CHANGED
Binary files a/app_format_prompt/app/__pycache__/chains.cpython-312.pyc and b/app_format_prompt/app/__pycache__/chains.cpython-312.pyc differ
|
|
app_format_prompt/app/__pycache__/prompts.cpython-312.pyc
CHANGED
Binary files a/app_format_prompt/app/__pycache__/prompts.cpython-312.pyc and b/app_format_prompt/app/__pycache__/prompts.cpython-312.pyc differ
|
|
app_format_prompt/app/test_components.ipynb
CHANGED
@@ -152,9 +152,18 @@
|
|
152 |
},
|
153 |
{
|
154 |
"cell_type": "code",
|
155 |
-
"execution_count":
|
156 |
"metadata": {},
|
157 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
"source": [
|
159 |
"from transformers import AutoTokenizer\n",
|
160 |
"from langchain_core.prompts import PromptTemplate\n",
|
@@ -183,7 +192,7 @@
|
|
183 |
},
|
184 |
{
|
185 |
"cell_type": "code",
|
186 |
-
"execution_count":
|
187 |
"metadata": {},
|
188 |
"outputs": [
|
189 |
{
|
@@ -205,37 +214,34 @@
|
|
205 |
"raw_prompt_formatted = format_prompt(raw_prompt)"
|
206 |
]
|
207 |
},
|
208 |
-
{
|
209 |
-
"cell_type": "markdown",
|
210 |
-
"metadata": {},
|
211 |
-
"source": []
|
212 |
-
},
|
213 |
{
|
214 |
"cell_type": "code",
|
215 |
-
"execution_count":
|
216 |
"metadata": {},
|
217 |
"outputs": [
|
218 |
{
|
219 |
-
"
|
220 |
-
"
|
221 |
-
"
|
222 |
-
|
223 |
-
"
|
224 |
-
"
|
225 |
-
"
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
"
|
233 |
-
"
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
|
|
239 |
]
|
240 |
}
|
241 |
],
|
@@ -246,7 +252,7 @@
|
|
246 |
"load_dotenv()\n",
|
247 |
"\n",
|
248 |
"from chains import formatted_chain\n",
|
249 |
-
"stream =
|
250 |
"for chunk in stream: # Each chunk corresponds to a token/word\n",
|
251 |
" #end=\"\": prints worlds one after each other, an not in a separate lines\n",
|
252 |
" #flush=True: prints world to the screen immidiately without any buffer\n",
|
@@ -258,61 +264,6 @@
|
|
258 |
"metadata": {},
|
259 |
"source": []
|
260 |
},
|
261 |
-
{
|
262 |
-
"cell_type": "code",
|
263 |
-
"execution_count": 28,
|
264 |
-
"metadata": {},
|
265 |
-
"outputs": [
|
266 |
-
{
|
267 |
-
"ename": "RemoteProtocolError",
|
268 |
-
"evalue": "peer closed connection without sending complete message body (incomplete chunked read)",
|
269 |
-
"output_type": "error",
|
270 |
-
"traceback": [
|
271 |
-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
272 |
-
"\u001b[0;31mRemoteProtocolError\u001b[0m Traceback (most recent call last)",
|
273 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_transports/default.py:72\u001b[0m, in \u001b[0;36mmap_httpcore_exceptions\u001b[0;34m()\u001b[0m\n\u001b[1;32m 71\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 72\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n",
|
274 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_transports/default.py:116\u001b[0m, in \u001b[0;36mResponseStream.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_httpcore_exceptions():\n\u001b[0;32m--> 116\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mpart\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_httpcore_stream\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 117\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01myield\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mpart\u001b[49m\n",
|
275 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py:367\u001b[0m, in \u001b[0;36mPoolByteStream.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 366\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclose()\n\u001b[0;32m--> 367\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n",
|
276 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpcore/_sync/connection_pool.py:363\u001b[0m, in \u001b[0;36mPoolByteStream.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 363\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mpart\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_stream\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 364\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01myield\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mpart\u001b[49m\n",
|
277 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpcore/_sync/http11.py:349\u001b[0m, in \u001b[0;36mHTTP11ConnectionByteStream.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 348\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclose()\n\u001b[0;32m--> 349\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n",
|
278 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpcore/_sync/http11.py:341\u001b[0m, in \u001b[0;36mHTTP11ConnectionByteStream.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 340\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m Trace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mreceive_response_body\u001b[39m\u001b[38;5;124m\"\u001b[39m, logger, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_request, kwargs):\n\u001b[0;32m--> 341\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_receive_response_body\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 342\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01myield\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\n",
|
279 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpcore/_sync/http11.py:210\u001b[0m, in \u001b[0;36mHTTP11Connection._receive_response_body\u001b[0;34m(self, request)\u001b[0m\n\u001b[1;32m 209\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m--> 210\u001b[0m event \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_receive_event\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 211\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(event, h11\u001b[38;5;241m.\u001b[39mData):\n",
|
280 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpcore/_sync/http11.py:220\u001b[0m, in \u001b[0;36mHTTP11Connection._receive_event\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 219\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m--> 220\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_exceptions({h11\u001b[38;5;241m.\u001b[39mRemoteProtocolError: RemoteProtocolError}):\n\u001b[1;32m 221\u001b[0m event \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_h11_state\u001b[38;5;241m.\u001b[39mnext_event()\n",
|
281 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/contextlib.py:158\u001b[0m, in \u001b[0;36m_GeneratorContextManager.__exit__\u001b[0;34m(self, typ, value, traceback)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 158\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgen\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrow\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 160\u001b[0m \u001b[38;5;66;03m# Suppress StopIteration *unless* it's the same exception that\u001b[39;00m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;66;03m# was passed to throw(). This prevents a StopIteration\u001b[39;00m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# raised inside the \"with\" statement from being suppressed.\u001b[39;00m\n",
|
282 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpcore/_exceptions.py:14\u001b[0m, in \u001b[0;36mmap_exceptions\u001b[0;34m(map)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(exc, from_exc):\n\u001b[0;32m---> 14\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m to_exc(exc) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mexc\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m\n",
|
283 |
-
"\u001b[0;31mRemoteProtocolError\u001b[0m: peer closed connection without sending complete message body (incomplete chunked read)",
|
284 |
-
"\nThe above exception was the direct cause of the following exception:\n",
|
285 |
-
"\u001b[0;31mRemoteProtocolError\u001b[0m Traceback (most recent call last)",
|
286 |
-
"Cell \u001b[0;32mIn[28], line 9\u001b[0m\n\u001b[1;32m 5\u001b[0m chain \u001b[38;5;241m=\u001b[39m RemoteRunnable(url) \u001b[38;5;66;03m#Client for iteracting with LangChain runnables that are hosted as LangServe endpoints\u001b[39;00m\n\u001b[1;32m 6\u001b[0m stream \u001b[38;5;241m=\u001b[39m chain\u001b[38;5;241m.\u001b[39mstream(\u001b[38;5;28minput\u001b[39m\u001b[38;5;241m=\u001b[39m{\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mquestion\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mHow are you?\u001b[39m\u001b[38;5;124m'\u001b[39m}) \u001b[38;5;66;03m# .stream() and .invoke() are standard methods to interact with hosted runnables\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m \u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# Each chunk corresponds to a token/word\u001b[39;49;00m\n\u001b[1;32m 10\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m#end=\"\": prints worlds one after each other, an not in a separate lines\u001b[39;49;00m\n\u001b[1;32m 11\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m#flush=True: prints world to the screen immidiately without any buffer\u001b[39;49;00m\n\u001b[1;32m 12\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mprint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mend\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mflush\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m \n",
|
287 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langserve/client.py:544\u001b[0m, in \u001b[0;36mRemoteRunnable.stream\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 540\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 541\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m connect_sse(\n\u001b[1;32m 542\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msync_client, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPOST\u001b[39m\u001b[38;5;124m\"\u001b[39m, endpoint, json\u001b[38;5;241m=\u001b[39mdata\n\u001b[1;32m 543\u001b[0m ) \u001b[38;5;28;01mas\u001b[39;00m event_source:\n\u001b[0;32m--> 544\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43msse\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mevent_source\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43miter_sse\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 545\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43msse\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mevent\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m==\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mdata\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\n\u001b[1;32m 546\u001b[0m \u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_lc_serializer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloads\u001b[49m\u001b[43m(\u001b[49m\u001b[43msse\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mdata\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
|
288 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langserve/server_sent_events.py:97\u001b[0m, in \u001b[0;36mEventSource.iter_sse\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 95\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_content_type()\n\u001b[1;32m 96\u001b[0m decoder \u001b[38;5;241m=\u001b[39m SSEDecoder()\n\u001b[0;32m---> 97\u001b[0m \u001b[43m\u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mline\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_response\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43miter_lines\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 98\u001b[0m \u001b[43m \u001b[49m\u001b[43mline\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mline\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrstrip\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 99\u001b[0m \u001b[43m \u001b[49m\u001b[43msse\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mdecoder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43mline\u001b[49m\u001b[43m)\u001b[49m\n",
|
289 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_models.py:863\u001b[0m, in \u001b[0;36mResponse.iter_lines\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 861\u001b[0m decoder \u001b[38;5;241m=\u001b[39m LineDecoder()\n\u001b[1;32m 862\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m request_context(request\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_request):\n\u001b[0;32m--> 863\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mtext\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43miter_text\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 864\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mline\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mdecoder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 865\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01myield\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mline\u001b[49m\n",
|
290 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_models.py:850\u001b[0m, in \u001b[0;36mResponse.iter_text\u001b[0;34m(self, chunk_size)\u001b[0m\n\u001b[1;32m 848\u001b[0m chunker \u001b[38;5;241m=\u001b[39m TextChunker(chunk_size\u001b[38;5;241m=\u001b[39mchunk_size)\n\u001b[1;32m 849\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m request_context(request\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_request):\n\u001b[0;32m--> 850\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mbyte_content\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43miter_bytes\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 851\u001b[0m \u001b[43m \u001b[49m\u001b[43mtext_content\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mdecoder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbyte_content\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 852\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunker\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext_content\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n",
|
291 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_models.py:831\u001b[0m, in \u001b[0;36mResponse.iter_bytes\u001b[0;34m(self, chunk_size)\u001b[0m\n\u001b[1;32m 829\u001b[0m chunker \u001b[38;5;241m=\u001b[39m ByteChunker(chunk_size\u001b[38;5;241m=\u001b[39mchunk_size)\n\u001b[1;32m 830\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m request_context(request\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_request):\n\u001b[0;32m--> 831\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mraw_bytes\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43miter_raw\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 832\u001b[0m \u001b[43m \u001b[49m\u001b[43mdecoded\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mdecoder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43mraw_bytes\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 833\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunker\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdecoded\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n",
|
292 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_models.py:885\u001b[0m, in \u001b[0;36mResponse.iter_raw\u001b[0;34m(self, chunk_size)\u001b[0m\n\u001b[1;32m 882\u001b[0m chunker \u001b[38;5;241m=\u001b[39m ByteChunker(chunk_size\u001b[38;5;241m=\u001b[39mchunk_size)\n\u001b[1;32m 884\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m request_context(request\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_request):\n\u001b[0;32m--> 885\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mraw_stream_bytes\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 886\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_num_bytes_downloaded\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mraw_stream_bytes\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 887\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunker\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43mraw_stream_bytes\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n",
|
293 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_client.py:127\u001b[0m, in \u001b[0;36mBoundSyncStream.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 126\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__iter__\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m typing\u001b[38;5;241m.\u001b[39mIterator[\u001b[38;5;28mbytes\u001b[39m]:\n\u001b[0;32m--> 127\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_stream\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 128\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01myield\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\n",
|
294 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_transports/default.py:115\u001b[0m, in \u001b[0;36mResponseStream.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 114\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__iter__\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m typing\u001b[38;5;241m.\u001b[39mIterator[\u001b[38;5;28mbytes\u001b[39m]:\n\u001b[0;32m--> 115\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m map_httpcore_exceptions():\n\u001b[1;32m 116\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m part \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_httpcore_stream:\n\u001b[1;32m 117\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m part\n",
|
295 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/contextlib.py:158\u001b[0m, in \u001b[0;36m_GeneratorContextManager.__exit__\u001b[0;34m(self, typ, value, traceback)\u001b[0m\n\u001b[1;32m 156\u001b[0m value \u001b[38;5;241m=\u001b[39m typ()\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 158\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgen\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrow\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 160\u001b[0m \u001b[38;5;66;03m# Suppress StopIteration *unless* it's the same exception that\u001b[39;00m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;66;03m# was passed to throw(). This prevents a StopIteration\u001b[39;00m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# raised inside the \"with\" statement from being suppressed.\u001b[39;00m\n\u001b[1;32m 163\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m exc \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m value\n",
|
296 |
-
"File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/httpx/_transports/default.py:89\u001b[0m, in \u001b[0;36mmap_httpcore_exceptions\u001b[0;34m()\u001b[0m\n\u001b[1;32m 86\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m\n\u001b[1;32m 88\u001b[0m message \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(exc)\n\u001b[0;32m---> 89\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m mapped_exc(message) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mexc\u001b[39;00m\n",
|
297 |
-
"\u001b[0;31mRemoteProtocolError\u001b[0m: peer closed connection without sending complete message body (incomplete chunked read)"
|
298 |
-
]
|
299 |
-
}
|
300 |
-
],
|
301 |
-
"source": [
|
302 |
-
"from langserve import RemoteRunnable\n",
|
303 |
-
"# Hit our enpoint with specified rout\n",
|
304 |
-
"# If we put /simple/stream, it complains; because chain.stream will hit /simple/stream endpoint\n",
|
305 |
-
"url = \"https://simafarazi-backend-c.hf.space/formatted\"\n",
|
306 |
-
"chain = RemoteRunnable(url) #Client for iteracting with LangChain runnables that are hosted as LangServe endpoints\n",
|
307 |
-
"stream = chain.stream(input={'question':'How are you?'}) # .stream() and .invoke() are standard methods to interact with hosted runnables\n",
|
308 |
-
"\n",
|
309 |
-
"\n",
|
310 |
-
"for chunk in stream: # Each chunk corresponds to a token/word\n",
|
311 |
-
" #end=\"\": prints worlds one after each other, an not in a separate lines\n",
|
312 |
-
" #flush=True: prints world to the screen immidiately without any buffer\n",
|
313 |
-
" print(chunk, end=\"\", flush=True) \n"
|
314 |
-
]
|
315 |
-
},
|
316 |
{
|
317 |
"cell_type": "code",
|
318 |
"execution_count": null,
|
|
|
152 |
},
|
153 |
{
|
154 |
"cell_type": "code",
|
155 |
+
"execution_count": 1,
|
156 |
"metadata": {},
|
157 |
+
"outputs": [
|
158 |
+
{
|
159 |
+
"name": "stderr",
|
160 |
+
"output_type": "stream",
|
161 |
+
"text": [
|
162 |
+
"/Users/sima/opt/anaconda3/envs/llm_app_rag/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",
|
163 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
164 |
+
]
|
165 |
+
}
|
166 |
+
],
|
167 |
"source": [
|
168 |
"from transformers import AutoTokenizer\n",
|
169 |
"from langchain_core.prompts import PromptTemplate\n",
|
|
|
192 |
},
|
193 |
{
|
194 |
"cell_type": "code",
|
195 |
+
"execution_count": 2,
|
196 |
"metadata": {},
|
197 |
"outputs": [
|
198 |
{
|
|
|
214 |
"raw_prompt_formatted = format_prompt(raw_prompt)"
|
215 |
]
|
216 |
},
|
|
|
|
|
|
|
|
|
|
|
217 |
{
|
218 |
"cell_type": "code",
|
219 |
+
"execution_count": 3,
|
220 |
"metadata": {},
|
221 |
"outputs": [
|
222 |
{
|
223 |
+
"name": "stdout",
|
224 |
+
"output_type": "stream",
|
225 |
+
"text": [
|
226 |
+
"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",
|
227 |
+
"Token is valid (permission: write).\n",
|
228 |
+
"Your token has been saved to /Users/sima/.cache/huggingface/token\n",
|
229 |
+
"Login successful\n"
|
230 |
+
]
|
231 |
+
},
|
232 |
+
{
|
233 |
+
"name": "stderr",
|
234 |
+
"output_type": "stream",
|
235 |
+
"text": [
|
236 |
+
"/Users/sima/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/huggingface_hub/inference/_client.py:2027: FutureWarning: `stop_sequences` is a deprecated argument for `text_generation` task and will be removed in version '0.28.0'. Use `stop` instead.\n",
|
237 |
+
" warnings.warn(\n"
|
238 |
+
]
|
239 |
+
},
|
240 |
+
{
|
241 |
+
"name": "stdout",
|
242 |
+
"output_type": "stream",
|
243 |
+
"text": [
|
244 |
+
"I'm doing well, thank you for asking! I'm a helpful AI assistant, and I'm here to assist you with any questions or tasks you may have. I'm functioning within normal parameters and ready to help with any inquiry or problem you'd like to discuss. How about you? How can I assist you today?"
|
245 |
]
|
246 |
}
|
247 |
],
|
|
|
252 |
"load_dotenv()\n",
|
253 |
"\n",
|
254 |
"from chains import formatted_chain\n",
|
255 |
+
"stream = formatted_chain.stream(input={'question':'How are you?'})\n",
|
256 |
"for chunk in stream: # Each chunk corresponds to a token/word\n",
|
257 |
" #end=\"\": prints worlds one after each other, an not in a separate lines\n",
|
258 |
" #flush=True: prints world to the screen immidiately without any buffer\n",
|
|
|
264 |
"metadata": {},
|
265 |
"source": []
|
266 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
{
|
268 |
"cell_type": "code",
|
269 |
"execution_count": null,
|
app_history_prompt/app/__pycache__/chains.cpython-312.pyc
ADDED
Binary file (948 Bytes). View file
|
|
app_history_prompt/app/__pycache__/prompts.cpython-312.pyc
ADDED
Binary file (1.13 kB). View file
|
|
app_history_prompt/app/__pycache__/schemas.cpython-312.pyc
ADDED
Binary file (490 Bytes). View file
|
|
app_history_prompt/app/chains.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
2 |
+
import os
|
3 |
+
from prompts import (
|
4 |
+
raw_prompt,
|
5 |
+
tokenizer,
|
6 |
+
raw_prompt_formatted
|
7 |
+
)
|
8 |
+
import schemas
|
9 |
+
|
10 |
+
# Instantiate HuggingFace endpoint with Llama model
|
11 |
+
llm = HuggingFaceEndpoint(
|
12 |
+
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
|
13 |
+
huggingfacehub_api_token=os.environ['HF_TOKEN'],
|
14 |
+
max_new_tokens=512, # Response will not exceed 512 words/tokens
|
15 |
+
stop_sequences=[tokenizer.eos_token],
|
16 |
+
streaming=True,
|
17 |
+
)
|
18 |
+
|
19 |
+
# Build a chain by pipping prompt object & HF endpoint
|
20 |
+
# Attach UserQuestion data model to chain to ensure validity of input data
|
21 |
+
simple_chain = (raw_prompt | llm).with_types(input_type=schemas.UserQuestion)
|
22 |
+
|
23 |
+
# Create formatted_chain by piping raw_prompt_formatted and the LLM endpoint.
|
24 |
+
formatted_chain = (raw_prompt_formatted | llm).with_types(input_type=schemas.UserQuestion)
|
25 |
+
|
app_history_prompt/app/main.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from langchain_core.runnables import Runnable
|
3 |
+
from langchain_core.callbacks import BaseCallbackHandler # Print what is sent to llm and what was recieved as answer
|
4 |
+
|
5 |
+
from sse_starlette.sse import EventSourceResponse # Implement Server-Sent Event protocol
|
6 |
+
from langserve.serialization import WellKnownLCSerializer # Encode data because we are using Langserve on the frontend to recieve events, and Langserve is using this class to decode the data
|
7 |
+
from typing import List
|
8 |
+
|
9 |
+
import schemas
|
10 |
+
from chains import (
|
11 |
+
simple_chain,
|
12 |
+
formatted_chain
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
app = FastAPI()
|
17 |
+
|
18 |
+
# Generate stream of data
|
19 |
+
async def generate_stream(input_data: schemas.BaseModel, runnable: Runnable, callbacks: List[BaseCallbackHandler]=[]):
|
20 |
+
for output in runnable.stream(input_data.dict(), config={"callbacks": callbacks}):
|
21 |
+
data = WellKnownLCSerializer().dumps(output).decode("utf-8") # Decode data
|
22 |
+
yield {'data': data, "event": "data"} # Use yield to create generator, which can pause the execution and return a value
|
23 |
+
|
24 |
+
yield {"event": "end"}
|
25 |
+
|
26 |
+
|
27 |
+
@app.post("/simple/stream")
|
28 |
+
async def simple_stream(request: Request):
|
29 |
+
data = await request.json()
|
30 |
+
user_question = schemas.UserQuestion(**data['input']) # Validate user question to be passed to the chain
|
31 |
+
return EventSourceResponse(generate_stream(user_question, simple_chain)) # Generate stream
|
32 |
+
|
33 |
+
@app.post("/formatted/stream")
|
34 |
+
async def simple_stream(request: Request):
|
35 |
+
data = await request.json()
|
36 |
+
user_question = schemas.UserQuestion(**data['input']) # Validate user question to be passed to the chain
|
37 |
+
return EventSourceResponse(generate_stream(user_question, formatted_chain)) # Generate stream
|
38 |
+
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
import uvicorn
|
42 |
+
uvicorn.run("main:app", host="localhost", reload=True, port=8000)
|
app_history_prompt/app/prompts.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer
|
2 |
+
from langchain_core.prompts import PromptTemplate
|
3 |
+
|
4 |
+
# Get tokenizer; required to get eos_token
|
5 |
+
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
7 |
+
|
8 |
+
# Initiate rwa_prompt with place holder "{question}"
|
9 |
+
prompt = "{question}"
|
10 |
+
|
11 |
+
# Convert prompt text into an object, with which Lanchain can build a chain
|
12 |
+
raw_prompt = PromptTemplate.from_template(prompt)
|
13 |
+
|
14 |
+
|
15 |
+
def format_prompt(prompt) -> PromptTemplate:
|
16 |
+
# Format the input prompt by using the model specific instruction template
|
17 |
+
chat = [
|
18 |
+
{"role": "system", "content": "You are a helpful AI assistant."},
|
19 |
+
{"role": "user", "content": prompt},
|
20 |
+
]
|
21 |
+
formatted_prompt = tokenizer.apply_chat_template(
|
22 |
+
chat,
|
23 |
+
tokenize=False,
|
24 |
+
add_generation_prompt=True
|
25 |
+
)
|
26 |
+
# Return a langchain PromptTemplate object
|
27 |
+
langchain_prompt = PromptTemplate.from_template(formatted_prompt)
|
28 |
+
|
29 |
+
return langchain_prompt
|
30 |
+
|
31 |
+
# Create raw_prompt_formatted by using format_prompt
|
32 |
+
raw_prompt_formatted = format_prompt(prompt)
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
app_history_prompt/app/schemas.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#from pydantic.v1 import BaseModel
|
2 |
+
from langserve._pydantic import BaseModel
|
3 |
+
|
4 |
+
# Create a data model with only one field named as question and type of string
|
5 |
+
class UserQuestion(BaseModel):
|
6 |
+
question: str
|
7 |
+
|
app_history_prompt/app/test_components.ipynb
ADDED
@@ -0,0 +1,296 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"#from pydantic.v1 import BaseModel\n",
|
10 |
+
"from langserve._pydantic import BaseModel\n",
|
11 |
+
"\n",
|
12 |
+
"\n",
|
13 |
+
"class UserQuestion(BaseModel):\n",
|
14 |
+
" question: str"
|
15 |
+
]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": 13,
|
20 |
+
"metadata": {},
|
21 |
+
"outputs": [],
|
22 |
+
"source": [
|
23 |
+
"user_question = \"How are you?\"\n",
|
24 |
+
"validated_user_question = UserQuestion(question=user_question)"
|
25 |
+
]
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"cell_type": "code",
|
29 |
+
"execution_count": 15,
|
30 |
+
"metadata": {},
|
31 |
+
"outputs": [
|
32 |
+
{
|
33 |
+
"data": {
|
34 |
+
"text/plain": [
|
35 |
+
"'How are you?'"
|
36 |
+
]
|
37 |
+
},
|
38 |
+
"execution_count": 15,
|
39 |
+
"metadata": {},
|
40 |
+
"output_type": "execute_result"
|
41 |
+
}
|
42 |
+
],
|
43 |
+
"source": [
|
44 |
+
"validated_user_question.question"
|
45 |
+
]
|
46 |
+
},
|
47 |
+
{
|
48 |
+
"cell_type": "code",
|
49 |
+
"execution_count": 18,
|
50 |
+
"metadata": {},
|
51 |
+
"outputs": [],
|
52 |
+
"source": [
|
53 |
+
"user_question = UserQuestion(**{'question': 'How are you?'})"
|
54 |
+
]
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"cell_type": "code",
|
58 |
+
"execution_count": 12,
|
59 |
+
"metadata": {},
|
60 |
+
"outputs": [
|
61 |
+
{
|
62 |
+
"data": {
|
63 |
+
"text/plain": [
|
64 |
+
"UserQuestion(question='How are you?')"
|
65 |
+
]
|
66 |
+
},
|
67 |
+
"execution_count": 12,
|
68 |
+
"metadata": {},
|
69 |
+
"output_type": "execute_result"
|
70 |
+
}
|
71 |
+
],
|
72 |
+
"source": [
|
73 |
+
"user_question"
|
74 |
+
]
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"cell_type": "code",
|
78 |
+
"execution_count": 1,
|
79 |
+
"metadata": {},
|
80 |
+
"outputs": [
|
81 |
+
{
|
82 |
+
"name": "stderr",
|
83 |
+
"output_type": "stream",
|
84 |
+
"text": [
|
85 |
+
"/Users/sima/opt/anaconda3/envs/llm_app_rag/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",
|
86 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
87 |
+
]
|
88 |
+
},
|
89 |
+
{
|
90 |
+
"name": "stdout",
|
91 |
+
"output_type": "stream",
|
92 |
+
"text": [
|
93 |
+
"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",
|
94 |
+
"Token is valid (permission: write).\n",
|
95 |
+
"Your token has been saved to /Users/sima/.cache/huggingface/token\n",
|
96 |
+
"Login successful\n"
|
97 |
+
]
|
98 |
+
},
|
99 |
+
{
|
100 |
+
"name": "stderr",
|
101 |
+
"output_type": "stream",
|
102 |
+
"text": [
|
103 |
+
"/Users/sima/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/huggingface_hub/inference/_client.py:2027: FutureWarning: `stop_sequences` is a deprecated argument for `text_generation` task and will be removed in version '0.28.0'. Use `stop` instead.\n",
|
104 |
+
" warnings.warn(\n"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"name": "stdout",
|
109 |
+
"output_type": "stream",
|
110 |
+
"text": [
|
111 |
+
" I hope you're doing well. I'm so excited to be writing to you today. I just wanted to say thank you for everything you do. I know it can't be easy, but you make it look effortless.\n",
|
112 |
+
"I was thinking about your job the other day, and I realized just how much you do. You're not just a [job title], you're a problem solver, a mediator, a leader, and so much more. You make a difference in people's lives every day, and that's truly amazing.\n",
|
113 |
+
"\n",
|
114 |
+
"I know that sometimes it can be tough, and you might feel like you're just going through the motions. But I want you to know that you're not. You're making a real impact, and it's not going unnoticed.\n",
|
115 |
+
"\n",
|
116 |
+
"I hope you know that you're appreciated, and that you're valued. You're an important part of [organization/team/community], and we're all better because of you.\n",
|
117 |
+
"\n",
|
118 |
+
"Thank you again for all that you do. I'm so grateful to have you in my life, and I hope you know that you're making a difference.\n",
|
119 |
+
"\n",
|
120 |
+
"Sincerely,\n",
|
121 |
+
"[Your Name] How are you? I hope you're doing well. I'm so excited to be writing to you today. I just wanted to say thank you for everything you do. I know it can't be easy, but you make it look effortless.\n",
|
122 |
+
"\n",
|
123 |
+
"I was thinking about your job the other day, and I realized just how much you do. You're not just a [job title], you're a problem solver, a mediator, a leader, and so much more. You make a difference in people's lives every day, and that's truly amazing.\n",
|
124 |
+
"\n",
|
125 |
+
"I know that sometimes it can be tough, and you might feel like you're just going through the motions. But I want you to know that you're not. You're making a real impact, and it's not going unnoticed.\n",
|
126 |
+
"\n",
|
127 |
+
"I hope you know that you're appreciated, and that you're valued. You're an important part of [organization/team/community], and we're all better because of you.\n",
|
128 |
+
"\n",
|
129 |
+
"Thank you again for all that you do. I'm so grateful to have you in my life, and I hope you know that you're making a difference.\n",
|
130 |
+
"\n",
|
131 |
+
"Sincerely,\n",
|
132 |
+
"[Your Name]\n",
|
133 |
+
"How are you? I hope you're doing well. I'm so excited to be writing to you today. I just wanted to say thank you for everything you do. I know it can't be easy, but you make it look effortless.\n",
|
134 |
+
"\n",
|
135 |
+
"I was thinking"
|
136 |
+
]
|
137 |
+
}
|
138 |
+
],
|
139 |
+
"source": [
|
140 |
+
"# To ensure os.environ['HF_TOKEN'] works use dotenv\n",
|
141 |
+
"import os\n",
|
142 |
+
"from dotenv import load_dotenv\n",
|
143 |
+
"load_dotenv()\n",
|
144 |
+
"\n",
|
145 |
+
"from chains import simple_chain\n",
|
146 |
+
"stream = simple_chain.stream(input={'question':'How are you?'})\n",
|
147 |
+
"for chunk in stream: # Each chunk corresponds to a token/word\n",
|
148 |
+
" #end=\"\": prints worlds one after each other, an not in a separate lines\n",
|
149 |
+
" #flush=True: prints world to the screen immidiately without any buffer\n",
|
150 |
+
" print(chunk, end=\"\", flush=True) "
|
151 |
+
]
|
152 |
+
},
|
153 |
+
{
|
154 |
+
"cell_type": "code",
|
155 |
+
"execution_count": 1,
|
156 |
+
"metadata": {},
|
157 |
+
"outputs": [
|
158 |
+
{
|
159 |
+
"name": "stderr",
|
160 |
+
"output_type": "stream",
|
161 |
+
"text": [
|
162 |
+
"/Users/sima/opt/anaconda3/envs/llm_app_rag/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",
|
163 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
164 |
+
]
|
165 |
+
}
|
166 |
+
],
|
167 |
+
"source": [
|
168 |
+
"from transformers import AutoTokenizer\n",
|
169 |
+
"from langchain_core.prompts import PromptTemplate\n",
|
170 |
+
"\n",
|
171 |
+
"# Get tokenizer; required to get eos_token\n",
|
172 |
+
"model_id = \"meta-llama/Meta-Llama-3-8B-Instruct\"\n",
|
173 |
+
"tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
|
174 |
+
"\n",
|
175 |
+
"def format_prompt(prompt) -> PromptTemplate:\n",
|
176 |
+
" # Format the input prompt by using the model specific instruction template\n",
|
177 |
+
" chat = [\n",
|
178 |
+
" {\"role\": \"system\", \"content\": \"You are a helpful AI assistant.\"},\n",
|
179 |
+
" {\"role\": \"user\", \"content\": prompt},\n",
|
180 |
+
" ]\n",
|
181 |
+
" formatted_prompt = tokenizer.apply_chat_template(\n",
|
182 |
+
" chat, \n",
|
183 |
+
" tokenize=False, \n",
|
184 |
+
" add_generation_prompt=True\n",
|
185 |
+
" )\n",
|
186 |
+
" # Return a langchain PromptTemplate object\n",
|
187 |
+
" print(formatted_prompt)\n",
|
188 |
+
" #langchain_prompt = PromptTemplate.from_template(formatted_prompt)\n",
|
189 |
+
"\n",
|
190 |
+
" return PromptTemplate.from_template(formatted_prompt)"
|
191 |
+
]
|
192 |
+
},
|
193 |
+
{
|
194 |
+
"cell_type": "code",
|
195 |
+
"execution_count": 2,
|
196 |
+
"metadata": {},
|
197 |
+
"outputs": [
|
198 |
+
{
|
199 |
+
"name": "stdout",
|
200 |
+
"output_type": "stream",
|
201 |
+
"text": [
|
202 |
+
"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n",
|
203 |
+
"\n",
|
204 |
+
"You are a helpful AI assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>\n",
|
205 |
+
"\n",
|
206 |
+
"{question}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n",
|
207 |
+
"\n",
|
208 |
+
"\n"
|
209 |
+
]
|
210 |
+
}
|
211 |
+
],
|
212 |
+
"source": [
|
213 |
+
"raw_prompt = \"{question}\"\n",
|
214 |
+
"raw_prompt_formatted = format_prompt(raw_prompt)"
|
215 |
+
]
|
216 |
+
},
|
217 |
+
{
|
218 |
+
"cell_type": "code",
|
219 |
+
"execution_count": 3,
|
220 |
+
"metadata": {},
|
221 |
+
"outputs": [
|
222 |
+
{
|
223 |
+
"name": "stdout",
|
224 |
+
"output_type": "stream",
|
225 |
+
"text": [
|
226 |
+
"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",
|
227 |
+
"Token is valid (permission: write).\n",
|
228 |
+
"Your token has been saved to /Users/sima/.cache/huggingface/token\n",
|
229 |
+
"Login successful\n"
|
230 |
+
]
|
231 |
+
},
|
232 |
+
{
|
233 |
+
"name": "stderr",
|
234 |
+
"output_type": "stream",
|
235 |
+
"text": [
|
236 |
+
"/Users/sima/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/huggingface_hub/inference/_client.py:2027: FutureWarning: `stop_sequences` is a deprecated argument for `text_generation` task and will be removed in version '0.28.0'. Use `stop` instead.\n",
|
237 |
+
" warnings.warn(\n"
|
238 |
+
]
|
239 |
+
},
|
240 |
+
{
|
241 |
+
"name": "stdout",
|
242 |
+
"output_type": "stream",
|
243 |
+
"text": [
|
244 |
+
"I'm doing well, thank you for asking! I'm a helpful AI assistant, and I'm here to assist you with any questions or tasks you may have. I'm functioning within normal parameters and ready to help with any inquiry or problem you'd like to discuss. How about you? How can I assist you today?"
|
245 |
+
]
|
246 |
+
}
|
247 |
+
],
|
248 |
+
"source": [
|
249 |
+
"# To ensure os.environ['HF_TOKEN'] works use dotenv\n",
|
250 |
+
"import os\n",
|
251 |
+
"from dotenv import load_dotenv\n",
|
252 |
+
"load_dotenv()\n",
|
253 |
+
"\n",
|
254 |
+
"from chains import formatted_chain\n",
|
255 |
+
"stream = formatted_chain.stream(input={'question':'How are you?'})\n",
|
256 |
+
"for chunk in stream: # Each chunk corresponds to a token/word\n",
|
257 |
+
" #end=\"\": prints worlds one after each other, an not in a separate lines\n",
|
258 |
+
" #flush=True: prints world to the screen immidiately without any buffer\n",
|
259 |
+
" print(chunk, end=\"\", flush=True) "
|
260 |
+
]
|
261 |
+
},
|
262 |
+
{
|
263 |
+
"cell_type": "markdown",
|
264 |
+
"metadata": {},
|
265 |
+
"source": []
|
266 |
+
},
|
267 |
+
{
|
268 |
+
"cell_type": "code",
|
269 |
+
"execution_count": null,
|
270 |
+
"metadata": {},
|
271 |
+
"outputs": [],
|
272 |
+
"source": []
|
273 |
+
}
|
274 |
+
],
|
275 |
+
"metadata": {
|
276 |
+
"kernelspec": {
|
277 |
+
"display_name": "llm_app_rag",
|
278 |
+
"language": "python",
|
279 |
+
"name": "python3"
|
280 |
+
},
|
281 |
+
"language_info": {
|
282 |
+
"codemirror_mode": {
|
283 |
+
"name": "ipython",
|
284 |
+
"version": 3
|
285 |
+
},
|
286 |
+
"file_extension": ".py",
|
287 |
+
"mimetype": "text/x-python",
|
288 |
+
"name": "python",
|
289 |
+
"nbconvert_exporter": "python",
|
290 |
+
"pygments_lexer": "ipython3",
|
291 |
+
"version": "3.12.6"
|
292 |
+
}
|
293 |
+
},
|
294 |
+
"nbformat": 4,
|
295 |
+
"nbformat_minor": 2
|
296 |
+
}
|
app_history_prompt/requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
langchain
|
4 |
+
langchain-community
|
5 |
+
langchain-core
|
6 |
+
langchain_huggingface
|
7 |
+
langchain_openai
|
8 |
+
langserve[all]
|
9 |
+
pinecone-client[grpc]
|
10 |
+
langchain-pinecone
|
11 |
+
pypdf
|
12 |
+
pydantic
|
13 |
+
transformers[torch]
|
app_history_prompt/test.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langserve import RemoteRunnable
|
2 |
+
# Hit our enpoint with specified rout
|
3 |
+
# If we put /simple/stream, it complains; because chain.stream will hit /simple/stream endpoint
|
4 |
+
url = "https://simafarazi-backend-c.hf.space/formatted"
|
5 |
+
chain = RemoteRunnable(url) #Client for iteracting with LangChain runnables that are hosted as LangServe endpoints
|
6 |
+
stream = chain.stream(input={'question':'How are you?'}) # .stream() and .invoke() are standard methods to interact with hosted runnables
|
7 |
+
|
8 |
+
|
9 |
+
for chunk in stream: # Each chunk corresponds to a token/word
|
10 |
+
#end="": prints worlds one after each other, an not in a separate lines
|
11 |
+
#flush=True: prints world to the screen immidiately without any buffer
|
12 |
+
print(chunk, end="", flush=True)
|