PHP code example of rhaymison / elephant_chain

1. Go to this page and download the library: Download rhaymison/elephant_chain 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/ */

    

rhaymison / elephant_chain example snippets


use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Chains\Chain;
use Rhaymison\ElephantChain\Prompts\PromptTemplate;

$openAi = new OpenAiChain('OPEN_AI_KEY', 'gpt-3.5-turbo');
$chain = new Chain($openAi);

$prompt = "Create a text about PHP";

$prompt = PromptTemplate::createPromptTemplate($prompt, []);
$response = $chain->run($prompt);
print($response);

$prompt = "Create a text about {theme}, especially about the model {model}";

$prompt = PromptTemplate::createPromptTemplate($prompt, ['theme' => 'AI', 'model' => 'gpt']);
$response = $chain->run($prompt);
print($response);

use Rhaymison\ElephantChain\Llm\OpenAiChain;
// OpenAI Model
$openAi = new OpenAiChain('OPEN_AI_KEY', 'MODEL');

use Rhaymison\ElephantChain\Llm\GeminiChain;
// Gemini Model
$gemini = new GeminiChain('GEMINI_KEY');

use Rhaymison\ElephantChain\Llm\MixtralChain;
// Mistral Model
$mixtral= new MixtralChain('MIXTRAL_KEY','MODEL');

// Ollama Model
$llm = new OllamaChain('http://127.0.0.1:11434', 'llama3');

use Rhaymison\ElephantChain\DocumentLoaders\TextLoaders;
$textLoader = new TextLoaders;
$documents = $textLoader->dirTextLoaders('./samples', 500, 20);

use Rhaymison\ElephantChain\DocumentLoaders\TextLoaders;
$textLoader = new TextLoaders;
$documents = $textLoader->singleTextFileLoader('./samples/cristiano_ronaldo.txt', 500, 20);

use Rhaymison\ElephantChain\DocumentLoaders\PdfLoaders;
$pdfLoader = new PdfLoaders;
$documents = $pdfLoader->dirPdfLoader('./samples', 500, 20);

use Rhaymison\ElephantChain\DocumentLoaders\PdfLoaders;
$pdfLoader = new PdfLoaders;
$documents = $pdfLoader->singlePdfLoader('./samples/inicial.pdf', 500, 20);

use Rhaymison\ElephantChain\DocumentLoaders\TabularLoaders;
$tabular = new TabularLoaders();
$dataTabular = $tabular->csvLoader('./samples/samples.csv', ',', 1000);

use Rhaymison\ElephantChain\DocumentLoaders\DocLoaders;
$pdfLoader = new DocLoaders;
$documents = $pdfLoader->dirDocLoaders('./samples', 500, 20);

use Rhaymison\ElephantChain\DocumentLoaders\DocLoaders;
$pdfLoader = new DocLoaders;
$documents = $pdfLoader->singleDocFileLoader('./samples/financial.doc', 500, 20);

use Rhaymison\ElephantChain\Chains\Chain;
use Rhaymison\ElephantChain\Llm\OpenAiChain;
$openAi = new OpenAiChain('OPEN_AI_KEY', 'gpt-3.5-turbo');
$chain = new Chain($openAi);

$prompt = "Create a text about PHP";

$prompt = PromptTemplate::createPromptTemplate($prompt, []);
$response = $chain->run($prompt);
print($response);


use Rhaymison\ElephantChain\Chains\RetrieverChain;
use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Databases\Chroma;
use Rhaymison\ElephantChain\Prompts\RetrieverPromptsTemplate;

$openAi = new OpenAiChain('OPEN_AI_KEY', 'gpt-3.5-turbo');
$chain = new RetrieverChain($openAi);
$retriever = $chroma->retriever($collection, [$question], 1);
$prompt = RetrieverPromptsTemplate::simpleRetrieverPromptTemplate($question);
$chain->dispatch($retriever->documents[0], $prompt);
print($response);

