ahncs commited on
Commit
1057395
·
1 Parent(s): 76f7763

upload project files

Browse files
Files changed (2) hide show
  1. app.py +3 -3
  2. testenvironment.ipynb +359 -0
app.py CHANGED
@@ -26,7 +26,7 @@ OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
26
 
27
  #Load PDF and split into chunks
28
  loader = PyMuPDFLoader (
29
- "https://github.com/ahncs/AIE3/blob/b08c6235d7aaaba1779283d2df8af52a4c83ec85/Final%20Project/WelchAllynVSM6000servicemanual.pdf"
30
  )
31
 
32
  documents = loader.load()
@@ -48,7 +48,7 @@ qdrant_vector_store = Qdrant.from_documents(
48
  documents,
49
  embeddings,
50
  location=":memory:",
51
- collection_name="AirBNB10k",
52
  )
53
 
54
  #Create Retriever
@@ -75,7 +75,7 @@ def rename(original_author: str):
75
  """
76
  This function can be used to rename the 'author' of a message.
77
 
78
- In this case, we're overriding the 'Assistant' author to be 'Airbnb10kBot'.
79
  """
80
  rename_dict = {
81
  "Assistant" : "HTMLLMBot"
 
26
 
27
  #Load PDF and split into chunks
28
  loader = PyMuPDFLoader (
29
+ "https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf"
30
  )
31
 
32
  documents = loader.load()
 
48
  documents,
49
  embeddings,
50
  location=":memory:",
51
+ collection_name="WelchAllynVSMServiceManual",
52
  )
53
 
54
  #Create Retriever
 
75
  """
76
  This function can be used to rename the 'author' of a message.
77
 
78
+ In this case, we're overriding the 'Assistant' author to be 'HTMLLMBot'.
79
  """
