Notebooks
E
Elastic
09 Geospatial Search

09 Geospatial Search

openai-chatgptlangchain-pythongeospatial-llmchatgptgenaielasticsearchelasticopenaiAIchatlogvectordatabasePythonsearchgenaistacksupporting-blog-contentvectorelasticsearch-labslangchainapplications

Install and Connect

To get started, we'll need to connect to our Elastic deployment using the Python client. Because we're using an Elastic Cloud deployment, we'll use the Cloud ID to identify our deployment.

[1]

Next, we need to import the modules we need. 🔐 NOTE: getpass enables us to securely prompt the user for credentials without echoing them to the terminal, or storing it in memory.

[2]

Now we can instantiate the Python Elasticsearch client.

First we prompt the user for their password and Cloud ID. Then we create a client object that instantiates an instance of the Elasticsearch class

[3]

Enable Telemetry

Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See telemetry.py for details. Thank you!

[4]
Telemetry enabled for "09-geospatial-search". Thank you!

Test the Client

Before you continue, confirm that the client has connected with this test.

[4]
{'name': 'instance-0000000001', 'cluster_name': 'c2826e6e8ae74e46b5911a3c89af31cf', 'cluster_uuid': 'jpiDThQURAKY93YeiDkfdA', 'version': {'number': '8.13.4', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': 'da95df118650b55a500dcc181889ac35c6d8da7c', 'build_date': '2024-05-06T22:04:45.107454559Z', 'build_snapshot': False, 'lucene_version': '9.10.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}

Download and Deploy ELSER Model

In this example, we are going to download and deploy the ELSER model in our ML node. Make sure you have an ML node in order to run the ELSER model.

[ ]
Model deleted successfully, We will proceed with creating one
ObjectApiResponse({'model_id': '.elser_model_2_linux-x86_64', 'model_type': 'pytorch', 'model_package': {'packaged_model_id': 'elser_model_2_linux-x86_64', 'model_repository': 'https://ml-models.elastic.co', 'minimum_version': '11.0.0', 'size': 274756282, 'sha256': 'be69211494bf9cdc57a7aa0ee06814fcccf999407237816c9d9f0963858e2a61', 'metadata': {}, 'tags': [], 'vocabulary_file': 'elser_model_2_linux-x86_64.vocab.json', 'platform_architecture': 'linux-x86_64'}, 'platform_architecture': 'linux-x86_64', 'created_by': 'api_user', 'version': '12.0.0', 'create_time': 1715935799552, 'model_size_bytes': 0, 'estimated_operations': 0, 'license_level': 'platinum', 'description': 'Elastic Learned Sparse EncodeR v2 optimized for linux-x86_64', 'tags': ['elastic'], 'metadata': {}, 'input': {'field_names': ['text_field']}, 'inference_config': {'text_expansion': {'vocabulary': {'index': '.ml-inference-native-000002'}, 'tokenization': {'bert': {'do_lower_case': True, 'with_special_tokens': True, 'max_sequence_length': 512, 'truncate': 'first', 'span': -1}}}}, 'location': {'index': {'name': '.ml-inference-native-000002'}}})

The above command will download the ELSER model. This will take a few minutes to complete. Use the following command to check the status of the model download.

[7]
ELSER Model is downloaded but not ready to be deployed.
ELSER Model is downloaded but not ready to be deployed.
ELSER Model is downloaded but not ready to be deployed.
ELSER Model is downloaded but not ready to be deployed.
ELSER Model is downloaded but not ready to be deployed.
ELSER Model is downloaded and ready to be deployed.

Once the model is downloaded, we can deploy the model in our ML node. Use the following command to deploy the model.

[8]
ELSER Model is currently being deployed.
ELSER Model is currently being deployed.
ELSER Model has been successfully deployed.

Indexing Documents with ELSER

In order to use ELSER on our Elastic Cloud deployment we'll need to create an ingest pipeline that contains an inference processor that runs the ELSER model. Let's add that pipeline using the put_pipeline method.

[9]
ObjectApiResponse({'acknowledged': True})

Preparing the AirBnB Listings

Next up we need to prepare the index. We will map everything as keyword unless otherwise specified. We will also map the name and the description of the listing as sparse_vectors using ELSER.

[15]
ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'airbnb-listings'})

Downloading the AirBnB data

next up we will download the AirBnB listings csv and upload it to Elasticsearch. This can take a couple of minutes! The AirBnB listing is roughly ~80mb of CSV expanded and roughly 40.000 documents. In the code below we added an if condition to only process the first 5.000 documents.