use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Llm\GeminiChain;
use Rhaymison\ElephantChain\Chains\SequentialChain;
use Rhaymison\ElephantChain\Chains\Chain;

$openAi = new OpenAiChain('OPEN_AI_KEY', 'gpt-3.5-turbo');
$gemini = new GeminiChain('GEMINI_KEY');

$chain1 = new Chain($openAi);
$chain2Callable = function () use ($chain1) {
    $text = "Write about Cristiano Ronaldo's life story";
    $prompt1 = PromptTemplate::createPromptTemplate($text, []);
    return $chain1->run($prompt2);
};

$chain2 = new Chain($openAi);
$chain2Callable = function ($input) use ($chain2) {
    $text = "Take the information given and create a poem on the topic. Information: {output}";
    $prompt2 = PromptTemplate::createPromptTemplate($text, ['output' => $input]);
    return $chain2->run($prompt2);
};

$chain3 = new Chain($gemini);
$chain3Callable = function ($input) use ($chain3) {
    $text = "Evaluate the past poem and say which period of history it fits into. Poem: {output}";
    $prompt3 = PromptTemplate::createPromptTemplate($text, ['output' => $input]);
    return $chain3->run($prompt3);
};

$sequentialChain = new SequentialChain();
$response = $sequentialChain->dispatchSequence([
    $chain1Callable,
    $chain2Callable,
    $chain3Callable
    // ...
]);

echo $response;

use Rhaymison\ElephantChain\Chains\TabularChain;
use Rhaymison\ElephantChain\DocumentLoaders\TabularLoaders;
use Rhaymison\ElephantChain\Llm\GeminiChain;

$gemini = new GeminiChain('GEMINI_KEY');
$tabular = new TabularLoaders();
$dataTabular = $tabular->csvLoader('./samples/samples.csv');

$chain = new TabularChain($gemini);

$question = "Take the first 10 data where the industry code is GH134, level is 4 and year is 2016. Then do an analysis";
$response = $chain->dispatchTabularChain($dataTabular, $question);
print($response);

use Rhaymison\ElephantChain\Chains\ChatMemoryChain;
use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Prompts\ChatPromptTemplate;

$openAi = new OpenAiChain('', 'gpt-3.5-turbo');
$chain = new ChatMemoryChain($openAi, 'test');
$chatPrompt = ChatPromptTemplate::chatTemplate('What was the first question I asked you?', []);
$chain->dispatchChainMemory($chatPrompt);
$memory = $chain->getConversation();
print_r($memory);



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Rhaymison\ElephantChain\Chains\ChatMemoryChain;
use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Prompts\ChatPromptTemplate;
use Symfony\Component\HttpFoundation\Response;

class SimpleChatController extends Controller
{
    private $model;

    public function __construct()
    {
        $this->model = new OpenAiChain('', 'gpt-3.5-turbo');
    }

    public function chat(Request $request)
    {
        $payload = $request->all();
        $question = $payload['question'];
        $chain = new ChatMemoryChain($this->model, 'room_2');
        $chatPrompt = ChatPromptTemplate::chatTemplate($question, []);
        $llm = $chain->dispatchChainMemory($chatPrompt);
        return response()->json(['msg' => $llm], Response::HTTP_OK);
    }
}


use Rhaymison\ElephantChain\Embeddings\OpenAIEmbeddingFunction;
$chain = new OpenAIEmbeddingFunction('OPENAI_KEY');
print_r($embeddingFunction->generate(['Cristiano Ronaldo was a galactic player']));

use Rhaymison\ElephantChain\Embeddings\GeminiEmbeddingsFunction;
$chain = new GeminiEmbeddingsFunction('GEMINI_KEY');
print_r($embeddingFunction->generate(['Cristiano Ronaldo was a galactic player']));

use Rhaymison\ElephantChain\Embeddings\MixtralEmbeddingFunction;
// Mistral Model
$embeddingFunction = new MixtralEmbeddingFunction('MISTRAL_KEY');
print_r($embeddingFunction->generate(['Cristiano Ronaldo was a galactic player']));

