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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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.

Ypu 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:

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.


All versions of elasticsearch-chatgpt-php with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
elasticsearch/elasticsearch Version ^8.7
openai-php/client Version ^0.4.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package elastic/elasticsearch-chatgpt-php contains the following files

Loading the files please wait ....