[16]
Processing document 0
Processing document 250
Processing document 500
Processing document 750
Processing document 1000
Processing document 1250
Processing document 1500
Processing document 1750
Processing document 2000
Processing document 2250
Processing document 2500
Processing document 2750
Processing document 3000
Processing document 3250
Processing document 3500
Processing document 3750
Processing document 4000
Processing document 4250
Processing document 4500
Processing document 4750
Processing document 5000
Processing document 5250
Processing document 5500
Processing document 5750
Processing document 6000
Processing document 6250
Processing document 6500
Processing document 6750
Processing document 7000
Processing document 7250
Processing document 7500
Processing document 7750
Processing document 8000
Processing document 8250
Processing document 8500
Processing document 8750
Processing document 9000
Processing document 9250
Processing document 9500
Processing document 9750
Processing document 10000
Processing document 10250
Processing document 10500
Processing document 10750
Processing document 11000
Processing document 11250
Processing document 11500
Processing document 11750
Processing document 12000
Processing document 12250
Processing document 12500
Processing document 12750
Processing document 13000
Processing document 13250
Processing document 13500
Processing document 13750
Processing document 14000
Processing document 14250
Processing document 14500
Processing document 14750
Processing document 15000
Processing document 15250
Processing document 15500
Processing document 15750
Processing document 16000
Processing document 16250
Processing document 16500
Processing document 16750
Processing document 17000
Processing document 17250
Processing document 17500
Processing document 17750
Processing document 18000
Processing document 18250
Processing document 18500
Processing document 18750
Processing document 19000
Processing document 19250
Processing document 19500
Processing document 19750
Processing document 20000
Processing document 20250
Processing document 20500
Processing document 20750
Processing document 21000
Processing document 21250
Processing document 21500
Processing document 21750
Processing document 22000
Processing document 22250
Processing document 22500
Processing document 22750
Processing document 23000
Processing document 23250
Processing document 23500
Processing document 23750
Processing document 24000
Processing document 24250
Processing document 24500
Processing document 24750
Processing document 25000
Processing document 25250
Processing document 25500
Processing document 25750
Processing document 26000
Processing document 26250
Processing document 26500
Processing document 26750
Processing document 27000
Processing document 27250
Processing document 27500
Processing document 27750
Processing document 28000
Processing document 28250
Processing document 28500
Processing document 28750
Processing document 29000
Processing document 29250
Processing document 29500
Processing document 29750
Processing document 30000
Processing document 30250
Processing document 30500
Processing document 30750
Processing document 31000
Processing document 31250
Processing document 31500
Processing document 31750
Processing document 32000
Processing document 32250
Processing document 32500
Processing document 32750
Processing document 33000
Processing document 33250
Processing document 33500
Processing document 33750
Processing document 34000
Processing document 34250
Processing document 34500
Processing document 34750
Processing document 35000
Processing document 35250
Processing document 35500
Processing document 35750
Processing document 36000
Processing document 36250
Processing document 36500
Processing document 36750
Processing document 37000
Processing document 37250
Processing document 37500
Processing document 37750
Processing document 38000
Processing document 38250
Processing document 38500
Processing document 38750
Processing document 39000
Processing document 39250
(39319, [])

Prepare the MTA subway stations index

We need to prepare the index and make sure that we treat the geo location as a geo location.

[17]
ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'mta-stations'})

Index the MTA data

We now need to index the data for the MTA.

[18]
(496, [])

Prepare points of interest

Same as before. We want to index the points of interests and use ELSER to make sure that any semantic searches are working. E.g. searching for sights with gardens should return Central Park even though it does not contain garden in the name.

[19]
ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'points-of-interest'})

Download points of interest

The the_geom looks like this: POINT (-74.00701717096757 40.724634757833414) which is formatted as a Well-Known Text point format and we officially support this. I personally always like to store my coordinates in lat & lon as an Object to make sure that there are no confusions.

[20]
(20481, [])

Now we have everything prepped

First let's see how well ELSER does with a "geo" query. Let's as it for an AirBnB next to Central Park and Empire State Building. Also we are just looking at the description, not the name or the about author as of now. Let's keep it simple.

[21]
Score: 20.003891
Title: Gorgeous 1 Bedroom - Upper East Side Manhattan -
Location: {'lon': -73.95856, 'lat': 40.76701}
Document ID: 3kn2hY8BHToGwgcU3erj

Score: 19.757303
Title: ~Lovely 4 Bedroom ~ Midtown ~ Grand Central~
Location: {'lon': -73.98293292692072, 'lat': 40.74578747448903}
Document ID: dknphY8BHToGwgcU-M6A

Score: 19.757303
Title: Lovely 4 bedrooms~Midtown grand central ~Park ave
Location: {'lon': -73.98119, 'lat': 40.74593}
Document ID: V0nxhY8BHToGwgcUWd78

Score: 19.71977
Title: The Center of Manhattan (West 57th Street) Apt3A
Location: {'lon': -73.9880189, 'lat': 40.7692096}
Document ID: n0nIhY8BHToGwgcU_IVZ

Score: 19.681244
Title: Lovely and spacious studio @Park Avenue (Midtown)
Location: {'lon': -73.97904, 'lat': 40.74853}
Document ID: 5knehY8BHToGwgcUmLen

Score: 19.443813
Title: Sunny Bedroom in quirky apartment
Location: {'lon': -73.98198, 'lat': 40.74792}
Document ID: l0n2hY8BHToGwgcUZum-

Score: 19.380022
Title: Midtown Manhattan Apt
Location: {'lon': -73.98094, 'lat': 40.75053}
Document ID: tUn4hY8BHToGwgcU1u85

Score: 19.33765
Title: Spacious 4BR ~ Empire ST ~ Grand Central~ Park Ave
Location: {'lon': -73.98324545084193, 'lat': 40.745539903474224}
Document ID: tUnrhY8BHToGwgcUFtDL

Score: 19.33765
Title: *NEW* Spacious 4BR ~Park Ave~Empire ST~Murray Hill
Location: {'lon': -73.98241, 'lat': 40.74386}
Document ID: 8UnxhY8BHToGwgcUWd38

Score: 19.135761
Title: Park Ave Apt in the Heart of NYC
Location: {'lon': -73.97944, 'lat': 40.75082}
Document ID: 8knzhY8BHToGwgcU4ePP

Analysing the response

