IBHS commited on
Commit
b0cf829
·
verified ·
1 Parent(s): ecd1de3

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -33
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
- citations = result['answer']['cited_answer'][1]['citations']
164
- for citation in citations:
165
- edited_item = citation['citation'][1]["source"].replace("Articles/", "")
166
- title, link = get_article_info(df, edited_item)
167
- if title not in titles:
168
- titles.append(title)
169
- if link not in links:
170
- links.append(link)
171
- question_search = retriever.invoke(query)
172
- for res_item in question_search:
173
- edited_item = res_item.metadata["source"].replace("Articles/", "")
174
- res_title, res_link = get_article_info(df, edited_item)
175
- if res_title not in res_titles:
176
- res_titles.append(res_title)
177
- if res_link not in res_links:
178
- res_links.append(res_link)
179
-
180
- # Build the answer with superscript citations
181
- answer_with_citations = f"{answer}"
182
- for i, (title, link) in enumerate(zip(titles, links), start=1):
183
- answer_with_citations += f" <sup>[[{i}]({link})]</sup> " # Append superscript citation numbers to the answer text
184
-
185
- # Build the references section with clickable links
186
- citations_section = "\n\nCitations:\n" + "\n".join(
187
- [f"[{i}]: [{title}]({link})" for i, (title, link) in enumerate(zip(titles, links), start=1)]
188
- )
189
-
190
- # Combine answer and citations for final markdown output
191
- markdown_list = f"{answer_with_citations}{citations_section}"
192
- markdown_list += f"\n\n\nHere is a list of resources that can provide more information about your inquiry:\n"
193
- markdown_list += "\n".join(
194
- [f"- [{res_title}]({res_link})" for res_title, res_link in zip(res_titles, res_links)])
 
 
 
 
 
 
 
 
 
 
 
 
 
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