Nelly-43 commited on
Commit
d113d61
·
2 Parent(s): d3dee93b82e7da

Merge branch 'main' of https://huggingface.co/spaces/CGIAR/PERI-AI

Browse files
Files changed (3) hide show
  1. app_web.py +3 -1
  2. src/ingest.py +1 -1
  3. src/prompts.py +211 -1
app_web.py CHANGED
@@ -1,5 +1,6 @@
1
  """Streamlit web UI for the RAG Research Chatbot."""
2
  import os
 
3
  import zipfile
4
  import streamlit as st
5
 
@@ -18,10 +19,11 @@ from huggingface_hub import snapshot_download
18
 
19
  snapshot_download(repo_id="CGIAR/peri-kb",
20
  repo_type="dataset",
 
21
  token=os.getenv('HF_TOKEN'),
22
  local_dir="./"
23
  )
24
-
25
  # ── Session initialisation ───────────────────────────────────────────────────
26
 
27
  def init_session():
 
1
  """Streamlit web UI for the RAG Research Chatbot."""
2
  import os
3
+ import shutil
4
  import zipfile
5
  import streamlit as st
6
 
 
19
 
20
  snapshot_download(repo_id="CGIAR/peri-kb",
21
  repo_type="dataset",
22
+ allow_patterns="ug/*",
23
  token=os.getenv('HF_TOKEN'),
24
  local_dir="./"
25
  )
26
+ shutil.copytree("./ug", "./", dirs_exist_ok=True)
27
  # ── Session initialisation ───────────────────────────────────────────────────
28
 
29
  def init_session():
src/ingest.py CHANGED
@@ -6,7 +6,7 @@ from pathlib import Path
6
 
7
  import chromadb
8
  from chromadb.utils import embedding_functions
9
- from sentence_transformers import SentenceTransformer
10
  from tqdm import tqdm
11
 
12
  from src.readers import read_file, READERS
 
6
 
7
  import chromadb
8
  from chromadb.utils import embedding_functions
9
+ # from sentence_transformers import SentenceTransformer
10
  from tqdm import tqdm
11
 
12
  from src.readers import read_file, READERS
src/prompts.py CHANGED
@@ -280,7 +280,217 @@ Output:
280
  USER'S QUESTION: {query}
281
  Return only the JSON object. Do not provide explanations, markdown, or additional text.
282
  """
283
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  def dict_to_string(d, indent=0):
285
  result = ""
286
  spacing = " " * indent
 
280
  USER'S QUESTION: {query}
281
  Return only the JSON object. Do not provide explanations, markdown, or additional text.
282
  """