We indexed all AirBnBs, so it might be little different to what you get when you only do the first 5.000.

The next step is to run a geo_distance query within Elasticsearch. First to analyse how far apart Central Park and Empire State Building is. Since the Central Park is pretty big and contains a multitude of points of interest, we will use the Bow Bridge an iconic sight.

We will use a simple terms query to get the geo location of Central Park Bow Bridge and then run a geo_distance query with a _geo_distance sort to get the exact distance back. The geo_distance query as of now always requires a distance parameter. We add a term to search for empire state building since we are just interested in this.

[22]
Name: central park bow bridge
Location: {'lon': -73.97178440451849, 'lat': 40.77577539823907}

Distance to Empire State Building: 3.247504472145157 km

Comparing to Elser

Now our top scoring document:

	Score: 20.003891
Title: Gorgeous 1 Bedroom - Upper East Side Manhattan -
Location: {'lon': -73.95856, 'lat': 40.76701}
Document ID: AkgfEI8BHToGwgcUA6-7

Let's run the calculation from above using geo_distance.

[13]
Distance between AirBnB and central park bow bridge 1.4799179352060348 km
Distance between AirBnB and empire state building 3.0577584374128617 km

Analysing

Only 1.4km and 3km away from the two sights. Not that bad. Let's see what we can find when we create a geo-bounding box with the Empire State Building and the Central Park Bow Bridge. Additionally we will sort the result by the distance to the Central Park Bow Bridge and then by distance to Empire State Building.

[29]
Distance to Central Park Bow Bridge: 0.4131817369844223 km
Distance to Empire State Building: 3.089519162860651 km
Title: Great Room by Central Park & Time Square
Document ID: 80n9hY8BHToGwgcUUfrA

Distance to Central Park Bow Bridge: 0.45350835952906815 km
Distance to Empire State Building: 3.1145514127463594 km
Title: 5 Beds, Steps from Lincoln Center, Central Park
Document ID: 8UnOhY8BHToGwgcUTJKv

Distance to Central Park Bow Bridge: 0.5671263759837657 km
Distance to Empire State Building: 2.955994026017429 km
Title: Huge 1 Bedroom Entire Unit on Central Park! UWS
Document ID: F0nThY8BHToGwgcUbJ95

Distance to Central Park Bow Bridge: 0.5952177487694544 km
Distance to Empire State Building: 3.0537706667049194 km
Title: Bright Comfy Studio!
Document ID: kknThY8BHToGwgcU-6D0

Distance to Central Park Bow Bridge: 0.5997105192552462 km
Distance to Empire State Building: 2.8883161884323183 km
Title: Large duplex apartment Lincoln Sq
Document ID: nEn1hY8BHToGwgcUbefs

Distance to Central Park Bow Bridge: 0.6145766439818324 km
Distance to Empire State Building: 2.964903765549427 km
Title: * Light & Bright Central Park West Brownstone
Document ID: ZEnthY8BHToGwgcUWtXB

Distance to Central Park Bow Bridge: 0.6179705737259344 km
Distance to Empire State Building: 3.0671730371540322 km
Title: Lovely 4 Bedroom/3 Bathroom/Garden. Central Park.
Document ID: FknVhY8BHToGwgcUCqMx

Distance to Central Park Bow Bridge: 0.6651772103309174 km
Distance to Empire State Building: 2.9487596701698107 km
Title: Stylish Cozy Entire Studio
Document ID: V0nIhY8BHToGwgcU_IVZ

Distance to Central Park Bow Bridge: 0.6806723256826838 km
Distance to Empire State Building: 2.953411336952507 km
Title: The Sunny Lincoln Center Apartment
Document ID: 3EnVhY8BHToGwgcUmKMH

Distance to Central Park Bow Bridge: 0.7233438162417364 km
Distance to Empire State Building: 2.9758539550319947 km
Title: Lincoln Center Gem - Short Term Available
Document ID: GEnJhY8BHToGwgcUM4Zy

Distance to Central Park Bow Bridge: 0.7443571694818886 km
Distance to Empire State Building: 3.0437869095436754 km
Title: Charming Studio UWS near Central Park
Document ID: U0oBho8BHToGwgcU8gWr

Distance to Central Park Bow Bridge: 0.7522774827930032 km
Distance to Empire State Building: 2.788981551632797 km
Title: Shared room at Lincoln Center
Document ID: PEnGhY8BHToGwgcUk3-t

Distance to Central Park Bow Bridge: 0.7613822055498712 km
Distance to Empire State Building: 2.917809424236674 km
Title: Cozy and charming NYC studio
Document ID: sknxhY8BHToGwgcUFN0Q

Distance to Central Park Bow Bridge: 0.7655012840614352 km
Distance to Empire State Building: 3.0378129044232347 km
Title: 1 Bedroom Apt, West 69th St, bwtn BWay & Columbus
Document ID: W0nWhY8BHToGwgcUl6ZH

Distance to Central Park Bow Bridge: 0.7733958540516488 km
Distance to Empire State Building: 2.7720609897741144 km
Title: Perfectly Located Lincoln Center One-Bedroom
Document ID: 2EnThY8BHToGwgcUsJ_H

Distance to Central Park Bow Bridge: 0.7863631036930467 km
Distance to Empire State Building: 2.917713385537012 km
Title: Apt. across from Lincoln Center
Document ID: _Un5hY8BHToGwgcUzPHd

