MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / _parse_snippets

Method _parse_snippets

bmtools/tools/google_serper/api.py:46–78  ·  view source on GitHub ↗
(self, results: dict)

Source from the content-addressed store, hash-verified

44 return self._parse_results(results)
45
46 def _parse_snippets(self, results: dict) -> List[str]:
47 snippets = []
48
49 if results.get("answerBox"):
50 answer_box = results.get("answerBox", {})
51 if answer_box.get("answer"):
52 return [answer_box.get("answer")]
53 elif answer_box.get("snippet"):
54 return [answer_box.get("snippet").replace("\n", " ")]
55 elif answer_box.get("snippetHighlighted"):
56 return answer_box.get("snippetHighlighted")
57
58 if results.get("knowledgeGraph"):
59 kg = results.get("knowledgeGraph", {})
60 title = kg.get("title")
61 entity_type = kg.get("type")
62 if entity_type:
63 snippets.append(f"{title}: {entity_type}.")
64 description = kg.get("description")
65 if description:
66 snippets.append(description)
67 for attribute, value in kg.get("attributes", {}).items():
68 snippets.append(f"{title} {attribute}: {value}.")
69
70 for result in results["organic"][: self.k]:
71 if "snippet" in result:
72 snippets.append(result["snippet"])
73 for attribute, value in result.get("attributes", {}).items():
74 snippets.append(f"{attribute}: {value}.")
75
76 if len(snippets) == 0:
77 return ["No good Google Search Result was found"]
78 return snippets
79
80 def _parse_results(self, results: dict) -> str:
81 return " ".join(self._parse_snippets(results))

Callers 1

_parse_resultsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected