From cc5ccaaca13a272e7e21bb463a181538923b7d5b Mon Sep 17 00:00:00 2001 From: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Date: Tue, 9 Apr 2024 14:35:25 +0800 Subject: [PATCH] fix: incomplete response (#3215) --- .../builtin/google/tools/google_search.py | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/api/core/tools/provider/builtin/google/tools/google_search.py b/api/core/tools/provider/builtin/google/tools/google_search.py index 964c7ef204..0b1978ad3e 100644 --- a/api/core/tools/provider/builtin/google/tools/google_search.py +++ b/api/core/tools/provider/builtin/google/tools/google_search.py @@ -70,43 +70,44 @@ class SerpAPI: raise ValueError(f"Got error from SerpAPI: {res['error']}") if typ == "text": + toret = "" if "answer_box" in res.keys() and type(res["answer_box"]) == list: - res["answer_box"] = res["answer_box"][0] + res["answer_box"] = res["answer_box"][0] + "\n" if "answer_box" in res.keys() and "answer" in res["answer_box"].keys(): - toret = res["answer_box"]["answer"] - elif "answer_box" in res.keys() and "snippet" in res["answer_box"].keys(): - toret = res["answer_box"]["snippet"] - elif ( + toret += res["answer_box"]["answer"] + "\n" + if "answer_box" in res.keys() and "snippet" in res["answer_box"].keys(): + toret += res["answer_box"]["snippet"] + "\n" + if ( "answer_box" in res.keys() and "snippet_highlighted_words" in res["answer_box"].keys() ): - toret = res["answer_box"]["snippet_highlighted_words"][0] - elif ( + for item in res["answer_box"]["snippet_highlighted_words"]: + toret += item + "\n" + if ( "sports_results" in res.keys() and "game_spotlight" in res["sports_results"].keys() ): - toret = res["sports_results"]["game_spotlight"] - elif ( + toret += res["sports_results"]["game_spotlight"] + "\n" + if ( "shopping_results" in res.keys() and "title" in res["shopping_results"][0].keys() ): - toret = res["shopping_results"][:3] - elif ( + toret += res["shopping_results"][:3] + "\n" + if ( "knowledge_graph" in res.keys() and "description" in res["knowledge_graph"].keys() ): - toret = res["knowledge_graph"]["description"] - elif "snippet" in res["organic_results"][0].keys(): - toret = res["organic_results"][0]["snippet"] - elif "link" in res["organic_results"][0].keys(): - toret = res["organic_results"][0]["link"] - elif ( + toret = res["knowledge_graph"]["description"] + "\n" + if "snippet" in res["organic_results"][0].keys(): + for item in res["organic_results"]: + toret += "content: " + item["snippet"] + "\n" + "link: " + item["link"] + "\n" + if ( "images_results" in res.keys() and "thumbnail" in res["images_results"][0].keys() ): thumbnails = [item["thumbnail"] for item in res["images_results"][:10]] toret = thumbnails - else: + if toret == "": toret = "No good search result found" elif typ == "link": if "knowledge_graph" in res.keys() and "title" in res["knowledge_graph"].keys() \