Distance to Central Park Bow Bridge: 0.8285887310207944 km
Distance to Empire State Building: 2.654302842845152 km
Title: Blueground | UWS, w/d, nr Lincon Center
Document ID: F0nBhY8BHToGwgcU4HA_

Distance to Central Park Bow Bridge: 0.8285887310207944 km
Distance to Empire State Building: 2.654302842845152 km
Title: Blueground | UWS, doorman, nr central park
Document ID: 0UnqhY8BHToGwgcUQM7z

Distance to Central Park Bow Bridge: 0.8317793813857818 km
Distance to Empire State Building: 2.989653305906252 km
Title: Large 1 bdrm apt, 1 block to central pk 30 DAY MIN
Document ID: 9UnxhY8BHToGwgcUWd38

Distance to Central Park Bow Bridge: 0.8450353341336536 km
Distance to Empire State Building: 2.990493364568853 km
Title: WONDERFUL, COMFORTABLE, SAFE LINCOLN CENTER SPACE
Document ID: jUnyhY8BHToGwgcUX-D6

Distance to Central Park Bow Bridge: 0.850150844706325 km
Distance to Empire State Building: 2.7935321567649516 km
Title: Blueground | UWS, doorman, elevator, nr park
Document ID: 8EnihY8BHToGwgcUPr5p

Distance to Central Park Bow Bridge: 0.850317121579706 km
Distance to Empire State Building: 2.793555713498908 km
Title: Blueground | UWS, doorman, nr Lincoln Center
Document ID: rknxhY8BHToGwgcUFN0Q

Distance to Central Park Bow Bridge: 0.850317121579706 km
Distance to Empire State Building: 2.793555713498908 km
Title: Blueground | UWS, doorman, elevator, nr park
Document ID: D0nhhY8BHToGwgcUU70s

Distance to Central Park Bow Bridge: 0.8607277425960257 km
Distance to Empire State Building: 2.855736774975596 km
Title: Cute 2BR by Lincoln Square
Document ID: cUnfhY8BHToGwgcUKbm8

Distance to Central Park Bow Bridge: 0.8849253434847821 km
Distance to Empire State Building: 2.6888159069336384 km
Title: Sunny room beside Central Park
Document ID: BEnehY8BHToGwgcUEbeg

Distance to Central Park Bow Bridge: 0.8899297974196853 km
Distance to Empire State Building: 2.593498488090597 km
Title: Lincoln Center  luxury condo
Document ID: 9En1hY8BHToGwgcUr-eV

Distance to Central Park Bow Bridge: 0.9089851862111872 km
Distance to Empire State Building: 2.756559907255608 km
Title: Writer's Retreat in the Heart of the City
Document ID: SUn9hY8BHToGwgcUmPvo

Distance to Central Park Bow Bridge: 0.9179057084098066 km
Distance to Empire State Building: 2.633649076174077 km
Title: Manhattan New York Upper West side
Document ID: LUnChY8BHToGwgcUjnIE

Distance to Central Park Bow Bridge: 0.9376523264959093 km
Distance to Empire State Building: 2.5614219378831278 km
Title: Literally steps from Central Park!
Document ID: Ukn8hY8BHToGwgcUm_kq

Distance to Central Park Bow Bridge: 0.9445686078004821 km
Distance to Empire State Building: 3.0297909425761445 km
Title: Sunny Studio Near Central Park
Document ID: VUn5hY8BHToGwgcUjvHq

Distance to Central Park Bow Bridge: 0.9642705205066279 km
Distance to Empire State Building: 2.9953515473116803 km
Title: one bedroom apt for long term stay up to 2 months
Document ID: ZEnYhY8BHToGwgcUZ6qt

Distance to Central Park Bow Bridge: 0.9722789279214233 km
Distance to Empire State Building: 2.539286718831572 km
Title: Stunning view - Columbus Circle area
Document ID: -knThY8BHToGwgcUKJ4Z

Distance to Central Park Bow Bridge: 0.9787646884032914 km
Distance to Empire State Building: 2.603929390209796 km
Title: Peaceful Central Park & Lincoln Center Home
Document ID: xkn6hY8BHToGwgcUdfMU

Distance to Central Park Bow Bridge: 0.9871880696323352 km
Distance to Empire State Building: 2.610369090495495 km
Title: PRIVATE ENTIRE FLOOR -NEXT TO Central PARK  !
Document ID: 10nthY8BHToGwgcUCdS_

Distance to Central Park Bow Bridge: 1.0178197740885386 km
Distance to Empire State Building: 2.6488315240312748 km
Title: Central Park / UWS
Document ID: TknvhY8BHToGwgcUW9n4

Distance to Central Park Bow Bridge: 1.0265048444154021 km
Distance to Empire State Building: 2.4909589840010624 km
Title: Best views of Manhattan
Document ID: HUnVhY8BHToGwgcUmKQH

Distance to Central Park Bow Bridge: 1.0451374806451885 km
Distance to Empire State Building: 2.523276699341922 km
Title: Spectacular Lincoln Center Condo in the sky
Document ID: sknrhY8BHToGwgcUqNF5

Distance to Central Park Bow Bridge: 1.047774729331466 km
Distance to Empire State Building: 2.561250086148971 km
Title: Gorgeous Apt. on Central Park
Document ID: -knuhY8BHToGwgcUNtZz

Distance to Central Park Bow Bridge: 1.0579310265078672 km
Distance to Empire State Building: 2.477170281896128 km
Title: Lincoln Center Luxury High Rise 1 bedroom
Document ID: 9EnehY8BHToGwgcUmLen

Distance to Central Park Bow Bridge: 1.0600173189705104 km
Distance to Empire State Building: 2.5544718006352745 km
Title: Take it now you won't find better
Document ID: fknwhY8BHToGwgcUU9v3

Distance to Central Park Bow Bridge: 1.0674248087467735 km
Distance to Empire State Building: 2.3463319833393945 km
Title: Experience paradise on earth in midtown
Document ID: 8En9hY8BHToGwgcUFfkh

Distance to Central Park Bow Bridge: 1.068922155759417 km
Distance to Empire State Building: 2.969897996609374 km
Title: An amazing 1 bdr apt next to The Met opera.
Document ID: c0nZhY8BHToGwgcUMaym

Distance to Central Park Bow Bridge: 1.0724820481304276 km
Distance to Empire State Building: 3.0238311506341047 km
Title: Beautiful 1-bedroom in Lincoln Sqr- gorgeous view
Document ID: mEnUhY8BHToGwgcUvaLK

Distance to Central Park Bow Bridge: 1.106649499174859 km
Distance to Empire State Building: 2.605055542768263 km
Title: Skyline view apt -Lincoln Square
Document ID: 7knxhY8BHToGwgcUWd38

Distance to Central Park Bow Bridge: 1.1240321000024536 km
Distance to Empire State Building: 2.333216200760106 km
Title: Times Square, Central Park, NYC, Luxury, Summer
Document ID: fknJhY8BHToGwgcUkYfd

Distance to Central Park Bow Bridge: 1.1243476248351183 km
Distance to Empire State Building: 2.4285558932557834 km
Title: Luxury 3 BR condo + balcony w views of Central PK
Document ID: BknBhY8BHToGwgcU4HA_

Distance to Central Park Bow Bridge: 1.1526079295801053 km
Distance to Empire State Building: 2.202056866206072 km
Title: Courtyard View Room in Manhattan
Document ID: tEnYhY8BHToGwgcUK6nx

Distance to Central Park Bow Bridge: 1.164720035883416 km
Distance to Empire State Building: 2.098650122275327 km
Title: Gorgeous view of the Hudson River
Document ID: FEnNhY8BHToGwgcU8ZKo

Distance to Central Park Bow Bridge: 1.1712213104190234 km
Distance to Empire State Building: 2.2972489327173635 km
Title: Upper West Apartment Block Away from Central Park!
Document ID: _knGhY8BHToGwgcUk36t

Distance to Central Park Bow Bridge: 1.174417314727233 km
Distance to Empire State Building: 2.0828398365159138 km
Title: King Suite with Central Park Views
Document ID: oUnThY8BHToGwgcUKJ4Z

AI

Now let's finally get to the AI part. All of this was the setup and understanding what makes geo spatial searches tick and how they work. There is still a lot more to discover. Let's hookup it up to our OpenAI instance. In here we use the Azure OpenAI resource.

[30]
[43]
": "Paris*", //

Now that this works, we are sure that we are in the correct place to start our question. We are writing a prompt that forces ChatGPT to create a JSON response and extract the information from the question.

[46]
```
{
  "what": "AirBnB",
  "near": "Empire State Building",
  "distance_in_km": 1.609344
}
```
Here is some sample code:
```
from pprint import pprint
sentence = "Get me the closest AirBnB between 1 miles distance from the Empire State Building"
pprint(nlbot.get_what_near_distance(sentence))
```
kpps_textex 2021-07-07: This

The answer in our case is the following

Here is the desired output:

	{
    "what": "AirBnB",
    "near": "Empire State Building",
    "distance_in_km": 1610
}

  1. Extract distance - done (1 miles)
  2. Convert distance to km - done (1.6 km)
  3. Extract location - This should be "Empire State Building", but in more general terms we should recognize that this is a location so we make a separate label called
[47]
Distance to Empire State Building: 0.004002111094864837 km
Title: Comfort and Convenience! 2 Units Near Bryant Park!
Document ID: DUn3hY8BHToGwgcUWuz5

Distance to Empire State Building: 0.011231615140053008 km
Title: Relax and Recharge! 3 Relaxing Units, Pets Allowed
Document ID: O0n3hY8BHToGwgcUWuz5

Distance to Empire State Building: 0.02349319910825568 km
Title: Quiet space.
Document ID: EEnohY8BHToGwgcUXMtf

Distance to Empire State Building: 0.027646218010495525 km
Title: 4 Serene Units In The Heart Of NYC! Pets Allowed!
Document ID: QEn2hY8BHToGwgcU3evj

Distance to Empire State Building: 0.033996880421693895 km
Title: Prime Location! 3 Relaxing Units, Pets Allowed
Document ID: okn3hY8BHToGwgcUmux7

Distance to Empire State Building: 0.03531856438046657 km
Title: Visit and Catch A Show On Broadway! 4 Deluxe Units
Document ID: lkn2hY8BHToGwgcUnOqR

Distance to Empire State Building: 0.0374488409537633 km
Title: City Escape! Unit Close to Madison Square Garden!
Document ID: oEn2hY8BHToGwgcUnOqR

Distance to Empire State Building: 0.037660236640884664 km
Title: Three Pet-friendly Units, Minutes from MoMA!
Document ID: qkn3hY8BHToGwgcUmux7

Distance to Empire State Building: 0.03987374959359285 km
Title: Private Luxury Room In Manhattan, New York City!
Document ID: tEnQhY8BHToGwgcUPJgZ