$embeddingFunction = new OllamaEmbeddingFunction('http://127.0.0.1:11434', 'llama3');
print_r($embeddingFunction->generate(['Cristiano Ronaldo was a galactic player']));

use Rhaymison\ElephantChain\Chains\RetrieverChain;
use Rhaymison\ElephantChain\DocumentLoaders\TextLoaders;
use Rhaymison\ElephantChain\Databases\ElephantVectors;
use Rhaymison\ElephantChain\Embeddings\GeminiEmbeddingsFunction;
use Rhaymison\ElephantChain\Llm\GeminiChain;
use Rhaymison\ElephantChain\Prompts\RetrieverPromptsTemplate;

$textLoader = new TextLoaders;
$documents = $textLoader->dirTextLoaders('./samples', 500, 20);

$embeddingFunction = new GeminiEmbeddingsFunction('GEMINI_KEY');
$elephantVectors = new ElephantVectors($embeddingFunction);
$vectors = $elephantVectors->generateEmbeddingsChunks($documents);

$question = 'What happened on July 28, the galactic player';

#embedding, question and k search
$texts = $elephantVectors->retriever($vectors, $question, 4);

$gemini = new GeminiChain('GEMINI_KEY');
$chain = new RetrieverChain($gemini);

$prompt = RetrieverPromptsTemplate::simpleRetrieverPromptTemplate($question);

$response = $chain->dispatch($texts, $prompt);
print($response);


use Rhaymison\ElephantChain\Databases\Chroma;
use Rhaymison\ElephantChain\DocumentLoaders\TextLoaders;
use Rhaymison\ElephantChain\Embeddings\OpenAIEmbeddingFunction;

$chroma = new Chroma('http://localhost', 6666, 'cr7', 'cr7');
$embeddingFunction = new OpenAIEmbeddingFunction('');
$collection = $chroma->getOrCreateCollection('cristiano', $embeddingFunction);
$textLoader = new TextLoaders;
$documents = $textLoader->dirTextLoaders('./samples', 500, 20);
$chroma->addVectors($collection, $documents[0], $documents[1], $documents[2]);

use Rhaymison\ElephantChain\Databases\Chroma;
use Rhaymison\ElephantChain\Prompts\RetrieverPromptsTemplate;
use Rhaymison\ElephantChain\Chains\RetrieverChain;
use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Embeddings\OpenAIEmbeddingFunction;

$question = 'On May 21, against rivals Chelsea, Ronaldo scored the first goal in the 26th minute, what happened next?';
$chroma = new Chroma('http://localhost', 6666, 'cr7', 'cr7');
$embeddingFunction = new OpenAIEmbeddingFunction('');
$collection = $chroma->getOrCreateCollection('cristiano', $embeddingFunction);
$retriever = $chroma->retriever($collection, [$question], 1);
$openAi = new OpenAiChain('OPEN_AI_KEY', 'gpt-3.5-turbo');
$chain = new RetrieverChain($openAi);
$prompt = RetrieverPromptsTemplate::simpleRetrieverPromptTemplate($question);
$chain->dispatch($retriever->documents[0], $prompt);
print($response);

$question = 'On May 21, against rivals Chelsea, Ronaldo scored the first goal in the 26th minute, what happened next?';
$chroma = new Chroma('http://localhost', 6666, 'cr7', 'cr7');
$embeddings = $chroma->geminiEmbeddingsFunction('GEMINI_KEY');
$collection = $chroma->getOrCreateCollection('cristiano', $embeddings);
$retriever = $chroma->retriever($collection, [$question], 1);
$gemini = new GeminiChain('GEMINI_KEY');
$chain = new RetrieverChain($gemini);
$prompt = RetrieverPromptsTemplate::simpleRetrieverPromptTemplate($question);
$chain->dispatch($retriever->documents[0], $prompt);
print($response);



namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Rhaymison\ElephantChain\Databases\Chroma;
use Rhaymison\ElephantChain\DocumentLoaders\TextLoaders;
use Rhaymison\ElephantChain\Embeddings\OpenAIEmbeddingFunction;

class GenerateVectorStoreSeed extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $chroma = new Chroma('http://localhost', 6666, 'cr7', 'cr7');
        $embeddingFunction = new OpenAIEmbeddingFunction('');
        $collection = $chroma->getOrCreateCollection('laravelVectors', $embeddingFunction);
        $textLoader = new TextLoaders;
        $directoryPath = public_path('documents/extintores');
        $documents = $textLoader->dirTextLoaders($directoryPath, 500, 20);
        $chroma->addVectors($collection, $documents[0], $documents[1], $documents[2]);
    }

}




namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Rhaymison\ElephantChain\Chains\RetrieverChain;
use Rhaymison\ElephantChain\Databases\Chroma;
use Rhaymison\ElephantChain\Embeddings\OpenAIEmbeddingFunction;
use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Prompts\RetrieverPromptsTemplate;
use Symfony\Component\HttpFoundation\Response;

class SimpleChatController extends Controller
{
    private $model;
    private $collection;
    private $chroma;

    public function __construct()
    {
        $this->model = new OpenAiChain('', 'gpt-3.5-turbo');
        $this->chroma = new Chroma('http://localhost', 6666, 'cr7', 'cr7');
        $embeddingFunction = new OpenAIEmbeddingFunction('');
        $this->collection = $this->chroma->getOrCreateCollection('laravelVectors', $embeddingFunction);
    }

    public function chat(Request $request)
    {
        $payload = $request->all();
        $question = $payload['question'];
        $retriever = $this->chroma->retriever($this->collection, [$question], 3);
        $chain = new RetrieverChain($this->model);
        $systemMessage = "You are a fire department document expert chatboot. You will receive a context. Make a summary and respond as requested by the user.";
        $prompt = RetrieverPromptsTemplate::simpleRetrieverPromptTemplate($question, $systemMessage);
        $llm = $chain->dispatch($retriever->documents[0], $prompt);
        return response()->json(['msg' => $llm], Response::HTTP_OK);
    }

}


use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Chains\Chain;
use Rhaymison\ElephantChain\Prompts\PromptTemplate;
use Rhaymison\ElephantChain\Tools\Wikipedia;

$wikipediaTool = new Wikipedia(5);
$openAi = new OpenAiChain('OPEN_AI_KEY', 'gpt-3.5-turbo');
$chain = new Chain($openAi, $wikipediaTool);
qustion = "Create a text about closures in php";
$prompt = PromptTemplate::createPromptTemplate($qustion, []);
$response = $chain->run($prompt);

use Rhaymison\ElephantChain\Llm\OpenAiChain;
use Rhaymison\ElephantChain\Chains\Chain;
use Rhaymison\ElephantChain\Prompts\PromptTemplate;
use Rhaymison\ElephantChain\Tools\DuckDuckGo;

$dk = new DuckDuckGo('pt');
$openAi = new OpenAiChain('OPEN_AI_KEY', 'gpt-3.5-turbo');
$chain = new Chain($openAi, $dk);
$qustion = "Create a text about closures in php";
$prompt = PromptTemplate::createPromptTemplate($qustion, []);
$response = $chain->run($prompt);

(
[system] => Your task is to answer the users question based on the provided context.
[user] => Create a text about closures in PHP
Additional info: Anonymous functions and closures in PHP allow you to create functions without a specific name, introduced in PHP 5.3. Although less common, they are useful in specific cases, often underestimated. Learn more about their advantages and peculiarities.
Closures are anonymous functions that can access variables outside their scope. In PHP, they are useful for encapsulating logic and creating callbacks.
Closures in PHP are useful for encapsulating and storing functions inside other functions. They allow access to external variables, facilitating data manipulation.
)