Download the PHP package elastic/elasticsearch-chatgpt-php without Composer
On this page you can find all versions of the php package elastic/elasticsearch-chatgpt-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download elastic/elasticsearch-chatgpt-php
More information about elastic/elasticsearch-chatgpt-php
Files in elastic/elasticsearch-chatgpt-php
Package elasticsearch-chatgpt-php
Short Description Elasticsearch ChatGPT for PHP
License Apache-2.0
Homepage https://github.com/elastic/elasticsearch-chatgpt-php
Informations about the package elasticsearch-chatgpt-php
Elasticsearch ChatGPT for PHP
This is an experimental library for using ChatGPT for searching in Elasticsearch.
This library exposes a search(string $index, string $prompt)
function for searching in Elasticsearch using natural language.
The parameters are: $index
where you specify the name of the index to search in and $prompt
that is the query specified in natural language.
For instance, a prompt can be Give me all the first 10 documents
or Return all the different values of the field name
.
How this library works
This library uses ChatGPT to translate query expressed in natural language to Elasticsearch DSL query.
Then, the translated query is executed in Elasticsearch using elasticsearch-php library.
Using ChatGPT
For using ChatGPT you need an API key from OpenAI. This API key is available only for paying users. You can read more about the procedure to activate ChatGPT Plus account here.
Using Elasticsearch
You need to have an Elasticsearch server running or use the Elastic Cloud.
You can read here how to start a server using Docker or using the Elastic Cloud environment.
Example of using the library
Here we provided an example that is also available in the examples/test.php script.
In order to execute the test.php
script you need to install the dependencies
using composer with the following command:
Then, you need to set the environmental variables for using OpenAI and Elasticsearch. In the following example we used the following env:
OPENAI_API_KEY
, for the OpenAI API key;ELASTIC_CLOUD_ENDPOINT
, containing the URL endpoint for Elastic Cloud;ELASTIC_CLOUD_API_KEY
, containing the API key for Elastic Cloud.
If you want to connect using a different Elasticsearch server, you can read the
connecting section
of the elasticsearch-php
documentation.
The test.php
script contains the following code:
The search function is the key point of this code. In this example we used a dataset of stock prices containing 5 years of stock of 500 Fortune companies, starting from February 2013.
The dataset ihas the following mapping:
Using the cache
By default this library uses a cache system, based on files stored in cache
folder.
Each time the library ask for a mapping to Elasticsearch or send a prompt to ChatGPT
it store the results in files. For the mapping, the library uses the name of the index.
For the Elasticsearch DSL produced by ChatGPT we use the MD5 of the prompt request as file name.
If you want to disable the cache, e.g. you changed the mapping of the index, you
can pass $cache = false
in the third optional parameter of the search()
function.
Multilanguage support
A very nice feature of ChatGPT is the ability to specify questions in different languages. That means, you can use this library and specify the query in different natural languges, like Italian, Spanish, French, German, etc.
Here an example:
All the previous search have the same results producing the following Elasticsearch query (more or less):
You can see the examples/multilanguage.php script for more information.
NOTE: It is important to know that ChatGPT is an LLM that has been optimized for English. That means, the best results are obtained using queries entered in English.
Limitations
Since ChatGPT is a LLM model it does not guarantee that the answer are correct from a semantic point of view.
That means you can expect invalid results using this library, that means not valid translation in
Elasticsearch DSL query. You should always check the DSL query translated by ChatGPT using the getLastQuery()
function. For these reasons, we suggest to do not use this library in production.
Disclaimer
See the full documentation online in this Elastic Blog Post. elastic/elasticsearch-chatgt-php is an Elastic Labs project. Elastic Labs projects are for illustrative and experimental purposes only. This Elastic Labs project is not part of any product or services offering provided or supported under a commercial license or subscription. This project is made available as-is under the terms of the license associated with this project. The release and timing of any features or functionality described in this project remain at Elastic's sole discretion. Any features or functionality not currently available may not be delivered on time or at all.
License
elasticsearch-chatgpt-php
is available under the Apache 2.0 license.
For more details see LICENSE.