Spaces:
Sleeping
Sleeping
update value chain prompts + output verification
Browse filesFiles changed (3)
- app_web.py+34-17
- src/prompts.py+467-1
- src/verifier.py+4-3
15 unmodified lines16171819202122105 unmodified lines12812913013113213313413513613713813914014114214314414514614714814915015 unmodified linesfrom src.peri import APPROVED_OPTIONSfrom src.peri.value_chains import categorize_vc_production, analyze_committment_to_vc , analyze_vc_committmentfrom src.peri.investments import analyze_investmentsfrom src.prompts import dict_to_string# from ui import render_dbfrom huggingface_hub import snapshot_download105 unmodified linesc2vc = {}for vc in pol_geo_ref["value_chain"].unique():status.update(label=f'Analyzing committment to {vc} production...')prompt = f"Look up and retrieve all relevant information on {vc} production in {country} from the knowledgebase."qu_result = understand_query(prompt, cfg)search_query = qu_result.get("search_query", prompt)display_query = qu_result.get("display_query", prompt)sql_query = qu_result.get("sql_query")print(sql_query)retrieval_result = retrieve(search_query, cfg, route="both", sql_query=sql_query)st.session_state.last_retrieval = retrieval_resultoutput = verify_and_respond(display_query, retrieval_result, cfg, vc, prompt,)print(output["response"])c2vc.update(_parse_vc_analyzer_result(output["response"]))status.update(label=f"Completed metadata preparation for {pol_geo_scores.shape[0]} value chains and {investments.shape[0]} investment areas in {country}.",15 unmodified lines16171819202122105 unmodified lines12812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616715 unmodified linesfrom src.peri import APPROVED_OPTIONSfrom src.peri.value_chains import categorize_vc_production, analyze_committment_to_vc , analyze_vc_committmentfrom src.peri.investments import analyze_investmentsfrom src.prompts import dict_to_string, VC_RAG# from ui import render_dbfrom huggingface_hub import snapshot_download105 unmodified linesc2vc = {}for vc in pol_geo_ref["value_chain"].unique():prompt = f"Look up and retrieve all relevant information on {vc} production in {country} from the knowledgebase."status.update(label=f'Analyzing committment to {vc} production...')# for it in iterations:retrieval_results = {}for var, txt in VC_RAG.items():# status.update(label=f'Analyzing {var.replace("_", " ")} to {vc} production...')rag_prompt = txt.format(value_chain=vc)qu_result = understand_query(rag_prompt, cfg)search_query = qu_result.get("search_query", rag_prompt)display_query = qu_result.get("display_query", rag_prompt)sql_query = qu_result.get("sql_query")print(sql_query)if var=="strategic_commitment":retrieval_result = retrieve(rag_prompt,cfg,route="both",sql_query=sql_query,db_filter=cfg["policy_docs"])retrieval_result["policy_docs"] = set([os.path.basename(r["metadata"]["source"])for r in retrieval_result["db_results"]])else:retrieval_result = retrieve(rag_prompt, cfg, route="both", sql_query=sql_query)retrieval_result["var"] = varoutput = verify_and_respond(prompt, retrieval_result, cfg, vc, prompt)retrieval_results[var] = _parse_vc_analyzer_result(output["response"])c2vc[vc] = retrieval_results# retrieval_results.append(output)# c2vc.update(_parse_vc_analyzer_result(output["response"]))status.update(label=f"Completed metadata preparation for {pol_geo_scores.shape[0]} value chains and {investments.shape[0]} investment areas in {country}.",
83 unmodified lines848586878889426 unmodified lines516517518519520521131 unmodified lines6536546556566576586 unmodified lines6656666676686696704 unmodified lines67567667767867968068183 unmodified lines{context}====================================================================="""VERIFICATION_PROMPT_TEMPLATE = """\--- RESPONSE TO VERIFY ---426 unmodified lines"strategic_commitment":STRATEGIC_COMMITMENT_BLOCK,"institutional_commitment":INSTITUTIONAL_COMMITMENT_BLOCK,}def build_vc_analyzer_prompt(cfg:Dict,retrieval_result:Dict,value_chain:str,131 unmodified linescontext: str,phrase_flags: list[str],similarity_flags: list[str],) -> str:"""Build the verification prompt for auditing an AI response.6 unmodified linesReturns:Formatted verification prompt string."""phrase_str = ("\n".join(f"- {flag}" for flag in phrase_flags)if phrase_flags4 unmodified linesif similarity_flagselse "No similarity flags detected.")return VERIFICATION_PROMPT_TEMPLATE.format(response=response,context=context,phrase_flags=phrase_str,83 unmodified lines84858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547426 unmodified lines974975976977978979980981982983984985131 unmodified lines11171118111911201121112211236 unmodified lines11301131113211331134113511364 unmodified lines114111421143114411451146114783 unmodified lines{context}====================================================================="""FC_VERIFICATION_PROMPT_TEMPLATE = """\--- RESPONSE TO VERIFY ---{response}--- END RESPONSE ------ CONTEXT ---{context}--- END CONTEXT ------ PHRASE-LEVEL FLAGS ---{phrase_flags}--- END PHRASE FLAGS ------ SIMILARITY FLAGS ---{similarity_flags}--- END SIMILARITY FLAGS ---The response should be a Python-style list of dictionaries matching the required output format.Run through this 12-point verification checklist and report results:1. Output Structure- Is the response a list?- Does every list item contain the required fields:- budget_year- budget_value- currency- responsible_institution- evidence2. Evidence Structure- Is the evidence field a list?- Does every evidence item contain:- quote- source_document- page_or_section3. Required Field Completeness- Are all extracted fields populated when information is available in the context?- Are values left blank only when the information cannot be found?4. Context Support- For every extracted budget_year, budget_value, currency, and responsible_institution value, can supporting evidence be found in the provided context?5. Evidence Alignment- Does each evidence quote directly support the candidate record it is attached to?- Verify that quoted text contains or clearly supports the extracted values.6. Quote Accuracy- Are all evidence quotes reproduced exactly from the context?- Flag paraphrased, modified, or fabricated quotations.7. Hallucinated Information- Are any extracted values absent from the context?- Are any responsible institutions, budget values, years, currencies, or program details inferred without evidence?8. Candidate Coverage- If multiple candidate values are present in the context, were all relevant candidate records extracted?- Flag omitted candidate values that should reasonably have been included.9. Temporal Coverage- Does the response contain:A. At least one budget allocation occurring five or more years before the current date, if such information exists in the context?B. The most recent budget allocation available in the context?- Flag cases where qualifying records exist but were omitted.10. Data Consistency- Do budget_year, budget_value, currency, and responsible_institution correspond to the same allocation, project, investment, or financing record?- Flag records that incorrectly combine values from different sources or contexts.11. Phrase-Level Flags- Are any phrase-level flags confirmed by the context review?- Distinguish between true hallucinations and supported content.12. Similarity Flags- Do similarity flags indicate unsupported semantic drift between the response and source evidence?- Flag extracted values that do not accurately reflect source meaning.Severity Guidance:- High: hallucinated values, unsupported extractions, incorrect quotes, invalid structure, missing key candidate records, or incorrect temporal selection.- Medium: incomplete extraction, weak evidence linkage, inconsistent field population, or partial coverage.- Low: formatting issues, minor omissions in evidence metadata, or non-critical organizational issues.Return your analysis as JSON in the following format:{{"errors": [{{"check": 1,"description": "...","severity": "high|medium|low"}}],"error_count": 0,"pass": true,"summary": "Brief overall assessment"}}Additional Rules:- Consider only evidence present in the supplied context.- Do not penalize blank values when the information is genuinely unavailable in the context.- A candidate record passes verification if every populated field is supported by the attached evidence.- If the response contains no valid supporting evidence for a populated field, treat this as a hallucination.- If no errors are found, return:- "errors": []- "error_count": 0- "pass": true"""SC_VERIFICATION_PROMPT_TEMPLATE = """\--- RESPONSE TO VERIFY ---{response}--- END RESPONSE ------ CONTEXT ---{context}--- END CONTEXT ------ PHRASE-LEVEL FLAGS ---{phrase_flags}--- END PHRASE FLAGS ------ SIMILARITY FLAGS ---{similarity_flags}--- END SIMILARITY FLAGS ---The response should be a dictionary containing the following keys:- development_plan- agricultural_strategy- standalone_strategy- flagship_projectEach key should contain a list of document records.Run through this 13-point verification checklist and report results:1. Output Structure- Is the response a dictionary/object?- Does it contain all required top-level keys?- development_plan- agricultural_strategy- standalone_strategy- flagship_project2. Record Structure- Does every document record contain:- quote- document- page_or_section- year3. Required Field Completeness- Are all fields populated when information is available in the context?- Are values left blank only when information cannot be found?4. Context Support- Can every returned document be located in the provided context?- Verify that the document name, publication year, and quote originate from the source material.5. Quote Accuracy- Are all quotes reproduced exactly from the source?- Flag any paraphrased, altered, truncated, or fabricated quotations.6. Mention Verification- Does every quote contain or directly support a mention of the target value chain?- Flag documents included without evidence of value-chain relevance.7. Document Classification Accuracy- Is each document assigned to the correct category?Development Plan:- National medium-term development plan.- Country-wide development framework.Agricultural Strategy:- Agriculture-specific strategy, investment plan,transformation plan, or equivalent.Standalone Strategy:- Strategy primarily dedicated to the target value chain.Flagship Project:- Major donor-supported initiative, project,or program focused on the value chain.Flag documents assigned to incorrect categories.8. Duplicate Document Detection- Has the same document been repeated multiple timeswithin a category?- Flag unnecessary duplicates.9. Coverage Completeness- Were all qualifying documents identified from the context?- Flag relevant documents that were omitted.10. Recency Prioritization- When multiple qualifying documents exist,does the response appropriately prioritizedocuments published within the last five years?- Flag if older documents are included while more recentqualifying documents were ignored.11. Hallucinated Information- Are any document names, years, categories,projects, or quotations absent from the context?- Flag any unsupported additions.12. Phrase-Level Flags- Are any phrase-level flags confirmed by review of the context?- Distinguish between genuine hallucinations and supported content.13. Similarity Flags- Do similarity flags indicate semantic drift betweenextracted information and the source material?- Flag cases where the meaning of the source has been altered.Severity Guidance:- High:- Hallucinated documents- Hallucinated quotes- Incorrect category assignment- Unsupported value-chain relevance- Missing major qualifying documents- Invalid output structure- Medium:- Incomplete document coverage- Missing years- Missing location information- Weak evidence linkage- Failure to prioritize recent documents- Low:- Duplicate records- Minor formatting issues- Missing non-critical metadataReturn your analysis as JSON in the following format:{{"errors": [{{"check": 1,"description": "...","severity": "high|medium|low"}}],"error_count": 0,"pass": true,"summary": "Brief overall assessment"}}Additional Rules:- Consider only evidence available in the supplied context.- Do not penalize empty categories if no qualifying documents exist in the context.- A document record passes verification only if its quote provides direct support for its inclusion.- If a document is included without supporting evidence, treat it as a hallucination.- If a document clearly belongs in a different category, record a classification error.- If no errors are found, return:- "errors": []- "error_count": 0- "pass": true"""IC_VERIFICATION_PROMPT_TEMPLATE = """\--- RESPONSE TO VERIFY ---{response}--- END RESPONSE ------ CONTEXT ---{context}--- END CONTEXT ------ PHRASE-LEVEL FLAGS ---{phrase_flags}--- END PHRASE FLAGS ------ SIMILARITY FLAGS ---{similarity_flags}--- END SIMILARITY FLAGS ---The response should be a dictionary containing the following keys:- dedicated_agency- dedicated_research_institute- dedicated_ministerial_departmentEach key should contain a list of organizational records.Run through this 13-point verification checklist and report results:1. Output Structure- Is the response a dictionary/object?- Does it contain all required top-level keys?- dedicated_agency- dedicated_research_institute- dedicated_ministerial_department2. Record Structure- Does every record contain the required fields?dedicated_agency:- agency_name- document- page_or_section- quotededicated_research_institute:- institution_name- document- page_or_section- quotededicated_ministerial_department:- department_name- document- page_or_section- quote3. Required Field Completeness- Are all fields populated when information is available in the context?- Are values left blank only when information cannot be determined from the context?4. Context Support- Can every organization name be verified from the provided context?- Can the cited document and page/section be located in the context?5. Quote Accuracy- Are all quotes reproduced exactly from the source material?- Flag paraphrased, altered, incomplete, or fabricated quotations.6. Organizational Relevance- 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?- Flag organizations included without clear value-chain relevance.7. Category Assignment Accuracy- Is each organization assigned to the correct category?Dedicated Agency:- Government agency, authority, board, corporation, commission,parastatal, or similar public institution dedicated to the value chain.Dedicated Research Institute:- Research institute, research center, laboratory,or specialized research organization dedicated to the value chain.Dedicated Ministerial Department:- Ministry department, directorate, unit, division,board, council, authority, or administrative officeresponsible for the value chain.Flag organizations assigned to incorrect categories.8. Evidence Strength- Does the quote provide direct evidence of the organization's mandate,role, or dedicated focus?- Flag records where organizational relevance is inferredwithout explicit supporting language.9. Duplicate Entity Detection- Has the same organization been returned multiple timeswithin the same category?- Flag unnecessary duplicate entries.10. Coverage Completeness- Were all qualifying organizations identified from the context?- Flag relevant agencies, institutes, departments,authorities, boards, councils, or units that were omitted.11. Hallucinated Information- Are any organization names, document names,quotations, or classifications absent from the context?- Flag unsupported additions.12. Phrase-Level Flags- Are any phrase-level flags confirmed by review of the context?- Distinguish between genuine hallucinations and supported content.13. Similarity Flags- Do similarity flags indicate semantic drift betweenextracted information and the source material?- Flag records where the extracted organizational rolediffers from what the source actually states.Severity Guidance:- High:- Hallucinated organizations- Hallucinated quotes- Unsupported value-chain relevance- Incorrect category assignment- Missing major qualifying organizations- Invalid output structure- Medium:- Weak supporting evidence- Missing organization names- Missing document locations- Incomplete extraction coverage- Ambiguous classification- Low:- Duplicate entries- Formatting issues- Minor metadata omissionsSpecial Verification Rules:- An organization should only be included if the source provides evidencethat it is specifically responsible for, focused on, mandated for,or significantly engaged with the target value chain.- Do not assume an organization is dedicated to the value chain simply becauseit operates within the broader agriculture sector.- General ministries (e.g., Ministry of Agriculture) should not automaticallyqualify as dedicated ministerial departments unless a specific department,directorate, unit, authority, board, or council connected to the value chainis identified.- Research institutes should only be included when there is evidence of adedicated value-chain focus or mandate.- If the same organization appears in multiple categories, verify that thesource material genuinely supports each classification.Otherwise record a classification error.- Consider only evidence available in the supplied context.- If no qualifying organizations are present in the context,empty lists are valid and should not be treated as errors.- If a record lacks supporting evidence, treat it as a hallucination.Return your analysis as JSON in the following format:{{"errors": [{{"check": 1,"description": "...","severity": "high|medium|low"}}],"error_count": 0,"pass": true,"summary": "Brief overall assessment"}}If no errors are found, return:- "errors": []- "error_count": 0- "pass": true"""VERIFICATION_PROMPT_TEMPLATE = """\--- RESPONSE TO VERIFY ---426 unmodified lines"strategic_commitment":STRATEGIC_COMMITMENT_BLOCK,"institutional_commitment":INSTITUTIONAL_COMMITMENT_BLOCK,}VC_VERIFY = {"financial_commitment":FC_VERIFICATION_PROMPT_TEMPLATE,"strategic_commitment":SC_VERIFICATION_PROMPT_TEMPLATE,"institutional_commitment":IC_VERIFICATION_PROMPT_TEMPLATE,}def build_vc_analyzer_prompt(cfg:Dict,retrieval_result:Dict,value_chain:str,131 unmodified linescontext: str,phrase_flags: list[str],similarity_flags: list[str],commitment_var:str,) -> str:"""Build the verification prompt for auditing an AI response.6 unmodified linesReturns:Formatted verification prompt string."""verification_prompt_template = VC_VERIFY[commitment_var]phrase_str = ("\n".join(f"- {flag}" for flag in phrase_flags)if phrase_flags4 unmodified linesif similarity_flagselse "No similarity flags detected.")return verification_prompt_template.format(response=response,context=context,phrase_flags=phrase_str,
439 unmodified lines4404414424434444454463 unmodified lines45045145245345445545645 unmodified lines5025035045055065071 unmodified line509510511512513514515439 unmodified lines# Layer 3: LLM-as-verifierall_sim_flags = sim_flag_strs + citation_flag_strsverification_prompt = build_verification_prompt(response, context, phrase_flag_strs, all_sim_flags,)try:raw_verification = generate(3 unmodified linesproblems.You are a strict verification agent. Return only JSON.""",verification_prompt,cfg,max_tokens=1024,)except Exception:# Verification LLM failed — skip correction, continue to next iteration45 unmodified linesresponse, context,[f["message"] for f in phrase_flags],all_sim_final,)try:vr = parse_verification_result(generate(1 unmodified lineYou are a verification agent. Your job is to audit the followingAI-generated response against the provided context and flag anyproblems.You are a strict verification agent. Return only JSON.""",vp, cfg, max_tokens=1024,))any_verification_ran = Trueexcept Exception:439 unmodified lines4404414424434444454463 unmodified lines45045145245345445545645 unmodified lines5025035045055065075081 unmodified line510511512513514515516439 unmodified lines# Layer 3: LLM-as-verifierall_sim_flags = sim_flag_strs + citation_flag_strsverification_prompt = build_verification_prompt(response, context, phrase_flag_strs, all_sim_flags, retrieval_result["var"],)try:raw_verification = generate(3 unmodified linesproblems.You are a strict verification agent. Return only JSON.""",verification_prompt,cfg,max_tokens=8192,)except Exception:# Verification LLM failed — skip correction, continue to next iteration45 unmodified linesresponse, context,[f["message"] for f in phrase_flags],all_sim_final,retrieval_result["var"],)try:vr = parse_verification_result(generate(1 unmodified lineYou are a verification agent. Your job is to audit the followingAI-generated response against the provided context and flag anyproblems.You are a strict verification agent. Return only JSON.""",vp, cfg, max_tokens=8192,))any_verification_ran = Trueexcept Exception:
app_web.py+34-17
src/prompts.py+467-1
src/verifier.py+4-3