Spaces:
Sleeping
Sleeping
Upload main.py
Browse files
main.py
CHANGED
|
@@ -15,6 +15,8 @@ import logging
|
|
| 15 |
from langchain_core.exceptions import OutputParserException
|
| 16 |
import os
|
| 17 |
|
|
|
|
|
|
|
| 18 |
# Constants
|
| 19 |
PERSIST_DIRECTORY = "chroma_store"
|
| 20 |
K_VALUE = 5
|
|
@@ -88,43 +90,55 @@ def llm_response(query):
|
|
| 88 |
titles, links, res_titles, res_links = [], [], [], []
|
| 89 |
try:
|
| 90 |
result = chain.invoke({"input": query})
|
| 91 |
-
if not result['answer']['cited_answer']:
|
| 92 |
return "There are no articles relevant to your inquiry..."
|
| 93 |
answer = result['answer']['cited_answer'][0]["answer"]
|
| 94 |
-
if result['answer']['cited_answer']:
|
|
|
|
|
|
|
| 95 |
citations = result['answer']['cited_answer'][1]['citations']
|
| 96 |
for citation in citations:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
question_search = retriever.invoke(query)
|
| 104 |
for res_item in question_search:
|
| 105 |
edited_item = res_item.metadata["source"].replace("Articles/", "")
|
| 106 |
res_title, res_link = get_article_info(df, edited_item)
|
| 107 |
-
if res_title not in res_titles:
|
| 108 |
res_titles.append(res_title)
|
| 109 |
-
if res_link not in res_links:
|
| 110 |
res_links.append(res_link)
|
| 111 |
# Build the answer with superscript citations
|
| 112 |
answer_with_citations = f"{answer}"
|
| 113 |
for i, (title, link) in enumerate(zip(titles, links), start=1):
|
| 114 |
answer_with_citations += f" <sup>[[{i}]({link})]</sup> "
|
| 115 |
-
else:
|
| 116 |
-
answer_with_citations = f"{answer}"
|
| 117 |
|
| 118 |
# Build the references section with clickable links
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# Combine answer and citations for final markdown output
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
except OutputParserException:
|
| 129 |
markdown_list = "There are no articles relevant to your inquiry..."
|
| 130 |
return markdown_list
|
|
|
|
| 15 |
from langchain_core.exceptions import OutputParserException
|
| 16 |
import os
|
| 17 |
|
| 18 |
+
from sympy.codegen.ast import continue_
|
| 19 |
+
|
| 20 |
# Constants
|
| 21 |
PERSIST_DIRECTORY = "chroma_store"
|
| 22 |
K_VALUE = 5
|
|
|
|
| 90 |
titles, links, res_titles, res_links = [], [], [], []
|
| 91 |
try:
|
| 92 |
result = chain.invoke({"input": query})
|
| 93 |
+
if not result['answer']['cited_answer'][0]["answer"]:
|
| 94 |
return "There are no articles relevant to your inquiry..."
|
| 95 |
answer = result['answer']['cited_answer'][0]["answer"]
|
| 96 |
+
if not result['answer']['cited_answer'][1]['citations']:
|
| 97 |
+
answer_with_citations = f"{answer}"
|
| 98 |
+
else:
|
| 99 |
citations = result['answer']['cited_answer'][1]['citations']
|
| 100 |
for citation in citations:
|
| 101 |
+
try:
|
| 102 |
+
edited_item = citation['citation'][1]["source"].replace("Articles/", "")
|
| 103 |
+
title, link = get_article_info(df, edited_item)
|
| 104 |
+
if title not in titles:
|
| 105 |
+
titles.append(title)
|
| 106 |
+
if link not in links:
|
| 107 |
+
links.append(link)
|
| 108 |
+
except (TypeError, KeyError, IndexError):
|
| 109 |
+
# Handle the error or simply pass if citation does not have the expected keys
|
| 110 |
+
continue
|
| 111 |
question_search = retriever.invoke(query)
|
| 112 |
for res_item in question_search:
|
| 113 |
edited_item = res_item.metadata["source"].replace("Articles/", "")
|
| 114 |
res_title, res_link = get_article_info(df, edited_item)
|
| 115 |
+
if res_title not in res_titles and res_title not in titles:
|
| 116 |
res_titles.append(res_title)
|
| 117 |
+
if res_link not in res_links and res_link not in links:
|
| 118 |
res_links.append(res_link)
|
| 119 |
# Build the answer with superscript citations
|
| 120 |
answer_with_citations = f"{answer}"
|
| 121 |
for i, (title, link) in enumerate(zip(titles, links), start=1):
|
| 122 |
answer_with_citations += f" <sup>[[{i}]({link})]</sup> "
|
|
|
|
|
|
|
| 123 |
|
| 124 |
# Build the references section with clickable links
|
| 125 |
+
if not links:
|
| 126 |
+
markdown_list = f"{answer_with_citations}"
|
| 127 |
+
else:
|
| 128 |
+
citations_section = "\n\nCitations:\n" + "\n".join(
|
| 129 |
+
[f"[{i}]: [{title}]({link})" for i, (title, link) in enumerate(zip(titles, links), start=1)]
|
| 130 |
+
)
|
| 131 |
+
markdown_list = f"{answer_with_citations}{citations_section}"
|
| 132 |
|
| 133 |
# Combine answer and citations for final markdown output
|
| 134 |
+
|
| 135 |
+
if not res_links:
|
| 136 |
+
return markdown_list
|
| 137 |
+
else:
|
| 138 |
+
markdown_list += f"\n\n\nHere is a list of articles that can provide more information about your inquiry:\n"
|
| 139 |
+
markdown_list += "\n".join([f"- [{res_title}]({res_link})" for res_title, res_link in zip(res_titles, res_links)])
|
| 140 |
+
|
| 141 |
+
|
| 142 |
except OutputParserException:
|
| 143 |
markdown_list = "There are no articles relevant to your inquiry..."
|
| 144 |
return markdown_list
|