PHP code example of elastic / elasticsearch-chatgpt-php

1. Go to this page and download the library: Download elastic/elasticsearch-chatgpt-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elastic / elasticsearch-chatgpt-php example snippets


use Elastic\Elasticsearch\ChatGPT\ChatGPT;
use Elastic\Elasticsearch\ClientBuilder;

// Openai-php/client library 
$openAI = OpenAI::client(getenv("OPENAI_API_KEY"));

// Connecting to Elasticsearch using Elastic Cloud
$elasticsearch = ClientBuilder::create()
    ->setHosts([getenv("ELASTIC_CLOUD_ENDPOINT")])
    ->setApiKey(getenv("ELASTIC_CLOUD_API_KEY"))
    ->build();

$chatGPT = new ChatGPT($elasticsearch, $openAI);
$result = $chatGPT->search('stocks', 'Return the first 10 documents of 2017');

// Print the Elasticsearch result
print_r($result->asArray());

// Print the Elasticsearch DSL used for the query
print($chatGPT->getLastQuery());

# English
$result = $chatGPT->search('stocks', 'Return the first 10 documents of 2017');
# Italian
$result = $chatGPT->search('stocks', 'Restituisci i primi 10 documenti del 2017');
# Spanish
$result = $chatGPT->search('stocks', 'Devuelve los 10 primeros documentos de 2017');
# French
$result = $chatGPT->search('stocks', 'Retourner les 10 premiers documents de 2017');
# German
$result = $chatGPT->search('stocks', 'Senden Sie die ersten 10 Dokumente des Jahres 2017 zurück');