Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -147,7 +147,6 @@ def generate_unique_string():
|
|
| 147 |
|
| 148 |
def llm_response(query, session_id):
|
| 149 |
titles, links, res_titles, res_links = [], [], [], []
|
| 150 |
-
print(session_id)
|
| 151 |
unique_id = session_id
|
| 152 |
config = {"configurable": {"thread_id": unique_id }}
|
| 153 |
try:
|
|
@@ -156,42 +155,57 @@ def llm_response(query, session_id):
|
|
| 156 |
start_on="human", end_on=("human", "tool"), include_system=True,)
|
| 157 |
modified_query = runnable.invoke({"input": query, "chat_history": filtered_history}).content
|
| 158 |
result = chain.invoke({"input": modified_query}, config=config)
|
|
|
|
|
|
|
| 159 |
answer = result['answer']['cited_answer'][0]["answer"]
|
| 160 |
history = get_session_history(unique_id)
|
| 161 |
history.add_user_message(modified_query)
|
| 162 |
history.add_ai_message(answer)
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
except OutputParserException:
|
| 196 |
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."
|
| 197 |
return markdown_list
|
|
|
|
| 147 |
|
| 148 |
def llm_response(query, session_id):
|
| 149 |
titles, links, res_titles, res_links = [], [], [], []
|
|
|
|
| 150 |
unique_id = session_id
|
| 151 |
config = {"configurable": {"thread_id": unique_id }}
|
| 152 |
try:
|
|
|
|
| 155 |
start_on="human", end_on=("human", "tool"), include_system=True,)
|
| 156 |
modified_query = runnable.invoke({"input": query, "chat_history": filtered_history}).content
|
| 157 |
result = chain.invoke({"input": modified_query}, config=config)
|
| 158 |
+
if not result['answer']['cited_answer'][0]["answer"]:
|
| 159 |
+
return "There is no direct information in our database relevant to your inquiry. Please contact [FORTIFIED customer support](https://fortifiedhome.org/contact/) for further assistance."
|
| 160 |
answer = result['answer']['cited_answer'][0]["answer"]
|
| 161 |
history = get_session_history(unique_id)
|
| 162 |
history.add_user_message(modified_query)
|
| 163 |
history.add_ai_message(answer)
|
| 164 |
+
|
| 165 |
+
if not result['answer']['cited_answer'][1]['citations']:
|
| 166 |
+
answer_with_citations = f"{answer}"
|
| 167 |
+
else:
|
| 168 |
+
citations = result['answer']['cited_answer'][1]['citations']
|
| 169 |
+
for citation in citations:
|
| 170 |
+
try:
|
| 171 |
+
edited_item = citation['citation'][1]["source"].replace("Articles/", "")
|
| 172 |
+
title, link = get_article_info(df, edited_item)
|
| 173 |
+
if title not in titles:
|
| 174 |
+
titles.append(title)
|
| 175 |
+
if link not in links:
|
| 176 |
+
links.append(link)
|
| 177 |
+
except (TypeError, KeyError, IndexError):
|
| 178 |
+
# Handle the error or simply pass if citation does not have the expected keys
|
| 179 |
+
continue
|
| 180 |
+
question_search = retriever.invoke(query)
|
| 181 |
+
for res_item in question_search:
|
| 182 |
+
edited_item = res_item.metadata["source"].replace("Articles/", "")
|
| 183 |
+
res_title, res_link = get_article_info(df, edited_item)
|
| 184 |
+
if res_title not in res_titles and res_title not in titles:
|
| 185 |
+
res_titles.append(res_title)
|
| 186 |
+
if res_link not in res_links and res_link not in links:
|
| 187 |
+
res_links.append(res_link)
|
| 188 |
+
|
| 189 |
+
# Build the answer with superscript citations
|
| 190 |
+
answer_with_citations = f"{answer}"
|
| 191 |
+
for i, (title, link) in enumerate(zip(titles, links), start=1):
|
| 192 |
+
answer_with_citations += f" <sup>[[{i}]({link})]</sup> " # Append superscript citation numbers to the answer text
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
if not links:
|
| 196 |
+
markdown_list = f"{answer_with_citations}"
|
| 197 |
+
else:
|
| 198 |
+
citations_section = "\n\nCitations:\n" + "\n".join(
|
| 199 |
+
[f"[{i}]: [{title}]({link})" for i, (title, link) in enumerate(zip(titles, links), start=1)]
|
| 200 |
+
)
|
| 201 |
+
markdown_list = f"{answer_with_citations}{citations_section}"
|
| 202 |
+
|
| 203 |
+
if not res_links:
|
| 204 |
+
return markdown_list
|
| 205 |
+
else:
|
| 206 |
+
markdown_list += f"\n\n\nHere is a list of articles that can provide more information about your inquiry:\n"
|
| 207 |
+
markdown_list += "\n".join([f"- [{res_title}]({res_link})" for res_title, res_link in zip(res_titles, res_links)])
|
| 208 |
+
|
| 209 |
except OutputParserException:
|
| 210 |
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."
|
| 211 |
return markdown_list
|