Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- .gitignore +1 -2
- articles_db.csv +60 -57
- chroma_store/11a56aaa-a850-4b98-a3f8-c059e49a501b/data_level0.bin +3 -0
- chroma_store/11a56aaa-a850-4b98-a3f8-c059e49a501b/header.bin +3 -0
- chroma_store/11a56aaa-a850-4b98-a3f8-c059e49a501b/length.bin +3 -0
- chroma_store/11a56aaa-a850-4b98-a3f8-c059e49a501b/link_lists.bin +0 -0
- chroma_store/chroma.sqlite3 +2 -2
- main.py +82 -19
- requirements.txt +2 -1
.gitignore
CHANGED
|
@@ -127,5 +127,4 @@ dmypy.json
|
|
| 127 |
|
| 128 |
# Pyre type checker
|
| 129 |
.pyre/
|
| 130 |
-
.idea
|
| 131 |
-
Articles
|
|
|
|
| 127 |
|
| 128 |
# Pyre type checker
|
| 129 |
.pyre/
|
| 130 |
+
.idea
|
|
|
articles_db.csv
CHANGED
|
@@ -1,57 +1,60 @@
|
|
| 1 |
-
id,
|
| 2 |
-
1,
|
| 3 |
-
2,
|
| 4 |
-
3,
|
| 5 |
-
4,
|
| 6 |
-
5,
|
| 7 |
-
6,
|
| 8 |
-
7,
|
| 9 |
-
8,
|
| 10 |
-
9,
|
| 11 |
-
10,
|
| 12 |
-
11,
|
| 13 |
-
12,
|
| 14 |
-
13,
|
| 15 |
-
14,
|
| 16 |
-
15,
|
| 17 |
-
16,
|
| 18 |
-
17,
|
| 19 |
-
18,
|
| 20 |
-
19,
|
| 21 |
-
20,
|
| 22 |
-
21,
|
| 23 |
-
22,
|
| 24 |
-
23,
|
| 25 |
-
24,
|
| 26 |
-
25,
|
| 27 |
-
26,
|
| 28 |
-
27,
|
| 29 |
-
28,
|
| 30 |
-
29,
|
| 31 |
-
30,
|
| 32 |
-
31,
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
id,Title,Link,Context,Category,SourceType,FileName,Description
|
| 2 |
+
1,2020 FORTIFIED Standard,https://fortifiedhome.org/wp-content/uploads/2020-FORTIFIED-Home-Standard.pdf,Re-Roofing and Roof,Construction Standards,file,2020_fortified_standard.pdf,Short description …
|
| 3 |
+
2,Sealed Roof Deck Supplemental Deck Attachment,https://fortifiedhome.org/wp-content/uploads/2015-04_technical-bulletin_sealed-roof-deck-supplemental-deck-attachment.pdf,Sealed Roof Deck,Technical Bulletin,file,sealed_roof_deck_supplemental_deck_attachment.pdf,Short description …
|
| 4 |
+
3,Roof Flashing,https://fortifiedhome.org/wp-content/uploads/2017-01_technical-bulletin_flashing.pdf,Roof Flashing,Technical Bulletin,file,roof_flashing.pdf,Short description …
|
| 5 |
+
4,Metal Panel Roof Covering Guidance,https://fortifiedhome.org/wp-content/uploads/2019-01_technical-bulletin_metal-roof-panel-selection_revised.pdf,Metal Panel and Roof,Technical Bulletin,file,metal_panel_roof_covering_guidance.pdf,Short description …
|
| 6 |
+
5,Design Pressure Guidance for Roof Coverings,https://fortifiedhome.org/wp-content/uploads/2020-01_technical-bulletin_design-pressure-guidance-for-roof-coverings_revised.pdf,Roof and Sealed Roof Deck,Technical Bulletin,file,design_pressure_guidance_for_roof_coverings.pdf,Short description …
|
| 7 |
+
6,Garage Door Requirements,https://fortifiedhome.org/wp-content/uploads/2021-01_technical-bulletin_garage-door-requirements.pdf,General,Technical Bulletin,file,garage_door_requirements.pdf,Short description …
|
| 8 |
+
7,PA - Vycor Product Advisory,https://fortifiedhome.org/wp-content/uploads/Vycor_Product_Advisory.pdf,General,Technical Bulletin,file,pa_vycor_product_advisory.pdf,Short description …
|
| 9 |
+
8,Corrosion Resistant Fasteners,https://fortifiedhome.org/wp-content/uploads/TB_FH_2021-02.pdf,"New Roof, Re-Roofing, Retrofit, and Roof",Technical Bulletin,file,corrosion_resistant_fasteners.pdf,Short description …
|
| 10 |
+
9,Sealed Roof Deck for Wood Shake and Shingle Roof Systems,https://fortifiedhome.org/wp-content/uploads/TB-FH-2021-03_SRD_options_for_wood_shake.pdf,"Re-Roofing, Retrofit, and Roof",Technical Bulletin,file,sealed_roof_deck_for_wood_shake_and_shingle_roof_systems.pdf,Short description …
|
| 11 |
+
10,IBHS Guidance Choosing the Right Tape,https://fortifiedhome.org/wp-content/uploads/Choosing-the-Right-Tape_FINAL.pdf,Roof,Technical Bulletin,file,ibhs_guidance_choosing_the_right_tape.pdf,Short description …
|
| 12 |
+
11,Roof Sheathing Nail Pattern Documentation Requirements,https://fortifiedhome.org/wp-content/uploads/TB_FH_2022_01-Roof-Sheathing-Attachment-Documentation.pdf,Roof,Technical Bulletin,file,roof_sheathing_nail_pattern_documentation_requirements.pdf,Short description …
|
| 13 |
+
12,FORTIFIED Roof Identification,https://fortifiedhome.org/wp-content/uploads/FORTIFIED-ROOF-IDENTIFICATIONR10.pdf,Roof,Technical Bulletin,file,fortified_roof_identification.pdf,Short description …
|
| 14 |
+
13,The FORTIFIED Definition of Roof,https://fortifiedhome.org/wp-content/uploads/TB_FH_2022-02-Definition-of-Roof.pdf,Roof,Technical Bulletin,file,the_fortified_definition_of_roof.pdf,Short description …
|
| 15 |
+
14,FORTIFIED Home Requirements for Elevated Roof-Mounted Decks,https://fortifiedhome.org/wp-content/uploads/TB_FH_2022-03-Elevated-Rooftop-Decks.pdf,Roof and Sealed Roof Deck,Technical Bulletin,file,fortified_home_requirements_for_elevated_roof_mounted_decks.pdf,Short description …
|
| 16 |
+
15,Product Substitution Due to Supply Chain Issues and Product Availability,https://fortifiedhome.org/wp-content/uploads/TB_FH_2022_04-Supply-Chain-Issues.pdf,Roof,Technical Bulletin,file,product_substitution_due_to_supply_chain_issues_and_product_availability.pdf,Short description …
|
| 17 |
+
16,Requirements for Re-Roofing Over Existing Self-Adhered Membranes,https://fortifiedhome.org/wp-content/uploads/TB_FH_2022-05-Reroofing-Over-Self-Adhered-Membranes.pdf,General,Technical Bulletin,file,requirements_for_re_roofing_over_existing_self_adhered_membranes.pdf,Short description …
|
| 18 |
+
17,Foundation Requirements for FORTIFIED Home Eligibility,https://fortifiedhome.org/wp-content/uploads/TB_FH_2022-06-Foundation-Requirements-FORTIFIED-Home.pdf,Roof,Technical Bulletin,file,foundation_requirements_for_fortified_home_eligibility.pdf,Short description …
|
| 19 |
+
18,Removal of Egress Requirement for Entry Doors,https://fortifiedhome.org/wp-content/uploads/TB-Update-Removal-of-Egress-Requirement.pdf,Roof,Technical Bulletin,file,removal_of_egress_requirement_for_entry_doors.pdf,Short description …
|
| 20 |
+
19,Product Advisory FH Eligibility of Existing Fiberglass Resin Deck Coatings as Roof Covers,https://fortifiedhome.org/wp-content/uploads/PA_FH_2023-01-Fiberglass-resin-deck-coatings-as-roof-covers.pdf,General,Technical Bulletin,file,product_advisory_fh_eligibility_of_existing_fiberglass_resin_deck_coatings_as_roof_covers.pdf,Short description …
|
| 21 |
+
20,Fastener Type Requirements for Asphalt Shingles and Drip Edge,https://fortifiedhome.org/wp-content/uploads/TB-Update-Fastener-Requirements-for-Asphalt-Shingles-and-Drip-Edge.pdf,Roof,Technical Bulletin,file,fastener_type_requirements_for_asphalt_shingles_and_drip_edge.pdf,Short description …
|
| 22 |
+
21,Eligibility and Compliance of Accessory Roof Structures,https://fortifiedhome.org/wp-content/uploads/TB-2023-03_Eligibility-and-Compliance-Accessory-Roof-Structures.pdf,General,Technical Bulletin,file,eligibility_and_compliance_of_accessory_roof_structures.pdf,Short description …
|
| 23 |
+
22,Use of Factory Seconds or Unlabeled Material in FORTIFIED,https://fortifiedhome.org/wp-content/uploads/TB_FH_2023-04-Factory-Seconds-or-Unlabeled-Materials.pdf,General,Technical Bulletin,file,use_of_factory_seconds_or_unlabeled_material_in_fortified.pdf,Short description …
|
| 24 |
+
23,FORTIFIED Requirements for Cement and Clay Hip and Ridge Tile Installed Over Asphalt Shingle Roof Cover,https://fortifiedhome.org/wp-content/uploads/TB_FH_2023-05-Ridge-or-Hip-Tiles-with-Asphalt-Shingles.pdf,General,Technical Bulletin,file,fortified_requirements_for_cement_and_clay_hip_and_ridge_tile_installed_over_asphalt_shingle_roof_cover.pdf,Short description …
|
| 25 |
+
24,2-ply Synthetic Underlayment is an Approved Sealed Roof Deck Option on Asphalt Shingle and Metal Roofs,https://fortifiedhome.org/wp-content/uploads/TB_FH_2023-06-2-Ply-Synthetic-SRD-Method.pdf,General,Technical Bulletin,file,2_ply_synthetic_underlayment_is_an_approved_sealed_roof_deck_option_on_asphalt_shingle_and_metal_roofs.pdf,Short description …
|
| 26 |
+
25,FORTIFIED Home Requirements for Homes with Excessive Gaps Between Wood Decking Boards,https://fortifiedhome.org/wp-content/uploads/TB_FH_2023_07-Gapped-Decking.pdf,General,Technical Bulletin,file,fortified_home_requirements_for_homes_with_excessive_gaps_between_wood_decking_boards.pdf,Short description …
|
| 27 |
+
26,Eligibility Requirements for Homes Constructed to the HUD Code,https://fortifiedhome.org/wp-content/uploads/TB_2023_08-Eligibility-of-HUD-Homes.pdf,General,Technical Bulletin,file,eligibility_requirements_for_homes_constructed_to_the_hud_code.pdf,Short description …
|
| 28 |
+
27,FORTIFIED Guidance on Leaf Guards and Gutters,https://fortifiedhome.org/wp-content/uploads/TB_FH_2023-09-Leaf-Guards-and-Gutters.pdf,General,Technical Bulletin,file,fortified_guidance_on_leaf_guards_and_gutters.pdf,Short description …
|
| 29 |
+
28,Redesignation Policy Update,https://fortifiedhome.org/wp-content/uploads/TB_FH_2023-10-Redesignation-Policy-Update.pdf,General,Technical Bulletin,file,redesignation_policy_update.pdf,Short description …
|
| 30 |
+
29,Companion Details for Roll Widths Greater than 36 Inches,https://fortifiedhome.org/wp-content/uploads/Fastening-Synthetic-Underlayment.pdf,General,Technical Bulletin,file,companion_details_for_roll_widths_greater_than_36_inches.pdf,Short description …
|
| 31 |
+
30,Underlayment Fastening for Taped Seams and Underlayment Sealed Roof Deck Installations Using Rolls Wider Than 36 Inches,https://fortifiedhome.org/wp-content/uploads/TB_FH_2023-11-Underlayment-Fastening.pdf,General,Technical Bulletin,file,underlayment_fastening_for_taped_seams_and_underlayment_sealed_roof_deck_installations_using_rolls_wider_than_36_inches.pdf,Short description …
|
| 32 |
+
31, FORTIFIED Home Requirements for Elevated Roof-Mounted HVAC Units,https://fortifiedhome.org/wp-content/uploads/TB_FH_2024-01-Elevated-Roof-Mounted-HVAC-Units.pdf,Sealed Roof Deck,Technical Bulletin,file,fortified_home_requirements_for_elevated_roof_mounted_hvac_units.pdf,Short description …
|
| 33 |
+
32,FORTIFIED Guidance ‚Äì Rain Diverters,https://fortifiedhome.org/wp-content/uploads/TB_FH_2024-02-Rain-Diverters.pdf,Sealed Roof Deck,Technical Bulletin,file,fortified_guidance_rain_diverters.pdf,Short description …
|
| 34 |
+
33,Dog Doors in FORTIFIED Homes,https://fortifiedhome.org/wp-content/uploads/FH-2024-03-Dog-Doors-in-FORTIFIED-Homes.pdf,General,Technical Bulletin,file,dog_doors_in_fortified_homes.pdf,Short description …
|
| 35 |
+
34,Modular Home Starter Kit ‚Äì Full Set,https://fortifiedhome.org/wp-content/uploads/Modular-Home-Toolkit-2024.pdf,General,Modular Home,file,modular_home_starter_kit_full_set.pdf,Short description …
|
| 36 |
+
35,Post Storm Audit Processes,https://fortifiedhome.org/wp-content/uploads/Post-Storm-Audit-Processes.pdf,list_only,Technical Bulletin,file,post_storm_audit_processes.pdf,Short description …
|
| 37 |
+
36,FORTIFIED Roofing Contractor Handbook,https://fortifiedhome.org/wp-content/uploads/FORTIFIED_Roof_Contractor_Handbook.pdf,General,Hankbook,file,fortified_roofing_contractor_handbook.pdf,Short description …
|
| 38 |
+
37,FORTIFIED Evaluator Handbook,https://fortifiedhome.org/wp-content/uploads/FORTIFIED_Home_Evaluator_Handbook.pdf,General,Hankbook,file,fortified_evaluator_handbook.pdf,Short description …
|
| 39 |
+
38,FORTIFIED Professional Handbook,https://fortifiedhome.org/wp-content/uploads/FORTIFIED-Home-Professional-Handbook.pdf,General,Hankbook,file,fortified_professional_handbook.pdf,Short description …
|
| 40 |
+
39,Certified FORTIFIED Roofing Contractor Agreement,https://fortifiedhome.org/wp-content/uploads/FORTIFIED-Roofing-Contractor-Agreement_5-25-22.pdf,General,Agreement,file,certified_fortified_roofing_contractor_agreement.pdf,Short description …
|
| 41 |
+
40,Certified FORTIFIED Evaluator Agreement,https://fortifiedhome.org/wp-content/uploads/FORTIFIED_Home_Evaluator-Agreement.pdf,General,Agreement,file,certified_fortified_evaluator_agreement.pdf,Short description …
|
| 42 |
+
41,FORTIFIED Professional Agreement,https://fortifiedhome.org/wp-content/uploads/FORTIFIED-Professional-Agreement.pdf,General,Agreement,file,fortified_professional_agreement.pdf,Short description …
|
| 43 |
+
42,What to Do After a Hurricane,https://fortifiedhome.org/article/what-to-do-after-a-hurricane/,General,Article,web,,Short description …
|
| 44 |
+
43,Extend the Life of Your Roof,https://fortifiedhome.org/article/extend-the-life-of-your-roof/,General,Article,web,,Short description …
|
| 45 |
+
44,How to Select a Roofing Contractor,https://fortifiedhome.org/article/how-to-select-a-roofing-contractor/,General,Article,web,,Short description …
|
| 46 |
+
45,The Next Line of Defense,https://fortifiedhome.org/article/the-next-line-of-defense/,General,Article,web,,Short description …
|
| 47 |
+
46,9 Questions You Should Absolutely Ask your Roofer,https://fortifiedhome.org/article/9-questions-you-should-absolutely-ask-your-roofer/,General,Article,web,,Short description …
|
| 48 |
+
47,Strengthen Your Home Against Hurricanes and Severe Weather,https://fortifiedhome.org/article/strengthen-your-home-against-hurricanes-and-severe-weather/,General,Article,web,,Short description …
|
| 49 |
+
48,8 Ways to Know If You Need a New Roof,https://fortifiedhome.org/article/8-ways-to-know-if-you-need-a-new-roof/,General,Article,web,,Short description …
|
| 50 |
+
49,Defend Against Frozen Pipes,https://fortifiedhome.org/article/preventing-frozen-pipes/,General,Article,web,,Short description …
|
| 51 |
+
50,Hail Protection That Works,https://fortifiedhome.org/article/hail-protection-that-works/,General,Article,web,,Short description …
|
| 52 |
+
51,Shut the Door,https://fortifiedhome.org/article/shut-the-door/,General,Article,web,,Short description …
|
| 53 |
+
52,Don t Jeopardize Your Home s Resilience,https://fortifiedhome.org/article/dont-jeopardize-your-homes-resilience/,General,Article,web,,Short description …
|
| 54 |
+
53, Resilience gains ground as storms increase in frequency and intensity,https://fortifiedhome.org/article/resilience-gains-ground-as-storms-increase-in-frequency-and-intensity/,General,Article,web,,Short description …
|
| 55 |
+
54,Reinforce Your Property Against Hurricanes,https://fortifiedhome.org/article/reinforce-your-property-against-hurricanes/,General,Article,web,,Short description …
|
| 56 |
+
55,Can You Weather the Cost of The Next Storm?,https://fortifiedhome.org/article/can-you-weather-the-cost-of-the-next-storm/,General,Article,web,,Short description …
|
| 57 |
+
56,Is Your Building Code Leaving You Vulnerable to Severe Weather?,https://fortifiedhome.org/article/is-your-building-code-leaving-you-vulnerable-to-severe-weather/,General,Article,web,,Short description …
|
| 58 |
+
57,Renewing Your Designation,https://fortifiedhome.org/renew-your-designation/,General,Article,web,,Short description …
|
| 59 |
+
58,How to Become a FORTIFIED Certified Service Provider,https://fortifiedhome.org/how-to-become-fortified-certified/,General,Article,web,,Short description …
|
| 60 |
+
59,Frequently Asked Questions,https://fortifiedhome.org/frequently-asked-questions/,General,FAQ,web,,Short description …
|
chroma_store/11a56aaa-a850-4b98-a3f8-c059e49a501b/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f18abd8c514282db82706e52b0a33ed659cd534e925a6f149deb7af9ce34bd8e
|
| 3 |
+
size 6284000
|
chroma_store/11a56aaa-a850-4b98-a3f8-c059e49a501b/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:effaa959ce2b30070fdafc2fe82096fc46e4ee7561b75920dd3ce43d09679b21
|
| 3 |
+
size 100
|
chroma_store/11a56aaa-a850-4b98-a3f8-c059e49a501b/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fc19b1997119425765295aeab72d76faa6927d4f83985d328c26f20468d6cc76
|
| 3 |
+
size 4000
|
chroma_store/11a56aaa-a850-4b98-a3f8-c059e49a501b/link_lists.bin
ADDED
|
File without changes
|
chroma_store/chroma.sqlite3
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a7e2afd1ab05004fdd615c6e13ced29d11e0a8ecad6188c4f3cb1006073f23f4
|
| 3 |
+
size 9400320
|
main.py
CHANGED
|
@@ -13,16 +13,36 @@ import gradio as gr
|
|
| 13 |
import pandas as pd
|
| 14 |
import logging
|
| 15 |
from langchain_core.exceptions import OutputParserException
|
|
|
|
| 16 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Constants
|
| 19 |
PERSIST_DIRECTORY = "chroma_store"
|
| 20 |
K_VALUE = 5
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
and
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
Remember, you must return both an answer and citations. A citation consists of a VERBATIM quote that
|
| 27 |
justifies the answer and the ID and also Source Name of the quote article. Return a citation for every quote across all articles
|
| 28 |
that justify the answer. Use the following format for your final output:
|
|
@@ -38,6 +58,25 @@ Here are the articles:{context}"""
|
|
| 38 |
|
| 39 |
xml_prompt = ChatPromptTemplate.from_messages([("system", xml_system), ("human", "{input}")])
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def format_docs_xml(docs: List[Document]) -> str:
|
| 43 |
formatted_docs = [
|
|
@@ -47,7 +86,7 @@ def format_docs_xml(docs: List[Document]) -> str:
|
|
| 47 |
return f"\n\n<sources>\n{chr(10).join(formatted_docs)}\n</sources>"
|
| 48 |
|
| 49 |
|
| 50 |
-
|
| 51 |
rag_chain_from_docs = (
|
| 52 |
RunnablePassthrough.assign(context=(lambda x: format_docs_xml(x["context"])))
|
| 53 |
| xml_prompt
|
|
@@ -63,8 +102,12 @@ chain = RunnablePassthrough.assign(context=retrieve_docs).assign(
|
|
| 63 |
)
|
| 64 |
|
| 65 |
def get_article_info(df, file_name):
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return title, link
|
| 69 |
|
| 70 |
|
|
@@ -83,12 +126,36 @@ def vectordb_search(query):
|
|
| 83 |
links.append(link)
|
| 84 |
return "\n".join([f"- [{title}]({link})" for title, link in zip(titles, links)])
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
def llm_response(query):
|
| 88 |
titles, links, res_titles, res_links = [], [], [], []
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
answer = result['answer']['cited_answer'][0]["answer"]
|
|
|
|
|
|
|
|
|
|
| 92 |
citations = result['answer']['cited_answer'][1]['citations']
|
| 93 |
for citation in citations:
|
| 94 |
edited_item = citation['citation'][1]["source"].replace("Articles/", "")
|
|
@@ -105,8 +172,6 @@ def llm_response(query):
|
|
| 105 |
res_titles.append(res_title)
|
| 106 |
if res_link not in res_links:
|
| 107 |
res_links.append(res_link)
|
| 108 |
-
# markdown_list = f"{answer}\n\nCitations:\n" + "\n".join(
|
| 109 |
-
# [f"- [{title}]({link})" for title, link in zip(titles, links)])
|
| 110 |
|
| 111 |
# Build the answer with superscript citations
|
| 112 |
answer_with_citations = f"{answer}"
|
|
@@ -117,31 +182,29 @@ def llm_response(query):
|
|
| 117 |
citations_section = "\n\nCitations:\n" + "\n".join(
|
| 118 |
[f"[{i}]: [{title}]({link})" for i, (title, link) in enumerate(zip(titles, links), start=1)]
|
| 119 |
)
|
| 120 |
-
|
| 121 |
# Combine answer and citations for final markdown output
|
| 122 |
markdown_list = f"{answer_with_citations}{citations_section}"
|
| 123 |
-
markdown_list += f"\n\n\nHere is a list of
|
| 124 |
markdown_list += "\n".join(
|
| 125 |
[f"- [{res_title}]({res_link})" for res_title, res_link in zip(res_titles, res_links)])
|
| 126 |
except OutputParserException:
|
| 127 |
-
markdown_list = "There
|
| 128 |
return markdown_list
|
| 129 |
|
| 130 |
|
|
|
|
| 131 |
with gr.Blocks() as demo:
|
| 132 |
-
gr.Markdown("##
|
| 133 |
-
gr.Markdown("###
|
| 134 |
with gr.Row():
|
| 135 |
with gr.Column():
|
| 136 |
chatbot = gr.Chatbot(type="messages", height=400)
|
| 137 |
msg = gr.Textbox(label="Hit the Enter to send your question", placeholder="What's on your mind?", show_copy_button=True)
|
| 138 |
send = gr.Button("Send")
|
| 139 |
|
| 140 |
-
|
| 141 |
def user(user_message, history: list):
|
| 142 |
return "", history + [{"role": "user", "content": user_message}]
|
| 143 |
|
| 144 |
-
|
| 145 |
def bot(history: list):
|
| 146 |
bot_message = llm_response(history[-1]['content'])
|
| 147 |
history.append({"role": "assistant", "content": ""})
|
|
@@ -154,4 +217,4 @@ with gr.Blocks() as demo:
|
|
| 154 |
send.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
-
demo.launch(
|
|
|
|
| 13 |
import pandas as pd
|
| 14 |
import logging
|
| 15 |
from langchain_core.exceptions import OutputParserException
|
| 16 |
+
from langchain_core.prompts import MessagesPlaceholder
|
| 17 |
import os
|
| 18 |
+
import uuid
|
| 19 |
+
import uuid
|
| 20 |
+
import hashlib
|
| 21 |
+
|
| 22 |
+
from langchain_community.chat_message_histories import ChatMessageHistory
|
| 23 |
+
from langchain_core.chat_history import BaseChatMessageHistory
|
| 24 |
+
from langchain_core.messages import (
|
| 25 |
+
AIMessage,
|
| 26 |
+
HumanMessage,
|
| 27 |
+
SystemMessage,
|
| 28 |
+
ToolMessage,
|
| 29 |
+
trim_messages,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
|
| 33 |
# Constants
|
| 34 |
PERSIST_DIRECTORY = "chroma_store"
|
| 35 |
K_VALUE = 5
|
| 36 |
+
store = {}
|
| 37 |
+
|
| 38 |
+
xml_system = """You are a helpful AI assistant for the FORTIFIED program, a voluntary initiative for construction
|
| 39 |
+
and re-roofing designed to strengthen homes and commercial buildings against severe weather threats, including
|
| 40 |
+
high winds, hail, hurricanes, and tornadoes. Your users may include homeowners, insurance agents, realtors,
|
| 41 |
+
design professionals and construction professionals. When given a user question, you should consult the provided
|
| 42 |
+
technical and general documents containing FORTIFIED standards, instructions, and program information to deliver
|
| 43 |
+
accurate and relevant responses. If the information necessary to answer the inquiry is not available within these
|
| 44 |
+
documents, simply respond: ‘There is no information relevant to your inquiry in our current resources.
|
| 45 |
+
Please contact FORTIFIED customer support for further assistance.’
|
| 46 |
Remember, you must return both an answer and citations. A citation consists of a VERBATIM quote that
|
| 47 |
justifies the answer and the ID and also Source Name of the quote article. Return a citation for every quote across all articles
|
| 48 |
that justify the answer. Use the following format for your final output:
|
|
|
|
| 58 |
|
| 59 |
xml_prompt = ChatPromptTemplate.from_messages([("system", xml_system), ("human", "{input}")])
|
| 60 |
|
| 61 |
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 62 |
+
|
| 63 |
+
contextualize_q_system_prompt = (
|
| 64 |
+
"Given a chat history and the latest user question "
|
| 65 |
+
"which might reference context in the chat history, "
|
| 66 |
+
"formulate a standalone question which can be understood "
|
| 67 |
+
"without the chat history. Do NOT answer the question, "
|
| 68 |
+
"just reformulate it if needed and otherwise return it as is."
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
contextualize_q_prompt = ChatPromptTemplate.from_messages(
|
| 72 |
+
[
|
| 73 |
+
("system", contextualize_q_system_prompt),
|
| 74 |
+
MessagesPlaceholder("chat_history"),
|
| 75 |
+
("human", "{input}"),
|
| 76 |
+
]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
runnable = contextualize_q_prompt | llm
|
| 80 |
|
| 81 |
def format_docs_xml(docs: List[Document]) -> str:
|
| 82 |
formatted_docs = [
|
|
|
|
| 86 |
return f"\n\n<sources>\n{chr(10).join(formatted_docs)}\n</sources>"
|
| 87 |
|
| 88 |
|
| 89 |
+
|
| 90 |
rag_chain_from_docs = (
|
| 91 |
RunnablePassthrough.assign(context=(lambda x: format_docs_xml(x["context"])))
|
| 92 |
| xml_prompt
|
|
|
|
| 102 |
)
|
| 103 |
|
| 104 |
def get_article_info(df, file_name):
|
| 105 |
+
if ".pdf" in file_name:
|
| 106 |
+
title = df[df["FileName"] == file_name]["Title"].iloc[0]
|
| 107 |
+
link = df[df["FileName"] == file_name]["Link"].iloc[0]
|
| 108 |
+
else:
|
| 109 |
+
title = df[df["Link"] == file_name]["Title"].iloc[0]
|
| 110 |
+
link = file_name
|
| 111 |
return title, link
|
| 112 |
|
| 113 |
|
|
|
|
| 126 |
links.append(link)
|
| 127 |
return "\n".join([f"- [{title}]({link})" for title, link in zip(titles, links)])
|
| 128 |
|
| 129 |
+
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
| 130 |
+
if session_id not in store:
|
| 131 |
+
store[session_id] = ChatMessageHistory()
|
| 132 |
+
return store[session_id]
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def generate_unique_string():
|
| 136 |
+
mac = uuid.getnode()
|
| 137 |
+
mac_str = f'{mac:012x}'
|
| 138 |
+
try:
|
| 139 |
+
with open('/proc/cpuinfo') as f:
|
| 140 |
+
cpu_info = f.read()
|
| 141 |
+
except FileNotFoundError:
|
| 142 |
+
cpu_info = str(uuid.getnode())
|
| 143 |
+
unique_string = hashlib.sha256((mac_str + cpu_info).encode()).hexdigest()
|
| 144 |
+
return unique_string
|
| 145 |
|
| 146 |
def llm_response(query):
|
| 147 |
titles, links, res_titles, res_links = [], [], [], []
|
| 148 |
+
unique_id = generate_unique_string()
|
| 149 |
+
config = {"configurable": {"thread_id": unique_id }}
|
| 150 |
try:
|
| 151 |
+
filtered_history = trim_messages(get_session_history(unique_id).messages, strategy="last", token_counter=len, max_tokens=5,
|
| 152 |
+
start_on="human", end_on=("human", "tool"), include_system=True,)
|
| 153 |
+
modified_query = runnable.invoke({"input": query, "chat_history": filtered_history}).content
|
| 154 |
+
result = chain.invoke({"input": modified_query}, config=config)
|
| 155 |
answer = result['answer']['cited_answer'][0]["answer"]
|
| 156 |
+
history = get_session_history(unique_id)
|
| 157 |
+
history.add_user_message(modified_query)
|
| 158 |
+
history.add_ai_message(answer)
|
| 159 |
citations = result['answer']['cited_answer'][1]['citations']
|
| 160 |
for citation in citations:
|
| 161 |
edited_item = citation['citation'][1]["source"].replace("Articles/", "")
|
|
|
|
| 172 |
res_titles.append(res_title)
|
| 173 |
if res_link not in res_links:
|
| 174 |
res_links.append(res_link)
|
|
|
|
|
|
|
| 175 |
|
| 176 |
# Build the answer with superscript citations
|
| 177 |
answer_with_citations = f"{answer}"
|
|
|
|
| 182 |
citations_section = "\n\nCitations:\n" + "\n".join(
|
| 183 |
[f"[{i}]: [{title}]({link})" for i, (title, link) in enumerate(zip(titles, links), start=1)]
|
| 184 |
)
|
|
|
|
| 185 |
# Combine answer and citations for final markdown output
|
| 186 |
markdown_list = f"{answer_with_citations}{citations_section}"
|
| 187 |
+
markdown_list += f"\n\n\nHere is a list of resources that can provide more information about your inquiry:\n"
|
| 188 |
markdown_list += "\n".join(
|
| 189 |
[f"- [{res_title}]({res_link})" for res_title, res_link in zip(res_titles, res_links)])
|
| 190 |
except OutputParserException:
|
| 191 |
+
markdown_list = "There is no information relevant to your inquiry in my current resources. Please contact [FORTIFIED customer support](https://fortifiedhome.org/contact/) for further assistance."
|
| 192 |
return markdown_list
|
| 193 |
|
| 194 |
|
| 195 |
+
|
| 196 |
with gr.Blocks() as demo:
|
| 197 |
+
gr.Markdown("## FORTIFIED AI Assistant!")
|
| 198 |
+
gr.Markdown("### I'll try to answer any questions related to FORTIFIED program. Tell me what's on your mind?")
|
| 199 |
with gr.Row():
|
| 200 |
with gr.Column():
|
| 201 |
chatbot = gr.Chatbot(type="messages", height=400)
|
| 202 |
msg = gr.Textbox(label="Hit the Enter to send your question", placeholder="What's on your mind?", show_copy_button=True)
|
| 203 |
send = gr.Button("Send")
|
| 204 |
|
|
|
|
| 205 |
def user(user_message, history: list):
|
| 206 |
return "", history + [{"role": "user", "content": user_message}]
|
| 207 |
|
|
|
|
| 208 |
def bot(history: list):
|
| 209 |
bot_message = llm_response(history[-1]['content'])
|
| 210 |
history.append({"role": "assistant", "content": ""})
|
|
|
|
| 217 |
send.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
|
| 218 |
|
| 219 |
if __name__ == "__main__":
|
| 220 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -16,4 +16,5 @@ gradio
|
|
| 16 |
gradio_log
|
| 17 |
langchain_chroma
|
| 18 |
defusedxml
|
| 19 |
-
fonttools
|
|
|
|
|
|
| 16 |
gradio_log
|
| 17 |
langchain_chroma
|
| 18 |
defusedxml
|
| 19 |
+
fonttools
|
| 20 |
+
lark
|