Distance to Empire State Building: 0.03988503702305329 km
Title: The Gregory Hotel, Tailored King
Document ID: 4EnyhY8BHToGwgcUn-AZ

Distance to Empire State Building: 0.0409575446637707 km
Title: Cozy Clean Room in Manhattan 32nd & 5th/Madison
Document ID: 3knyhY8BHToGwgcU2eFr

Distance to Empire State Building: 0.04197878171563393 km
Title: Prime Location! 2 Units Near Rockefeller Center!
Document ID: 2kn3hY8BHToGwgcUmux7

Distance to Empire State Building: 0.04390474835251669 km
Title: Look No Further! Pet-friendly Property in NYC!
Document ID: AUn2hY8BHToGwgcUZuq-

Distance to Empire State Building: 0.05098654306582594 km
Title: Prime Location! 4 Units Near Top of the Rock!
Document ID: gEn3hY8BHToGwgcUmux7

Distance to Empire State Building: 0.053455403752978005 km
Title: Amazing Studio-EmpireStateBuilding
Document ID: FUnbhY8BHToGwgcUubJO

Distance to Empire State Building: 0.05892683320207764 km
Title: You Found It! Minutes to Verizon Plaza
Document ID: eUnWhY8BHToGwgcUl6ZH

Distance to Empire State Building: 0.06445554302109083 km
Title: Prime Location! Pets Allowed, Walk to Bryant Park!
Document ID: g0n2hY8BHToGwgcUnOqR

Distance to Empire State Building: 0.06902579654288031 km
Title: 1 Bedroom Apt near the Empire State
Document ID: V0nXhY8BHToGwgcU6anA

Distance to Empire State Building: 0.07179165027056177 km
Title: Gorgeous 1 bedroom luxury condo
Document ID: ZUnChY8BHToGwgcUUXGa

Distance to Empire State Building: 0.07241259883694023 km
Title: Great Relaxing Stay! Two Pet-friendly Units!
Document ID: o0r_hY8BHToGwgcU3gB9

Distance to Empire State Building: 0.07734670628382642 km
Title: Times Square centrally located large room
Document ID: wEnPhY8BHToGwgcUr5cL

Distance to Empire State Building: 0.08380964458513444 km
Title: Unforgettable NYC Trip! Minutes from Times Square!
Document ID: E0n0hY8BHToGwgcUY-UA

Distance to Empire State Building: 0.09093210159321162 km
Title: LARGE PRIVATE ROOM BY EVERYTHING MIDTOWN MANHATTAN
Document ID: PknwhY8BHToGwgcUGNup

Distance to Empire State Building: 0.09349715729963792 km
Title: !!Studio next to Empire State Bldg.
Document ID: bEn6hY8BHToGwgcUAvJ5

Distance to Empire State Building: 0.09395181480762141 km
Title: ICONIC STUDIO MIDTOWN 5TH AVENUE!
Document ID: mknWhY8BHToGwgcUl6ZH

Distance to Empire State Building: 0.09437030513272562 km
Title: A Unique Gem in the Heart of NYC! Pets Allowed!
Document ID: u0nehY8BHToGwgcUmLen

Distance to Empire State Building: 0.09443764032765753 km
Title: You Found it! Pet-friendly Property in NYC!
Document ID: wUr_hY8BHToGwgcU3gB9

Distance to Empire State Building: 0.09558644957518557 km
Title: City Escape! 2 Units, Walk to Times Square!
Document ID: I0nRhY8BHToGwgcUqZxI

Distance to Empire State Building: 0.09577186077892401 km
Title: Bright and Sunny Luxury Modern Midtown Apartment
Document ID: jEoBho8BHToGwgcUsARr

Distance to Empire State Building: 0.09854289242947946 km
Title: Spacious Room in the heart of Manhattan
Document ID: hUoBho8BHToGwgcUsARr

Distance to Empire State Building: 0.1002689897194992 km
Title: Look No Further! 4 Comfortable Units, Pets Allowed
Document ID: uEn_hY8BHToGwgcUIf4h

Distance to Empire State Building: 0.10410058755391106 km
Title: 2 Comfortable Units Close to Rockefeller Center!
Document ID: c0nRhY8BHToGwgcUY5vj

Distance to Empire State Building: 0.11491975195324128 km
Title: Private Room 717 | Shared Bath
Document ID: HEn2hY8BHToGwgcUJukN

Distance to Empire State Building: 0.11556008344777294 km
Title: Cute Studio flat near Times Square
Document ID: ZUn-hY8BHToGwgcUH_y4

Distance to Empire State Building: 0.1165303750375657 km
Title: Rare Gem! 2 Convenient Units, Pets Allowed
Document ID: K0nbhY8BHToGwgcUPLF-

Distance to Empire State Building: 0.11711484106305878 km
Title: Exclusive Private Studio 1103 | Private Bathroom
Document ID: xEnChY8BHToGwgcUyHLx

Distance to Empire State Building: 0.11786577229462668 km
Title: Room available in Midtown
Document ID: SUnFhY8BHToGwgcUsnxM

Distance to Empire State Building: 0.12034939010787557 km
Title: Amazing City View Ultra Luxury One Bed in Midtown
Document ID: G0oBho8BHToGwgcUcASn

Distance to Empire State Building: 0.12110771866347951 km
Title: Small Cozy Room in Herald Square
Document ID: O0n5hY8BHToGwgcUjvHq

