Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- .gitignore +2 -1
- articles_db.csv +57 -60
- chroma_store/chroma.sqlite3 +2 -2
- main.py +19 -82
.DS_Store
CHANGED
|
Binary files a/.DS_Store and b/.DS_Store differ
|
|
|
.gitignore
CHANGED
|
@@ -127,4 +127,5 @@ dmypy.json
|
|
| 127 |
|
| 128 |
# Pyre type checker
|
| 129 |
.pyre/
|
| 130 |
-
.idea
|
|
|
|
|
|
| 127 |
|
| 128 |
# Pyre type checker
|
| 129 |
.pyre/
|
| 130 |
+
.idea
|
| 131 |
+
Articles
|
articles_db.csv
CHANGED
|
@@ -1,60 +1,57 @@
|
|
| 1 |
-
|
| 2 |
-
1,
|
| 3 |
-
2,
|
| 4 |
-
3,
|
| 5 |
-
4,Metal
|
| 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 |
-
|
| 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 …
|
|
|
|
| 1 |
+
id,title,file_name,link
|
| 2 |
+
1,2008 Florida residential wind loss mitigation study.,2008_florida_residential_wind_loss_mitigation_study.pdf,https://www.floir.com/
|
| 3 |
+
2,A Comparative Impact Assessment of Hail Damage to Tile and Built-Up Roofing Systems: Technical Review and Field Study,a_comparative_impact_assessment_of_hail_damage_to_tile_and_built_up_roofing_systems_technical_review_and_field_study.pdf,https://doi.org/10.20944/preprints202407.2020.v1
|
| 4 |
+
3,"A comprehensive observational study of graupel and hail terminal velocity, mass flux, and kinetic energy",a_comprehensive_observational_study_of_grauple_and_hail_terminal_velocities_mass_flux_and_kinetic_energies.pdf,https://doi.org/10.1175/JAS-D-18-0035.1
|
| 5 |
+
4,Analysis of Standing Seam Metal Roofs Subjected to Extreme Wind Loads,analysis_of_standing_seam_metal_roofs_subjected_to_extreme_wind_loads.pdf,https://ir.lib.uwo.ca/etd/8489/
|
| 6 |
+
5,Asphalt Shingle Durability,asphalt_shingle_durability.pdf,https://ibhs.org/?a=download&key=34b09091cee84523c013ee3f7fde4ee1
|
| 7 |
+
6,Asphalt Shingle Hail Impact Performance Guide,asphalt_shingle_hail_impact_performance_guide.pdf,https://ibhs.org/?a=download&key=645d49bb0ac86c1c50595cdc7d3f6adc
|
| 8 |
+
7,Asphalt Shingle Patterns Identification Guide,asphalt_shingle_patterns_identification_guide_ibhs.pdf,https://ibhs.org/?a=download&key=3f3a09d69e82e7b2e171e531be8ef3a9
|
| 9 |
+
8,"Cedar Rapids, Iowa and the 2020 Midwest Derecho: The Performance of Asphalt Shingle Roofs in Extreme Severe Convective Storm Winds",cedar_rapids_iowa_and_the_2020_midwest_derecho_the_performance_of_asphalt_shingle_roofs_in_extreme_severe_convective_storm_winds.pdf,https://ibhs.org/?a=download&key=e15f92e76ecd052fa60e183cc022fd4e
|
| 10 |
+
9,Claims Analysis Study of May 24 2011 Hailstorms in Dallas-Fort Worth,claims_analysis_study_of_may_24_2011_hailstorms_in_dallas_fort_worth.pdf,https://ibhs.org/wp-content/uploads/Claims-Analysis-Study-of-May-24-2011-Hailstorms-in-Dallas-Fort-Worth.pdf
|
| 11 |
+
10,"Community Collaborative Rain, Hail and Snow (CoCoRaHS) Measurement Program Reanalysis of Hailstorm Characteristics 1998-2018",community_collaborative_rain_hail_and_snow_(cocorahs)_measurement_program_reanalysis_of_hailstorm_characteristics_1998_2018.pdf,https://ibhs.org/?a=download&key=3749d26907ffc99bb6729e601e1ab341
|
| 12 |
+
11,Damage Mechanics Based Analysis of Hail Impact on Metal Roofs,damage_mechanics_based_analysis_of_hail_impact_on_metal_roofs.pdf,https://doi.org/10.1016/j.engfracmech.2022.108688
|
| 13 |
+
12,Developing a Test Method for a Very Severe Hail Rating for Low Slope Roofing Assemblies,developing_a_test_method_for_a_very_severe_hail_rating_for_low_slope_roofing_assemblies.pdf,https://doi.org/10.1520/STP159020150022
|
| 14 |
+
13,Effects of hail on Metal Roofing Systems,the_effects_of_hail_on_metal_roofing_systems.pdf,https://iibec.org/wp-content/uploads/2014-CTS-koontz-white.pdf
|
| 15 |
+
14,Evaluating the Hardness Characteristics of Hail through Compressive Strength Measurements,evaluating_the_hardness_characteristics_of_hail_through_compressive_strength_measurements.pdf,https://doi.org/10.1175/JTECH-D-15-0081.1
|
| 16 |
+
15,Failure Mechanisms and Load Paths in a Standing Seam Metal Roof under Extreme Wind Loads,failure_mechanisms_and_load_paths_in_a_standing_seam_metal_roof_under_extreme_wind_loads.pdf,https://doi.org/10.1016/j.engstruct.2023.116954
|
| 17 |
+
16,Full-Scale Study of Wind Loads on Roof Tiles and Felt Underlay and Comparisons With Design Data,full_scale_study_of_wind_loads_on_roof_tiles_and_felt_underlay_and_comparisons_with_design_data.pdf,https://dx.doi.org/10.12989/was.2007.10.6.495
|
| 18 |
+
17,Full-Scale Testing to Evaluate the Performance of Standing Seam Metal Roofs Under Simulated Wind Loading,full_scale_testing_to_evaluate_the_performance_of_standing_seam_metal_roofs_under_simulated_wind_loading.pdf,https://doi.org/10.1016/j.engstruct.2015.10.006
|
| 19 |
+
18,Full-scale Wind Tunnel Testing of North American and Australian Tile Roofing Systems,full_scale_wind_tunnel_testing_of_tile_roofing_systems_ibhs.pdf,https://ibhs.org/wind/full-scale-wind-tunnel-testing-of-roof-tile-systems/
|
| 20 |
+
19,Hail damage threshold sizes for common roofing materials,hail_damage_threshold_sizes_for_common_roofing_materials.pdf,https://www.researchgate.net/publication/327022658_HAIL_DAMAGE_THRESHOLD_SIZES_FOR_COMMON_ROOFING_MATERIALS
|
| 21 |
+
20,Hail Impact Performance of Low-Slope Metal Roofing,hail_impact_performance_of_low_slope_metal_roofing.pdf,https://ibhs.org/?a=download&key=9959d1924143a289caa917cecc9e80e0
|
| 22 |
+
21,Hail in the Front Range of the Rockies and High Plains: Is it more damaging?,hail_in_the_front_range_of_the_rockies_and_high_plains_is_it_more_damaging.pdf,https://ibhs.org/?a=download&key=e1b999c263f65ada940d4d6b120ba69c
|
| 23 |
+
22,Hailstone Shapes,hailstone_shapes.pdf,https://doi.org/10.1175/JAS-D-20-0250.1
|
| 24 |
+
23,Hurricane Harvey Wind Damage Investigation,hurricane_harvey_wind_damage_investigation_ibhs.pdf,https://ibhs1.wpenginepowered.com/wp-content/uploads/wpmembers/files/Hurricane-Harvey-Wind-Damage-Investigation_IBHS.pdf
|
| 25 |
+
24,Hurricane Ike Nature’s Force vs. Structural Strength,hurricane_ike_natures_force_vs_structural_strength_ibhs.pdf,https://ibhs.org/wp-content/uploads/member_docs/Hurricane-Ike-Natures-Force-vs-Structural-Strength_IBHS.pdf
|
| 26 |
+
25,Hurricane Michael,ibhs_report_hurricane_michael_post_investigation.pdf,https://ibhs.org/?a=download&key=cbd8660b1421b0c86fdfaa05cebea7c8
|
| 27 |
+
26,IBHS ASTM D3161Results by Manufacturer for Roof Slope and Installation Temperature,ibhs_astm_d3161_results_by_manufacturer_for_roof_slope_and_installation_temperature.pdf,https://ibhs.org/?a=download&key=2964423feb04da3e8a262313a8d1fdc1
|
| 28 |
+
27,IBHS Impact Resistance Test Protocol for Asphalt Shingles,ibhs_impact_resistance_test_protocol_for_asphalt_shingles.pdf,https://ibhs.org/ibhs-impact-resistance-test-protocol-for-asphalt-shingles/
|
| 29 |
+
28,IBHS Roof Aging Farm: Five-Year Hail Performance Summary,ibhs_roof_aging_farm_five_year_hail_performance_summary.pdf,https://ibhs.org/?a=download&key=f4b7e4d0d1f6f6c17d65671ddf469ec5
|
| 30 |
+
29,IBHS Roof Aging Farm: Five-Year Wind Performance Summary,ibhs_roof_aging_farm_five_year_wind_performance_summary.pdf,https://ibhs.org/?a=download&key=ab89007603000da08a9aafc1db3e78bd
|
| 31 |
+
30,Impact Resistance of Synthetic Composite Shingles,impact_resistance_of_synthetic_composite_shingles.pdf,https://ibhs.org/?a=download&key=d4363d52d5bb8867676d1a9bcc2e81b2
|
| 32 |
+
31,Impact testing of high concentrations of small hail,impact_testing_of_high_concentrations_of_small_hail.pdf,https://ibhs.org/?a=download&key=5d014d376a0d334c3633317bf93e76d6
|
| 33 |
+
33,Impact-Resistant Shingle Performance Ratings,impact_resistant_shingle_performance_ratings_–_insurance_institute_for_business_&_home_safety.pdf,https://ibhs.org/hail/shingle-performance-ratings/
|
| 34 |
+
34,Investigation of the Wind Resistance of Asphalt Shingles,investigation_of_the_wind_resistance_of_asphalt_shingles.pdf,https://doi.org/10.1061/9780784412626.045
|
| 35 |
+
35,New Asphalt Shingle Hail Impact Performance Test Protocol and Damage Assessment,new_asphalt_shingle_hail_impact_performance_test_protocol_and_damage_assessment_natural_hazards_review.pdf,https://doi.org/10.1061/(ASCE)NH.1527-6996.0000509
|
| 36 |
+
36,Observations of Building Performance in southwest Florida during Hurricane Ian (2022): Roof Cover Damage Assessment on Residential and Light Commercial Structures,building_performance_in_southwest_florida_during_hurricane_ian_2022_part_i.pdf,https://ibhs.org/wind/building-performance-in-sw-florida-during-hurricane-ian-2022/
|
| 37 |
+
37,Observations of Building Performance in southwest Florida during Hurricane Ian 2022: Performance of the Florida Building Code,building_performance_in_hurricane_ian_partii_fbc.pdf,https://ibhs1.wpenginepowered.com/wp-content/uploads/HurricaneIan_PartII_FBC.pdf
|
| 38 |
+
38,Performance of Metal Roofing to Realistic Wind Loads and Evaluation of Current Test Standards,performance_of_metal_roofing_to_realistic_wind_loads_and_evaluation_of_current_test_standards_icwe_ibhs.pdf,https://ibhs.org/wp-content/uploads/member_docs/Performance-of-Metal-Roofing-to-Realistic-Wind-Loads_ICWE_IBHS.pdf
|
| 39 |
+
39,Performance of Roof Tiles under Simulated Hurricane Impact,performance_of_roof_tiles_under_simulated_hurricane_impact.pdf,https://doi.org/10.1061/(ASCE)1076-0431(2009)15:1(26)
|
| 40 |
+
40,Post-2004 Hurricane Field Survey of Residential Building Performance,post_2004_hurricane_field_survey_of_residential_building_performance.pdf,https://doi.org/10.1061/(ASCE)NH.1527-6996.0000044
|
| 41 |
+
41,Prediction of extent of damage to metal roof panels under hail impact,prediction_of_extent_of_damage_to_metal_roof_panels_under_hail_impact.pdf,https://doi.org/10.1016/j.engstruct.2019.02.036
|
| 42 |
+
42,Relative Impact Resistance of Asphalt Shingles Summary of UL2218 Impact Tests,relative_impact_resistance_of_asphalt_shingles_summary_of_ul2218_impact_tests_ibhs.pdf,https://ibhs1.wpenginepowered.com/wp-content/uploads/wpmembers/files/Relative-Impact-Resistance-of-Asphalt-Shingles_IBHS.pdf
|
| 43 |
+
43,Study of wind loads on asphalt shingles using full-scale experimentation,study_of_wind_loads_on_asphalt_shingles_using_full_scale_experimentation.pdf,http://dx.doi.org/10.1016/j.jweia.2022.105005
|
| 44 |
+
44,Surviving nature's fury: Performance of asphalt shingle roofs in the real world,surviving_natures_fury_performance_of_asphalt_shingle_roofs_in_the_real_world_ibhs.pdf,https://ibhs.org/wp-content/uploads/member_docs/Surviving-Natures-Fury-Performance-of-Asphalt-Shingle-Roofs-in-the-Real-World_IBHS.pdf
|
| 45 |
+
45,Terminal velocities and kinetic energies of natural hailstones,geophysical_research_letters___2014___heymsfield___terminal_velocities_and_kinetic_energies_of_natural_hailstones.pdf,https://doi.org/10.1002/2014GL062324
|
| 46 |
+
46,The Effect of Roof Age on Asphalt Shingle Performance: Hurricane Rita to Hurricane Laura,the_effect_of_roof_age_on_asphalt_shingle_performance_hurricane_rita_to_hurricane_laura.pdf,https://ibhs.org/?a=download&key=a02fc5cec325e2120de41895fbb47933
|
| 47 |
+
47,Using 3D Laser Scanning Technology to Create Digital Models of Hailstones,using_3d_laser_scanning_technology_to_create_digital_models_of_hailstones.pdf,https://doi.org/10.1175/BAMS-D-15-00314.1
|
| 48 |
+
48,Water Leakage Vulnerabilities for Shingles With Hail Damage,water_leakage_vulnerabilities_for_shingles_with_hail_damage.pdf,https://ibhs.org/?a=download&key=4953e3320e70248b837125ddeddbba01
|
| 49 |
+
49,Wind Damage to Envelopes of Houses and Consequent Insurance Losses,wind_damage_to_envelopes_of_houses_and_consequent_insurance_losses.pdf,https://doi.org/10.1016/0167-6105(94)90023-X
|
| 50 |
+
50,Wind Effects on Roofs with High Profile Tiles: Experimental Study,wind_effects_on_roofs_with_high_profile_tiles_experimental_study.pdf,https://doi.org/10.1061/(ASCE)AE.1943-5568.0000156
|
| 51 |
+
51,"Wind Loading on Ridge, Hip, and Perimeter Roof Tiles: A Full-Scale Experimental Study",wind_loading_on_ridge_hip_and_perimeter_roof_tiles__a_full_scale_experimental_study.pdf,https://doi.org/10.1016/j.jweia.2017.04.002
|
| 52 |
+
52,Wind Loads on Discontinuous Metal Roofing,wind_loads_on_discontinuous_metal_roofing_ibhs.pdf,https://ibhs.org/wp-content/uploads/member_docs/Wind-Loads-on-Discontinuous-Metal-Roofing_IBHS.pdf
|
| 53 |
+
53,Wind Loads on Discontinuous Metal Roofing – Executive Summary,wind_loads_on_discontinuous_metal_roofing_executive_summary_ibhs.pdf,https://ibhs.org/?a=download&key=7e7fa645e98982df9dee911ca8904bc1
|
| 54 |
+
54,Wind Uplift of Asphalt Shingles: Sensitivity to Roof Slope and Installation Temperature,wind_uplift_of_asphalt_shingles_ibhs.pdf,https://ibhs1.wpenginepowered.com/wp-content/uploads/wpmembers/files/Wind-Uplift-of-Asphalt-Shingles_IBHS.pdf
|
| 55 |
+
55,Wind Uplift Resistance of Artificially and Naturally Aged Asphalt Shingles,wind_uplift_resistance_of_artificially_and_naturally_aged_asphalt_shingles.pdf,https://doi.org/10.1061/(ASCE)AE.1943-5568.0000158
|
| 56 |
+
56,Wind Vulnerability Analysis of Standing Seam Roof System Considering Fatigue Damage,wind_vulnerability_analysis_of_standing_seam_roof_system_considering_fatigue_damage.pdf,https://doi.org/10.1016/j.tws.2023.110550
|
| 57 |
+
57,Evaluating Hail Damage Using Property Insurance Claims Data,evaluating_hail_damage_using_property_insurance_claims_data.pdf,https://doi.org/10.1175/WCAS-D-15-0011.1
|
|
|
|
|
|
|
|
|
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:a7c01e5e0c9cdeaee0314bcfd0f09a4dbcb9c733650c346bbe0cef1de56e68f7
|
| 3 |
+
size 38989824
|
main.py
CHANGED
|
@@ -13,36 +13,16 @@ import gradio as gr
|
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
and
|
| 40 |
-
|
| 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,25 +38,6 @@ Here are the articles:{context}"""
|
|
| 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,7 +47,7 @@ def format_docs_xml(docs: List[Document]) -> str:
|
|
| 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,12 +63,8 @@ chain = RunnablePassthrough.assign(context=retrieve_docs).assign(
|
|
| 102 |
)
|
| 103 |
|
| 104 |
def get_article_info(df, file_name):
|
| 105 |
-
|
| 106 |
-
|
| 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,36 +83,12 @@ def vectordb_search(query):
|
|
| 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 |
-
|
| 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,6 +105,8 @@ def llm_response(query):
|
|
| 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,29 +117,31 @@ def llm_response(query):
|
|
| 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
|
| 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
|
| 192 |
return markdown_list
|
| 193 |
|
| 194 |
|
| 195 |
-
|
| 196 |
with gr.Blocks() as demo:
|
| 197 |
-
gr.Markdown("##
|
| 198 |
-
gr.Markdown("###
|
| 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,4 +154,4 @@ with gr.Blocks() as demo:
|
|
| 217 |
send.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
|
| 218 |
|
| 219 |
if __name__ == "__main__":
|
| 220 |
-
demo.launch()
|
|
|
|
| 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 |
+
xml_system = """You're a helpful AI assistant. Given a user question and some scientific literature
|
| 23 |
+
documents which highlight research on different roof cover materials (e.g., asphalt shingles, metal, tile)
|
| 24 |
+
and their performance against natural hazards(e.g., wind, hail), answer the user
|
| 25 |
+
question. If none of the articles answer the question, simply say that there are no articles relevant to your inquiry.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 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 |
return f"\n\n<sources>\n{chr(10).join(formatted_docs)}\n</sources>"
|
| 48 |
|
| 49 |
|
| 50 |
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 51 |
rag_chain_from_docs = (
|
| 52 |
RunnablePassthrough.assign(context=(lambda x: format_docs_xml(x["context"])))
|
| 53 |
| xml_prompt
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
def get_article_info(df, file_name):
|
| 66 |
+
title = df[df["file_name"] == file_name]["title"].iloc[0]
|
| 67 |
+
link = df[df["file_name"] == file_name]["link"].iloc[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return title, link
|
| 69 |
|
| 70 |
|
|
|
|
| 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 |
+
result = chain.invoke({"input": query})
|
|
|
|
|
|
|
|
|
|
| 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 |
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 |
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 articles that can provide more information about your inquiry:\n"
|
| 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 are no articles relevant to your inquiry..."
|
| 128 |
return markdown_list
|
| 129 |
|
| 130 |
|
|
|
|
| 131 |
with gr.Blocks() as demo:
|
| 132 |
+
gr.Markdown("## Article Finder Based on Your Question!")
|
| 133 |
+
gr.Markdown("### Tell me what's on your mind, and I'll find you the most relevant articles.")
|
| 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 |
send.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
+
demo.launch(share=True)
|