Spaces:
Sleeping
Sleeping
update value chain prompts + output verification
Browse files- app_web.py +34 -17
- src/prompts.py +467 -1
- src/verifier.py +4 -3
app_web.py
CHANGED
|
@@ -16,7 +16,7 @@ from src.stata import explain_
|
|
| 16 |
from src.peri import APPROVED_OPTIONS
|
| 17 |
from src.peri.value_chains import categorize_vc_production, analyze_committment_to_vc , analyze_vc_committment
|
| 18 |
from src.peri.investments import analyze_investments
|
| 19 |
-
from src.prompts import dict_to_string
|
| 20 |
# from ui import render_db
|
| 21 |
from huggingface_hub import snapshot_download
|
| 22 |
|
|
@@ -128,23 +128,40 @@ def render_landing_page():
|
|
| 128 |
|
| 129 |
c2vc = {}
|
| 130 |
for vc in pol_geo_ref["value_chain"].unique():
|
| 131 |
-
status.update(label=f'Analyzing committment to {vc} production...')
|
| 132 |
prompt = f"Look up and retrieve all relevant information on {vc} production in {country} from the knowledgebase."
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
status.update(
|
| 150 |
label=f"Completed metadata preparation for {pol_geo_scores.shape[0]} value chains and {investments.shape[0]} investment areas in {country}.",
|
|
|
|
| 16 |
from src.peri import APPROVED_OPTIONS
|
| 17 |
from src.peri.value_chains import categorize_vc_production, analyze_committment_to_vc , analyze_vc_committment
|
| 18 |
from src.peri.investments import analyze_investments
|
| 19 |
+
from src.prompts import dict_to_string, VC_RAG
|
| 20 |
# from ui import render_db
|
| 21 |
from huggingface_hub import snapshot_download
|
| 22 |
|
|
|
|
| 128 |
|
| 129 |
c2vc = {}
|
| 130 |
for vc in pol_geo_ref["value_chain"].unique():
|
|
|
|
| 131 |
prompt = f"Look up and retrieve all relevant information on {vc} production in {country} from the knowledgebase."
|
| 132 |
+
status.update(label=f'Analyzing committment to {vc} production...')
|
| 133 |
+
# for it in iterations:
|
| 134 |
+
retrieval_results = {}
|
| 135 |
+
for var, txt in VC_RAG.items():
|
| 136 |
+
# status.update(label=f'Analyzing {var.replace("_", " ")} to {vc} production...')
|
| 137 |
+
rag_prompt = txt.format(value_chain=vc)
|
| 138 |
+
qu_result = understand_query(rag_prompt, cfg)
|
| 139 |
+
|
| 140 |
+
search_query = qu_result.get("search_query", rag_prompt)
|
| 141 |
+
display_query = qu_result.get("display_query", rag_prompt)
|
| 142 |
+
sql_query = qu_result.get("sql_query")
|
| 143 |
+
print(sql_query)
|
| 144 |
+
|
| 145 |
+
if var=="strategic_commitment":
|
| 146 |
+
retrieval_result = retrieve(rag_prompt,
|
| 147 |
+
cfg,
|
| 148 |
+
route="both",
|
| 149 |
+
sql_query=sql_query,
|
| 150 |
+
db_filter=cfg["policy_docs"])
|
| 151 |
+
retrieval_result["policy_docs"] = set([os.path.basename(r["metadata"]["source"])
|
| 152 |
+
for r in retrieval_result["db_results"]])
|
| 153 |
+
|
| 154 |
+
else:
|
| 155 |
+
retrieval_result = retrieve(rag_prompt, cfg, route="both", sql_query=sql_query)
|
| 156 |
+
|
| 157 |
+
retrieval_result["var"] = var
|
| 158 |
+
output = verify_and_respond(
|
| 159 |
+
prompt, retrieval_result, cfg, vc, prompt
|
| 160 |
+
)
|
| 161 |
+
retrieval_results[var] = _parse_vc_analyzer_result(output["response"])
|
| 162 |
+
c2vc[vc] = retrieval_results
|
| 163 |
+
# retrieval_results.append(output)
|
| 164 |
+
# c2vc.update(_parse_vc_analyzer_result(output["response"]))
|
| 165 |
|
| 166 |
status.update(
|
| 167 |
label=f"Completed metadata preparation for {pol_geo_scores.shape[0]} value chains and {investments.shape[0]} investment areas in {country}.",
|
src/prompts.py
CHANGED
|
@@ -84,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:Dict,
|
| 520 |
retrieval_result:Dict,
|
| 521 |
value_chain:str,
|
|
@@ -653,6 +1117,7 @@ def build_verification_prompt(
|
|
| 653 |
context: str,
|
| 654 |
phrase_flags: list[str],
|
| 655 |
similarity_flags: list[str],
|
|
|
|
| 656 |
) -> str:
|
| 657 |
"""Build the verification prompt for auditing an AI response.
|
| 658 |
|
|
@@ -665,6 +1130,7 @@ def build_verification_prompt(
|
|
| 665 |
Returns:
|
| 666 |
Formatted verification prompt string.
|
| 667 |
"""
|
|
|
|
| 668 |
phrase_str = (
|
| 669 |
"\n".join(f"- {flag}" for flag in phrase_flags)
|
| 670 |
if phrase_flags
|
|
@@ -675,7 +1141,7 @@ def build_verification_prompt(
|
|
| 675 |
if similarity_flags
|
| 676 |
else "No similarity flags detected."
|
| 677 |
)
|
| 678 |
-
return
|
| 679 |
response=response,
|
| 680 |
context=context,
|
| 681 |
phrase_flags=phrase_str,
|
|
|
|
| 84 |
{context}
|
| 85 |
=====================================================================
|
| 86 |
"""
|
| 87 |
+
FC_VERIFICATION_PROMPT_TEMPLATE = """\
|
| 88 |
+
--- RESPONSE TO VERIFY ---
|
| 89 |
+
{response}
|
| 90 |
+
--- END RESPONSE ---
|
| 91 |
+
|
| 92 |
+
--- CONTEXT ---
|
| 93 |
+
{context}
|
| 94 |
+
--- END CONTEXT ---
|
| 95 |
+
|
| 96 |
+
--- PHRASE-LEVEL FLAGS ---
|
| 97 |
+
{phrase_flags}
|
| 98 |
+
--- END PHRASE FLAGS ---
|
| 99 |
+
|
| 100 |
+
--- SIMILARITY FLAGS ---
|
| 101 |
+
{similarity_flags}
|
| 102 |
+
--- END SIMILARITY FLAGS ---
|
| 103 |
+
|
| 104 |
+
The response should be a Python-style list of dictionaries matching the required output format.
|
| 105 |
+
|
| 106 |
+
Run through this 12-point verification checklist and report results:
|
| 107 |
+
|
| 108 |
+
1. Output Structure
|
| 109 |
+
- Is the response a list?
|
| 110 |
+
- Does every list item contain the required fields:
|
| 111 |
+
- budget_year
|
| 112 |
+
- budget_value
|
| 113 |
+
- currency
|
| 114 |
+
- responsible_institution
|
| 115 |
+
- evidence
|
| 116 |
+
|
| 117 |
+
2. Evidence Structure
|
| 118 |
+
- Is the evidence field a list?
|
| 119 |
+
- Does every evidence item contain:
|
| 120 |
+
- quote
|
| 121 |
+
- source_document
|
| 122 |
+
- page_or_section
|
| 123 |
+
|
| 124 |
+
3. Required Field Completeness
|
| 125 |
+
- Are all extracted fields populated when information is available in the context?
|
| 126 |
+
- Are values left blank only when the information cannot be found?
|
| 127 |
+
|
| 128 |
+
4. Context Support
|
| 129 |
+
- For every extracted budget_year, budget_value, currency, and responsible_institution value, can supporting evidence be found in the provided context?
|
| 130 |
+
|
| 131 |
+
5. Evidence Alignment
|
| 132 |
+
- Does each evidence quote directly support the candidate record it is attached to?
|
| 133 |
+
- Verify that quoted text contains or clearly supports the extracted values.
|
| 134 |
+
|
| 135 |
+
6. Quote Accuracy
|
| 136 |
+
- Are all evidence quotes reproduced exactly from the context?
|
| 137 |
+
- Flag paraphrased, modified, or fabricated quotations.
|
| 138 |
+
|
| 139 |
+
7. Hallucinated Information
|
| 140 |
+
- Are any extracted values absent from the context?
|
| 141 |
+
- Are any responsible institutions, budget values, years, currencies, or program details inferred without evidence?
|
| 142 |
+
|
| 143 |
+
8. Candidate Coverage
|
| 144 |
+
- If multiple candidate values are present in the context, were all relevant candidate records extracted?
|
| 145 |
+
- Flag omitted candidate values that should reasonably have been included.
|
| 146 |
+
|
| 147 |
+
9. Temporal Coverage
|
| 148 |
+
- Does the response contain:
|
| 149 |
+
A. At least one budget allocation occurring five or more years before the current date, if such information exists in the context?
|
| 150 |
+
B. The most recent budget allocation available in the context?
|
| 151 |
+
- Flag cases where qualifying records exist but were omitted.
|
| 152 |
+
|
| 153 |
+
10. Data Consistency
|
| 154 |
+
- Do budget_year, budget_value, currency, and responsible_institution correspond to the same allocation, project, investment, or financing record?
|
| 155 |
+
- Flag records that incorrectly combine values from different sources or contexts.
|
| 156 |
+
|
| 157 |
+
11. Phrase-Level Flags
|
| 158 |
+
- Are any phrase-level flags confirmed by the context review?
|
| 159 |
+
- Distinguish between true hallucinations and supported content.
|
| 160 |
+
|
| 161 |
+
12. Similarity Flags
|
| 162 |
+
- Do similarity flags indicate unsupported semantic drift between the response and source evidence?
|
| 163 |
+
- Flag extracted values that do not accurately reflect source meaning.
|
| 164 |
+
|
| 165 |
+
Severity Guidance:
|
| 166 |
+
- High: hallucinated values, unsupported extractions, incorrect quotes, invalid structure, missing key candidate records, or incorrect temporal selection.
|
| 167 |
+
- Medium: incomplete extraction, weak evidence linkage, inconsistent field population, or partial coverage.
|
| 168 |
+
- Low: formatting issues, minor omissions in evidence metadata, or non-critical organizational issues.
|
| 169 |
+
|
| 170 |
+
Return your analysis as JSON in the following format:
|
| 171 |
+
|
| 172 |
+
{{
|
| 173 |
+
"errors": [
|
| 174 |
+
{{
|
| 175 |
+
"check": 1,
|
| 176 |
+
"description": "...",
|
| 177 |
+
"severity": "high|medium|low"
|
| 178 |
+
}}
|
| 179 |
+
],
|
| 180 |
+
"error_count": 0,
|
| 181 |
+
"pass": true,
|
| 182 |
+
"summary": "Brief overall assessment"
|
| 183 |
+
}}
|
| 184 |
+
|
| 185 |
+
Additional Rules:
|
| 186 |
+
- Consider only evidence present in the supplied context.
|
| 187 |
+
- Do not penalize blank values when the information is genuinely unavailable in the context.
|
| 188 |
+
- A candidate record passes verification if every populated field is supported by the attached evidence.
|
| 189 |
+
- If the response contains no valid supporting evidence for a populated field, treat this as a hallucination.
|
| 190 |
+
- If no errors are found, return:
|
| 191 |
+
- "errors": []
|
| 192 |
+
- "error_count": 0
|
| 193 |
+
- "pass": true
|
| 194 |
+
"""
|
| 195 |
+
|
| 196 |
+
SC_VERIFICATION_PROMPT_TEMPLATE = """\
|
| 197 |
+
--- RESPONSE TO VERIFY ---
|
| 198 |
+
{response}
|
| 199 |
+
--- END RESPONSE ---
|
| 200 |
+
|
| 201 |
+
--- CONTEXT ---
|
| 202 |
+
{context}
|
| 203 |
+
--- END CONTEXT ---
|
| 204 |
+
|
| 205 |
+
--- PHRASE-LEVEL FLAGS ---
|
| 206 |
+
{phrase_flags}
|
| 207 |
+
--- END PHRASE FLAGS ---
|
| 208 |
+
|
| 209 |
+
--- SIMILARITY FLAGS ---
|
| 210 |
+
{similarity_flags}
|
| 211 |
+
--- END SIMILARITY FLAGS ---
|
| 212 |
+
|
| 213 |
+
The response should be a dictionary containing the following keys:
|
| 214 |
+
|
| 215 |
+
- development_plan
|
| 216 |
+
- agricultural_strategy
|
| 217 |
+
- standalone_strategy
|
| 218 |
+
- flagship_project
|
| 219 |
+
|
| 220 |
+
Each key should contain a list of document records.
|
| 221 |
+
|
| 222 |
+
Run through this 13-point verification checklist and report results:
|
| 223 |
+
|
| 224 |
+
1. Output Structure
|
| 225 |
+
- Is the response a dictionary/object?
|
| 226 |
+
- Does it contain all required top-level keys?
|
| 227 |
+
- development_plan
|
| 228 |
+
- agricultural_strategy
|
| 229 |
+
- standalone_strategy
|
| 230 |
+
- flagship_project
|
| 231 |
+
|
| 232 |
+
2. Record Structure
|
| 233 |
+
- Does every document record contain:
|
| 234 |
+
- quote
|
| 235 |
+
- document
|
| 236 |
+
- page_or_section
|
| 237 |
+
- year
|
| 238 |
+
|
| 239 |
+
3. Required Field Completeness
|
| 240 |
+
- Are all fields populated when information is available in the context?
|
| 241 |
+
- Are values left blank only when information cannot be found?
|
| 242 |
+
|
| 243 |
+
4. Context Support
|
| 244 |
+
- Can every returned document be located in the provided context?
|
| 245 |
+
- Verify that the document name, publication year, and quote originate from the source material.
|
| 246 |
+
|
| 247 |
+
5. Quote Accuracy
|
| 248 |
+
- Are all quotes reproduced exactly from the source?
|
| 249 |
+
- Flag any paraphrased, altered, truncated, or fabricated quotations.
|
| 250 |
+
|
| 251 |
+
6. Mention Verification
|
| 252 |
+
- Does every quote contain or directly support a mention of the target value chain?
|
| 253 |
+
- Flag documents included without evidence of value-chain relevance.
|
| 254 |
+
|
| 255 |
+
7. Document Classification Accuracy
|
| 256 |
+
- Is each document assigned to the correct category?
|
| 257 |
+
|
| 258 |
+
Development Plan:
|
| 259 |
+
- National medium-term development plan.
|
| 260 |
+
- Country-wide development framework.
|
| 261 |
+
|
| 262 |
+
Agricultural Strategy:
|
| 263 |
+
- Agriculture-specific strategy, investment plan,
|
| 264 |
+
transformation plan, or equivalent.
|
| 265 |
+
|
| 266 |
+
Standalone Strategy:
|
| 267 |
+
- Strategy primarily dedicated to the target value chain.
|
| 268 |
+
|
| 269 |
+
Flagship Project:
|
| 270 |
+
- Major donor-supported initiative, project,
|
| 271 |
+
or program focused on the value chain.
|
| 272 |
+
|
| 273 |
+
Flag documents assigned to incorrect categories.
|
| 274 |
+
|
| 275 |
+
8. Duplicate Document Detection
|
| 276 |
+
- Has the same document been repeated multiple times
|
| 277 |
+
within a category?
|
| 278 |
+
- Flag unnecessary duplicates.
|
| 279 |
+
|
| 280 |
+
9. Coverage Completeness
|
| 281 |
+
- Were all qualifying documents identified from the context?
|
| 282 |
+
- Flag relevant documents that were omitted.
|
| 283 |
+
|
| 284 |
+
10. Recency Prioritization
|
| 285 |
+
- When multiple qualifying documents exist,
|
| 286 |
+
does the response appropriately prioritize
|
| 287 |
+
documents published within the last five years?
|
| 288 |
+
- Flag if older documents are included while more recent
|
| 289 |
+
qualifying documents were ignored.
|
| 290 |
+
|
| 291 |
+
11. Hallucinated Information
|
| 292 |
+
- Are any document names, years, categories,
|
| 293 |
+
projects, or quotations absent from the context?
|
| 294 |
+
- Flag any unsupported additions.
|
| 295 |
+
|
| 296 |
+
12. Phrase-Level Flags
|
| 297 |
+
- Are any phrase-level flags confirmed by review of the context?
|
| 298 |
+
- Distinguish between genuine hallucinations and supported content.
|
| 299 |
+
|
| 300 |
+
13. Similarity Flags
|
| 301 |
+
- Do similarity flags indicate semantic drift between
|
| 302 |
+
extracted information and the source material?
|
| 303 |
+
- Flag cases where the meaning of the source has been altered.
|
| 304 |
+
|
| 305 |
+
Severity Guidance:
|
| 306 |
+
|
| 307 |
+
- High:
|
| 308 |
+
- Hallucinated documents
|
| 309 |
+
- Hallucinated quotes
|
| 310 |
+
- Incorrect category assignment
|
| 311 |
+
- Unsupported value-chain relevance
|
| 312 |
+
- Missing major qualifying documents
|
| 313 |
+
- Invalid output structure
|
| 314 |
+
|
| 315 |
+
- Medium:
|
| 316 |
+
- Incomplete document coverage
|
| 317 |
+
- Missing years
|
| 318 |
+
- Missing location information
|
| 319 |
+
- Weak evidence linkage
|
| 320 |
+
- Failure to prioritize recent documents
|
| 321 |
+
|
| 322 |
+
- Low:
|
| 323 |
+
- Duplicate records
|
| 324 |
+
- Minor formatting issues
|
| 325 |
+
- Missing non-critical metadata
|
| 326 |
+
|
| 327 |
+
Return your analysis as JSON in the following format:
|
| 328 |
+
|
| 329 |
+
{{
|
| 330 |
+
"errors": [
|
| 331 |
+
{{
|
| 332 |
+
"check": 1,
|
| 333 |
+
"description": "...",
|
| 334 |
+
"severity": "high|medium|low"
|
| 335 |
+
}}
|
| 336 |
+
],
|
| 337 |
+
"error_count": 0,
|
| 338 |
+
"pass": true,
|
| 339 |
+
"summary": "Brief overall assessment"
|
| 340 |
+
}}
|
| 341 |
+
|
| 342 |
+
Additional Rules:
|
| 343 |
+
|
| 344 |
+
- Consider only evidence available in the supplied context.
|
| 345 |
+
- Do not penalize empty categories if no qualifying documents exist in the context.
|
| 346 |
+
- A document record passes verification only if its quote provides direct support for its inclusion.
|
| 347 |
+
- If a document is included without supporting evidence, treat it as a hallucination.
|
| 348 |
+
- If a document clearly belongs in a different category, record a classification error.
|
| 349 |
+
- If no errors are found, return:
|
| 350 |
+
- "errors": []
|
| 351 |
+
- "error_count": 0
|
| 352 |
+
- "pass": true
|
| 353 |
+
"""
|
| 354 |
+
|
| 355 |
+
IC_VERIFICATION_PROMPT_TEMPLATE = """\
|
| 356 |
+
--- RESPONSE TO VERIFY ---
|
| 357 |
+
{response}
|
| 358 |
+
--- END RESPONSE ---
|
| 359 |
+
|
| 360 |
+
--- CONTEXT ---
|
| 361 |
+
{context}
|
| 362 |
+
--- END CONTEXT ---
|
| 363 |
+
|
| 364 |
+
--- PHRASE-LEVEL FLAGS ---
|
| 365 |
+
{phrase_flags}
|
| 366 |
+
--- END PHRASE FLAGS ---
|
| 367 |
+
|
| 368 |
+
--- SIMILARITY FLAGS ---
|
| 369 |
+
{similarity_flags}
|
| 370 |
+
--- END SIMILARITY FLAGS ---
|
| 371 |
+
|
| 372 |
+
The response should be a dictionary containing the following keys:
|
| 373 |
+
|
| 374 |
+
- dedicated_agency
|
| 375 |
+
- dedicated_research_institute
|
| 376 |
+
- dedicated_ministerial_department
|
| 377 |
+
|
| 378 |
+
Each key should contain a list of organizational records.
|
| 379 |
+
|
| 380 |
+
Run through this 13-point verification checklist and report results:
|
| 381 |
+
|
| 382 |
+
1. Output Structure
|
| 383 |
+
- Is the response a dictionary/object?
|
| 384 |
+
- Does it contain all required top-level keys?
|
| 385 |
+
- dedicated_agency
|
| 386 |
+
- dedicated_research_institute
|
| 387 |
+
- dedicated_ministerial_department
|
| 388 |
+
|
| 389 |
+
2. Record Structure
|
| 390 |
+
- Does every record contain the required fields?
|
| 391 |
+
|
| 392 |
+
dedicated_agency:
|
| 393 |
+
- agency_name
|
| 394 |
+
- document
|
| 395 |
+
- page_or_section
|
| 396 |
+
- quote
|
| 397 |
+
|
| 398 |
+
dedicated_research_institute:
|
| 399 |
+
- institution_name
|
| 400 |
+
- document
|
| 401 |
+
- page_or_section
|
| 402 |
+
- quote
|
| 403 |
+
|
| 404 |
+
dedicated_ministerial_department:
|
| 405 |
+
- department_name
|
| 406 |
+
- document
|
| 407 |
+
- page_or_section
|
| 408 |
+
- quote
|
| 409 |
+
|
| 410 |
+
3. Required Field Completeness
|
| 411 |
+
- Are all fields populated when information is available in the context?
|
| 412 |
+
- Are values left blank only when information cannot be determined from the context?
|
| 413 |
+
|
| 414 |
+
4. Context Support
|
| 415 |
+
- Can every organization name be verified from the provided context?
|
| 416 |
+
- Can the cited document and page/section be located in the context?
|
| 417 |
+
|
| 418 |
+
5. Quote Accuracy
|
| 419 |
+
- Are all quotes reproduced exactly from the source material?
|
| 420 |
+
- Flag paraphrased, altered, incomplete, or fabricated quotations.
|
| 421 |
+
|
| 422 |
+
6. Organizational Relevance
|
| 423 |
+
- Does each quote directly indicate that the organization has responsibility for, oversight of, coordination of, regulation of, research on, or support for the target value chain?
|
| 424 |
+
- Flag organizations included without clear value-chain relevance.
|
| 425 |
+
|
| 426 |
+
7. Category Assignment Accuracy
|
| 427 |
+
- Is each organization assigned to the correct category?
|
| 428 |
+
|
| 429 |
+
Dedicated Agency:
|
| 430 |
+
- Government agency, authority, board, corporation, commission,
|
| 431 |
+
parastatal, or similar public institution dedicated to the value chain.
|
| 432 |
+
|
| 433 |
+
Dedicated Research Institute:
|
| 434 |
+
- Research institute, research center, laboratory,
|
| 435 |
+
or specialized research organization dedicated to the value chain.
|
| 436 |
+
|
| 437 |
+
Dedicated Ministerial Department:
|
| 438 |
+
- Ministry department, directorate, unit, division,
|
| 439 |
+
board, council, authority, or administrative office
|
| 440 |
+
responsible for the value chain.
|
| 441 |
+
|
| 442 |
+
Flag organizations assigned to incorrect categories.
|
| 443 |
+
|
| 444 |
+
8. Evidence Strength
|
| 445 |
+
- Does the quote provide direct evidence of the organization's mandate,
|
| 446 |
+
role, or dedicated focus?
|
| 447 |
+
- Flag records where organizational relevance is inferred
|
| 448 |
+
without explicit supporting language.
|
| 449 |
+
|
| 450 |
+
9. Duplicate Entity Detection
|
| 451 |
+
- Has the same organization been returned multiple times
|
| 452 |
+
within the same category?
|
| 453 |
+
- Flag unnecessary duplicate entries.
|
| 454 |
+
|
| 455 |
+
10. Coverage Completeness
|
| 456 |
+
- Were all qualifying organizations identified from the context?
|
| 457 |
+
- Flag relevant agencies, institutes, departments,
|
| 458 |
+
authorities, boards, councils, or units that were omitted.
|
| 459 |
+
|
| 460 |
+
11. Hallucinated Information
|
| 461 |
+
- Are any organization names, document names,
|
| 462 |
+
quotations, or classifications absent from the context?
|
| 463 |
+
- Flag unsupported additions.
|
| 464 |
+
|
| 465 |
+
12. Phrase-Level Flags
|
| 466 |
+
- Are any phrase-level flags confirmed by review of the context?
|
| 467 |
+
- Distinguish between genuine hallucinations and supported content.
|
| 468 |
+
|
| 469 |
+
13. Similarity Flags
|
| 470 |
+
- Do similarity flags indicate semantic drift between
|
| 471 |
+
extracted information and the source material?
|
| 472 |
+
- Flag records where the extracted organizational role
|
| 473 |
+
differs from what the source actually states.
|
| 474 |
+
|
| 475 |
+
Severity Guidance:
|
| 476 |
+
|
| 477 |
+
- High:
|
| 478 |
+
- Hallucinated organizations
|
| 479 |
+
- Hallucinated quotes
|
| 480 |
+
- Unsupported value-chain relevance
|
| 481 |
+
- Incorrect category assignment
|
| 482 |
+
- Missing major qualifying organizations
|
| 483 |
+
- Invalid output structure
|
| 484 |
+
|
| 485 |
+
- Medium:
|
| 486 |
+
- Weak supporting evidence
|
| 487 |
+
- Missing organization names
|
| 488 |
+
- Missing document locations
|
| 489 |
+
- Incomplete extraction coverage
|
| 490 |
+
- Ambiguous classification
|
| 491 |
+
|
| 492 |
+
- Low:
|
| 493 |
+
- Duplicate entries
|
| 494 |
+
- Formatting issues
|
| 495 |
+
- Minor metadata omissions
|
| 496 |
+
|
| 497 |
+
Special Verification Rules:
|
| 498 |
+
|
| 499 |
+
- An organization should only be included if the source provides evidence
|
| 500 |
+
that it is specifically responsible for, focused on, mandated for,
|
| 501 |
+
or significantly engaged with the target value chain.
|
| 502 |
+
|
| 503 |
+
- Do not assume an organization is dedicated to the value chain simply because
|
| 504 |
+
it operates within the broader agriculture sector.
|
| 505 |
+
|
| 506 |
+
- General ministries (e.g., Ministry of Agriculture) should not automatically
|
| 507 |
+
qualify as dedicated ministerial departments unless a specific department,
|
| 508 |
+
directorate, unit, authority, board, or council connected to the value chain
|
| 509 |
+
is identified.
|
| 510 |
+
|
| 511 |
+
- Research institutes should only be included when there is evidence of a
|
| 512 |
+
dedicated value-chain focus or mandate.
|
| 513 |
+
|
| 514 |
+
- If the same organization appears in multiple categories, verify that the
|
| 515 |
+
source material genuinely supports each classification.
|
| 516 |
+
Otherwise record a classification error.
|
| 517 |
+
|
| 518 |
+
- Consider only evidence available in the supplied context.
|
| 519 |
+
|
| 520 |
+
- If no qualifying organizations are present in the context,
|
| 521 |
+
empty lists are valid and should not be treated as errors.
|
| 522 |
+
|
| 523 |
+
- If a record lacks supporting evidence, treat it as a hallucination.
|
| 524 |
+
|
| 525 |
+
Return your analysis as JSON in the following format:
|
| 526 |
+
|
| 527 |
+
{{
|
| 528 |
+
"errors": [
|
| 529 |
+
{{
|
| 530 |
+
"check": 1,
|
| 531 |
+
"description": "...",
|
| 532 |
+
"severity": "high|medium|low"
|
| 533 |
+
}}
|
| 534 |
+
],
|
| 535 |
+
"error_count": 0,
|
| 536 |
+
"pass": true,
|
| 537 |
+
"summary": "Brief overall assessment"
|
| 538 |
+
}}
|
| 539 |
+
|
| 540 |
+
If no errors are found, return:
|
| 541 |
+
- "errors": []
|
| 542 |
+
- "error_count": 0
|
| 543 |
+
- "pass": true
|
| 544 |
+
"""
|
| 545 |
|
| 546 |
VERIFICATION_PROMPT_TEMPLATE = """\
|
| 547 |
--- RESPONSE TO VERIFY ---
|
|
|
|
| 974 |
"strategic_commitment":STRATEGIC_COMMITMENT_BLOCK,
|
| 975 |
"institutional_commitment":INSTITUTIONAL_COMMITMENT_BLOCK,
|
| 976 |
}
|
| 977 |
+
|
| 978 |
+
VC_VERIFY = {
|
| 979 |
+
"financial_commitment":FC_VERIFICATION_PROMPT_TEMPLATE,
|
| 980 |
+
"strategic_commitment":SC_VERIFICATION_PROMPT_TEMPLATE,
|
| 981 |
+
"institutional_commitment":IC_VERIFICATION_PROMPT_TEMPLATE,
|
| 982 |
+
}
|
| 983 |
def build_vc_analyzer_prompt(cfg:Dict,
|
| 984 |
retrieval_result:Dict,
|
| 985 |
value_chain:str,
|
|
|
|
| 1117 |
context: str,
|
| 1118 |
phrase_flags: list[str],
|
| 1119 |
similarity_flags: list[str],
|
| 1120 |
+
commitment_var:str,
|
| 1121 |
) -> str:
|
| 1122 |
"""Build the verification prompt for auditing an AI response.
|
| 1123 |
|
|
|
|
| 1130 |
Returns:
|
| 1131 |
Formatted verification prompt string.
|
| 1132 |
"""
|
| 1133 |
+
verification_prompt_template = VC_VERIFY[commitment_var]
|
| 1134 |
phrase_str = (
|
| 1135 |
"\n".join(f"- {flag}" for flag in phrase_flags)
|
| 1136 |
if phrase_flags
|
|
|
|
| 1141 |
if similarity_flags
|
| 1142 |
else "No similarity flags detected."
|
| 1143 |
)
|
| 1144 |
+
return verification_prompt_template.format(
|
| 1145 |
response=response,
|
| 1146 |
context=context,
|
| 1147 |
phrase_flags=phrase_str,
|
src/verifier.py
CHANGED
|
@@ -440,7 +440,7 @@ def verify_and_respond(
|
|
| 440 |
# Layer 3: LLM-as-verifier
|
| 441 |
all_sim_flags = sim_flag_strs + citation_flag_strs
|
| 442 |
verification_prompt = build_verification_prompt(
|
| 443 |
-
response, context, phrase_flag_strs, all_sim_flags,
|
| 444 |
)
|
| 445 |
try:
|
| 446 |
raw_verification = generate(
|
|
@@ -450,7 +450,7 @@ def verify_and_respond(
|
|
| 450 |
problems.You are a strict verification agent. Return only JSON.""",
|
| 451 |
verification_prompt,
|
| 452 |
cfg,
|
| 453 |
-
max_tokens=
|
| 454 |
)
|
| 455 |
except Exception:
|
| 456 |
# Verification LLM failed — skip correction, continue to next iteration
|
|
@@ -502,6 +502,7 @@ def verify_and_respond(
|
|
| 502 |
response, context,
|
| 503 |
[f["message"] for f in phrase_flags],
|
| 504 |
all_sim_final,
|
|
|
|
| 505 |
)
|
| 506 |
try:
|
| 507 |
vr = parse_verification_result(generate(
|
|
@@ -509,7 +510,7 @@ def verify_and_respond(
|
|
| 509 |
You are a verification agent. Your job is to audit the following
|
| 510 |
AI-generated response against the provided context and flag any
|
| 511 |
problems.You are a strict verification agent. Return only JSON.""",
|
| 512 |
-
vp, cfg, max_tokens=
|
| 513 |
))
|
| 514 |
any_verification_ran = True
|
| 515 |
except Exception:
|
|
|
|
| 440 |
# Layer 3: LLM-as-verifier
|
| 441 |
all_sim_flags = sim_flag_strs + citation_flag_strs
|
| 442 |
verification_prompt = build_verification_prompt(
|
| 443 |
+
response, context, phrase_flag_strs, all_sim_flags, retrieval_result["var"],
|
| 444 |
)
|
| 445 |
try:
|
| 446 |
raw_verification = generate(
|
|
|
|
| 450 |
problems.You are a strict verification agent. Return only JSON.""",
|
| 451 |
verification_prompt,
|
| 452 |
cfg,
|
| 453 |
+
max_tokens=8192,
|
| 454 |
)
|
| 455 |
except Exception:
|
| 456 |
# Verification LLM failed — skip correction, continue to next iteration
|
|
|
|
| 502 |
response, context,
|
| 503 |
[f["message"] for f in phrase_flags],
|
| 504 |
all_sim_final,
|
| 505 |
+
retrieval_result["var"],
|
| 506 |
)
|
| 507 |
try:
|
| 508 |
vr = parse_verification_result(generate(
|
|
|
|
| 510 |
You are a verification agent. Your job is to audit the following
|
| 511 |
AI-generated response against the provided context and flag any
|
| 512 |
problems.You are a strict verification agent. Return only JSON.""",
|
| 513 |
+
vp, cfg, max_tokens=8192,
|
| 514 |
))
|
| 515 |
any_verification_ran = True
|
| 516 |
except Exception:
|