PHPackages                             rhaymison/elephant\_chain - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. rhaymison/elephant\_chain

ActiveLibrary

rhaymison/elephant\_chain
=========================

LLM chain for PHP projects

v0.0.1(1y ago)27Apache-2.0PHPPHP ^8.2

Since Jun 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/rhaymisonbetini/ElephantChain)[ Packagist](https://packagist.org/packages/rhaymison/elephant_chain)[ RSS](/packages/rhaymison-elephant-chain/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (10)Versions (12)Used By (0)

Elephant Chain 🐘 - A Powerful Library for PHP Environments with LLMs
====================================================================

[](#elephant-chain----a-powerful-library-for-php-environments-with-llms)

 [![Banner](https://raw.githubusercontent.com/rhaymisonbetini/huggphotos/main/elephantchain.webp)](https://raw.githubusercontent.com/rhaymisonbetini/huggphotos/main/elephantchain.webp)

Welcome to **Elephant Chain 🐘**, a robust library crafted to seamlessly integrate Large Language Models (LLMs) into PHP environments. Whether you're developing sophisticated natural language processing applications or enhancing your PHP projects with AI capabilities, Elephant Chain equips you with the tools and functionalities to achieve this effortlessly and efficiently.

Key Features
------------

[](#key-features)

- **Seamless Integration:** Easily incorporate LLMs into your existing PHP projects with minimal setup.
- **High Performance:** Optimized for performance, ensuring fast AI-powered applications.
- **Flexibility:** Supports a variety of LLMs, allowing you to select the model that best suits your needs.
- **User-Friendly:** Straightforward and intuitive, making it accessible for developers of all skill levels.
- **Comprehensive Documentation:** Detailed documentation and examples to help you quickly get started and fully utilize the library's capabilities.

Table of Contents
-----------------

[](#table-of-contents)

1. [Installation](#installation)
2. [Basic Usage](#basic-usage)
3. [Available Models](#available-models)
    - [OpenAi](#openai)
    - [Gemini](#gemini)
    - [Mistral](#mistral)
    - [Ollama](#ollama)
4. [Loaders](#loaders)
    - [TXT Files](#txt-files)
    - [PDF Loaders](#pdf-loaders)
    - [CSV Loaders](#csv-loaders)
    - [Doc Loaders](#doc-loaders)
5. [Chains](#chains)
    - [Chain](#chain)
    - [RetrieverChain](#retrieverchain)
    - [SequentialChain](#sequentialchain)
    - [TabularChain](#tabularchain)
    - [ChatMemoryChain](#chatmemorychain)
6. [Embedding](#embedding)
    - [OpenAi Embeddings](#openai-embeddings)
    - [Gemini Embeddings](#gemini-embeddings)
    - [Mistral Embeddings](#mistral-embeddings)
    - [Ollama Embeddings](#ollama-embeddings)
7. [Vector Databases](#vector-databases)
    - [ElephantVectors](#elephantvectors)
    - [ChromaDB](#chromadb)
8. [Tools](#tools)
    - [Wikipedia](#wikipedia)
    - [DuckDuckGo](#duckduckgo)

Installation
------------

[](#installation)

To get started with Elephant Chain, follow these simple steps:

1. **Installation:** Install the library using Composer: ```
    composer require rhaymison/elephant_chain
    ```

Basic Usage
-----------

[](#basic-usage)

```
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);
```

Available Models
----------------

[](#available-models)

Elephant Chain currently supports the use of models from OpenAI and Gemini. Integration with Mixtral and LlamaCPP is currently being implemented. Here are examples of how to initialize and use these models:

### OpenAi

[](#openai)

You must pass your integration key with your api. The model used by default is the gpt3.5 turbo and with a temperature of 0.5

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

### Gemini

[](#gemini)

You must provide your Gemini API key and pass the temperature if you want, by default it is set to 0.5

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

### Mistral

[](#mistral)

You must pass your integration key with the mistral and choose one of the available models. By default the model is: mistral-large-latest and with temperature: 0.5

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

### Ollama

[](#ollama)

Add the ollama endpoint and select the model you want to use and have downloaded in your ollama environment

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

Loaders
-------

[](#loaders)

### TXT Files

[](#txt-files)

The `TxtLoader` class allows you to load and process text files for use within Elephant Chain.

The first parameter is the directory, the second is the chunk size and the third is the overlapping window.

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

If you want to load only one txt file you can use this method and the last two parameters remain chunk and overlaping

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

### PDF Loaders

[](#pdf-loaders)

The `PdfLoader` class enables you to load and extract text from PDF documents, making it easy to integrate document data into your workflows.

```
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);
```

### CSV Loaders

[](#csv-loaders)

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

### Doc Loaders

[](#doc-loaders)

This class allows you to load only .doc files and not docx files. You can load an entire directory or just a single file.

```
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);
```

Chains
------

[](#chains)

### Chain

[](#chain)

The `Chain` class is the fundamental building block for creating and managing sequences of operations in Elephant Chain.

```
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);
```

### RetrieverChain

[](#retrieverchain)

The `RetrieverChain` class extends the functionality of `Chain` by incorporating mechanisms to retrieve relevant data based on provided prompts.

```
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);
```

### SequentialChain

[](#sequentialchain)

The `SequentialChain` class allows you to create a series of dependent operations, where the output of one operation serves as the input for the next.

```
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;
```

If you wish, you can include a retriever chain at the beginning, end or middle of the chain. Just ensure that the exit must be passed forward.

### TabularChain

[](#tabularchain)

The TabularChain class loads data from CSV/XLSX spreadsheets and applies user-defined transformations and filters to the extracted data. This class enables flexible manipulation and analysis of data through dynamically generated functions.

```
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);
```

### ChatMemoryChain

[](#chatmemorychain)

If you want to add memory to your conversation. You can use the Memory chat with the memory template. A memory cell will be stored and you can manipulate, remove or clear it whenever you want. ChainMemory accepts the template and name of the chat room you want to create.

```
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);
```

The memory is added automatically and you don't need to worry about it. If you want to start the conversation with a memory, just pass it as the third parameter to the chatTemplate... The ChatMemoryChain already has a native getMemory function that you can use.

#### LARAVEL USAGE

[](#laravel-usage)

```
