File size: 81,249 Bytes
115f44c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d0bb4874-7f7b-40a9-88ea-922aaed0f3a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# +\n",
    "from langchain_chroma import Chroma\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_core.runnables import RunnablePassthrough\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
    "from langchain_community.document_loaders import PyPDFLoader\n",
    "\n",
    "## dockerized streamlit app wants to read from os.getenv(), otherwise use st.secrets\n",
    "import streamlit as st\n",
    "import os\n",
    "api_key = os.getenv(\"LITELLM_KEY\")\n",
    "if api_key is None:\n",
    "    api_key = st.secrets[\"LITELLM_KEY\"]\n",
    "cirrus_key = os.getenv(\"CIRRUS_KEY\")\n",
    "if cirrus_key is None:\n",
    "    cirrus_key = st.secrets[\"CIRRUS_KEY\"]        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "95ed10f3-5339-40cd-bf16-b0854f8b4b91",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import requests\n",
    "import zipfile\n",
    "\n",
    "def download_and_unzip(url, output_dir):\n",
    "    \"\"\"\n",
    "    Downloads a ZIP file from a URL and unzips it to a specified directory.\n",
    "    \n",
    "    Args:\n",
    "        url (str): The URL of the ZIP file.\n",
    "        output_dir (str): The directory where the ZIP file will be unzipped.\n",
    "    \"\"\"\n",
    "    # Download the ZIP file\n",
    "    response = requests.get(url)\n",
    "    zip_file_path = os.path.basename(url)\n",
    "\n",
    "    # Save the ZIP file to the current directory\n",
    "    with open(zip_file_path, 'wb') as f:\n",
    "        f.write(response.content)\n",
    "\n",
    "    # Unzip the ZIP file\n",
    "    with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n",
    "        zip_ref.extractall(output_dir)\n",
    "\n",
    "    # Remove the ZIP file\n",
    "    os.remove(zip_file_path)\n",
    "\n",
    "# Example usage:\n",
    "url = \"https://minio.carlboettiger.info/public-data/hwc.zip\"\n",
    "output_dir = \"hwc\"\n",
    "\n",
    "# Create the output directory if it doesn't exist\n",
    "if not os.path.exists(output_dir):\n",
    "    os.makedirs(output_dir)\n",
    "\n",
    "download_and_unzip(url, output_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2fbca6dc-a90b-4dd4-8225-8baac5c6622d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pathlib\n",
    "\n",
    "def pdf_loader(path):\n",
    "    all_documents = []\n",
    "    docs_dir = pathlib.Path(path)\n",
    "    for file in docs_dir.iterdir():\n",
    "        loader = PyPDFLoader(file)\n",
    "        documents = loader.load()\n",
    "        all_documents.extend(documents)\n",
    "    return all_documents\n",
    "\n",
    "docs = pdf_loader('/home/rstudio/data/hwc/')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c6e99791-8f34-4722-9708-665e409c26bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Set up the language model\n",
    "from langchain_openai import ChatOpenAI\n",
    "llm = ChatOpenAI(model = \"llama3-sdsc\",\n",
    "                 api_key = api_key, \n",
    "                 base_url = \"https://llm.nrp-nautilus.io\",  \n",
    "                 temperature=0)\n",
    "\n",
    "# Set up the embedding model\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "embedding = OpenAIEmbeddings(model = \"embed-mistral\", api_key = api_key, base_url = \"https://llm.nrp-nautilus.io\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0a8a004d-bb80-42bf-b7e6-15a54e2dd804",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Cirrus instead:\n",
    "embedding = OpenAIEmbeddings(\n",
    "                 model = \"cirrus\",\n",
    "                 api_key =  cirrus_key, \n",
    "                 base_url = \"https://llm.cirrus.carlboettiger.info/v1\",\n",
    ")\n",
    "\n",
    "\n",
    "\n",
    "text = \"A test\"\n",
    "\n",
    "vectorstore = InMemoryVectorStore.from_texts(\n",
    "    [text],\n",
    "    embedding=embedding,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "fd8bcc13-d06d-43dd-9e06-4f29da803133",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# Build a retrival agent\n",
    "from langchain_core.vectorstores import InMemoryVectorStore\n",
    "from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
    "text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n",
    "splits = text_splitter.split_documents(docs)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7f388394-5da2-4db8-8e48-e10436c8532d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# slow part here, runs on remote GPU\n",
    "from langchain_core.vectorstores import InMemoryVectorStore\n",
    "vectorstore = InMemoryVectorStore.from_documents(documents=splits, embedding=embedding)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2bf50abf-5ccd-4de5-9fc4-c9043a66a108",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = vectorstore.as_retriever()\n",
    "\n",
    "from langchain.chains import create_retrieval_chain\n",
    "from langchain.chains.combine_documents import create_stuff_documents_chain\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "system_prompt = (\n",
    "    \"You are an assistant for question-answering tasks. \"\n",
    "    \"Use the following pieces of retrieved context to answer \"\n",
    "    \"the question. If you don't know the answer, say that you \"\n",
    "    \"don't know. Use three sentences maximum and keep the \"\n",
    "    \"answer concise.\"\n",
    "    \"\\n\\n\"\n",
    "    \"{context}\"\n",
    ")\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", system_prompt),\n",
    "        (\"human\", \"{input}\"),\n",
    "    ]\n",
    ")\n",
    "question_answer_chain = create_stuff_documents_chain(llm, prompt)\n",
    "rag_chain = create_retrieval_chain(retriever, question_answer_chain)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e15c64e7-0916-4042-8274-870e4fdb1af7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'I live in Tanzania and am having issues with lions breaking into my boma and preying on cattle. What interventions might work best for me?',\n",
       " 'context': [Document(id='898b2a4c-40ac-482a-8da6-33d50a8a1daa', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 13, 'page_label': '14'}, page_content='in the Tsavo-Mkomazi Ecosystem (February 2017); Kenya Wildlife Service (KWS); Tanzania Wildlife Research Institute (TAWIRI):\\nArusha, Tanzania, 2017.\\n43. Suissa, S.; Shuster, J.J. Exact unconditional sample sizes for the 2 × 2 binomial trial. J. R. Stat. Soc. 1985, 148, 317–327. [CrossRef]\\n44. R Development Core Team. R: A Language and Environment for Statistical Computing; R Foundation for Statistical Computing:\\nVienna, Austria, 2020.\\n45. Osborn, F.V . Capsicum oleoresin as an elephant repellent: Field trials in the communal lands of Zimbabwe.J. Wildl. Manag. 2002,\\n66, 674–677. [CrossRef]\\n46. Wright, M.G.; Spencer, C.; Cook, R.M.; Henley, M.D.; North, W.; Mafra-Neto, A. African bush elephants respond to a honeybee\\nalarm pheromone blend. Curr. Biol. 2018, 28, 778–780. [CrossRef]\\n47. Valenta, K.; Schmitt, M.H.; Ayasse, M.; Nevo, O. The sensory ecology of fear: African elephants show aversion to olfactory\\npredator signals. Conserv. Sci. Pract. 2021, 3, 333. [CrossRef]'),\n",
       "  Document(id='959100a0-dd92-4fc2-8e19-6b32770dc0c3', metadata={'producer': 'Acrobat Distiller 11.0 (Windows); modified using iText 4.2.0 by 1T3XT', 'creator': 'Arbortext Advanced Print Publisher 9.1.520/W Unicode', 'creationdate': '2023-06-16T15:56:20+05:30', 'keywords': '', 'moddate': '2025-05-27T12:15:01-07:00', 'subject': 'Conservat Sci and Prac 2023.5:e12948', 'wps-proclevel': '3', 'wps-journaldoi': '10.1111/(ISSN)2578-4854', 'title': 'Using behavioral studies to adapt management decisions and reduce negative interactions between humans and baboons in Cape Town, South Africa', 'wps-articledoi': '10.1111/csp2.12948', 'source': '/home/rstudio/data/hwc/Fehlmann et al. 2022.pdf', 'total_pages': 16, 'page': 11, 'page_label': '12'}, page_content=\"of chasing the baboons away. Most GPS fixes in the vine-\\nyards are close to the main sleeping sites (Figure 2f).\\nBaboons, like many other primates, sleep in high trees or\\ncliffs (Fruth et al.,2018) and unsurprisingly the distribu-\\ntion of suitable sites strongly influences daily travel\\nroutes (Fei et al.,\\n2022; Schreier & Grove,2014). A further\\nrecommendation to farmers would be to prevent access\\nto these sleeping sites, which would encourage baboons\\nto range further away from the farm.\\nWhen baboons were prevented from accessing urban\\nareas, they spent more time foraging and less time groom-\\ning. Food available to baboons in urban or croplands\\ntends to be rich in energy (Fehlmann, O'Riain, Kerr-\\nSmith, Hailes, et al.,\\n2017; Johnson et al.,2013), locally\\nabundant, and predictable in time and space (Hill,2017).\\nUrban or crop-foraging individuals, therefore, benefit\\nfrom increased energetic intake allowing them to reduce\\nthe time spent foraging (Altmann & Muruthi, 1988;\"),\n",
       "  Document(id='36a5ffec-76c4-465f-b96f-d09f11da2fdd', metadata={'producer': 'Canon iR C5800', 'creator': 'Canon iR C5800', 'creationdate': '2009-03-09T08:39:40-05:00', 'subject': 'Image', 'source': '/home/rstudio/data/hwc/Dolbeer et al. 1994.pdf', 'total_pages': 34, 'page': 15, 'page_label': '16'}, page_content='have questionable effcacy), toxic baits, snap trps, live\\ntrps, shooting.\\nCOMMENSAL RODENTS\\n- The three speies of commensal rodents (those that live\\npnmanly around human habitation) are NQi:ay rats, roof\\n(black) rats, and house mice. These omnivorous rodents\\nconsume millions of bushels of grain each year: in the\\nfield, on the far, in the elevator, mill, store, and home,\\nand in transit. They also waste many more milions of\\nbushels by contamination. These rodents typically drop\\n25- i 50 pellets and void 10-20 cc of unne every 24 hours,\\nand constantly shed fine hairs.\\nRats cause extensive damage to sugarcane in Hawaii\\nand Flonda (Fig. II), and roof rats are senous pests in\\nHawaiian macadamia nut plantations. These rodents will\\nfeed on poultry chicks and occasionally will attack adult\\npoultry, wild birds, newborn pigs, lambs, and calves.\\nHealth departments annually report hundreds of human\\nbabies bitten by rats. Many viral and bactenal diseases are'),\n",
       "  Document(id='c5030f53-ec58-4cfe-a6e3-136b2e72571c', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 13, 'page_label': '14'}, page_content='deterrent innovations in the Okavango Delta, Botswana. Afr. J. Sci. Technol. Innov. Dev. 2015, 7, 408–419. [CrossRef]\\n51. Snyder, K.D.; Renstch, D. Rethinking assessment of success of mitigation strategies for elephant-induced crop damage. Conserv.\\nBiol. 2020, 34, 829–842. [CrossRef]')],\n",
       " 'answer': \"I don't know of any specific interventions mentioned in the provided context that directly address lions breaking into a boma and preying on cattle in Tanzania. The context mentions various studies and interventions related to elephants, baboons, and rodents, but not lions. You may need to consult with local wildlife experts or authorities for guidance on effective lion deterrents and boma protection methods.\"}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt = \"I live in Tanzania and am having issues with lions breaking into my boma and preying on cattle. What interventions might work best for me?\"\n",
    "results = rag_chain.invoke({\"input\": prompt})\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "35613607-2c36-4761-a8ea-8c0889530f34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'What are the most cost-effective prevention methods for elephants raiding my crops?',\n",
       " 'context': [Document(id='6b17529c-1918-481c-ba42-819c173a3917', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 13, 'page_label': '14'}, page_content='trials in northern Kenya. Afr. J. Ecol. 2011, 49, 431–439. [CrossRef]\\n28. Pekor, A.; Miller, J.R.; Flyman, M.V .; Kasiki, S.; Kesch, M.K.; Miller, S.M.; Uiseb, K.; van der Merve, V .; Lindsey, P .A. Fencing\\nAfrica’s protected areas: Costs, benefits, and management issues. Biol. Conserv. 2019, 229, 67–75. [CrossRef]\\n29. Mutinda, M.; Chenge, G.; Gakuya, F.; Otiende, M.; Omondi, P .; Kasiki, S.; Soriguer, R.C.; Alasaad, S. Detusking fence-breaker\\nelephants as an approach in human-elephant conflict mitigation. PLoS ONE 2014, 9, e91749. [CrossRef]\\n30. King, L.E.; Lala, F.; Nzumu, H.; Mwambingu, E.; Douglas-Hamilton, I. Beehive fences as a multidimensional conflict-mitigation\\ntool for farmers coexisting with elephants. Conserv. Biol. 2017, 31, 743–752. [CrossRef]\\n31. Santiapillai, C.; Read, B. Would masking the smell of ripening paddy-fields help mitigate human–elephant conflict in Sri Lanka?\\nOryx 2010, 44, 509–511. [CrossRef]'),\n",
       "  Document(id='fbd6a497-84df-425f-b1ae-84f95871c46e', metadata={'producer': 'Acrobat Distiller 7.0 (Windows)', 'creator': '3B2 Total Publishing System 8.07e/W', 'creationdate': '2007-09-07T18:28:49+05:30', 'moddate': '2007-09-07T19:48:20+05:30', 'title': '10531_2006_9124_Article 1245..1', 'source': '/home/rstudio/data/hwc/Woodroffe et al. 2007.pdf', 'total_pages': 16, 'page': 5, 'page_label': '6'}, page_content='The results of case–control analyses are presented as odds ratios (and their\\nassociated 95% confidence intervals (CI)). These odds ratios indicate the magnitude\\nof the estimated effects; for example the odds ratio of 0.371 (95% CI 0.195–0.705)\\nassociated with domestic dogs’ presence in Table 1 indicates that dogs reduce the\\nprobability of attack by a factor of about 0.37, i.e. a 63% reduction.\\nIn addition to these case–control comparisons, we also compared the habitat\\ndensity and terrain at sites where attacks occurred on grazing herds, with those in the\\nTable 1 Predictors of the probability of attack, by any predator species, on herds grazing during the\\ndaytime, based on conditional logistic regression analysis of 147 matched case–control sets\\nVariable Odds ratio (95% CI) v 2 df P\\nDomestic dogs\\nPresent vs. absent 0.371 (0.195–0.705) 9.14 1 0.003\\nHerd size 1.006 (1.002–1.011) 7.27 1 0.007\\nHabitat density 11.71 3 0.008\\nLight bush vs. open 1.667 (0.635–4.381)'),\n",
       "  Document(id='898b2a4c-40ac-482a-8da6-33d50a8a1daa', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 13, 'page_label': '14'}, page_content='in the Tsavo-Mkomazi Ecosystem (February 2017); Kenya Wildlife Service (KWS); Tanzania Wildlife Research Institute (TAWIRI):\\nArusha, Tanzania, 2017.\\n43. Suissa, S.; Shuster, J.J. Exact unconditional sample sizes for the 2 × 2 binomial trial. J. R. Stat. Soc. 1985, 148, 317–327. [CrossRef]\\n44. R Development Core Team. R: A Language and Environment for Statistical Computing; R Foundation for Statistical Computing:\\nVienna, Austria, 2020.\\n45. Osborn, F.V . Capsicum oleoresin as an elephant repellent: Field trials in the communal lands of Zimbabwe.J. Wildl. Manag. 2002,\\n66, 674–677. [CrossRef]\\n46. Wright, M.G.; Spencer, C.; Cook, R.M.; Henley, M.D.; North, W.; Mafra-Neto, A. African bush elephants respond to a honeybee\\nalarm pheromone blend. Curr. Biol. 2018, 28, 778–780. [CrossRef]\\n47. Valenta, K.; Schmitt, M.H.; Ayasse, M.; Nevo, O. The sensory ecology of fear: African elephants show aversion to olfactory\\npredator signals. Conserv. Sci. Pract. 2021, 3, 333. [CrossRef]'),\n",
       "  Document(id='c5030f53-ec58-4cfe-a6e3-136b2e72571c', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 13, 'page_label': '14'}, page_content='deterrent innovations in the Okavango Delta, Botswana. Afr. J. Sci. Technol. Innov. Dev. 2015, 7, 408–419. [CrossRef]\\n51. Snyder, K.D.; Renstch, D. Rethinking assessment of success of mitigation strategies for elephant-induced crop damage. Conserv.\\nBiol. 2020, 34, 829–842. [CrossRef]')],\n",
       " 'answer': \"The most cost-effective prevention methods for elephants raiding crops include beehive fences, which have been shown to be a multidimensional conflict-mitigation tool. Additionally, using capsaicin oleoresin, a chili pepper extract, as an elephant repellent has also been effective in field trials. Domestic dogs' presence has also been found to reduce the probability of elephant attacks by 63%.\"}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt = \"What are the most cost-effective prevention methods for elephants raiding my crops?\"\n",
    "\n",
    "results = rag_chain.invoke({\"input\": prompt})\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3dfc39f6-86e9-47c3-ab67-08f90ebbb823",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'I have a small herd of goats and cattle and I am worried about jaguars preying on them. What preventative measures can I take?',\n",
       " 'context': [Document(id='959100a0-dd92-4fc2-8e19-6b32770dc0c3', metadata={'producer': 'Acrobat Distiller 11.0 (Windows); modified using iText 4.2.0 by 1T3XT', 'creator': 'Arbortext Advanced Print Publisher 9.1.520/W Unicode', 'creationdate': '2023-06-16T15:56:20+05:30', 'keywords': '', 'moddate': '2025-05-27T12:15:01-07:00', 'subject': 'Conservat Sci and Prac 2023.5:e12948', 'wps-proclevel': '3', 'wps-journaldoi': '10.1111/(ISSN)2578-4854', 'title': 'Using behavioral studies to adapt management decisions and reduce negative interactions between humans and baboons in Cape Town, South Africa', 'wps-articledoi': '10.1111/csp2.12948', 'source': '/home/rstudio/data/hwc/Fehlmann et al. 2022.pdf', 'total_pages': 16, 'page': 11, 'page_label': '12'}, page_content=\"of chasing the baboons away. Most GPS fixes in the vine-\\nyards are close to the main sleeping sites (Figure 2f).\\nBaboons, like many other primates, sleep in high trees or\\ncliffs (Fruth et al.,2018) and unsurprisingly the distribu-\\ntion of suitable sites strongly influences daily travel\\nroutes (Fei et al.,\\n2022; Schreier & Grove,2014). A further\\nrecommendation to farmers would be to prevent access\\nto these sleeping sites, which would encourage baboons\\nto range further away from the farm.\\nWhen baboons were prevented from accessing urban\\nareas, they spent more time foraging and less time groom-\\ning. Food available to baboons in urban or croplands\\ntends to be rich in energy (Fehlmann, O'Riain, Kerr-\\nSmith, Hailes, et al.,\\n2017; Johnson et al.,2013), locally\\nabundant, and predictable in time and space (Hill,2017).\\nUrban or crop-foraging individuals, therefore, benefit\\nfrom increased energetic intake allowing them to reduce\\nthe time spent foraging (Altmann & Muruthi, 1988;\"),\n",
       "  Document(id='65c01063-cb6a-469a-b03d-019861cf1ffd', metadata={'producer': 'PDFlib PLOP 2.0.0p6 (SunOS)/Acrobat Distiller 6.0 (Windows); modified using iText 4.2.0 by 1T3XT', 'creator': '3B2 Total Publishing System 8.07c/W', 'creationdate': '2006-04-19T19:41:41-05:00', 'moddate': '2025-05-27T12:08:26-07:00', 'subject': 'Wildlife Society Bulletin 2006.34:191-200', 'wps-proclevel': '2', 'wps-journaldoi': '10.1111/wsb4.2006.34.issue-1', 'title': 'From the Field: Fences and Deer‐Damage Management: A Review of Designs and Efficacy', 'wps-articledoi': '10.2193/0091-7648(2006)34[191:FADMAR]2.0.CO;2', 'source': '/home/rstudio/data/hwc/VerCauteren et al. 2006.pdf', 'total_pages': 10, 'page': 3, 'page_label': '4'}, page_content='harm to an animal impacting it and facilitates installation on\\nuneven terrain (Bryant et al. 1993). Wire-mesh fence is available\\nwith wider spacing near the top rows and should be installed with\\nthe larger mesh spaces (.0.19 m) well above the height of an\\napproaching deer, to discourage attempts at penetration.\\nWire-mesh fence is used commonly to minimize wildlife-vehicle\\ncollisions along busy highways within migration corridors. A 2.4-\\nm wire-mesh fence along an interstate in Wyoming was effective\\nFigure 1. Fencing considerations: (A) as the area enclosed by a fence\\nincreases, the perimeter length increases at a lower rate, and (B) perimeter\\nlength of a square plot is shorter than a rectangular plot enclosing the same\\narea.\\n194 Wildlife Society Bulletin/C15 34(1)'),\n",
       "  Document(id='99421239-009c-4a82-9291-0ff16c9eaf43', metadata={'producer': 'iText 4.2.0 by 1T3XT', 'creator': 'John Wiley & Sons', 'creationdate': '2024-01-03T16:15:52+05:30', 'keywords': 'management; Mephitis mephitis; mitigation; Procyon lotor; raccoon; red fox; striped skunk; Vulpes vulpes', 'moddate': '2025-05-27T12:12:05-07:00', 'subject': 'J Wildl Manag 2024.88:e22526', 'wps-proclevel': '3', 'wps-journaldoi': '10.1002/(ISSN)1937-2817', 'author': '', 'title': 'Methods to mitigate human–wildlife conflicts involving common mesopredators: a meta‐analysis', 'wps-articledoi': '10.1002/jwmg.22526', 'source': '/home/rstudio/data/hwc/Lazure and Weladji 2023.pdf', 'total_pages': 21, 'page': 18, 'page_label': '19'}, page_content='challenges predator control: An experimental evidence‐based approach improves management. Journal of Wildlife\\nManagement 79:425–434.\\nLiss, C. A. 1997. The public is attracted by the use of repellents. Pages 429–433 in J. R. Mason, editor. Repellents in wildlife\\nmanagement. Colorado State University Press, Denver, USA.\\nLotze, J.‐H., and S. Anderson. 1979.Procyon lotor. Mammalian Species 119:1–8.\\nLozano, J., A. Olszańska, Z. Morales‐Reyes, A. A. Castro, A. F. Malo, M. Moleón, J. A. Sánchez‐Zapata, A. Cortés‐Avizanda,\\nH. von Wehrden, I. Dorresteijn, et al. 2019. Human‐carnivore relations: a systematic review. Biological Conservation\\n237:480–492.\\nMabee, T. J., and V. B. Estelle. 2000. Assessing the effectiveness of predator exclosures for plovers. Wilson Journal of\\nOrnithology 112:14–20.\\nMacdonald, D. W., and J. C. Reynolds. 2004. Red fox (Vulpes vulpes). Pages 129–135 in C. Sillero‐Zubiri, M. Hoffmann, and'),\n",
       "  Document(id='c5030f53-ec58-4cfe-a6e3-136b2e72571c', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 13, 'page_label': '14'}, page_content='deterrent innovations in the Okavango Delta, Botswana. Afr. J. Sci. Technol. Innov. Dev. 2015, 7, 408–419. [CrossRef]\\n51. Snyder, K.D.; Renstch, D. Rethinking assessment of success of mitigation strategies for elephant-induced crop damage. Conserv.\\nBiol. 2020, 34, 829–842. [CrossRef]')],\n",
       " 'answer': \"To prevent jaguar predation on your goats and cattle, you can consider using fencing, such as wire-mesh fencing, to enclose your livestock area. Additionally, removing attractants like food sources and preventing access to sleeping sites or habitats near your farm may help deter jaguars. However, I don't have specific information on jaguar deterrents, and it's recommended to consult with a wildlife expert for tailored advice.\"}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rag_chain.invoke({\"input\": \n",
    "                  \"I have a small herd of goats and cattle and I am worried about jaguars preying on them. What preventative measures can I take?\"\n",
    "                 })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "56091874-0e41-4b35-be4f-08d8ec6faf56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'I am trying to prevent coyotes from eating the calves of my free-range cattle. What may work best?',\n",
       " 'context': [Document(id='65c01063-cb6a-469a-b03d-019861cf1ffd', metadata={'producer': 'PDFlib PLOP 2.0.0p6 (SunOS)/Acrobat Distiller 6.0 (Windows); modified using iText 4.2.0 by 1T3XT', 'creator': '3B2 Total Publishing System 8.07c/W', 'creationdate': '2006-04-19T19:41:41-05:00', 'moddate': '2025-05-27T12:08:26-07:00', 'subject': 'Wildlife Society Bulletin 2006.34:191-200', 'wps-proclevel': '2', 'wps-journaldoi': '10.1111/wsb4.2006.34.issue-1', 'title': 'From the Field: Fences and Deer‐Damage Management: A Review of Designs and Efficacy', 'wps-articledoi': '10.2193/0091-7648(2006)34[191:FADMAR]2.0.CO;2', 'source': '/home/rstudio/data/hwc/VerCauteren et al. 2006.pdf', 'total_pages': 10, 'page': 3, 'page_label': '4'}, page_content='harm to an animal impacting it and facilitates installation on\\nuneven terrain (Bryant et al. 1993). Wire-mesh fence is available\\nwith wider spacing near the top rows and should be installed with\\nthe larger mesh spaces (.0.19 m) well above the height of an\\napproaching deer, to discourage attempts at penetration.\\nWire-mesh fence is used commonly to minimize wildlife-vehicle\\ncollisions along busy highways within migration corridors. A 2.4-\\nm wire-mesh fence along an interstate in Wyoming was effective\\nFigure 1. Fencing considerations: (A) as the area enclosed by a fence\\nincreases, the perimeter length increases at a lower rate, and (B) perimeter\\nlength of a square plot is shorter than a rectangular plot enclosing the same\\narea.\\n194 Wildlife Society Bulletin/C15 34(1)'),\n",
       "  Document(id='99421239-009c-4a82-9291-0ff16c9eaf43', metadata={'producer': 'iText 4.2.0 by 1T3XT', 'creator': 'John Wiley & Sons', 'creationdate': '2024-01-03T16:15:52+05:30', 'keywords': 'management; Mephitis mephitis; mitigation; Procyon lotor; raccoon; red fox; striped skunk; Vulpes vulpes', 'moddate': '2025-05-27T12:12:05-07:00', 'subject': 'J Wildl Manag 2024.88:e22526', 'wps-proclevel': '3', 'wps-journaldoi': '10.1002/(ISSN)1937-2817', 'author': '', 'title': 'Methods to mitigate human–wildlife conflicts involving common mesopredators: a meta‐analysis', 'wps-articledoi': '10.1002/jwmg.22526', 'source': '/home/rstudio/data/hwc/Lazure and Weladji 2023.pdf', 'total_pages': 21, 'page': 18, 'page_label': '19'}, page_content='challenges predator control: An experimental evidence‐based approach improves management. Journal of Wildlife\\nManagement 79:425–434.\\nLiss, C. A. 1997. The public is attracted by the use of repellents. Pages 429–433 in J. R. Mason, editor. Repellents in wildlife\\nmanagement. Colorado State University Press, Denver, USA.\\nLotze, J.‐H., and S. Anderson. 1979.Procyon lotor. Mammalian Species 119:1–8.\\nLozano, J., A. Olszańska, Z. Morales‐Reyes, A. A. Castro, A. F. Malo, M. Moleón, J. A. Sánchez‐Zapata, A. Cortés‐Avizanda,\\nH. von Wehrden, I. Dorresteijn, et al. 2019. Human‐carnivore relations: a systematic review. Biological Conservation\\n237:480–492.\\nMabee, T. J., and V. B. Estelle. 2000. Assessing the effectiveness of predator exclosures for plovers. Wilson Journal of\\nOrnithology 112:14–20.\\nMacdonald, D. W., and J. C. Reynolds. 2004. Red fox (Vulpes vulpes). Pages 129–135 in C. Sillero‐Zubiri, M. Hoffmann, and'),\n",
       "  Document(id='c5030f53-ec58-4cfe-a6e3-136b2e72571c', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 13, 'page_label': '14'}, page_content='deterrent innovations in the Okavango Delta, Botswana. Afr. J. Sci. Technol. Innov. Dev. 2015, 7, 408–419. [CrossRef]\\n51. Snyder, K.D.; Renstch, D. Rethinking assessment of success of mitigation strategies for elephant-induced crop damage. Conserv.\\nBiol. 2020, 34, 829–842. [CrossRef]'),\n",
       "  Document(id='36a5ffec-76c4-465f-b96f-d09f11da2fdd', metadata={'producer': 'Canon iR C5800', 'creator': 'Canon iR C5800', 'creationdate': '2009-03-09T08:39:40-05:00', 'subject': 'Image', 'source': '/home/rstudio/data/hwc/Dolbeer et al. 1994.pdf', 'total_pages': 34, 'page': 15, 'page_label': '16'}, page_content='have questionable effcacy), toxic baits, snap trps, live\\ntrps, shooting.\\nCOMMENSAL RODENTS\\n- The three speies of commensal rodents (those that live\\npnmanly around human habitation) are NQi:ay rats, roof\\n(black) rats, and house mice. These omnivorous rodents\\nconsume millions of bushels of grain each year: in the\\nfield, on the far, in the elevator, mill, store, and home,\\nand in transit. They also waste many more milions of\\nbushels by contamination. These rodents typically drop\\n25- i 50 pellets and void 10-20 cc of unne every 24 hours,\\nand constantly shed fine hairs.\\nRats cause extensive damage to sugarcane in Hawaii\\nand Flonda (Fig. II), and roof rats are senous pests in\\nHawaiian macadamia nut plantations. These rodents will\\nfeed on poultry chicks and occasionally will attack adult\\npoultry, wild birds, newborn pigs, lambs, and calves.\\nHealth departments annually report hundreds of human\\nbabies bitten by rats. Many viral and bactenal diseases are')],\n",
       " 'answer': 'A wire-mesh fence with wider spacing near the top rows, installed with the larger mesh spaces well above the height of an approaching coyote, may be effective in preventing coyotes from accessing your free-range cattle. Additionally, using a fence that is at least 2.4 meters high, similar to the one used along an interstate in Wyoming, may also be beneficial. However, the most effective solution may depend on various factors, including the size of your cattle area and the local coyote population.'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rag_chain.invoke({\"input\": \"I am trying to prevent coyotes from eating the calves of my free-range cattle. What may work best?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "918dc691-6c66-46b2-8930-01dbeb6f712b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'We have major issues with deer raiding our large agricultural fields. Is there anything I can try to prevent this that won’t break the bank?',\n",
       " 'context': [Document(id='63bb63bd-43db-48a1-a246-a0dcd346830f', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 13, 'page_label': '14'}, page_content='Diversity 2022, 14, 509 14 of 15\\n23. Thuppil, V .; Coss, R.G. Wild Asian elephants distinguish aggressive tiger and leopard growls according to perceived danger.Biol.\\nLett. 2013, 9, 20130518. [CrossRef]\\n24. O’Connell-Rodwell, C.E.; Rodwell, T.; Rice, M.; Hart, L.A. Living with the modern conservation paradigm: Can agricultural\\ncommunities co-exist with elephants? A five-year case study in East Caprivi, Namibia. Biol. Conserv. 2000, 93, 381–391. [CrossRef]\\n25. Hoare, R. Lessons from 15 years of human–elephant conflict mitigation in Africa. Hum. Dimens. Wildl. 2015, 20, 289–295.\\n[CrossRef]\\n26. Kioko, J.; Muruthi, P .; Omondi, P .; Chiyo, P .I. The performance of electric fences as elephant barriers in Amboseli, Kenya.South\\nAfr. J. Wildl. Res. 2008, 38, 52–58. [CrossRef]\\n27. King, L.E.; Lawrence, A.; Douglas-Hamilton, I.; Vollrath, F. Beehive fences as effective deterrents for crop raiding elephants: Field\\ntrials in northern Kenya. Afr. J. Ecol. 2011, 49, 431–439. [CrossRef]'),\n",
       "  Document(id='b8b1e6e6-e92b-48ba-9ddf-5d443911d472', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 14, 'page_label': '15'}, page_content='56. McArthur, C.; Finnerty, P .B.; Schmitt, M.H.; Shuttleworth, A.; Shrader, A.M. Plant volatiles are a salient cue for foraging mammals:\\nElephants target preferred plants despite background plant odour. Anim. Behav. 2019, 155, 199–216. [CrossRef]\\n57. Battisti, C. Unifying the trans-disciplinary arsenal of project management tools in a single logical framework: Further suggestion\\nfor IUCN project cycle development. J. Nat. Conserv. 2018, 41, 63–72. [CrossRef]'),\n",
       "  Document(id='3c8d2a07-5365-4e2a-9075-e38f71a82713', metadata={'producer': 'Acrobat Distiller 4.05 for Macintosh; modified using iText 4.2.0 by 1T3XT', 'creator': 'Textures¨: PSPrinter 8.3.1', 'creationdate': 'D:20031120165048', 'moddate': '2025-05-27T11:42:09-07:00', 'subject': 'Conservation Biology 2003.17:1531-1537', 'author': 'BNMAC10', 'title': 'Nonlethal Techniques for Managing Predation: Primary and Secondary Repellents', 'source': '/home/rstudio/data/hwc/Shivik et al. 2003.pdf', 'total_pages': 7, 'page': 5, 'page_label': '6'}, page_content='skills performing fieldwork, A. Wydeven and all associ-\\nated personnel at the Wisconsin Department of Natural\\nResources, B. Hawkinson for providing deer carcasses,\\nand supporters and personnel of the Wildlife Science\\nCenter. This work was completed under auspices of the\\nNational Wildlife Research Center’s Project (NWRC), Al-\\nternative Capture Systems and Aversive Stimulus Appli-\\ncations for Managing Predation, with financial support\\nfor the Wyoming Animal Damage Management Board\\nand financial and other aid from the Born Free Founda-\\ntion. All protocols were approved by the NWRC and the\\nWildlife Science Center Institutional Animal Care and Use\\nCommittees.\\nLiterature Cited\\nAndelt, W . F ., R. L. Phillips, K. S. Gruver, and J. W . Guthrie. 1999. Coyote\\npredation on domestic sheep deterred with electronic dog-training\\ncollar. Wildlife Society Bulletin 27:12–18.\\nBombford, M., and P . H. O’Brien. 1990. Sonic deterrents in animal dam-'),\n",
       "  Document(id='d78c070a-c1ea-447f-bb65-4bd03b484471', metadata={'producer': 'GPL Ghostscript 9.26', 'creator': '', 'creationdate': '2022-06-06T23:09:49-07:00', 'moddate': '2022-06-06T23:09:49-07:00', 'title': '', 'author': '', 'subject': '', 'keywords': '', 'source': '/home/rstudio/data/hwc/Kavcic et al. 2013.pdf', 'total_pages': 9, 'page': 7, 'page_label': '8'}, page_content='(Analysis of gut contents of bears ( Ursus arctos L.)\\ntaken from the wild in Slovenia from 2006–2008.)\\nBiotechnical Faculty, University of Ljubljana, Ljubl-\\njana, Slovenia. (In Slovenian.)\\n118 S UPPLEMENTAL FEEDING AND BEAR DEPREDATIONS N Kavcˇicˇ et al.\\nUrsus 24(2):111–119 (2013)')],\n",
       " 'answer': 'You can try using electric fences or beehive fences as deterrents, as they have been shown to be effective in preventing crop raiding by elephants and may also work for deer. Additionally, using sonic deterrents or plant volatiles may also be a cost-effective option to explore.'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rag_chain.invoke({\"input\": \"We have major issues with deer raiding our large agricultural fields. Is there anything I can try to prevent this that won’t break the bank?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "07b9578c-9a89-4874-a34d-30a060ed3407",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'We live in a suburban area and bears sometimes come into our town to eat from our fruit trees and trash. What are the best ways for us to prevent this as a community? We don’t want to have to get rid of our fruit trees…',\n",
       " 'context': [Document(id='d17e3a26-e842-4e14-94d0-68e8bf12c897', metadata={'producer': 'pdfTeX-1.40.21', 'creator': 'LaTeX with hyperref', 'creationdate': '2022-06-23T12:24:35+08:00', 'author': 'Lydia N. Tiller, Ernest Oniba, Godfrey Opira, Ewan J. Brennan, Lucy E. King, Victor Ndombi, Derick Wanjala and Marion R. Robertson', 'keywords': 'African elephants; human-elephant conflict; crop raiding; olfactory mitigation;elephant repellent', 'moddate': '2022-06-23T06:36:53+02:00', 'subject': 'Human–elephant conflict is increasing across many parts of Asia and Africa. Mitigating elephant crop raiding has become a major focus of conservation intervention, however, many existing methods for tackling this problem are expensive and difficult to execute. Thus, there is a need for more affordable, farm-based methods. Testing these methods is key to ensuring their effectiveness and feasibility. In this study, we tested a novel olfactory deterrent, the “smelly elephant repellent”, a foul-smelling organic liquid, on 40 farms in Uganda and Kenya. Our results show that the repellent was effective at deterring elephants from crop raiding. Over the study period, 82% of 309 elephant crop raids were deterred in Uganda. In Kenya, the repellent deterred 63% of 24 crop raiding incidents, and there was a significant effect of the repellent on test sites compared with control sites. The smelly repellent could be a helpful crop raiding mitigation tool for farmers, as this study showed it to be effective, relatively cheap, quick to produce from locally available ingredients, and communities have a positive attitude towards using it. Ongoing work is exploring the potential for a market-based approach to take this to scale in a financially sustainable way.', 'title': '“Smelly” Elephant Repellent: Assessing the Efficacy of a Novel Olfactory Approach to Mitigating Elephant Crop Raiding in Uganda and Kenya', 'source': '/home/rstudio/data/hwc/Tiller et al. 2022.pdf', 'total_pages': 15, 'page': 6, 'page_label': '7'}, page_content='site was adjacent to the test site and constituted a one-acre plot with crops but no crop\\nraiding deterrents. If an elephant approached the farm at any time, data collectors later\\nvisited the farm and collected the same data as in Uganda, but in addition, they gathered\\ndata such as whether the elephants ate crops in the test site, control site, or both, and\\ninformation about the age of the repellent.\\nAt the end of the study at both sites, we conducted semi-structured interviews with 16\\nof the farmers (eight from Kenya and eight from Uganda) to understand more about their\\nperception of the repellent. Each interview took 30–45 min, and respondents were selected\\nrandomly from the repellent trial farmers.\\n2.5. Data Analysis\\nWe conducted descriptive statistics on the data to calculate the percentage of crop\\nraiding incidents in which the repellent deterred elephants. To compare successful raids\\nbetween the test sites and control sites, we used a Fisher’s exact test, which is useful for'),\n",
       "  Document(id='e2853082-5aac-49ca-83d1-bb9dc8491f8d', metadata={'producer': 'PDF Architect 3', 'creator': 'PDF Architect 3', 'creationdate': '2017-01-25T14:50:41+00:00', 'author': 'V. Pimenta', 'moddate': '2017-01-25T14:52:31+00:00', 'source': '/home/rstudio/data/hwc/Pimenta et al. 2017.pdf', 'total_pages': 20, 'page': 2, 'page_label': '3'}, page_content='age number (log10-transformed) of wolf attacks per farm and per\\nyear (Legendre and Legendre, 1998). Binary and ordinal variables\\nwere coded as for logistic regression. First, we used univariate\\n19V. Pimenta et al. / Biological Conservation 207 (2017) 17–26'),\n",
       "  Document(id='edccf21c-1e8e-4e5d-a370-ec299c7720e1', metadata={'producer': 'iText 4.2.0 by 1T3XT', 'creator': 'John Wiley & Sons', 'creationdate': '2024-01-03T16:15:52+05:30', 'keywords': 'management; Mephitis mephitis; mitigation; Procyon lotor; raccoon; red fox; striped skunk; Vulpes vulpes', 'moddate': '2025-05-27T12:12:05-07:00', 'subject': 'J Wildl Manag 2024.88:e22526', 'wps-proclevel': '3', 'wps-journaldoi': '10.1002/(ISSN)1937-2817', 'author': '', 'title': 'Methods to mitigate human–wildlife conflicts involving common mesopredators: a meta‐analysis', 'wps-articledoi': '10.1002/jwmg.22526', 'source': '/home/rstudio/data/hwc/Lazure and Weladji 2023.pdf', 'total_pages': 21, 'page': 14, 'page_label': '15'}, page_content='Bateman and Fleming 2012,D r a k e2014, Barrett et al. 2019, Schell et al. 2020). Additionally, most\\nstudies come from North America, Australia, and a fe w European countries. Red foxes and raccoons are\\nf o u n di nn u m e r o u sc o u n t r i e sa sn a t i v ea n di n t r o d u c e ds p e c i e s( L o t z ea n dA n d e r s o n1979, Larivière and\\nPasitschniak ‐Arts 1996,H o h m a n ne ta l .2002,I k e d ae ta l .2004,S t o p e2023). This could be due to a language\\nbarrier, but searching French literature did not contribute any additional studies to the meta‐analysis data set.\\nAs i n g l et h e s i sf r o mQ u é b e c( B é l a n g e r‐Smith 2014) and 1 article from France (Lieury et al. 2015)w e r e\\npublished in English.\\nHeterogeneity measures suggest that there is substantial heterogeneity and that all studies do not share a\\ncommon effect size. This level of heterogeneity makes it more difficult to draw overall conclusions (Higgins and'),\n",
       "  Document(id='8a95b965-ea65-4179-b1b2-0db15de34339', metadata={'producer': 'Acrobat Distiller 10.1.8 (Windows)', 'creator': 'Springer', 'creationdate': '2021-06-24T08:47:24+05:30', 'author': 'Robert A. Montgomery', 'crossmarkdomains[1]': 'springer.com', 'crossmarkdomains[2]': 'springerlink.com', 'crossmarkdomainexclusive': 'true', 'crossmarkmajorversiondate': '2010-04-23', 'keywords': 'Coexistence,Conservation,Crop raiding,Elephant,Human-wildlife conflict,Interventions', 'moddate': '2022-01-18T00:16:47+05:30', 'subject': 'Ambio, https://doi.org/10.1007/s13280-021-01587-x', 'title': 'The efficacy of interventions to protect crops from raiding elephants', 'doi': '10.1007/s13280-021-01587-x', 'robots': 'noindex', 'source': '/home/rstudio/data/hwc/Montgomery et al. 2022.pdf', 'total_pages': 12, 'page': 9, 'page_label': '725'}, page_content='Graham, M.D., W.M. Adams, and G.N. Kahiro. 2012. Mobile phone\\ncommunication in effective human elephant–conflict manage-\\nment in Laikipia County, Kenya. Oryx 46: 137–144.\\nGratzer, K., F. Susilo, D. Purnomo, S. Fiedler, and R. Brodschneider.\\n2019. Challenges for beekeeping in Indonesia with autochtho-\\nnous and introduced bees. Bee World 96: 40–44.\\nGray, S.M., C. Booher, D.B. Kramer, K. Elliott, J. Waller, B. Kissui,\\nJ.J. Millspaugh, and R.A. Montgomery. 2020. Research-imple-\\nmentation gap limits the actionability of human-carnivore\\nconflict studies in East Africa. Animal Conservation 23: 7–17.\\nGross, E.M., N. Drouet-Hoguet, N. Subedi, and J. Gross. 2017. The\\npotential of medicinal and aromatic plants (MAPs) to reduce\\ncrop damages by Asian Elephants (Elephas maximus). Crop\\nProtection 100: 29–37.\\nGross, E.M., B.P. Lahkar, N. Subedi, V.R. Nyirenda, L.L. Lichten-\\nfeld, and O. Jakoby. 2019. Does traditional and advanced\\nguarding reduce crop losses due to wildlife? A comparative')],\n",
       " 'answer': 'To prevent bears from visiting your suburban area, consider implementing community-wide measures such as securing trash cans with bear-proof lids and educating residents on proper waste management. Additionally, using electric fencing or repellents around fruit trees can help deter bears without having to remove the trees. Collaborating with local wildlife experts and authorities can also help develop effective strategies to peacefully coexist with bears.'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rag_chain.invoke({\"input\": \"We live in a suburban area and bears sometimes come into our town to eat from our fruit trees and trash. What are the best ways for us to prevent this as a community? We don’t want to have to get rid of our fruit trees…\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ba272b88-1622-4d06-9361-7f1e2ca89e73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'input': 'What cattle husbandry strategies might be helpful to prevent conflict if we live in wolf country?',\n",
       " 'context': [Document(id='8e5f9f92-0821-4c43-bb26-33f927c55cb5', metadata={'producer': 'Adobe PDF Library 15.0; modified using iText® 5.3.5 ©2000-2012 1T3XT BVBA (SPRINGER SBM; licensed version)', 'creator': 'Springer', 'creationdate': '2020-09-14T15:09:33+05:30', 'crossmarkdomains[1]': 'springer.com', 'moddate': '2020-09-14T15:58:07+02:00', 'crossmarkmajorversiondate': '2010-04-23', 'subject': 'Scientific Reports, https://doi.org/10.1038/s41598-020-72343-6', 'author': 'Igor Khorozyan', 'title': 'Variation and conservation implications of the effectiveness of anti-bear interventions', 'crossmarkdomainexclusive': 'true', 'robots': 'noindex', 'doi': '10.1038/s41598-020-72343-6', 'crossmarkdomains[2]': 'springerlink.com', 'source': '/home/rstudio/data/hwc/Khorozyan and Waltert 2020.pdf', 'total_pages': 9, 'page': 2, 'page_label': '3'}, page_content='Guarding animals 3 1\\nMixed techniques 1\\nInvasive management (n = 11) Translocation 6 5\\nLethal control (n = 5) Shooting 3 2\\nNon-invasive management \\n(n = 16)\\nCalving control 1\\nChange of habits 3\\nFood/garbage isolation 3 1\\nLivestock replacement 1\\nSupplemental feeding 2 2\\nVegetation care 1 2'),\n",
       "  Document(id='d8e29472-7c05-404b-ba09-a484af07af07', metadata={'producer': 'pdfTeX-1.40.25', 'creator': 'LaTeX with hyperref', 'creationdate': '2024-03-06T16:58:23+01:00', 'author': 'Justin Raycraft', 'keywords': 'human–carnivore conflict; human–wildlife conflict; human–wildlife coexistence; human dimensions of wildlife; human dimensions of conservation; local ecological knowledge; environmental anthropology; ecological anthropology; anthropology; East Africa; Tanzania; wildlife conservation; spotted hyena', 'moddate': '2024-03-06T17:04:06+01:00', 'subject': 'Interactions between people and large carnivores on shared landscapes can have harmful social and ecological consequences. Human–carnivore coexistence depends on an assemblage of sociological factors including effective management institutions that address the social costs of carnivore conservation and promote tolerance toward wildlife. In East Africa, large carnivores are particularly troublesome for herders who depend on livestock for subsistence and wellbeing. This paper provides an overview of human–hyena conflict in the Tarangire ecosystem of northern Tanzania. It presents descriptive results from a questionnaire survey (n = 1076) administered as part of an anthropological study (2019–2020; 2022; 2023) of human–wildlife interactions across twelve villages inhabited by Maasai agropastoralists. The survey instrument was designed through community-based participatory research methods to convey herder concerns about the impacts of spotted hyenas (Crocuta crocuta) on the livestock economy. Based on the perceptions and local ecological knowledge of Maasai interlocutors, the paper provides an overview of the spatial and temporal patterns of human–hyena interactions. Perceived frequencies of hyena attacks on kraaled livestock were unevenly distributed geographically, with those homesteads surrounding Manyara Ranch most heavily affected. Based on herder-reported livestock losses, the costs of depredation by spotted hyenas across the study area were estimated at approximately USD 904.84 per household per year. Most homesteads lacked fortified bomas and would benefit from the provision of lights and fencing materials to improve kraal structures. The paper’s central finding is that spotted hyenas represent a pressing, everyday concern for local pastoralists. Unsurprisingly, herders despise hyenas and are intolerant of sharing landscapes with them. For carnivore conservation outside protected areas to thrive in Tanzania, conservationists and policy makers must engage more meaningfully with the lived experiences of local herders who bear the brunt of conservation costs on their livelihoods.', 'title': 'Human–Hyena (Crocuta crocuta) Conflict in the Tarangire Ecosystem, Tanzania', 'source': '/home/rstudio/data/hwc/Raycraft 2024.pdf', 'total_pages': 16, 'page': 13, 'page_label': '14'}, page_content='34. Deneke, Y.; Megaze, A.; Tekalegn, W.; Dobamo, T.; Leirs, H. Population estimation and livestock loss by spotted hyena (Crocuta\\ncrocuta) in Damota community managed forest, Southern Ethiopia. Glob. Ecol. Conserv.2022, 34, e02037. [CrossRef]\\n35. Bohm, T.; Höner, O.R. Crocuta crocuta. InThe IUCN Red List of Threatened Species 2015; International Union for Conservation of\\nNature: Gland, Switzerland, 2015. [CrossRef]\\n36. Holekamp, K.E.; Benson-Amram, S. The evolution of intelligence in mammalian carnivores. Interface Focus2017, 7, 20160108.\\n[CrossRef]\\n37. Holekamp, K.E.; Smale, L.; Berg, R.; Cooper, S.M. Hunting rates and hunting success in the spotted hyena (Crocuta crocuta). J.\\nZool. 1997, 242, 1–15. [CrossRef]\\n38. Holekamp, K.E.; Smith, J.E.; Strelioff, C.C.; Van Horn, R.C.; Watts, H.E. Society, demography and genetic structure in the spotted\\nhyena. Mol. Ecol.2012, 21, 613–632. [CrossRef]'),\n",
       "  Document(id='6e842b37-161c-43d6-9631-667d1222b7d0', metadata={'producer': 'PyPDF', 'creator': 'Elsevier', 'creationdate': '2022-06-07T02:40:21+00:00', 'author': 'Charlotte Lorand', 'crossmarkdomains[1]': 'elsevier.com', 'crossmarkdomains[2]': 'sciencedirect.com', 'crossmarkdomainexclusive': 'true', 'crossmarkmajorversiondate': '2010-04-23', 'elsevierwebpdfspecifications': '7.0', 'keywords': 'Human-carnivore coexistence; Lethal control; Non-lethal management; Conservation interventions; Effectiveness; Evidence-based', 'moddate': '2022-06-07T02:40:21+00:00', 'subject': 'Science of the Total Environment, 838 (2022) 156195. doi:10.1016/j.scitotenv.2022.156195', 'title': \"Effectiveness of interventions for managing human-large carnivore conflicts worldwide: Scare them off, don't remove them\", 'doi': '10.1016/j.scitotenv.2022.156195', 'robots': 'noindex', 'source': '/home/rstudio/data/hwc/Lorand et al. 2022.pdf', 'total_pages': 11, 'page': 4, 'page_label': '5'}, page_content='fined success as the durable (preferably months or years before a new oc-\\ncurrence; Khorozyan and Waltert, 2019; Rossler et al., 2012 Shivik et al.,\\n2003; Miller et al., 2016; Bangs et al., 2006) reduction of the negative con-\\nsequences resulting from human-large carnivore co-occurrence after the\\nimplementation of the intervention.\\nDrawing on the evidence and interpretations provided by the authors of\\nthe original case studies, each case was blindly and independently assessed\\nby three out of a panel offive academics specialized in conservation ecol-\\nogy. In this process, in line with the features, evidence and conclusions pro-\\nvided by the original author, each evaluator attributed quantitative scores\\nto indicate the reported effectiveness of the intervention (criterion 1,“Ef-\\nfectiveness”), the evaluated certainty of the conclusions given the quality\\nof evidence provided by the author (criterion 2,“Certainty of evidence”'),\n",
       "  Document(id='b5ed8ae4-1a9c-4dbb-a34c-281b3525f8ef', metadata={'producer': 'GPL Ghostscript 9.26', 'creator': '', 'creationdate': '2022-06-06T23:09:49-07:00', 'moddate': '2022-06-06T23:09:49-07:00', 'title': '', 'author': '', 'subject': '', 'keywords': '', 'source': '/home/rstudio/data/hwc/Kavcic et al. 2013.pdf', 'total_pages': 9, 'page': 5, 'page_label': '6'}, page_content='for anthropogenic food sources such as sheep. In\\nour study area, several radiocollared bears visited\\nfeeding sites and human settlements or pastures\\nduring the same night (Jerina et al. 2012). In spite of\\nhigh abundance of food placed at feeding sites, the\\nnumber of livestock depredations in Slovenia is still\\nrelatively high (Krofel and Jerina 2012). At the same\\nTable 1. Comparison of depredations, sheep numbers, and brown bear numbers before (1999–2003; pre-ban)\\nand after the ban on feeding with carrion (2005–09; post-ban) in Slovenia.\\nVariable\\nPre-ban\\naverage (95 % CI)\\nPost-ban average\\n(95% CI)\\nRatio post-/pre ban\\naverage (95 % CI)\\nNo. sheep killed 245 (174–347) 400 (319–500) 1.64 (1.1–2.5)\\nTotal sheep 95,200 (83,700–104,800) 133,800 (130,300–137,400) 1.41 (1.3–1.6)\\nProportion sheep killed 0.28 % (0.18–0.44) 0.30 % (0.24–0.38) 1.08 (0.6–1.9)\\nTotal bears 389 (357–417) 450 (434–464) 1.16 (1.1–1.3)\\nSheep killed per bear 0.65 (0.44–0.99) 0.89 (0.71–1.10) 1.36 (0.9–2.2)')],\n",
       " 'answer': 'To prevent conflict in wolf country, cattle husbandry strategies such as livestock replacement, change of habits, and food/garbage isolation may be helpful. Additionally, supplemental feeding and vegetation care can also be considered. These non-invasive management techniques can help reduce the likelihood of wolf attacks on livestock.'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt = \"What cattle husbandry strategies might be helpful to prevent conflict if we live in wolf country?\"\n",
    "\n",
    "rag_chain.invoke({\"input\": prompt})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d4d1bf4-4084-430d-8b2d-39ce1d6815db",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d4bf2492-6852-43a7-8527-06ee4e9848c0",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'torch'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mModuleNotFoundError\u001b[39m                       Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[17]\u001b[39m\u001b[32m, line 8\u001b[39m\n\u001b[32m      6\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mqdrant_client\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mmodels\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Distance, VectorParams\n\u001b[32m      7\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mgc\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m8\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mtorch\u001b[39;00m\n\u001b[32m     10\u001b[39m \u001b[38;5;66;03m# Option 1: FAISS (Facebook AI Similarity Search) - Most memory efficient\u001b[39;00m\n\u001b[32m     11\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mcreate_faiss_vectorstore\u001b[39m(splits, embedding, persist_directory=\u001b[33m\"\u001b[39m\u001b[33m./faiss_db\u001b[39m\u001b[33m\"\u001b[39m, batch_size=\u001b[32m100\u001b[39m):\n",
      "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'torch'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_community.vectorstores import Chroma\n",
    "from langchain_community.vectorstores import Qdrant\n",
    "from qdrant_client import QdrantClient\n",
    "from qdrant_client.models import Distance, VectorParams\n",
    "import gc\n",
    "import torch\n",
    "\n",
    "# Option 1: FAISS (Facebook AI Similarity Search) - Most memory efficient\n",
    "def create_faiss_vectorstore(splits, embedding, persist_directory=\"./faiss_db\", batch_size=100):\n",
    "    \"\"\"\n",
    "    Create FAISS vector store with batched processing to minimize GPU RAM usage\n",
    "    \"\"\"\n",
    "    os.makedirs(persist_directory, exist_ok=True)\n",
    "    \n",
    "    # Process documents in batches to avoid GPU memory overflow\n",
    "    vectorstore = None\n",
    "    \n",
    "    for i in range(0, len(splits), batch_size):\n",
    "        batch = splits[i:i + batch_size]\n",
    "        print(f\"Processing batch {i//batch_size + 1}/{(len(splits) + batch_size - 1)//batch_size}\")\n",
    "        \n",
    "        if vectorstore is None:\n",
    "            # Create initial vectorstore with first batch\n",
    "            vectorstore = FAISS.from_documents(\n",
    "                documents=batch,\n",
    "                embedding=embedding\n",
    "            )\n",
    "        else:\n",
    "            # Add subsequent batches to existing vectorstore\n",
    "            batch_vectorstore = FAISS.from_documents(\n",
    "                documents=batch,\n",
    "                embedding=embedding\n",
    "            )\n",
    "            vectorstore.merge_from(batch_vectorstore)\n",
    "            \n",
    "            # Clean up temporary vectorstore\n",
    "            del batch_vectorstore\n",
    "        \n",
    "        # Force garbage collection and clear GPU cache if using CUDA\n",
    "        gc.collect()\n",
    "        if torch.cuda.is_available():\n",
    "            torch.cuda.empty_cache()\n",
    "    \n",
    "    # Save to disk\n",
    "    vectorstore.save_local(persist_directory)\n",
    "    print(f\"Vector store saved to {persist_directory}\")\n",
    "    \n",
    "    return vectorstore\n",
    "\n",
    "def load_faiss_vectorstore(embedding, persist_directory=\"./faiss_db\"):\n",
    "    \"\"\"Load existing FAISS vector store from disk\"\"\"\n",
    "    return FAISS.load_local(\n",
    "        persist_directory,\n",
    "        embedding,\n",
    "        allow_dangerous_deserialization=True  # Only if you trust the source\n",
    "    )\n",
    "\n",
    "# Option 2: Chroma - Persistent SQLite-based storage\n",
    "def create_chroma_vectorstore(splits, embedding, persist_directory=\"./chroma_db\", batch_size=100):\n",
    "    \"\"\"\n",
    "    Create Chroma vector store with batched processing\n",
    "    \"\"\"\n",
    "    # Initialize Chroma with persistence\n",
    "    vectorstore = Chroma(\n",
    "        persist_directory=persist_directory,\n",
    "        embedding_function=embedding\n",
    "    )\n",
    "    \n",
    "    # Add documents in batches\n",
    "    for i in range(0, len(splits), batch_size):\n",
    "        batch = splits[i:i + batch_size]\n",
    "        print(f\"Processing batch {i//batch_size + 1}/{(len(splits) + batch_size - 1)//batch_size}\")\n",
    "        \n",
    "        vectorstore.add_documents(batch)\n",
    "        \n",
    "        # Force garbage collection and clear GPU cache\n",
    "        gc.collect()\n",
    "        if torch.cuda.is_available():\n",
    "            torch.cuda.empty_cache()\n",
    "    \n",
    "    # Persist to disk\n",
    "    vectorstore.persist()\n",
    "    print(f\"Vector store persisted to {persist_directory}\")\n",
    "    \n",
    "    return vectorstore\n",
    "\n",
    "def load_chroma_vectorstore(embedding, persist_directory=\"./chroma_db\"):\n",
    "    \"\"\"Load existing Chroma vector store from disk\"\"\"\n",
    "    return Chroma(\n",
    "        persist_directory=persist_directory,\n",
    "        embedding_function=embedding\n",
    "    )\n",
    "\n",
    "# Option 3: Qdrant - High-performance vector database\n",
    "def create_qdrant_vectorstore(splits, embedding, collection_name=\"documents\", \n",
    "                            path=\"./qdrant_db\", batch_size=100):\n",
    "    \"\"\"\n",
    "    Create Qdrant vector store with local file-based storage\n",
    "    \"\"\"\n",
    "    # Initialize local Qdrant client\n",
    "    client = QdrantClient(path=path)\n",
    "    \n",
    "    # Get embedding dimension (embed a sample text)\n",
    "    sample_embedding = embedding.embed_query(\"sample text\")\n",
    "    embedding_dim = len(sample_embedding)\n",
    "    \n",
    "    # Create collection if it doesn't exist\n",
    "    try:\n",
    "        client.create_collection(\n",
    "            collection_name=collection_name,\n",
    "            vectors_config=VectorParams(size=embedding_dim, distance=Distance.COSINE)\n",
    "        )\n",
    "    except Exception as e:\n",
    "        print(f\"Collection might already exist: {e}\")\n",
    "    \n",
    "    # Create vectorstore\n",
    "    vectorstore = Qdrant(\n",
    "        client=client,\n",
    "        collection_name=collection_name,\n",
    "        embeddings=embedding\n",
    "    )\n",
    "    \n",
    "    # Add documents in batches\n",
    "    for i in range(0, len(splits), batch_size):\n",
    "        batch = splits[i:i + batch_size]\n",
    "        print(f\"Processing batch {i//batch_size + 1}/{(len(splits) + batch_size - 1)//batch_size}\")\n",
    "        \n",
    "        vectorstore.add_documents(batch)\n",
    "        \n",
    "        # Force garbage collection and clear GPU cache\n",
    "        gc.collect()\n",
    "        if torch.cuda.is_available():\n",
    "            torch.cuda.empty_cache()\n",
    "    \n",
    "    print(f\"Vector store created in {path}\")\n",
    "    return vectorstore\n",
    "\n",
    "def load_qdrant_vectorstore(embedding, collection_name=\"documents\", path=\"./qdrant_db\"):\n",
    "    \"\"\"Load existing Qdrant vector store from disk\"\"\"\n",
    "    client = QdrantClient(path=path)\n",
    "    return Qdrant(\n",
    "        client=client,\n",
    "        collection_name=collection_name,\n",
    "        embeddings=embedding\n",
    "    )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cf725ad-69a3-4abd-9907-52427babf6d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Usage examples:\n",
    "\n",
    "# Replace your original code with one of these options:\n",
    "\n",
    "# Option 1: FAISS (Recommended for most use cases)\n",
    "vectorstore = create_faiss_vectorstore(\n",
    "    splits=splits, \n",
    "    embedding=embedding, \n",
    "    persist_directory=\"./my_faiss_db\",\n",
    "    batch_size=50  # Adjust based on your GPU memory\n",
    ")\n",
    "\n",
    "# To load later:\n",
    "# vectorstore = load_faiss_vectorstore(embedding, \"./my_faiss_db\")\n",
    "\n",
    "# Option 2: Chroma (Good for development and moderate scale)\n",
    "# vectorstore = create_chroma_vectorstore(\n",
    "#     splits=splits,\n",
    "#     embedding=embedding,\n",
    "#     persist_directory=\"./my_chroma_db\",\n",
    "#     batch_size=50\n",
    "# )\n",
    "\n",
    "# Option 3: Qdrant (Best for production and very large scale)\n",
    "# vectorstore = create_qdrant_vectorstore(\n",
    "#     splits=splits,\n",
    "#     embedding=embedding,\n",
    "#     collection_name=\"my_documents\",\n",
    "#     path=\"./my_qdrant_db\",\n",
    "#     batch_size=50\n",
    "# )\n",
    "\n",
    "# Memory optimization settings\n",
    "def optimize_gpu_memory():\n",
    "    \"\"\"Additional GPU memory optimization\"\"\"\n",
    "    if torch.cuda.is_available():\n",
    "        # Set memory fraction if needed\n",
    "        torch.cuda.set_per_process_memory_fraction(0.8)  # Use 80% of GPU memory\n",
    "        \n",
    "        # Enable memory mapping for large tensors\n",
    "        torch.backends.cuda.matmul.allow_tf32 = True\n",
    "        torch.backends.cudnn.allow_tf32 = True\n",
    "\n",
    "# Call before processing if you have GPU memory issues\n",
    "# optimize_gpu_memory()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}