Distance to Empire State Building: 0.12212194882486152 km
Title: Nice Studio in Midtown Manhattan
Document ID: v0nVhY8BHToGwgcUT6Op

Distance to Empire State Building: 0.12218665438647491 km
Title: A+ Location Deluxe Studio(3 beds) #5
Document ID: w0nDhY8BHToGwgcUU3QL

Distance to Empire State Building: 0.12492688971642316 km
Title: Midtown Oasis - Steps from Empire State Building
Document ID: hkn0hY8BHToGwgcUouWl

Distance to Empire State Building: 0.12874431225566033 km
Title: Family Studio With 3 Beds close Empire State #29
Document ID: 6knwhY8BHToGwgcUztx6

Distance to Empire State Building: 0.12887493094730135 km
Title: Studio in luxury building at the Center of NYC
Document ID: eEn8hY8BHToGwgcUWPg6

Distance to Empire State Building: 0.12888385348560277 km
Title: Relaxation Meets Convenience! Pets Allowed
Document ID: LEn2hY8BHToGwgcU3evj

Distance to Empire State Building: 0.1291062171791793 km
Title: Exclusive Private Studio *35 | Private Bathroom
Document ID: 1Un2hY8BHToGwgcUZum-

Distance to Empire State Building: 0.13174203146474467 km
Title: Imperial Midtown Studios 829
Document ID: d0nwhY8BHToGwgcUkNxN

Distance to Empire State Building: 0.13292008444030254 km
Title: Studio Apartment #2
Document ID: cEnXhY8BHToGwgcUGacW

Distance to Empire State Building: 0.134363624510592 km
Title: Cozy corner near Empire State Building
Document ID: j0oBho8BHToGwgcUsARr

Distance to Empire State Building: 0.13472984786797276 km
Title: A+ Location Comfort Studio Apartment (3 Beds) #2
Document ID: 7En-hY8BHToGwgcUYPyr

Distance to Empire State Building: 0.1349246423105236 km
Title: Exclusive Private Room 613 | Shared Bathroom Only
Document ID: mkn9hY8BHToGwgcUUfrA

Distance to Empire State Building: 0.13522578965449172 km
Title: A+ Location Superior Studio (3 Beds)
Document ID: e0nJhY8BHToGwgcUkYfd

Distance to Empire State Building: 0.1356533641942277 km
Title: Ultimate NYC Trip! Unit w/ Empire State View
Document ID: AEn0hY8BHToGwgcUY-UA

Distance to Empire State Building: 0.1361089243182925 km
Title: Well appointed queen with ADA features
Document ID: k0nFhY8BHToGwgcUhXt2

Distance to Empire State Building: 0.1361089243182925 km
Title: Easy access to transit for seeing all the sights
Document ID: NEnFhY8BHToGwgcUsnxM

Distance to Empire State Building: 0.1361089243182925 km
Title: High-speed WiFi and in-room coffee maker
Document ID: IUn0hY8BHToGwgcUY-UA

Distance to Empire State Building: 0.1361089243182925 km
Title: Incredible views from your own private terrace
Document ID: ikn3hY8BHToGwgcUmux7

Distance to Empire State Building: 0.1361089243182925 km
Title: Bring the kiddos – there’s room!
Document ID: 60n3hY8BHToGwgcUmux7

Distance to Empire State Building: 0.1361089243182925 km
Title: Comfortable king with accessible features
Document ID: 9kn3hY8BHToGwgcUWuv5

Distance to Empire State Building: 0.1361089243182925 km
Title: 24/7 gym and business center
Document ID: Akn3hY8BHToGwgcUWuz5

Distance to Empire State Building: 0.1361089243182925 km
Title: Close to Madison Square Garden for games & events
Document ID: BUn3hY8BHToGwgcUWuz5

Distance to Empire State Building: 0.1361089243182925 km
Title: Accessible-equipped, with 2 beds
Document ID: S0n3hY8BHToGwgcUWuz5

Distance to Empire State Building: 0.1370607600773514 km
Title: A+ Location Superior Studio with 3 Beds
Document ID: X0nxhY8BHToGwgcUFN0Q

Distance to Empire State Building: 0.13903034558343705 km
Title: Just What You Were Looking For! Pets Allowed
Document ID: f0nUhY8BHToGwgcUvaLK

Distance to Empire State Building: 0.1391429203202154 km
Title: A+ Location Corner Deluxe Studio with 3 beds #7
Document ID: eEnzhY8BHToGwgcUoeNe

Distance to Empire State Building: 0.13945671370959836 km
Title: Exclusive Private Studio 623 | Private Bathroom
Document ID: iEn9hY8BHToGwgcUUfrA

Distance to Empire State Building: 0.13953141538534714 km
Title: Private Room 719 | Share bath
Document ID: JEnehY8BHToGwgcUmLin

Distance to Empire State Building: 0.13967442701583113 km
Title: Rare Find! Near Museum of Modern Art
Document ID: KEn3hY8BHToGwgcU2-18

