Enhanced geocoding with pgeocode and LLM fallback
Project description
GeoGPT
Enhanced geocoding with pgeocode and LLM fallback
Overview
GeoGPT is a Python package that provides enhanced geocoding capabilities by combining the efficiency of pgeocode with the power of large language models (LLMs). The package first attempts to geocode using pgeocode for direct lookups, and if that fails or provides incomplete information, it falls back to using an LLM to fill in the gaps.
Features
- Efficient geocoding with pgeocode
- LLM fallback for improved accuracy and coverage
- Support for multiple LLM providers (OpenAI, Google, Anthropic, DeepSeek)
- Distance calculations between locations
- Find nearby locations within a radius
- Command-line interface
Installation
pip install geo-gpt
Requirements
- Python 3.8+
- pgeocode
- pandas
- numpy
- langchain and related packages
- python-dotenv
- pycountry (optional, for enhanced country code handling)
Setting Up Environment Variables (IMPORTANT)
GeoGPT requires environment variables to be set for LLM access. Follow these steps:
-
Copy the template: A template file is provided in the repository:
cp .env.template .env
-
Edit the file: Open
.envand add your API key for the LLM provider you want to use:LLM_PROVIDER=openai # Choose: openai, google, anthropic, deepseek OPENAI_API_KEY=your-api-key-here LLM_MODEL_OPENAI=gpt-4o-mini # Optional, will use default if not specified -
Alternative: Set environment variables directly:
# For OpenAI export LLM_PROVIDER=openai export OPENAI_API_KEY=your-api-key-here export LLM_MODEL_OPENAI=gpt-4o-mini # For Google export LLM_PROVIDER=google export GOOGLE_API_KEY=your-api-key-here export LLM_MODEL_GOOGLE=gemini-1.5-pro # For Anthropic export LLM_PROVIDER=anthropic export ANTHROPIC_API_KEY=your-api-key-here export LLM_MODEL_ANTHROPIC=claude-3-5-sonnet-latest # For DeepSeek export LLM_PROVIDER=deepseek export DEEPSEEK_API_KEY=your-api-key-here export LLM_MODEL_DEEPSEEK=deepseek-chat
IMPORTANT: Always set environment variables BEFORE importing the package. In production environments, use secure environment variable management rather than .env files.
Basic Usage
# IMPORTANT: Set up environment variables first
import os
from dotenv import load_dotenv
# Load environment variables (in development)
load_dotenv() # Will look for .env in current directory
# Or set them directly (better for production)
os.environ["LLM_PROVIDER"] = "openai"
os.environ["OPENAI_API_KEY"] = "your-api-key-here"
os.environ["LLM_MODEL_OPENAI"] = "gpt-4o-mini" # Optional
# Now import and use the package
from geo_gpt import GeoCoder
# Initialize the geocoder
geocoder = GeoCoder()
# Geocode a location
location = geocoder.geocode(
city_name="San Francisco",
state_name="California",
country="US"
)
# Get the results
print(f"City: {location.city}")
print(f"Coordinates: {location.latitude}, {location.longitude}")
print(f"Postal Code: {location.postal_code}")
Command Line Interface
The command-line interface automatically attempts to load environment variables from a .env file in the current directory:
# Geocode a location
geo-gpt geocode --city "San Francisco" --state "California" --country "US" --pretty
# Calculate distance between locations
geo-gpt distance --origin "90210" --destination "10001" --country "US"
# Find nearby locations
geo-gpt nearby --reference "90210" --radius 10 --country "US" --pretty
API Reference
GeoCoder
The main class for geocoding operations.
geocoder = GeoCoder(cache_dir=None, llm_provider=None)
Parameters:
cache_dir(optional): Directory to store geocoding cache datallm_provider(optional): LLM provider to use for fallback ("openai", "google", "anthropic", "deepseek")
Methods:
-
geocode(city_name="", state_name="", zip_code="", business_name="", country="", use_llm=True) -> GeoLocationGeocode a location using pgeocode with LLM fallback.
-
calculate_distance(origin, destination, country_code=None) -> floatCalculate the distance between two locations.
-
find_nearby_locations(reference, radius_km=50.0, country_code=None, postal_codes=None) -> List[Dict]Find locations within a certain radius of a reference location.
GeoLocation
A data model representing a geocoded location.
Attributes:
country: Three-letter country codecountry_full: Full country namepostal_code: Postal/ZIP codecity: City namestate_full: State/province/region namestate_code: Two-letter state/province codelatitude: Latitude coordinatelongitude: Longitude coordinateaccuracy: Estimated accuracy ("high", "medium", "low")formatted_address: Complete formatted address
Environment Variables
These environment variables configure GeoGPT's behavior:
Required Variables
For geocoding with LLM fallback, you MUST set:
-
LLM_PROVIDER: Which LLM provider to use (required)- Options:
openai,google,anthropic,deepseek
- Options:
-
API key for your chosen provider (one of these is required):
OPENAI_API_KEY: Your OpenAI API keyGOOGLE_API_KEY: Your Google API keyANTHROPIC_API_KEY: Your Anthropic API keyDEEPSEEK_API_KEY: Your DeepSeek API key
Optional Variables
- Model name for your chosen provider (optional, defaults provided):
LLM_MODEL_OPENAI: OpenAI model (default: "gpt-4o")LLM_MODEL_GOOGLE: Google model (default: "gemini-2.0-flash-exp")LLM_MODEL_ANTHROPIC: Anthropic model (default: "claude-3-5-sonnet-latest")LLM_MODEL_DEEPSEEK: DeepSeek model (default: "deepseek-chat")
Troubleshooting
-
API Key errors: Make sure your API key is correct and has appropriate permissions
-
ImportError for pycountry: This optional dependency improves country code handling
pip install pycountry
-
LLM provider errors: Check that you've set the correct environment variables for your chosen provider
-
"No module named dotenv": Install python-dotenv if you're using .env files
pip install python-dotenv
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file geo_gpt-0.1.6.tar.gz.
File metadata
- Download URL: geo_gpt-0.1.6.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4383d1ca5649453a2df9ff3a65d03a77c0153579a188aa0d84a4e802d49a661a
|
|
| MD5 |
200ae647cb2f79779e2bd73b88e9f308
|
|
| BLAKE2b-256 |
d93d1d277597419d12477cce36ef657a0f35f1f4c88b74a6ea9c31daf6faecb8
|
File details
Details for the file geo_gpt-0.1.6-py3-none-any.whl.
File metadata
- Download URL: geo_gpt-0.1.6-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3397c5fc2b2ec9df60c3848b3d9a89ce889e759fd5f89b0ee0593d1fee40599e
|
|
| MD5 |
956c5590114c4142078b560db9e39dd9
|
|
| BLAKE2b-256 |
92525b2dd1e56e383e8cb88e0ff4e64c2a402f09508d24fb0a96f5344916ee02
|