From 7752ea15a8259c5fbdf81f65d5b5065eb43f92a4 Mon Sep 17 00:00:00 2001 From: twosee Date: Tue, 19 Mar 2024 22:08:54 +0800 Subject: [PATCH] Fix Google search engine Fix the bug caused by inconsistent struct formats returned by Google and Bing. Not all engines will return the "id" field, so defensive handling has been implemented on the front end. --- search_with_lepton.py | 17 +++++++++++------ web/src/app/components/sources.tsx | 5 +++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/search_with_lepton.py b/search_with_lepton.py index 75c9ee2..ea552db 100644 --- a/search_with_lepton.py +++ b/search_with_lepton.py @@ -135,7 +135,12 @@ def search_with_google(query: str, subscription_key: str, cx: str): raise HTTPException(response.status_code, "Search engine error.") json_content = response.json() try: - contexts = json_content["items"][:REFERENCE_COUNT] + # convert to the same format as bing + items = json_content["items"][:REFERENCE_COUNT] + contexts = [ + {"id": c["cacheId"], "name": c["title"], "url": c["link"], "snippet": c["snippet"]} + for c in items + ] except KeyError: logger.error(f"Error encountered: {json_content}") return [] @@ -169,7 +174,7 @@ def search_with_serper(query: str, subscription_key: str): raise HTTPException(response.status_code, "Search engine error.") json_content = response.json() try: - # convert to the same format as bing/google + # convert to the same format as bing contexts = [] if json_content.get("knowledgeGraph"): url = json_content["knowledgeGraph"].get("descriptionUrl") or json_content["knowledgeGraph"].get("website") @@ -226,7 +231,7 @@ def search_with_searchapi(query: str, subscription_key: str): raise HTTPException(response.status_code, "Search engine error.") json_content = response.json() try: - # convert to the same format as bing/google + # convert to the same format as bing contexts = [] if json_content.get("answer_box"): @@ -266,14 +271,14 @@ def search_with_searchapi(query: str, subscription_key: str): {"name": c["title"], "url": c["link"], "snippet": c.get("snippet", "")} for c in json_content["organic_results"] ] - + if json_content.get("related_questions"): for question in json_content["related_questions"]: if question.get("source"): url = question.get("source").get("link", "") else: - url = "" - + url = "" + snippet = question.get("answer", "") if url and snippet: diff --git a/web/src/app/components/sources.tsx b/web/src/app/components/sources.tsx index 6e966d6..569ee1a 100644 --- a/web/src/app/components/sources.tsx +++ b/web/src/app/components/sources.tsx @@ -8,7 +8,8 @@ const SourceItem: FC<{ source: Source; index: number }> = ({ source, index, }) => { - const { id, name, url } = source; + const { name, url } = source; + const id = source.id || url; const domain = new URL(url).hostname; return (
= ({ sources }) => { {sources.length > 0 ? ( sources.map((item, index) => (