80
  rename_dict = {
81
  "Assistant" : "HTMLLMBot"
testenvironment.ipynb ADDED
@@ -0,0 +1,359 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "!pip install -U -q langchain langchain-openai langchain_core langchain-community langchainhub openai"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 2,
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "!pip install -qU qdrant-client pymupdf pandas"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": 21,
24
+ "metadata": {},
25
+ "outputs": [
26
+ {
27
+ "name": "stdout",
28
+ "output_type": "stream",
29
+ "text": [
30
+ "2024-07-18 14:47:19 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
31
+ "2024-07-18 14:47:20 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
32
+ "2024-07-18 14:47:21 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
33
+ "2024-07-18 14:47:21 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
34
+ "2024-07-18 14:47:22 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
35
+ "2024-07-18 14:47:23 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
36
+ "2024-07-18 14:47:24 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
37
+ "2024-07-18 14:47:25 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
38
+ "2024-07-18 14:47:25 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
39
+ "2024-07-18 14:47:26 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
40
+ "2024-07-18 14:47:29 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
41
+ ]
42
+ }
43
+ ],
44
+ "source": [
45
+ "import os\n",
46
+ "import openai\n",
47
+ "import getpass\n",
48
+ "import chainlit as cl\n",
49
+ "from langchain_community.document_loaders import PyMuPDFLoader\n",
50
+ "from langchain_openai import OpenAIEmbeddings\n",
51
+ "from langchain_openai import ChatOpenAI\n",
52
+ "from langchain_community.vectorstores import Qdrant\n",
53
+ "from langchain.prompts import ChatPromptTemplate\n",
54
+ "\n",
55
+ "from dotenv import load_dotenv\n",
56
+ "from operator import itemgetter\n",
57
+ "from langchain_huggingface import HuggingFaceEndpoint\n",
58
+ "from langchain_community.document_loaders import TextLoader\n",
59
+ "from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
60
+ "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
61
+ "from langchain_community.vectorstores import FAISS\n",
62
+ "from langchain_huggingface import HuggingFaceEndpointEmbeddings\n",
63
+ "from langchain_core.prompts import PromptTemplate\n",
64
+ "from langchain.schema.output_parser import StrOutputParser\n",
65
+ "from langchain.schema.runnable import RunnablePassthrough\n",
66
+ "from langchain.schema.runnable.config import RunnableConfig\n",
67
+ "\n",
68
+ "#Load environment variables\n",
69
+ "load_dotenv()\n",
70
+ "OPENAI_API_KEY = os.environ[\"OPENAI_API_KEY\"]\n",
71
+ "\n",
72
+ "\n",
73
+ "#Load 10-K PDF and split into chunks\n",
74
+ "loader = PyMuPDFLoader (\n",
75
+ " \"https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf\"\n",
76
+ ")\n",
77
+ "\n",
78
+ "documents = loader.load()\n",
79
+ "\n",
80
+ "\n",
81
+ "text_splitter = RecursiveCharacterTextSplitter(\n",
82
+ " chunk_size = 1000,\n",
83
+ " chunk_overlap = 100\n",
84
+ ")\n",
85
+ "\n",
86
+ "documents = text_splitter.split_documents(documents)\n",
87
+ "\n",
88
+ "#Load embeddings model - we'll use OpenAI's text-embedding-3-small\n",
89
+ "embeddings = OpenAIEmbeddings(\n",
90
+ " model=\"text-embedding-3-small\"\n",
91
+ ")\n",
92
+ "\n",
93
+ "#Create QDrant vector store\n",
94
+ "qdrant_vector_store = Qdrant.from_documents(\n",
95
+ " documents,\n",
96
+ " embeddings,\n",
97
+ " location=\":memory:\",\n",
98
+ " collection_name=\"WelchAllynConnex6000VSMServiceManual\",\n",
99
+ ")\n",
100
+ "\n",
101
+ "#Create Retriever\n",
102
+ "retriever = qdrant_vector_store.as_retriever()\n",
103
+ "\n",
104
+ "#Create Prompt Template\n",
105
+ "template = \"\"\"Answer the question based only on the following context. If you cannot answer the question with the context, please respond with 'I don't know':\n",
106
+ "\n",
107
+ "Context:\n",
108
+ "{context}\n",
109
+ "\n",
110
+ "Question:\n",
111
+ "{question}\n",
112
+ "\"\"\"\n",
113
+ "\n",
114
+ "prompt = ChatPromptTemplate.from_template(template)\n",
115
+ "\n",
116
+ "#Choose LLM - we'll use gpt-4o.\n",
117
+ "primary_llm = ChatOpenAI(model_name=\"gpt-4o\", temperature=0)"
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": 24,
123
+ "metadata": {},
124
+ "outputs": [
125
+ {
126
+ "name": "stdout",
127
+ "output_type": "stream",
128
+ "text": [
129
+ "2024-07-18 14:50:04 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
130
+ ]
131
+ }
132
+ ],
133
+ "source": [
134
+ "retrieved_documents = retriever.invoke(\"How do I specify advanced alarm settings?\")"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "code",
139
+ "execution_count": 25,
140
+ "metadata": {},
141
+ "outputs": [
142
+ {
143
+ "name": "stdout",
144
+ "output_type": "stream",
145
+ "text": [
146
+ "page_content='tab in the Device Status area.\n",
147
+ "Enable NTP\n",
148
+ "Select to use the NTP server for time\n",
149
+ "synchronization rather than the episodic host.\n",
150
+ "Host name or IP address\n",
151
+ "Enter the host name, IP address, or domain name of\n",
152
+ "the NTP server.\n",
153
+ "Test\n",
154
+ "Touch Test to test the connection to the NTP\n",
155
+ "server.\n",
156
+ "Information messages indicate that a test is in\n",
157
+ "progress and then the result of the test (pass or fail).\n",
158
+ "4.\n",
159
+ "Do one of the following:\n",
160
+ "•\n",
161
+ "To continue in the Advanced settings, touch another tab.\n",
162
+ "•\n",
163
+ "To exit the Advanced settings and return to the Home tab, touch Exit.\n",
164
+ "Specify advanced alarm settings\n",
165
+ "1.\n",
166
+ "Access the Advanced settings.\n",
167
+ "a.\n",
168
+ "Touch the Settings tab.\n",
169
+ "b.\n",
170
+ "Touch the Advanced tab.\n",
171
+ "c.\n",
172
+ "Touch Enter password.\n",
173
+ "d.\n",
174
+ "Enter your password and touch OK.\n",
175
+ "The General tab appears.\n",
176
+ "2.\n",
177
+ "Touch the Alarms tab.\n",
178
+ "3.\n",
179
+ "Specify settings.\n",
180
+ "24 Advanced settings' metadata={'source': 'https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf', 'file_path': 'https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf', 'page': 27, 'total_pages': 349, 'format': 'PDF 1.6', 'title': 'Service manual - Welch Allyn Connex Vital Signs Monitor 6000 Series™', 'author': 'Welch Allyn Inc.', 'subject': '', 'keywords': '', 'creator': 'AH Formatter V5.1 MR2 (5,1,2010,0409) for Windows', 'producer': 'Antenna House PDF Output Library 2.6.0 (Windows)', 'creationDate': \"D:20230214101552-06'00'\", 'modDate': \"D:20230214143156-05'00'\", 'trapped': '', 'encryption': 'Standard V4 R4 128-bit RC4', '_id': 'd2315a8070da40b6a7732e0886169c9a', '_collection_name': 'WelchAllynConnex6000VSMServiceManual'}\n",
181
+ "page_content='Specify the time that an adult no breath detected\n",
182
+ "condition must be active in an Oridion CO2\n",
183
+ "configuration before audio and visual signals occur.\n",
184
+ "Pediatric no breath detected alarm delay\n",
185
+ "Specify the time that a pediatric no breath detected\n",
186
+ "condition must be active in an Oridion CO2\n",
187
+ "configuration before audio and visual signals occur.\n",
188
+ "Neonate no breath detected alarm delay\n",
189
+ "Specify the time that a neonate no breath detected\n",
190
+ "condition must be active in an Oridion CO2\n",
191
+ "configuration before audio and visual signals occur.\n",
192
+ "ECG HR alarm delay\n",
193
+ "Specifies the time that an ECG heart rate alarm\n",
194
+ "condition must be active before audio and visual\n",
195
+ "signals occur.\n",
196
+ "4.\n",
197
+ "Do one of the following:\n",
198
+ "•\n",
199
+ "To continue in the Advanced settings, touch another tab.\n",
200
+ "•\n",
201
+ "To exit the Advanced settings and return to the Home tab, touch Exit.\n",
202
+ "Specify advanced display settings\n",
203
+ "1.\n",
204
+ "Access the Advanced settings.\n",
205
+ "a.\n",
206
+ "Touch the Settings tab.\n",
207
+ "26 Advanced settings' metadata={'source': 'https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf', 'file_path': 'https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf', 'page': 29, 'total_pages': 349, 'format': 'PDF 1.6', 'title': 'Service manual - Welch Allyn Connex Vital Signs Monitor 6000 Series™', 'author': 'Welch Allyn Inc.', 'subject': '', 'keywords': '', 'creator': 'AH Formatter V5.1 MR2 (5,1,2010,0409) for Windows', 'producer': 'Antenna House PDF Output Library 2.6.0 (Windows)', 'creationDate': \"D:20230214101552-06'00'\", 'modDate': \"D:20230214143156-05'00'\", 'trapped': '', 'encryption': 'Standard V4 R4 128-bit RC4', '_id': '7a61d18dcf9940d48dd0becdd2473c85', '_collection_name': 'WelchAllynConnex6000VSMServiceManual'}\n",
208
+ "page_content='Admin mode), enabling nurse administrators, biomedical engineers, and/or service engineers to\n",
209
+ "configure specific features. The Advanced tab also presents read-only information about the\n",
210
+ "monitor.\n",
211
+ "The password to enter Advanced settings is configurable and can also be set to expire. See the\n",
212
+ "\"Password\" topic at the end of this section for more details.\n",
213
+ "NOTE You cannot enter Advanced settings if sensors or physiological alarms are\n",
214
+ "active or if vital sign measurements are displayed.\n",
215
+ "Access the Service tabs\n",
216
+ "NOTE You cannot access the Service tabs if sensors or physiological alarms are\n",
217
+ "active or if vital sign measurements are displayed.\n",
218
+ "1.\n",
219
+ "From the Home tab, touch the Settings tab.\n",
220
+ "2.\n",
221
+ "Touch the Advanced tab.\n",
222
+ "3.\n",
223
+ "Touch Enter password.\n",
224
+ "48 Advanced settings' metadata={'source': 'https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf', 'file_path': 'https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf', 'page': 51, 'total_pages': 349, 'format': 'PDF 1.6', 'title': 'Service manual - Welch Allyn Connex Vital Signs Monitor 6000 Series™', 'author': 'Welch Allyn Inc.', 'subject': '', 'keywords': '', 'creator': 'AH Formatter V5.1 MR2 (5,1,2010,0409) for Windows', 'producer': 'Antenna House PDF Output Library 2.6.0 (Windows)', 'creationDate': \"D:20230214101552-06'00'\", 'modDate': \"D:20230214143156-05'00'\", 'trapped': '', 'encryption': 'Standard V4 R4 128-bit RC4', '_id': '8e4aa373a6e547deac9c12c2a02c6b21', '_collection_name': 'WelchAllynConnex6000VSMServiceManual'}\n",
225
+ "page_content='•\n",
226
+ "In Advanced settings, the left pane on the Network > Radio tab includes only Enable radio\n",
227
+ "and Enable radio network alarms options (no Enable dynamic frequency option).\n",
228
+ "1.\n",
229
+ "Access the Advanced settings.\n",
230
+ "a.\n",
231
+ "Touch the Settings tab.\n",
232
+ "b.\n",
233
+ "Touch the Advanced tab.\n",
234
+ "c.\n",
235
+ "Touch Enter password.\n",
236
+ "d.\n",
237
+ "Enter your password and touch OK.\n",
238
+ "The General tab appears.\n",
239
+ "2.\n",
240
+ "Touch the Network tab.\n",
241
+ "3.\n",
242
+ "Touch the Radio tab.\n",
243
+ "4.\n",
244
+ "Specify settings.\n",
245
+ "Setting\n",
246
+ "Action/Description\n",
247
+ "Enable radio\n",
248
+ "Enable the radio for device communications. When\n",
249
+ "disabled, the radio is not available.\n",
250
+ "Enable radio network alarms\n",
251
+ "Activate radio network alarms when an alarm\n",
252
+ "condition occurs. When disabled, radio network\n",
253
+ "alarms are not available.\n",
254
+ "SSID\n",
255
+ "Touch \n",
256
+ " and enter the service set identifier\n",
257
+ "(SSID). SSIDs that are longer than 16 characters may\n",
258
+ "be truncated in the user view. Enter a maximum of\n",
259
+ "32 characters.\n",
260
+ "Radio band\n",
261
+ "Select the radio band.\n",
262
+ "Authentication type\n",
263
+ "Select an authentication scheme. Then specify any\n",
264
+ "additional settings that appear.' metadata={'source': 'https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf', 'file_path': 'https://www.hillrom.com/content/dam/hillrom-aem/us/en/sap-documents/LIT/80026/80026025LITPDF.pdf', 'page': 47, 'total_pages': 349, 'format': 'PDF 1.6', 'title': 'Service manual - Welch Allyn Connex Vital Signs Monitor 6000 Series™', 'author': 'Welch Allyn Inc.', 'subject': '', 'keywords': '', 'creator': 'AH Formatter V5.1 MR2 (5,1,2010,0409) for Windows', 'producer': 'Antenna House PDF Output Library 2.6.0 (Windows)', 'creationDate': \"D:20230214101552-06'00'\", 'modDate': \"D:20230214143156-05'00'\", 'trapped': '', 'encryption': 'Standard V4 R4 128-bit RC4', '_id': '21f61f7963c84a2a8af1f35a1e02452e', '_collection_name': 'WelchAllynConnex6000VSMServiceManual'}\n"
265
+ ]
266
+ }
267
+ ],
268
+ "source": [
269
+ "for doc in retrieved_documents:\n",
270
+ " print(doc)"
271
+ ]
272
+ },
273
+ {
274
+ "cell_type": "code",
275
+ "execution_count": 26,
276
+ "metadata": {},
277
+ "outputs": [],
278
+ "source": [
279
+ "primary_llm = ChatOpenAI(model_name=\"gpt-4o\", temperature=0)\n",
280
+ "\n",
281
+ "retrieval_augmented_chain = (\n",
282
+ " # INVOKE CHAIN WITH: {\"question\" : \"<<SOME USER QUESTION>>\"}\n",
283
+ " # \"question\" : populated by getting the value of the \"question\" key\n",
284
+ " # \"context\" : populated by getting the value of the \"question\" key and chaining it into the base_retriever\n",
285
+ " {\"context\": itemgetter(\"question\") | retriever, \"question\": itemgetter(\"question\")}\n",
286
+ " | prompt | primary_llm\n",
287
+ " )"
288
+ ]
289
+ },
290
+ {
291
+ "cell_type": "code",
292
+ "execution_count": 28,
293
+ "metadata": {},
294
+ "outputs": [
295
+ {
296
+ "name": "stdout",
297
+ "output_type": "stream",
298
+ "text": [
299
+ "2024-07-18 14:52:25 - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n",
300
+ "2024-07-18 14:52:27 - HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
301
+ ]
302
+ }
303
+ ],
304
+ "source": [
305
+ "question = \"How do I specify advanced alarm settings?\"\n",
306
+ "\n",
307
+ "result = retrieval_augmented_chain.invoke({\"question\" : question})\n"
308
+ ]
309
+ },
310
+ {
311
+ "cell_type": "code",
312
+ "execution_count": null,
313
+ "metadata": {},
314
+ "outputs": [],
315
+ "source": [
316
+ "\n",
317
+ "print(result[\"response\"].content)"
318
+ ]
319
+ },
320
+ {
321
+ "cell_type": "code",
322
+ "execution_count": 29,
323
+ "metadata": {},
324
+ "outputs": [
325
+ {
326
+ "name": "stdout",
327
+ "output_type": "stream",
328
+ "text": [
329
+ "content='To specify advanced alarm settings, follow these steps:\\n\\n1. Access the Advanced settings:\\n a. Touch the Settings tab.\\n b. Touch the Advanced tab.\\n c. Touch Enter password.\\n d. Enter your password and touch OK. The General tab appears.\\n2. Touch the Alarms tab.\\n3. Specify settings.' response_metadata={'token_usage': {'completion_tokens': 68, 'prompt_tokens': 2027, 'total_tokens': 2095}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_c4e5b6fa31', 'finish_reason': 'stop', 'logprobs': None} id='run-e022a522-caec-4748-b093-24ca2b030a81-0' usage_metadata={'input_tokens': 2027, 'output_tokens': 68, 'total_tokens': 2095}\n"
330
+ ]
331
+ }
332
+ ],
333
+ "source": [
334
+ "print(result)"
335
+ ]
336
+ }
337
+ ],
338
+ "metadata": {
339
+ "kernelspec": {
340
+ "display_name": "llmops-course",
341
+ "language": "python",
342
+ "name": "python3"
343
+ },
344
+ "language_info": {
345
+ "codemirror_mode": {
346
+ "name": "ipython",
347
+ "version": 3
348
+ },
349
+ "file_extension": ".py",
350
+ "mimetype": "text/x-python",
351
+ "name": "python",
352
+ "nbconvert_exporter": "python",
353
+ "pygments_lexer": "ipython3",
354
+ "version": "3.11.9"
355
+ }
356
+ },
357
+ "nbformat": 4,
358
+ "nbformat_minor": 2
359
+ }