mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
115 lines
4.0 KiB
Python
115 lines
4.0 KiB
Python
"""Chain that calls Google Places API.
|
|
"""
|
|
|
|
import logging
|
|
from typing import Any, Dict, Optional
|
|
|
|
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
|
|
from langchain_core.utils import get_from_dict_or_env
|
|
|
|
|
|
class GooglePlacesAPIWrapper(BaseModel):
|
|
"""Wrapper around Google Places API.
|
|
|
|
To use, you should have the ``googlemaps`` python package installed,
|
|
**an API key for the google maps platform**,
|
|
and the environment variable ''GPLACES_API_KEY''
|
|
set with your API key , or pass 'gplaces_api_key'
|
|
as a named parameter to the constructor.
|
|
|
|
By default, this will return the all the results on the input query.
|
|
You can use the top_k_results argument to limit the number of results.
|
|
|
|
Example:
|
|
.. code-block:: python
|
|
|
|
|
|
from langchain_community.utilities import GooglePlacesAPIWrapper
|
|
gplaceapi = GooglePlacesAPIWrapper()
|
|
"""
|
|
|
|
gplaces_api_key: Optional[str] = None
|
|
google_map_client: Any #: :meta private:
|
|
top_k_results: Optional[int] = None
|
|
|
|
class Config:
|
|
"""Configuration for this pydantic object."""
|
|
|
|
extra = Extra.forbid
|
|
arbitrary_types_allowed = True
|
|
|
|
@root_validator()
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
"""Validate that api key is in your environment variable."""
|
|
gplaces_api_key = get_from_dict_or_env(
|
|
values, "gplaces_api_key", "GPLACES_API_KEY"
|
|
)
|
|
values["gplaces_api_key"] = gplaces_api_key
|
|
try:
|
|
import googlemaps
|
|
|
|
values["google_map_client"] = googlemaps.Client(gplaces_api_key)
|
|
except ImportError:
|
|
raise ImportError(
|
|
"Could not import googlemaps python package. "
|
|
"Please install it with `pip install googlemaps`."
|
|
)
|
|
return values
|
|
|
|
def run(self, query: str) -> str:
|
|
"""Run Places search and get k number of places that exists that match."""
|
|
search_results = self.google_map_client.places(query)["results"]
|
|
num_to_return = len(search_results)
|
|
|
|
places = []
|
|
|
|
if num_to_return == 0:
|
|
return "Google Places did not find any places that match the description"
|
|
|
|
num_to_return = (
|
|
num_to_return
|
|
if self.top_k_results is None
|
|
else min(num_to_return, self.top_k_results)
|
|
)
|
|
|
|
for i in range(num_to_return):
|
|
result = search_results[i]
|
|
details = self.fetch_place_details(result["place_id"])
|
|
|
|
if details is not None:
|
|
places.append(details)
|
|
|
|
return "\n".join([f"{i+1}. {item}" for i, item in enumerate(places)])
|
|
|
|
def fetch_place_details(self, place_id: str) -> Optional[str]:
|
|
try:
|
|
place_details = self.google_map_client.place(place_id)
|
|
place_details["place_id"] = place_id
|
|
formatted_details = self.format_place_details(place_details)
|
|
return formatted_details
|
|
except Exception as e:
|
|
logging.error(f"An Error occurred while fetching place details: {e}")
|
|
return None
|
|
|
|
def format_place_details(self, place_details: Dict[str, Any]) -> Optional[str]:
|
|
try:
|
|
name = place_details.get("result", {}).get("name", "Unknown")
|
|
address = place_details.get("result", {}).get(
|
|
"formatted_address", "Unknown"
|
|
)
|
|
phone_number = place_details.get("result", {}).get(
|
|
"formatted_phone_number", "Unknown"
|
|
)
|
|
website = place_details.get("result", {}).get("website", "Unknown")
|
|
place_id = place_details.get("result", {}).get("place_id", "Unknown")
|
|
|
|
formatted_details = (
|
|
f"{name}\nAddress: {address}\n"
|
|
f"Google place ID: {place_id}\n"
|
|
f"Phone: {phone_number}\nWebsite: {website}\n\n"
|
|
)
|
|
return formatted_details
|
|
except Exception as e:
|
|
logging.error(f"An error occurred while formatting place details: {e}")
|
|
return None
|