ElasticMage is a tool that generates Elasticsearch queries using OpenAI API and LangChain. The project dynamically retrieves the index mapping from Elasticsearch, stores it in ChromaDB and uses this mapping to create accurate Elasticsearch queries based on user input questions.
This project is designed to help developers create valid Elasticsearch queries based on natural language questions. By using OpenAI's GPT-4 model and LangChain, the project converts user questions into structured Elasticsearch queries that adhere to a provided index mapping.
- Convert natural language questions into Elasticsearch queries.
- Ensure queries adhere to the retrieved index mapping.
- Use OpenAI's powerful language models for accurate query generation.
Python 3.7 or higher
OpenAI API Key
Elastic instance
-
Clone the repository:
git clone https://github.com/yourusername/elasticsearch-query-generator.git cd elasticsearch-query-generator
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Create a
.env
file in the root directory to set your OpenAI API key and Elasticsearch details:OPENAI_API_KEY='your-api-key' ELASTICSEARCH_URL='your-elasticsearch-url' ELASTICSEARCH_API_KEY='your-elasticsearch-api-key'
-
Run the script:
python generate_query.py
-
Enter index pattern and elastic query needed:
Enter index pattern (type 'all' for all indexes): omdb Enter the kind of elastic query to be formed:Find all movies acted by Robert Downey Jr
-
The generated Elasticsearch query will be printed in the console.
{ "query": { "bool": { "must": { "match": { "actors": "Robert Downey Jr" } } } } }
Since the question is "Find all movies acted by Robert Downey Jr" and "omdb" index mapping has a field called
actors
.
Contributions are welcome! Please open an issue or submit a pull request for any bugs, features, or documentation improvements.
This project is licensed under the MIT License. See the LICENSE file for details.