283
+
284
+ VALUE_CHAIN_PROMPT_TEMPLATE = """\
285
+ You are an expert policy and public expenditure analyst for a {domain} research chatbot.
286
+
287
+ Your task is to review retrieved documents from a knowledge base and extract evidence related to government commitment to a specified agricultural value chain.
288
+
289
+ IMPORTANT RULES
290
+
291
+ 1. Only use information explicitly found in the provided documents.
292
+ 2. Do not infer facts that are not supported by evidence.
293
+ 3. If evidence is not found, return "NOT_FOUND".
294
+ 4. Every extracted value must be accompanied by:
295
+ - supporting quotation
296
+ - source document name
297
+ - page/section reference when available
298
+ 5. Multiple documents may be provided.
299
+ 6. Consolidate information across all documents.
300
+ 7. Return output strictly as valid JSON.
301
+
302
+ --------------------------------------------------------
303
+ TARGET VALUE CHAIN
304
+ --------------------------------------------------------
305
+
306
+ Value Chain: {value_chain}
307
+
308
+ --------------------------------------------------------
309
+ EXTRACTION TASKS
310
+ --------------------------------------------------------
311
+
312
+ STEP 1: FINANCIAL COMMITMENT
313
+
314
+ Search for budget allocations, expenditure allocations, projects, programs, or public investments associated with the value chain.
315
+
316
+ Identify:
317
+
318
+ A. Budget allocation during FY2020/2021 (or nearest baseline year)
319
+
320
+ B. Budget allocation during FY2025/2026
321
+ (or most recent available year)
322
+
323
+ Extract:
324
+
325
+ - budget_start_year
326
+ - budget_start_value
327
+ - budget_end_year
328
+ - budget_end_value
329
+ - currency
330
+ - responsible institution
331
+
332
+ If multiple values are found, report all candidate values and explain differences.
333
+
334
+ --------------------------------------------------------
335
+
336
+ STEP 2: STRATEGIC COMMITMENT
337
+
338
+ Across all supplied policy documents determine:
339
+
340
+ 1. Total number of documents examined
341
+
342
+ 2. Number of documents where the value chain is discussed
343
+
344
+ 3. Whether the value chain is discussed in:
345
+
346
+ a) Most recent National Development Plan
347
+
348
+ b) Most recent Agricultural Development Strategy,
349
+ Agricultural Sector Plan, Agricultural
350
+ Transformation Strategy, or equivalent
351
+
352
+ c) Standalone value chain strategy
353
+
354
+ d) Flagship value chain project
355
+
356
+ For each item provide:
357
+
358
+ - YES
359
+ - NO
360
+ - NOT_FOUND
361
+
362
+ and supporting evidence.
363
+
364
+ Definitions:
365
+
366
+ National Development Plan:
367
+ A country's primary medium-term national development policy document.
368
+
369
+ Agricultural Strategy:
370
+ The most recent agricultural sector strategy, investment plan, agricultural transformation plan, or equivalent.
371
+
372
+ Standalone Value Chain Strategy:
373
+ A policy, roadmap, strategy, investment plan, master plan, or action plan dedicated primarily to the target value chain.
374
+
375
+ Flagship Project:
376
+ A major government-supported, donor-supported, or co-financed project whose main focus is the target value chain.
377
+
378
+ --------------------------------------------------------
379
+
380
+ STEP 3: INSTITUTIONAL COMMITMENT
381
+
382
+ Determine whether the value chain has:
383
+
384
+ A. Dedicated government agency or parastatal
385
+
386
+ B. Dedicated research institute or research center
387
+
388
+ C. Dedicated ministry department,
389
+ directorate, authority, or unit
390
+
391
+ Return:
392
+
393
+ YES
394
+ NO
395
+ NOT_FOUND
396
+
397
+ for each category.
398
+
399
+ --------------------------------------------------------
400
+
401
+ STEP 4: DOCUMENT COVERAGE
402
+
403
+ Compute:
404
+
405
+ documents_examined
406
+
407
+ documents_discussing_value_chain
408
+
409
+ coverage_weight
410
+
411
+ Coverage Weight Formula:
412
+
413
+ documents_discussing_value_chain /
414
+ documents_examined
415
+
416
+ --------------------------------------------------------
417
+
418
+ OUTPUT FORMAT
419
+ --------------------------------------------------------
420
+
421
+ {
422
+ "value_chain": "",
423
+
424
+ "financial_commitment": {
425
+ "budget_start_year": "",
426
+ "budget_start_value": "",
427
+ "budget_end_year": "",
428
+ "budget_end_value": "",
429
+ "currency": "",
430
+ "responsible_institution": "",
431
+ "evidence": [
432
+ {
433
+ "quote": "",
434
+ "source_document": "",
435
+ "page_or_section": ""
436
+ }
437
+ ]
438
+ },
439
+
440
+ "strategic_commitment": {
441
+ "documents_examined": 0,
442
+ "documents_discussing_value_chain": 0,
443
+ "coverage_weight": 0,
444
+
445
+ "development_plan": {
446
+ "value": "YES|NO|NOT_FOUND",
447
+ "evidence": []
448
+ },
449
+
450
+ "agricultural_strategy": {
451
+ "value": "YES|NO|NOT_FOUND",
452
+ "evidence": []
453
+ },
454
+
455
+ "standalone_vc_strategy": {
456
+ "value": "YES|NO|NOT_FOUND",
457
+ "evidence": []
458
+ },
459
+
460
+ "flagship_project": {
461
+ "value": "YES|NO|NOT_FOUND",
462
+ "evidence": []
463
+ }
464
+ },
465
+
466
+ "institutional_commitment": {
467
+ "dedicated_agency": {
468
+ "value": "YES|NO|NOT_FOUND",
469
+ "evidence": []
470
+ },
471
+
472
+ "dedicated_research_institute": {
473
+ "value": "YES|NO|NOT_FOUND",
474
+ "evidence": []
475
+ },
476
+
477
+ "dedicated_ministerial_department": {
478
+ "value": "YES|NO|NOT_FOUND",
479
+ "evidence": []
480
+ }
481
+ }
482
+ }
483
+
484
+ """
485
+
486
+ def build_vc_prompt(value_chain:str,
487
+ domain:str
488
+ ):
489
+ return VALUE_CHAIN_PROMPT_TEMPLATE.format(
490
+ domain=domain,
491
+ value_chain=value_chain,
492
+ )
493
+
494
  def dict_to_string(d, indent=0):
495
  result = ""
496
  spacing = " " * indent