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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 4,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"Using deprecated Together LLM. Please use langchain_together instead.\n"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"data": {
|
17 |
+
"text/plain": [
|
18 |
+
"True"
|
19 |
+
]
|
20 |
+
},
|
21 |
+
"execution_count": 4,
|
22 |
+
"metadata": {},
|
23 |
+
"output_type": "execute_result"
|
24 |
+
}
|
25 |
+
],
|
26 |
+
"source": [
|
27 |
+
"import os \n",
|
28 |
+
"from dotenv import load_dotenv\n",
|
29 |
+
"\n",
|
30 |
+
"from langchain_community.document_loaders.csv_loader import CSVLoader\n",
|
31 |
+
"from langchain_community.embeddings import HuggingFaceEmbeddings\n",
|
32 |
+
"from langchain_community.vectorstores import Vectara\n",
|
33 |
+
"from langchain_core.output_parsers import StrOutputParser\n",
|
34 |
+
"from langchain_core.prompts import ChatPromptTemplate\n",
|
35 |
+
"from langchain_core.runnables import RunnablePassthrough\n",
|
36 |
+
"\n",
|
37 |
+
"try:\n",
|
38 |
+
" from langchain_together import Together\n",
|
39 |
+
"except:\n",
|
40 |
+
" print('Using deprecated Together LLM. Please use langchain_together instead.')\n",
|
41 |
+
" from langchain_community.llms import Together\n",
|
42 |
+
"\n",
|
43 |
+
"MODEL_NAME = \"mistralai/Mixtral-8x7B-Instruct-v0.1\"\n",
|
44 |
+
"\n",
|
45 |
+
"load_dotenv()"
|
46 |
+
]
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"cell_type": "code",
|
50 |
+
"execution_count": 1,
|
51 |
+
"metadata": {},
|
52 |
+
"outputs": [],
|
53 |
+
"source": [
|
54 |
+
"from langchain.prompts import PromptTemplate\n"
|
55 |
+
]
|
56 |
+
},
|
57 |
+
{
|
58 |
+
"cell_type": "code",
|
59 |
+
"execution_count": 5,
|
60 |
+
"metadata": {},
|
61 |
+
"outputs": [],
|
62 |
+
"source": [
|
63 |
+
"vectara_customer_id = os.environ['VECTARA_CUSTOMER_ID']\n",
|
64 |
+
"vectara_corpus_id = os.environ['VECTARA_CORPUS_ID']\n",
|
65 |
+
"vectara_api_key = os.environ['VECTARA_API_KEY']\n",
|
66 |
+
"\n",
|
67 |
+
"embeddings = HuggingFaceEmbeddings(model_name=\"intfloat/multilingual-e5-large\")\n",
|
68 |
+
"\n",
|
69 |
+
"vectara = Vectara(vectara_customer_id=vectara_customer_id,\n",
|
70 |
+
" vectara_corpus_id=vectara_corpus_id,\n",
|
71 |
+
" vectara_api_key=vectara_api_key)\n"
|
72 |
+
]
|
73 |
+
},
|
74 |
+
{
|
75 |
+
"cell_type": "code",
|
76 |
+
"execution_count": 2,
|
77 |
+
"metadata": {},
|
78 |
+
"outputs": [
|
79 |
+
{
|
80 |
+
"ename": "NameError",
|
81 |
+
"evalue": "name 'ChatPromptTemplate' is not defined",
|
82 |
+
"output_type": "error",
|
83 |
+
"traceback": [
|
84 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
85 |
+
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
86 |
+
"Cell \u001b[0;32mIn[2], line 16\u001b[0m\n\u001b[1;32m 1\u001b[0m template \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;124mpassage: You are a helpful assistant that understands BIM building documents and engineering disciplines, specifically related to \u001b[39m\u001b[38;5;132;01m{discipline}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;124mpassage: You will analyze two sets of BIM document metadata comprising of filename, description, and discipline. One set of metadata was automatically generated by AI in English, and the other was annotated by a human in German with a letter prefix.\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;124mquery: \u001b[39m\u001b[38;5;132;01m{question}\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[0;32m---> 16\u001b[0m prompt \u001b[38;5;241m=\u001b[39m \u001b[43mChatPromptTemplate\u001b[49m\u001b[38;5;241m.\u001b[39mfrom_template(template)\n",
|
87 |
+
"\u001b[0;31mNameError\u001b[0m: name 'ChatPromptTemplate' is not defined"
|
88 |
+
]
|
89 |
+
}
|
90 |
+
],
|
91 |
+
"source": [
|
92 |
+
"template = \"\"\"\n",
|
93 |
+
"passage: You are a helpful assistant that understands BIM building documents and engineering disciplines, specifically related to {discipline}.\n",
|
94 |
+
"passage: You will analyze two sets of BIM document metadata comprising of filename, description, and discipline. One set of metadata was automatically generated by AI in English, and the other was annotated by a human in German with a letter prefix.\n",
|
95 |
+
"passage: It is expected that several sets of annotated metadata are incorrect. You will translate the annotated metadata into English and determine if the information is contextually similar between each other.\n",
|
96 |
+
"passage: If the metadata sets are somewhat similar, you will advise so. If they are too different, you will specify that the annotated metadata is an anomaly. An anomaly is defined as metadata that is significantly different from the automatically generated metadata or does not match the expected format.\n",
|
97 |
+
"passage: Here are some examples of correct and incorrect metadata sets:\n",
|
98 |
+
"passage: Correct metadata set:\n",
|
99 |
+
"passage: Filename: ISB-020-U3-W-H-01-B17012-001-000.pdf, Description: Novocon S demontiert und Stellantriebe montiert, passage: Discipline: H - Heizung\n",
|
100 |
+
"passage: Incorrect metadata set:\n",
|
101 |
+
"passage: Filename: incorrect_document.pdf\n",
|
102 |
+
"passage: Description: This document contains specifications for the plumbing system in the building.\n",
|
103 |
+
"passage: Discipline: Electrical\n",
|
104 |
+
"passage: {context}\n",
|
105 |
+
"query: {question}\n",
|
106 |
+
"\"\"\"\n",
|
107 |
+
"prompt = ChatPromptTemplate.from_template(template)\n",
|
108 |
+
"\n"
|
109 |
+
]
|
110 |
+
},
|
111 |
+
{
|
112 |
+
"cell_type": "code",
|
113 |
+
"execution_count": 51,
|
114 |
+
"metadata": {},
|
115 |
+
"outputs": [],
|
116 |
+
"source": [
|
117 |
+
"retriever = vectara.as_retriever()\n",
|
118 |
+
"\n",
|
119 |
+
"model = Together(model=MODEL_NAME,\n",
|
120 |
+
" temperature=0.7,\n",
|
121 |
+
" max_tokens=256,\n",
|
122 |
+
" top_k=50,\n",
|
123 |
+
" together_api_key=os.environ[\"TOGETHER_API_KEY\"]\n",
|
124 |
+
" )\n",
|
125 |
+
"\n",
|
126 |
+
"chain = (\n",
|
127 |
+
" {\"context\": retriever, \"question\": RunnablePassthrough()}\n",
|
128 |
+
" | prompt\n",
|
129 |
+
" | model\n",
|
130 |
+
" | StrOutputParser()\n",
|
131 |
+
")"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
{
|
135 |
+
"cell_type": "code",
|
136 |
+
"execution_count": 36,
|
137 |
+
"metadata": {},
|
138 |
+
"outputs": [],
|
139 |
+
"source": [
|
140 |
+
"example_metadata = {\"filename\":\"BedienungsanleitungFlanschen-Schieber\", \n",
|
141 |
+
" \"description\":\"User manual for Flanschen-Schieber (Flange valves) PN 16. It includes information about storage, handling, transport, safety instructions, usage, product description, mounting, demounting, and spare parts.\",\n",
|
142 |
+
" \"discipline\":\"plumbing\"}\n",
|
143 |
+
"madeup_meta = {'name':'electrical_doc.pdf',\n",
|
144 |
+
" 'description': 'Electrical wiring scheme and specifications for a generator room',\n",
|
145 |
+
" 'discipline':'ventilation'}\n",
|
146 |
+
"\n",
|
147 |
+
"input_query = f'automatically_generated_metadata: {madeup_meta}, annotated_metadata: {example_metadata}'\n",
|
148 |
+
"\n",
|
149 |
+
"output = chain.invoke(input_query)"
|
150 |
+
]
|
151 |
+
},
|
152 |
+
{
|
153 |
+
"cell_type": "code",
|
154 |
+
"execution_count": 52,
|
155 |
+
"metadata": {},
|
156 |
+
"outputs": [],
|
157 |
+
"source": [
|
158 |
+
"example_metadata = {\"filename\":\"BedienungsanleitungFlanschen-Schieber\", \n",
|
159 |
+
" \"description\":\"User manual for Flanschen-Schieber (Flange valves) PN 16. It includes information about storage, handling, transport, safety instructions, usage, product description, mounting, demounting, and spare parts.\",\n",
|
160 |
+
" \"discipline\":\"plumbing\"}\n",
|
161 |
+
"real_meta = {'name':'ISB-020-U3-W-S-01-B18003-001-020.pdf',\n",
|
162 |
+
" 'description': 'Schieber / Hawle / Schieber 4000 + Handrad 7800 DN100 Schutzraum',\n",
|
163 |
+
" 'discipline':'S - Sanitaer'}\n",
|
164 |
+
"\n",
|
165 |
+
"input_query = f'automatically_generated_metadata: {real_meta}, annotated_metadata: {example_metadata}'\n",
|
166 |
+
"\n",
|
167 |
+
"output = chain.invoke(input_query)\t \t\n"
|
168 |
+
]
|
169 |
+
},
|
170 |
+
{
|
171 |
+
"cell_type": "code",
|
172 |
+
"execution_count": 53,
|
173 |
+
"metadata": {},
|
174 |
+
"outputs": [
|
175 |
+
{
|
176 |
+
"data": {
|
177 |
+
"text/plain": [
|
178 |
+
"\"\\nBased on the provided passage and query, the automatically generated metadata and the annotated metadata are different in terms of filename, description, and discipline. The automatically generated metadata refers to a specific document with the name 'ISB-020-U3-W-S-01-B18003-001-020.pdf', a description 'Schieber / Hawle / Schieber 4000 + Handrad 7800 DN100 Schutzraum', and a discipline 'S - Sanitaer'. On the other hand, the annotated metadata refers to a user manual for Flanschen-Schieber (Flange valves) PN 16 with a filename 'BedienungsanleitungFlanschen-Schieber', a description that includes information about storage, handling, transport, safety instructions, usage, product description, mounting, demounting, and spare parts, and a discipline 'plumbing'. Therefore, the annotated metadata can be considered an anomaly in this case.\""
|
179 |
+
]
|
180 |
+
},
|
181 |
+
"execution_count": 53,
|
182 |
+
"metadata": {},
|
183 |
+
"output_type": "execute_result"
|
184 |
+
}
|
185 |
+
],
|
186 |
+
"source": [
|
187 |
+
"output"
|
188 |
+
]
|
189 |
+
},
|
190 |
+
{
|
191 |
+
"cell_type": "code",
|
192 |
+
"execution_count": 56,
|
193 |
+
"metadata": {},
|
194 |
+
"outputs": [],
|
195 |
+
"source": [
|
196 |
+
"summary_config = {\"is_enabled\": True, \"max_results\": 3, \"response_lang\": \"eng\"}\n",
|
197 |
+
"retriever = vectara.as_retriever(\n",
|
198 |
+
" search_kwargs={\"k\": 3, \"summary_config\": summary_config}\n",
|
199 |
+
")"
|
200 |
+
]
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"cell_type": "code",
|
204 |
+
"execution_count": 58,
|
205 |
+
"metadata": {},
|
206 |
+
"outputs": [],
|
207 |
+
"source": [
|
208 |
+
"def get_sources(documents):\n",
|
209 |
+
" return documents[:-1]\n",
|
210 |
+
"\n",
|
211 |
+
"\n",
|
212 |
+
"def get_summary(documents):\n",
|
213 |
+
" return documents[-1].page_content"
|
214 |
+
]
|
215 |
+
},
|
216 |
+
{
|
217 |
+
"cell_type": "code",
|
218 |
+
"execution_count": 60,
|
219 |
+
"metadata": {},
|
220 |
+
"outputs": [
|
221 |
+
{
|
222 |
+
"data": {
|
223 |
+
"text/plain": [
|
224 |
+
"'The metadata set provided appears to be related to plumbing, specifically Flanschen-Schieber (Flange valves) PN 16. The description includes details about storage, handling, transport, safety instructions, usage, product description, mounting, demounting, and spare parts. The discipline associated with this metadata is plumbing [3]. The filenames and descriptions of various files related to plumbing and ventilation systems are listed in the search results, indicating a match with the usual schema for such metadata sets [2, 3].'"
|
225 |
+
]
|
226 |
+
},
|
227 |
+
"execution_count": 60,
|
228 |
+
"metadata": {},
|
229 |
+
"output_type": "execute_result"
|
230 |
+
}
|
231 |
+
],
|
232 |
+
"source": [
|
233 |
+
"query_str = f'Does this metadata set match the usual schema or is it an anomaly? metadata:{example_metadata}'\n",
|
234 |
+
"ans = (retriever | get_summary).invoke(query_str)\n",
|
235 |
+
"ans \n",
|
236 |
+
"\n"
|
237 |
+
]
|
238 |
+
},
|
239 |
+
{
|
240 |
+
"cell_type": "code",
|
241 |
+
"execution_count": 63,
|
242 |
+
"metadata": {},
|
243 |
+
"outputs": [
|
244 |
+
{
|
245 |
+
"data": {
|
246 |
+
"text/plain": [
|
247 |
+
"'The metadata set provided with the name \"ISB-020-U3-W-S-01-B18003-001-020.pdf,\" description \"Schieber / Hawle / Schieber 4000 + Handrad 7800 DN100 Schutzraum,\" and discipline \"S - Sanitaer\" matches the usual schema as it follows the pattern of other entries such as \"ISB-020-U3-W-S-01-B17012-011-000\" with a similar structure and discipline [160]. The discipline \"S - Sanitaer\" is consistent with entries like \"ISB-020-U3-W-S-01-B19009-001-020\" that also belong to the Sanitaer discipline [161]. The description detailing specific items like \"Schieber / Hawle / Schieber 4000 + Handrad 7800 DN100 Schutzraum\" aligns with the content found in other entries under the same discipline [162]. The metadata set appears to be in line with the expected schema and does not seem to be an anomaly based on the available search results.'"
|
248 |
+
]
|
249 |
+
},
|
250 |
+
"execution_count": 63,
|
251 |
+
"metadata": {},
|
252 |
+
"output_type": "execute_result"
|
253 |
+
}
|
254 |
+
],
|
255 |
+
"source": [
|
256 |
+
"query_str = f\"\"\"Does this metadata set match the usual schema or is it an anomaly? metadata:\n",
|
257 |
+
"{real_meta}\"\"\"\n",
|
258 |
+
"ans = (retriever | get_summary).invoke(query_str)\n",
|
259 |
+
"ans \n"
|
260 |
+
]
|
261 |
+
},
|
262 |
+
{
|
263 |
+
"cell_type": "code",
|
264 |
+
"execution_count": 65,
|
265 |
+
"metadata": {},
|
266 |
+
"outputs": [
|
267 |
+
{
|
268 |
+
"data": {
|
269 |
+
"text/plain": [
|
270 |
+
"\"The metadata provided for 'electrical_doc.pdf' does not appear to match the usual schema for electrical documents. The discipline specified as 'ventilation' seems to be inconsistent with typical electrical document schemas [3]. The descriptions and names in the search results are more aligned with electrical systems rather than ventilation [10, 12, 13]. This indicates a potential anomaly in the metadata provided, suggesting a deviation from the expected schema for electrical documentation. It would be advisable to review the metadata to ensure accuracy and alignment with the appropriate discipline to avoid any confusion or misinterpretation of the document's content.\""
|
271 |
+
]
|
272 |
+
},
|
273 |
+
"execution_count": 65,
|
274 |
+
"metadata": {},
|
275 |
+
"output_type": "execute_result"
|
276 |
+
}
|
277 |
+
],
|
278 |
+
"source": [
|
279 |
+
"query_str = f\"\"\"query: Does this metadata in either english or german set match the usual schema or is it an anomaly? metadata:\n",
|
280 |
+
"{madeup_meta}.\"\"\"\n",
|
281 |
+
"ans = (retriever | get_summary).invoke(query_str)\n",
|
282 |
+
"ans \n"
|
283 |
+
]
|
284 |
+
},
|
285 |
+
{
|
286 |
+
"cell_type": "code",
|
287 |
+
"execution_count": null,
|
288 |
+
"metadata": {},
|
289 |
+
"outputs": [],
|
290 |
+
"source": []
|
291 |
+
}
|
292 |
+
],
|
293 |
+
"metadata": {
|
294 |
+
"kernelspec": {
|
295 |
+
"display_name": "Python 3",
|
296 |
+
"language": "python",
|
297 |
+
"name": "python3"
|
298 |
+
},
|
299 |
+
"language_info": {
|
300 |
+
"codemirror_mode": {
|
301 |
+
"name": "ipython",
|
302 |
+
"version": 3
|
303 |
+
},
|
304 |
+
"file_extension": ".py",
|
305 |
+
"mimetype": "text/x-python",
|
306 |
+
"name": "python",
|
307 |
+
"nbconvert_exporter": "python",
|
308 |
+
"pygments_lexer": "ipython3",
|
309 |
+
"version": "3.11.8"
|
310 |
+
}
|
311 |
+
},
|
312 |
+
"nbformat": 4,
|
313 |
+
"nbformat_minor": 2
|
314 |
+
}
|
not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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"Using deprecated Together LLM. Please use langchain_together instead.\n"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"data": {
|
17 |
+
"text/plain": [
|
18 |
+
"True"
|
19 |
+
]
|
20 |
+
},
|
21 |
+
"execution_count": 1,
|
22 |
+
"metadata": {},
|
23 |
+
"output_type": "execute_result"
|
24 |
+
}
|
25 |
+
],
|
26 |
+
"source": [
|
27 |
+
"import os \n",
|
28 |
+
"from dotenv import load_dotenv\n",
|
29 |
+
"\n",
|
30 |
+
"from langchain_community.document_loaders.csv_loader import CSVLoader\n",
|
31 |
+
"from langchain_community.embeddings import HuggingFaceEmbeddings\n",
|
32 |
+
"from langchain_community.vectorstores import Vectara\n",
|
33 |
+
"from langchain_core.output_parsers import StrOutputParser\n",
|
34 |
+
"from langchain_core.prompts import ChatPromptTemplate\n",
|
35 |
+
"from langchain_core.runnables import RunnablePassthrough\n",
|
36 |
+
"\n",
|
37 |
+
"try:\n",
|
38 |
+
" from langchain_together import Together\n",
|
39 |
+
"except:\n",
|
40 |
+
" print('Using deprecated Together LLM. Please use langchain_together instead.')\n",
|
41 |
+
" from langchain_community.llms import Together\n",
|
42 |
+
"\n",
|
43 |
+
"MODEL_NAME = \"mistralai/Mixtral-8x7B-Instruct-v0.1\"\n",
|
44 |
+
"\n",
|
45 |
+
"load_dotenv()"
|
46 |
+
]
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"cell_type": "code",
|
50 |
+
"execution_count": 2,
|
51 |
+
"metadata": {},
|
52 |
+
"outputs": [],
|
53 |
+
"source": [
|
54 |
+
"vectara_customer_id = os.environ['VECTARA_CUSTOMER_ID']\n",
|
55 |
+
"vectara_corpus_id = os.environ['VECTARA_CORPUS_ID']\n",
|
56 |
+
"vectara_api_key = os.environ['VECTARA_API_KEY']\n",
|
57 |
+
"\n",
|
58 |
+
"embeddings = HuggingFaceEmbeddings(model_name=\"intfloat/multilingual-e5-large\")\n",
|
59 |
+
"\n",
|
60 |
+
"vectara = Vectara(vectara_customer_id=vectara_customer_id,\n",
|
61 |
+
" vectara_corpus_id=vectara_corpus_id,\n",
|
62 |
+
" vectara_api_key=vectara_api_key)\n"
|
63 |
+
]
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"cell_type": "code",
|
67 |
+
"execution_count": 21,
|
68 |
+
"metadata": {},
|
69 |
+
"outputs": [
|
70 |
+
{
|
71 |
+
"name": "stderr",
|
72 |
+
"output_type": "stream",
|
73 |
+
"text": [
|
74 |
+
"/home/salgadev/code/DocVerifyRAG/.venv/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.llms.together.Together` was deprecated in langchain-community 0.0.12 and will be removed in 0.2. An updated version of the class exists in the langchain-together package and should be used instead. To use it run `pip install -U langchain-together` and import as `from langchain_together import Together`.\n",
|
75 |
+
" warn_deprecated(\n"
|
76 |
+
]
|
77 |
+
}
|
78 |
+
],
|
79 |
+
"source": [
|
80 |
+
"model = Together(model=MODEL_NAME,\n",
|
81 |
+
" temperature=0.7,\n",
|
82 |
+
" max_tokens=256,\n",
|
83 |
+
" top_k=50,\n",
|
84 |
+
" together_api_key=os.environ[\"TOGETHER_API_KEY\"]\n",
|
85 |
+
" )"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"cell_type": "code",
|
90 |
+
"execution_count": 6,
|
91 |
+
"metadata": {},
|
92 |
+
"outputs": [],
|
93 |
+
"source": [
|
94 |
+
"def get_sources(documents):\n",
|
95 |
+
" return documents[:-1]\n",
|
96 |
+
"\n",
|
97 |
+
"def get_summary(documents):\n",
|
98 |
+
" return documents[-1].page_content"
|
99 |
+
]
|
100 |
+
},
|
101 |
+
{
|
102 |
+
"cell_type": "code",
|
103 |
+
"execution_count": 26,
|
104 |
+
"metadata": {},
|
105 |
+
"outputs": [],
|
106 |
+
"source": [
|
107 |
+
"from langchain.prompts import PromptTemplate\n",
|
108 |
+
"\n",
|
109 |
+
"summary_config = {\"is_enabled\": True, \"max_results\": 3, \"response_lang\": \"eng\"}\n",
|
110 |
+
"retriever = vectara.as_retriever(\n",
|
111 |
+
" search_kwargs={\"k\": 3, \"summary_config\": summary_config}\n",
|
112 |
+
")"
|
113 |
+
]
|
114 |
+
},
|
115 |
+
{
|
116 |
+
"cell_type": "code",
|
117 |
+
"execution_count": 19,
|
118 |
+
"metadata": {},
|
119 |
+
"outputs": [],
|
120 |
+
"source": [
|
121 |
+
"wrong_disc_meta = 'electrical_doc.pdf, Electrical wiring scheme and specifications for a generator room, S - Sanitaer'\n",
|
122 |
+
"good_meta = 'ISB-020-U3-W-S-01-B18003-001-020.pdf, Schieber / Hawle / Schieber 4000 + Handrad 7800 DN100 Schutzraum, S - Sanitaer'"
|
123 |
+
]
|
124 |
+
},
|
125 |
+
{
|
126 |
+
"cell_type": "code",
|
127 |
+
"execution_count": 65,
|
128 |
+
"metadata": {},
|
129 |
+
"outputs": [],
|
130 |
+
"source": [
|
131 |
+
"template = \"\"\"\n",
|
132 |
+
"passage: You are a helpful assistant that understands BIM building documents and engineering disciplines.\n",
|
133 |
+
"passage: You will analyze BIM document metadata composed of filename, description, and discipline.\n",
|
134 |
+
"passage: The metadata is written in German.\n",
|
135 |
+
"passage: metadata: {metadata}\n",
|
136 |
+
"query: Does the filename match other filenames within the same discipline?\n",
|
137 |
+
"query: Does the description match the engineering discipline?\n",
|
138 |
+
"query: How different is the metadata to your curated information?\n",
|
139 |
+
"query: Highligh any discrepancies and comment on wether or not the metadata is anomalous.\n",
|
140 |
+
"\"\"\"\n",
|
141 |
+
"\n",
|
142 |
+
"prompt = PromptTemplate(template=template, input_variables=[\"metadata\"])\n"
|
143 |
+
]
|
144 |
+
},
|
145 |
+
{
|
146 |
+
"cell_type": "code",
|
147 |
+
"execution_count": 66,
|
148 |
+
"metadata": {},
|
149 |
+
"outputs": [
|
150 |
+
{
|
151 |
+
"data": {
|
152 |
+
"text/plain": [
|
153 |
+
"'\\npassage: You are a helpful assistant that understands BIM building documents and engineering disciplines.\\npassage: You will analyze BIM document metadata composed of filename, description, and discipline.\\npassage: The metadata is written in German.\\npassage: metadata: ISB-020-U3-W-S-01-B18003-001-020.pdf, Schieber / Hawle / Schieber 4000 + Handrad 7800 DN100 Schutzraum, S - Sanitaer\\nquery: Does the filename match other filenames within the same discipline?\\nquery: Does the description match the engineering discipline?\\nquery: How different is the metadata to your curated information?\\nquery: Highligh any discrepancies and comment on wether or not the metadata is anomalous.\\n'"
|
154 |
+
]
|
155 |
+
},
|
156 |
+
"execution_count": 66,
|
157 |
+
"metadata": {},
|
158 |
+
"output_type": "execute_result"
|
159 |
+
}
|
160 |
+
],
|
161 |
+
"source": [
|
162 |
+
"formatted_prompt = prompt.format(metadata=good_meta)\n",
|
163 |
+
"formatted_prompt"
|
164 |
+
]
|
165 |
+
},
|
166 |
+
{
|
167 |
+
"cell_type": "code",
|
168 |
+
"execution_count": 67,
|
169 |
+
"metadata": {},
|
170 |
+
"outputs": [
|
171 |
+
{
|
172 |
+
"data": {
|
173 |
+
"text/plain": [
|
174 |
+
"'Based on the provided BIM document metadata in German, the filename \"ISB-020-U3-W-S-01-B18003-001-020.pdf\" belongs to the discipline S - Sanitaer [2]. Comparing it to other filenames within the same discipline, there are similar filenames like \"ISB-020-U3-W-S-01-B17012-011-000\" and \"ISB-020-U3-W-S-01-B19009-001-020\" [3]. The description \"Schieber / Hawle / Schieber 4000 + Handrad 7800 DN100 Schutzraum\" corresponds to the engineering discipline S - Sanitaer [2]. The metadata displays a specific naming convention and content related to sanitary engineering, aligning with the discipline indicated [2]. No significant discrepancies were found in the metadata analyzed, suggesting that the provided metadata is consistent and not anomalous within the context of BIM building documents and engineering disciplines.'"
|
175 |
+
]
|
176 |
+
},
|
177 |
+
"execution_count": 67,
|
178 |
+
"metadata": {},
|
179 |
+
"output_type": "execute_result"
|
180 |
+
}
|
181 |
+
],
|
182 |
+
"source": [
|
183 |
+
"ans = (retriever | get_summary).invoke(formatted_prompt)\n",
|
184 |
+
"ans"
|
185 |
+
]
|
186 |
+
},
|
187 |
+
{
|
188 |
+
"cell_type": "markdown",
|
189 |
+
"metadata": {},
|
190 |
+
"source": [
|
191 |
+
"# metadata matches!"
|
192 |
+
]
|
193 |
+
},
|
194 |
+
{
|
195 |
+
"cell_type": "code",
|
196 |
+
"execution_count": 68,
|
197 |
+
"metadata": {},
|
198 |
+
"outputs": [
|
199 |
+
{
|
200 |
+
"data": {
|
201 |
+
"text/plain": [
|
202 |
+
"'\\npassage: You are a helpful assistant that understands BIM building documents and engineering disciplines.\\npassage: You will analyze BIM document metadata composed of filename, description, and discipline.\\npassage: The metadata is written in German.\\npassage: metadata: electrical_doc.pdf, Electrical wiring scheme and specifications for a generator room, S - Sanitaer\\nquery: Does the filename match other filenames within the same discipline?\\nquery: Does the description match the engineering discipline?\\nquery: How different is the metadata to your curated information?\\nquery: Highligh any discrepancies and comment on wether or not the metadata is anomalous.\\n'"
|
203 |
+
]
|
204 |
+
},
|
205 |
+
"execution_count": 68,
|
206 |
+
"metadata": {},
|
207 |
+
"output_type": "execute_result"
|
208 |
+
}
|
209 |
+
],
|
210 |
+
"source": [
|
211 |
+
"formatted_prompt = prompt.format(metadata=wrong_disc_meta)\n",
|
212 |
+
"formatted_prompt"
|
213 |
+
]
|
214 |
+
},
|
215 |
+
{
|
216 |
+
"cell_type": "code",
|
217 |
+
"execution_count": 69,
|
218 |
+
"metadata": {},
|
219 |
+
"outputs": [
|
220 |
+
{
|
221 |
+
"data": {
|
222 |
+
"text/plain": [
|
223 |
+
"'Based on the provided search results, the filename \"electrical_doc.pdf\" matches other filenames within the same discipline of E - Elektroanlagen [7]. However, the description \"Electrical wiring scheme and specifications for a generator room\" aligns more with the discipline of electrical engineering rather than \"S - Sanitaer\" [7]. The metadata presents discrepancies as the description does not directly correspond to the discipline mentioned, indicating a mismatch [7]. This inconsistency suggests that the metadata could be considered anomalous due to the mismatch between the description and the specified discipline [7]. The metadata exhibits a clear discrepancy between the content of the file and the discipline it is categorized under, raising a question about the accuracy of the metadata classification.'"
|
224 |
+
]
|
225 |
+
},
|
226 |
+
"execution_count": 69,
|
227 |
+
"metadata": {},
|
228 |
+
"output_type": "execute_result"
|
229 |
+
}
|
230 |
+
],
|
231 |
+
"source": [
|
232 |
+
"ans = (retriever | get_summary).invoke(formatted_prompt)\n",
|
233 |
+
"ans"
|
234 |
+
]
|
235 |
+
},
|
236 |
+
{
|
237 |
+
"cell_type": "markdown",
|
238 |
+
"metadata": {},
|
239 |
+
"source": [
|
240 |
+
"# Anomaly detected!"
|
241 |
+
]
|
242 |
+
},
|
243 |
+
{
|
244 |
+
"cell_type": "markdown",
|
245 |
+
"metadata": {},
|
246 |
+
"source": []
|
247 |
+
}
|
248 |
+
],
|
249 |
+
"metadata": {
|
250 |
+
"kernelspec": {
|
251 |
+
"display_name": "Python 3",
|
252 |
+
"language": "python",
|
253 |
+
"name": "python3"
|
254 |
+
},
|
255 |
+
"language_info": {
|
256 |
+
"codemirror_mode": {
|
257 |
+
"name": "ipython",
|
258 |
+
"version": 3
|
259 |
+
},
|
260 |
+
"file_extension": ".py",
|
261 |
+
"mimetype": "text/x-python",
|
262 |
+
"name": "python",
|
263 |
+
"nbconvert_exporter": "python",
|
264 |
+
"pygments_lexer": "ipython3",
|
265 |
+
"version": "3.11.8"
|
266 |
+
}
|
267 |
+
},
|
268 |
+
"nbformat": 4,
|
269 |
+
"nbformat_minor": 2
|
270 |
+
}
|