Notebooks
E
Elastic
Unifying Elastic Vector Database And LLMs For Intelligent Query

Unifying Elastic Vector Database And LLMs For Intelligent Query

openai-chatgptlangchain-pythonchatgptgenaielasticsearchelasticopenaiunifying-elastic-vector-database-and-llms-for-intelligent-queryAIchatlogvectordatabasePythonsearchgenaistacksupporting-blog-contentvectorelasticsearch-labslangchainapplications

Objective

This notebook demonstrates how blending the capabilities of Elasticsearch as a vector database (VectorDB), search templates, and LLM functions can provide an intelligent query layer.

Open In Colab

  • Elasticsearch as the VectorDB: Acts as the core search engine, storing and retrieving dense vector embeddings efficiently.
  • Search Templates: Marry index capabilities to query parameters, enabling dynamic query generation and structured search execution.
  • LLM Functions: Parse the possible available parameters within a query and inject them into the search template for a more intelligent and context-aware retrieval process.

This combination enables a more sophisticated search experience, leveraging both structured and unstructured data retrieval methods.

[ ]
[ ]

##Variables

##Elasticsearch Client

[ ]

Get Completion Method

###Completions Endpoint & API Key

2025-02-21_15-48-14.jpg

[ ]

###Deployment Name

2025-02-21_15-55-02.jpg

[ ]

###API Version

2025-02-21_16-01-29.jpg

[ ]

##Google Maps API Key Required

2025-02-21_16-25-38.jpg

[ ]
[ ]

##Inferencing Endpoint Methods

[ ]

##Indexing and Ingestion Methods

[ ]

##Search Template Removes the existing hotel_search_template if present and replaces it with an updated version. This ensures the template is always current and correctly structured for search operations.

[ ]

Find A Hotel Method

Manages interactions between user queries, LLM functions, and Elasticsearch, orchestrating tool calls to extract search parameters and execute queries. """

[ ]

Format and Print Messages Functions

Formats and prints ChatCompletionMessages for better readability, displaying roles, content, function calls, and tool interactions in a structured way.

[ ]

Call Elasticsearch Function

Executes a search query using Elasticsearch with structured parameters, leveraging a search template for dynamic query generation.

[ ]

Handle Extract Hotel Search Parameters Function

Validates and processes extracted search parameters, ensuring required values like distance are set for location-based hotel searches.

[ ]

##Geo Location Resolves a location name into latitude and longitude using Google Geocoding API for geospatial search integration.

[ ]

##Create inferencing endpoints, Index, and Ingestion

[ ]

Find a hotel

Here, issue a query and notice how the query understanding layer matches possible attributes in the query to the attributes supported within the hotels index.

[19]

Formatted Messages:
Message 1:
Role: assistant
Tool Calls:
  Tool Call ID: call_VHhjy0TMxPnefibSssrTa5r0
  Function Name: extract_hotel_search_parameters
  Arguments: {"query":"recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym","distance":"250m","rating":4,"location":"Belongil Beach","attraction":"recently renovated, pool, gym"}

--------------------------------------------------
Function Arguments for extract_hotel_search_parameters:
{'query': 'recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym', 'distance': '250m', 'rating': 4, 'location': 'Belongil Beach', 'attraction': 'recently renovated, pool, gym'}
Response from handle_extract_hotel_search_parameters:
{"query": "recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym", "distance": "250m", "rating": 4, "location": "Belongil Beach", "attraction": "recently renovated, pool, gym"}
Updated parameters after extract_hotel_search_parameters:
{'query': 'recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym', 'distance': '250m', 'rating': 4, 'location': 'Belongil Beach', 'attraction': 'recently renovated, pool, gym'}

Formatted Messages:
Message 1:
Role: assistant
Tool Calls:
  Tool Call ID: call_u1fUp6CBJ8i1p9OZlQMk9kXV
  Function Name: geocode_location
  Arguments: {"location":"Belongil Beach"}

--------------------------------------------------
Updated parameters after geocode_location:
{'query': 'recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym', 'distance': '250m', 'rating': 4, 'location': 'Belongil Beach', 'attraction': 'recently renovated, pool, gym', 'latitude': -28.6337328, 'longitude': 153.6003455}

Formatted Messages:
Message 1:
Role: assistant
Tool Calls:
  Tool Call ID: call_jr4jJ04lW0y0E2AKnVmNumyh
  Function Name: query_elasticsearch
  Arguments: {"query":"recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym","latitude":-28.6337328,"longitude":153.6003455,"distance":"250m","rating":4,"attraction":"recently renovated, pool, gym"}

--------------------------------------------------
Function Arguments for query_elasticsearch:
{'query': 'recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym', 'latitude': -28.6337328, 'longitude': 153.6003455, 'distance': '250m', 'rating': 4, 'attraction': 'recently renovated, pool, gym'}
Parameters for Elasticsearch:
{'query': 'recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym', 'latitude': -28.6337328, 'longitude': 153.6003455, 'distance': '250m', 'rating': 4, 'attraction': 'recently renovated, pool, gym'}
Elasticsearch Query:
{
  "id": "hotel_search_template",
  "params": {
    "query": "recently renovated accommodations 250m from Belongil Beach with at least 4 stars and with a pool and gym",
    "latitude": -28.6337328,
    "longitude": 153.6003455,
    "distance": "250m",
    "rating": 4,
    "attraction": "recently renovated, pool, gym"
  }
}
Elasticsearch query successful.
Number of results found: 1

Formatted Messages:
Message 1:
Role: assistant
Content: I found one recently renovated accommodation within 250 meters of Belongil Beach that has at least 4 stars and offers both a pool and gym:

- **Hotel Name:** Belongil Beach Apartment
- **Rating:** 4 stars
- **Location:** Byron Bay, New South Wales, Australia

If you need more information or assistance with booking, please let me know!

--------------------------------------------------