mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-15 14:36:54 +00:00
Enhance Google search tool SerpApi response (#10157)
Enhance SerpApi response which potential to have more relevant output. <img width="345" alt="Screenshot 2023-09-01 at 8 26 13 AM" src="https://github.com/langchain-ai/langchain/assets/10222402/80ff684d-e02e-4143-b218-5c1b102cbf75"> Query: What is the weather in Pomfret? **Before:** > I should look up the current weather conditions. ... Final Answer: The current weather in Pomfret is 73°F with 1% chance of precipitation and winds at 10 mph. **After:** > I should look up the current weather conditions. ... Final Answer: The current weather in Pomfret is 62°F, 1% precipitation, 61% humidity, and 4 mph wind. --- Query: Top team in english premier league? **Before:** > I need to find out which team is currently at the top of the English Premier League ... Final Answer: Liverpool FC is currently at the top of the English Premier League. **After:** > I need to find out which team is currently at the top of the English Premier League ... Final Answer: Man City is currently at the top of the English Premier League. --- Query: Top team in english premier league? **Before:** > I need to find out which team is currently at the top of the English Premier League ... Final Answer: Liverpool FC is currently at the top of the English Premier League. **After:** > I need to find out which team is currently at the top of the English Premier League ... Final Answer: Man City is currently at the top of the English Premier League. --- Query: Any upcoming events in Paris? **Before:** > I should look for events in Paris Action: Search ... Final Answer: Upcoming events in Paris this month include Whit Sunday & Whit Monday (French National Holiday), Makeup in Paris, Paris Jazz Festival, Fete de la Musique, and Salon International de la Maison de. **After:** > I should look for events in Paris Action: Search ... Final Answer: Upcoming events in Paris include Elektric Park 2023, The Aces, and BEING AS AN OCEAN.
This commit is contained in:
@@ -129,42 +129,92 @@ class SerpAPIWrapper(BaseModel):
|
||||
"""Process response from SerpAPI."""
|
||||
if "error" in res.keys():
|
||||
raise ValueError(f"Got error from SerpAPI: {res['error']}")
|
||||
if "answer_box" in res.keys() and isinstance(res["answer_box"], list):
|
||||
res["answer_box"] = res["answer_box"][0]
|
||||
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 (
|
||||
"answer_box" in res.keys()
|
||||
and "snippet_highlighted_words" in res["answer_box"].keys()
|
||||
):
|
||||
toret = res["answer_box"]["snippet_highlighted_words"][0]
|
||||
elif (
|
||||
"sports_results" in res.keys()
|
||||
and "game_spotlight" in res["sports_results"].keys()
|
||||
):
|
||||
toret = res["sports_results"]["game_spotlight"]
|
||||
if "answer_box_list" in res.keys():
|
||||
res["answer_box"] = res["answer_box_list"]
|
||||
if "answer_box" in res.keys():
|
||||
answer_box = res["answer_box"]
|
||||
if isinstance(answer_box, list):
|
||||
answer_box = answer_box[0]
|
||||
if "result" in answer_box.keys():
|
||||
return answer_box["result"]
|
||||
elif "answer" in answer_box.keys():
|
||||
return answer_box["answer"]
|
||||
elif "snippet" in answer_box.keys():
|
||||
return answer_box["snippet"]
|
||||
elif "snippet_highlighted_words" in answer_box.keys():
|
||||
return answer_box["snippet_highlighted_words"]
|
||||
else:
|
||||
answer = {}
|
||||
for key, value in answer_box.items():
|
||||
if not isinstance(value, (list, dict)) and not (
|
||||
type(value) == str and value.startswith("http")
|
||||
):
|
||||
answer[key] = value
|
||||
return str(answer)
|
||||
elif "events_results" in res.keys():
|
||||
return res["events_results"][:10]
|
||||
elif "sports_results" in res.keys():
|
||||
return res["sports_results"]
|
||||
elif "top_stories" in res.keys():
|
||||
return res["top_stories"]
|
||||
elif "news_results" in res.keys():
|
||||
return res["news_results"]
|
||||
elif "jobs_results" in res.keys() and "jobs" in res["jobs_results"].keys():
|
||||
return res["jobs_results"]["jobs"]
|
||||
elif (
|
||||
"shopping_results" in res.keys()
|
||||
and "title" in res["shopping_results"][0].keys()
|
||||
):
|
||||
toret = res["shopping_results"][:3]
|
||||
return res["shopping_results"][:3]
|
||||
elif "questions_and_answers" in res.keys():
|
||||
return res["questions_and_answers"]
|
||||
elif (
|
||||
"knowledge_graph" in res.keys()
|
||||
and "description" in res["knowledge_graph"].keys()
|
||||
"popular_destinations" in res.keys()
|
||||
and "destinations" in res["popular_destinations"].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"]
|
||||
return res["popular_destinations"]["destinations"]
|
||||
elif "top_sights" in res.keys() and "sights" in res["top_sights"].keys():
|
||||
return res["top_sights"]["sights"]
|
||||
elif (
|
||||
"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
|
||||
return str([item["thumbnail"] for item in res["images_results"][:10]])
|
||||
|
||||
snippets = []
|
||||
if "knowledge_graph" in res.keys():
|
||||
knowledge_graph = res["knowledge_graph"]
|
||||
title = knowledge_graph["title"] if "title" in knowledge_graph else ""
|
||||
if "description" in knowledge_graph.keys():
|
||||
snippets.append(knowledge_graph["description"])
|
||||
for key, value in knowledge_graph.items():
|
||||
if (
|
||||
type(key) == str
|
||||
and type(value) == str
|
||||
and key not in ["title", "description"]
|
||||
and not key.endswith("_stick")
|
||||
and not key.endswith("_link")
|
||||
and not value.startswith("http")
|
||||
):
|
||||
snippets.append(f"{title} {key}: {value}.")
|
||||
if "organic_results" in res.keys():
|
||||
first_organic_result = res["organic_results"][0]
|
||||
if "snippet" in first_organic_result.keys():
|
||||
snippets.append(first_organic_result["snippet"])
|
||||
elif "snippet_highlighted_words" in first_organic_result.keys():
|
||||
snippets.append(first_organic_result["snippet_highlighted_words"])
|
||||
elif "rich_snippet" in first_organic_result.keys():
|
||||
snippets.append(first_organic_result["rich_snippet"])
|
||||
elif "rich_snippet_table" in first_organic_result.keys():
|
||||
snippets.append(first_organic_result["rich_snippet_table"])
|
||||
elif "link" in first_organic_result.keys():
|
||||
snippets.append(first_organic_result["link"])
|
||||
if "buying_guide" in res.keys():
|
||||
snippets.append(res["buying_guide"])
|
||||
if "local_results" in res.keys() and "places" in res["local_results"].keys():
|
||||
snippets.append(res["local_results"]["places"])
|
||||
|
||||
if len(snippets) > 0:
|
||||
return str(snippets)
|
||||
else:
|
||||
toret = "No good search result found"
|
||||
return toret
|
||||
return "No good search result found"
|
||||
|
Reference in New Issue
Block a user