community[patch]: Fix for github issue #17690 (#20117)

…/17690

Thank you for contributing to LangChain!

- [x] **Fix Google Lens knowledge graph issue**: "langchain: community"
- Fix for [No "knowledge_graph" property in Google Lens API call from
SerpAPI](https://github.com/langchain-ai/langchain/issues/17690)


- [x] **PR message**: ***Delete this entire checklist*** and replace
with
- **Description:** handled the existence of keys in the json response of
Google Lens
- **Issue:** [No "knowledge_graph" property in Google Lens API call from
SerpAPI](https://github.com/langchain-ai/langchain/issues/17690)



- [x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/


If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, hwchase17.

Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
pull/20997/head
davidkgp 2 months ago committed by GitHub
parent a7a4630bf4
commit 28b0b0d863
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -65,7 +65,10 @@ class GoogleLensAPIWrapper(BaseModel):
return "Google Lens search failed"
xs = ""
if len(responseValue["knowledge_graph"]) > 0:
if (
"knowledge_graph" in responseValue
and len(responseValue["knowledge_graph"]) > 0
):
subject = responseValue["knowledge_graph"][0]
xs += f"Subject:{subject['title']}({subject['subtitle']})\n"
xs += f"Link to subject:{subject['link']}\n\n"
@ -74,10 +77,11 @@ class GoogleLensAPIWrapper(BaseModel):
xs += f"Title: {image['title']}\n"
xs += f"Source({image['source']}): {image['link']}\n"
xs += f"Image: {image['thumbnail']}\n\n"
xs += (
"Reverse Image Search"
+ f"Link: {responseValue['reverse_image_search']['link']}\n"
)
if "reverse_image_search" in responseValue:
xs += (
"Reverse Image Search"
+ f"Link: {responseValue['reverse_image_search']['link']}\n"
)
print(xs) # noqa: T201
docs = [xs]

Loading…
Cancel
Save