SimaFarazi commited on
Commit
d95d830
1 Parent(s): e4e0003

add history prompt app dir

Browse files
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 ./app_format_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_format_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"]
 
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": 22,
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": 27,
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": 26,
216
  "metadata": {},
217
  "outputs": [
218
  {
219
- "ename": "KeyError",
220
- "evalue": "\"Input to PromptTemplate is missing variables {''}. Expected: ['', 'question'] Received: ['question']\\nNote: if you intended {} to be part of the string and not a variable, please escape it with double curly braces like: '{{}}'.\"",
221
- "output_type": "error",
222
- "traceback": [
223
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
224
- "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
225
- "Cell \u001b[0;32mIn[26], line 8\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mchains\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m formatted_chain\n\u001b[1;32m 7\u001b[0m stream \u001b[38;5;241m=\u001b[39m formatted_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})\n\u001b[0;32m----> 8\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 9\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 10\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 11\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",
226
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/base.py:3405\u001b[0m, in \u001b[0;36mRunnableSequence.stream\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 3399\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mstream\u001b[39m(\n\u001b[1;32m 3400\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 3401\u001b[0m \u001b[38;5;28minput\u001b[39m: Input,\n\u001b[1;32m 3402\u001b[0m config: Optional[RunnableConfig] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 3403\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Optional[Any],\n\u001b[1;32m 3404\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Iterator[Output]:\n\u001b[0;32m-> 3405\u001b[0m \u001b[38;5;28;01myield from\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtransform(\u001b[38;5;28miter\u001b[39m([\u001b[38;5;28minput\u001b[39m]), config, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
227
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/base.py:3392\u001b[0m, in \u001b[0;36mRunnableSequence.transform\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 3386\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtransform\u001b[39m(\n\u001b[1;32m 3387\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 3388\u001b[0m \u001b[38;5;28minput\u001b[39m: Iterator[Input],\n\u001b[1;32m 3389\u001b[0m config: Optional[RunnableConfig] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 3390\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Optional[Any],\n\u001b[1;32m 3391\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Iterator[Output]:\n\u001b[0;32m-> 3392\u001b[0m \u001b[38;5;28;01myield from\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_transform_stream_with_config(\n\u001b[1;32m 3393\u001b[0m \u001b[38;5;28minput\u001b[39m,\n\u001b[1;32m 3394\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_transform,\n\u001b[1;32m 3395\u001b[0m patch_config(config, run_name\u001b[38;5;241m=\u001b[39m(config \u001b[38;5;129;01mor\u001b[39;00m {})\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrun_name\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname),\n\u001b[1;32m 3396\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[1;32m 3397\u001b[0m )\n",
228
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/base.py:2193\u001b[0m, in \u001b[0;36mRunnable._transform_stream_with_config\u001b[0;34m(self, input, transformer, config, run_type, **kwargs)\u001b[0m\n\u001b[1;32m 2191\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 2192\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m-> 2193\u001b[0m chunk: Output \u001b[38;5;241m=\u001b[39m \u001b[43mcontext\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[1;32m 2194\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m chunk\n\u001b[1;32m 2195\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m final_output_supported:\n",
229
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/base.py:3355\u001b[0m, in \u001b[0;36mRunnableSequence._transform\u001b[0;34m(self, input, run_manager, config, **kwargs)\u001b[0m\n\u001b[1;32m 3352\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 3353\u001b[0m final_pipeline \u001b[38;5;241m=\u001b[39m step\u001b[38;5;241m.\u001b[39mtransform(final_pipeline, config)\n\u001b[0;32m-> 3355\u001b[0m \u001b[38;5;28;01myield from\u001b[39;00m final_pipeline\n",
230
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/base.py:1409\u001b[0m, in \u001b[0;36mRunnable.transform\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 1406\u001b[0m final: Input\n\u001b[1;32m 1407\u001b[0m got_first_val \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m-> 1409\u001b[0m \u001b[43m\u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43michunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m:\u001b[49m\n\u001b[1;32m 1410\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# The default implementation of transform is to buffer input and\u001b[39;49;00m\n\u001b[1;32m 1411\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# then call stream.\u001b[39;49;00m\n\u001b[1;32m 1412\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# It'll attempt to gather all input into a single chunk using\u001b[39;49;00m\n\u001b[1;32m 1413\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# the `+` operator.\u001b[39;49;00m\n\u001b[1;32m 1414\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# If the input is not addable, then we'll assume that we can\u001b[39;49;00m\n\u001b[1;32m 1415\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# only operate on the last chunk,\u001b[39;49;00m\n\u001b[1;32m 1416\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# and we'll iterate until we get to the last chunk.\u001b[39;49;00m\n\u001b[1;32m 1417\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mgot_first_val\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 1418\u001b[0m \u001b[43m \u001b[49m\u001b[43mfinal\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43michunk\u001b[49m\n",
231
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/base.py:1427\u001b[0m, in \u001b[0;36mRunnable.transform\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 1424\u001b[0m final \u001b[38;5;241m=\u001b[39m ichunk\n\u001b[1;32m 1426\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m got_first_val:\n\u001b[0;32m-> 1427\u001b[0m \u001b[38;5;28;01myield from\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstream(final, config, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
232
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/base.py:993\u001b[0m, in \u001b[0;36mRunnable.stream\u001b[0;34m(self, input, config, **kwargs)\u001b[0m\n\u001b[1;32m 975\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mstream\u001b[39m(\n\u001b[1;32m 976\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 977\u001b[0m \u001b[38;5;28minput\u001b[39m: Input,\n\u001b[1;32m 978\u001b[0m config: Optional[RunnableConfig] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 979\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Optional[Any],\n\u001b[1;32m 980\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Iterator[Output]:\n\u001b[1;32m 981\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 982\u001b[0m \u001b[38;5;124;03m Default implementation of stream, which calls invoke.\u001b[39;00m\n\u001b[1;32m 983\u001b[0m \u001b[38;5;124;03m Subclasses should override this method if they support streaming output.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 991\u001b[0m \u001b[38;5;124;03m The output of the Runnable.\u001b[39;00m\n\u001b[1;32m 992\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 993\u001b[0m \u001b[38;5;28;01myield\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\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\n",
233
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/prompts/base.py:192\u001b[0m, in \u001b[0;36mBasePromptTemplate.invoke\u001b[0;34m(self, input, config)\u001b[0m\n\u001b[1;32m 190\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtags:\n\u001b[1;32m 191\u001b[0m config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtags\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtags\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtags\n\u001b[0;32m--> 192\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call_with_config\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 193\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_format_prompt_with_error_handling\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 194\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 195\u001b[0m \u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 196\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mprompt\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 197\u001b[0m \u001b[43m \u001b[49m\u001b[43mserialized\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_serialized\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 198\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
234
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/base.py:1923\u001b[0m, in \u001b[0;36mRunnable._call_with_config\u001b[0;34m(self, func, input, config, run_type, serialized, **kwargs)\u001b[0m\n\u001b[1;32m 1919\u001b[0m context \u001b[38;5;241m=\u001b[39m copy_context()\n\u001b[1;32m 1920\u001b[0m context\u001b[38;5;241m.\u001b[39mrun(_set_config_context, child_config)\n\u001b[1;32m 1921\u001b[0m output \u001b[38;5;241m=\u001b[39m cast(\n\u001b[1;32m 1922\u001b[0m Output,\n\u001b[0;32m-> 1923\u001b[0m \u001b[43mcontext\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1924\u001b[0m \u001b[43m \u001b[49m\u001b[43mcall_func_with_variable_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# type: ignore[arg-type]\u001b[39;49;00m\n\u001b[1;32m 1925\u001b[0m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# type: ignore[arg-type]\u001b[39;49;00m\n\u001b[1;32m 1926\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# type: ignore[arg-type]\u001b[39;49;00m\n\u001b[1;32m 1927\u001b[0m \u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1928\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1929\u001b[0m \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\n\u001b[1;32m 1930\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m,\n\u001b[1;32m 1931\u001b[0m )\n\u001b[1;32m 1932\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 1933\u001b[0m run_manager\u001b[38;5;241m.\u001b[39mon_chain_error(e)\n",
235
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/runnables/config.py:396\u001b[0m, in \u001b[0;36mcall_func_with_variable_args\u001b[0;34m(func, input, config, run_manager, **kwargs)\u001b[0m\n\u001b[1;32m 394\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m run_manager \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m accepts_run_manager(func):\n\u001b[1;32m 395\u001b[0m kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrun_manager\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m run_manager\n\u001b[0;32m--> 396\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\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\n",
236
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/prompts/base.py:166\u001b[0m, in \u001b[0;36mBasePromptTemplate._format_prompt_with_error_handling\u001b[0;34m(self, inner_input)\u001b[0m\n\u001b[1;32m 165\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_format_prompt_with_error_handling\u001b[39m(\u001b[38;5;28mself\u001b[39m, inner_input: \u001b[38;5;28mdict\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m PromptValue:\n\u001b[0;32m--> 166\u001b[0m _inner_input \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_validate_input\u001b[49m\u001b[43m(\u001b[49m\u001b[43minner_input\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 167\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mformat_prompt(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m_inner_input)\n",
237
- "File \u001b[0;32m~/opt/anaconda3/envs/llm_app_rag/lib/python3.12/site-packages/langchain_core/prompts/base.py:162\u001b[0m, in \u001b[0;36mBasePromptTemplate._validate_input\u001b[0;34m(self, inner_input)\u001b[0m\n\u001b[1;32m 156\u001b[0m example_key \u001b[38;5;241m=\u001b[39m missing\u001b[38;5;241m.\u001b[39mpop()\n\u001b[1;32m 157\u001b[0m msg \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 158\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mNote: if you intended \u001b[39m\u001b[38;5;130;01m{{\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00mexample_key\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m}}\u001b[39;00m\u001b[38;5;124m to be part of the string\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m and not a variable, please escape it with double curly braces like: \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 160\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m{{\u001b[39;00m\u001b[38;5;130;01m{{\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00mexample_key\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m}}\u001b[39;00m\u001b[38;5;130;01m}}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 161\u001b[0m )\n\u001b[0;32m--> 162\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(msg)\n\u001b[1;32m 163\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m inner_input\n",
238
- "\u001b[0;31mKeyError\u001b[0m: \"Input to PromptTemplate is missing variables {''}. Expected: ['', 'question'] Received: ['question']\\nNote: if you intended {} to be part of the string and not a variable, please escape it with double curly braces like: '{{}}'.\""
 
 
239
  ]
240
  }
241
  ],
@@ -246,7 +252,7 @@
246
  "load_dotenv()\n",
247
  "\n",
248
  "from chains import formatted_chain\n",
249
- "stream = chain.stream(input={'question':'How are you?'})\n",
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)