Distance to Empire State Building: 0.14136577000585135 km
Title: Imperial Studios (Apt Selected at Check in #7
Document ID: jkn2hY8BHToGwgcUnOqR

Distance to Empire State Building: 0.14394037260994672 km
Title: Private Room 541 | Shared bath
Document ID: 6kn9hY8BHToGwgcU3_sk

Distance to Empire State Building: 0.14475059845528132 km
Title: Studio638 In the heart of Midtown Manhattan
Document ID: 80nzhY8BHToGwgcU4ePP

Distance to Empire State Building: 0.1447984852402037 km
Title: *** HEART OF MANHATTAN (Herald Square) ***
Document ID: bknyhY8BHToGwgcUX-D6

Distance to Empire State Building: 0.14488109531419482 km
Title: A+ Location Studio(APT Selected at Check-in) #4
Document ID: N0nQhY8BHToGwgcU1JoO

Distance to Empire State Building: 0.14520810510518647 km
Title: 曼哈顿奢侈公寓次卧Manhattan luxury apartment second bedroom
Document ID: pUoCho8BHToGwgcU8gfJ

Distance to Empire State Building: 0.14632347379161145 km
Title: Your Home Away From Home! 3 Convenient Units!
Document ID: tUn_hY8BHToGwgcUIf4h

Distance to Empire State Building: 0.14637379605706793 km
Title: Next-Level Fun Only in NYC! Pets are Allowed!
Document ID: Ikn0hY8BHToGwgcUY-UA

Distance to Empire State Building: 0.14812006828137478 km
Title: Cozy Scandinavian private room in Midtown
Document ID: DEnGhY8BHToGwgcUk3-t

Distance to Empire State Building: 0.14835485714012148 km
Title: Сentered location*9th floor*studio for 5 people
Document ID: sEn9hY8BHToGwgcUUfrA

Distance to Empire State Building: 0.1487830005359137 km
Title: Bunk(2full size) & 1 Full bed, next to KoreaTown10
Document ID: gEndhY8BHToGwgcUy7Z6

Distance to Empire State Building: 0.14921255320498628 km
Title: Private Room 724 | Shared bath
Document ID: I0nzhY8BHToGwgcUoeNe

Distance to Empire State Building: 0.1492540503440509 km
Title: A+ Location Comfort Lofty & Bright Queen Studio #5
Document ID: 2UnFhY8BHToGwgcU1Hz2

Distance to Empire State Building: 0.15110026929869902 km
Title: Prime Location for Tourists! Near Times Square!
Document ID: Kkn3hY8BHToGwgcU2-18

Distance to Empire State Building: 0.1518058856616463 km
Title: A+ Location City Studio with Two Full Beds
Document ID: yEn9hY8BHToGwgcU3_sk

Distance to Empire State Building: 0.15225274311294673 km
Title: Near Times Square & Central Park! 3 Deluxe Units!
Document ID: 4UnFhY8BHToGwgcUWnpw

Distance to Empire State Building: 0.15418163408928168 km
Title: Relax and Recharge! Four Units, Pets Allowed!
Document ID: q0n_hY8BHToGwgcUIf4h

Distance to Empire State Building: 0.1542895482106335 km
Title: Manhattan Cozy Studio Near Empire State Building
Document ID: VEnxhY8BHToGwgcUFN0P

Distance to Empire State Building: 0.15436889515286376 km
Title: Private Room 1616 | Shared bath
Document ID: _En2hY8BHToGwgcUZum-

Distance to Empire State Building: 0.1543975924221713 km
Title: Your Own Studio Room Prime Midtown N833
Document ID: 8knChY8BHToGwgcUyHLx

Distance to Empire State Building: 0.1544440444562663 km
Title: 4 people, center of the city
Document ID: 1EnthY8BHToGwgcUCdS_

Distance to Empire State Building: 0.15454404900499236 km
Title: Exclusive Private Room A*13 | Shared Bathroom Only
Document ID: pEn2hY8BHToGwgcUZum-

Distance to Empire State Building: 0.154721101740187 km
Title: Hotel 32 32, King Deluxe
Document ID: GknRhY8BHToGwgcUY5vj

Distance to Empire State Building: 0.15611194640292012 km
Title: Exclusive Private Studio 1008 | Private Bathroom
Document ID: 7EnIhY8BHToGwgcUwoSA

Distance to Empire State Building: 0.15674564993112247 km
Title: Great One Bedroom  Apartment Next  Empire State #9
Document ID: TEnfhY8BHToGwgcUxLre

Distance to Empire State Building: 0.15711486419513304 km
Title: A+ Location Studio (APT Selected at Check-in) #5
Document ID: qEnrhY8BHToGwgcUqNF5

Distance to Empire State Building: 0.15751621275777955 km
Title: NYC Vacay! Prime Location in Manhattan
Document ID: _0n1hY8BHToGwgcUJ-an

Distance to Empire State Building: 0.15891724131853355 km
Title: Centered location Manhattan* 5th floor * 5 people
Document ID: aUnBhY8BHToGwgcUkW7r

Distance to Empire State Building: 0.15891724131853355 km
Title: Centered location Manhattan* 5th floor * 5 people
Document ID: LUnBhY8BHToGwgcU5nDB

Distance to Empire State Building: 0.15892612790250735 km
Title: 3beds next to Korea-town, Times Square2
Document ID: FUnrhY8BHToGwgcUXdH0

Distance to Empire State Building: 0.15900148684777546 km
Title: Affordable in ENTiRE APT in heart of Manhattan.
Document ID: S0nwhY8BHToGwgcUkNxN

Distance to Empire State Building: 0.15930482830098514 km
Title: A Place You'll Surely Enjoy! Pets are Allowed!
Document ID: 0UnThY8BHToGwgcU-6D0

With that we now have combined geo spatial search with LLMs.

Some idea for further exploration:

  • Let any LLM generate an itinerary with sights. ...