rhaymison / elephant_chain
PHP项目的LLM链
Requires
- php: ^8.2
- ext-curl: *
- ext-mbstring: *
- codewithkyrian/chromadb-php: dev-main
- gemini-api-php/client: dev-main
- modelflow-ai/ollama: 0.2.x-dev
- ncjoes/office-converter: dev-master
- openai-php/client: dev-main
- partitech/php-mistral: dev-main
- phpoffice/phpword: dev-master
- spatie/pdf-to-text: dev-main
- symfony/css-selector: ^6.0
- symfony/dom-crawler: ^6.0
This package is auto-updated.
Last update: 2024-09-03 18:11:28 UTC
README
欢迎来到 Elephant Chain 🐘,这是一个强大的库,旨在无缝地将大型语言模型(LLM)集成到PHP环境中。无论是开发复杂自然语言处理应用,还是增强PHP项目的人工智能能力,Elephant Chain都为您提供轻松高效实现这一目标的工具和功能。
主要功能
- 无缝集成:轻松将LLM集成到现有的PHP项目中,设置简单。
- 高性能:优化性能,确保快速的人工智能应用程序。
- 灵活性:支持多种LLM,允许您选择最适合您需求的模型。
- 用户友好:直观易用,使所有技能水平的开发者都能使用。
- 全面文档:详细的文档和示例,帮助您快速入门并充分利用库的功能。
目录
安装
要开始使用Elephant Chain,请按照以下简单步骤操作
- 安装:使用Composer安装库
composer require rhaymison/elephant_chain
基本用法
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);
可用模型
Elephant Chain目前支持使用来自OpenAI和Gemini的模型。Mistral和LlamaCPP的集成目前正在实施。以下是如何初始化和使用这些模型的示例
OpenAi
您必须使用API将您的集成密钥传递。默认使用的模型是gpt3.5 turbo,温度设置为0.5
use Rhaymison\ElephantChain\Llm\OpenAiChain; // OpenAI Model $openAi = new OpenAiChain('OPEN_AI_KEY', 'MODEL');
Gemini
您必须提供Gemini API密钥,并可以选择传递温度,默认设置为0.5
use Rhaymison\ElephantChain\Llm\GeminiChain; // Gemini Model $gemini = new GeminiChain('GEMINI_KEY');
Mistral
您必须使用Mistral传递您的集成密钥并选择一个可用的模型。默认模型是:mistral-large-latest,温度为0.5
use Rhaymison\ElephantChain\Llm\MixtralChain; // Mistral Model $mixtral= new MixtralChain('MIXTRAL_KEY','MODEL');
Ollama
添加ollama端点并选择您想要在ollama环境中使用并已下载的模型
// Ollama Model $llm = new OllamaChain('http://127.0.0.1:11434', 'llama3');
加载器
TXT文件
TxtLoader类允许您加载和处理文本文件,以便在Elephant Chain中使用。
第一个参数是目录,第二个参数是块大小,第三个参数是重叠窗口。
use Rhaymison\ElephantChain\DocumentLoaders\TextLoaders; $textLoader = new TextLoaders; $documents = $textLoader->dirTextLoaders('./samples', 500, 20);
如果您只想加载一个txt文件,可以使用此方法,最后两个参数保持块和重叠不变
use Rhaymison\ElephantChain\DocumentLoaders\TextLoaders; $textLoader = new TextLoaders; $documents = $textLoader->singleTextFileLoader('./samples/cristiano_ronaldo.txt', 500, 20);
PDF加载器
PdfLoader类允许您加载并从PDF文档中提取文本,使其轻松将文档数据集成到工作流程中。
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加载器
use Rhaymison\ElephantChain\DocumentLoaders\TabularLoaders; $tabular = new TabularLoaders(); $dataTabular = $tabular->csvLoader('./samples/samples.csv', ',', 1000);
文档加载器
此类允许您仅加载.doc文件而不是.docx文件。您可以加载整个目录或单个文件。
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);
链
链
Chain类是创建和管理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类通过结合基于提供的提示检索相关数据的功能,扩展了Chain的功能。
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类允许您创建一系列依赖操作,其中某个操作的输出作为下一个操作的输入。
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;
如果您愿意,可以在链的开始、结束或中间包含检索链。只需确保退出必须传递即可。
TabularChain
TabularChain 类从 CSV/XLSX 电子表格中加载数据,并应用用户定义的转换和过滤器来提取数据。该类通过动态生成的函数实现数据的灵活操作和分析。
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
如果你想为你的对话添加内存。你可以使用带有内存模板的 Memory 聊天。一个内存单元将被存储,你可以在任何时候对其进行操作、删除或清除。ChainMemory 接受你想要创建的聊天室模板和名称。
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);
内存将自动添加,你无需担心。如果你想从带有记忆开始对话,只需将其作为第三个参数传递给 chatTemplate... ChatMemoryChain 已经有了一个本地的 getMemory 函数,你可以使用。
LARAVEL 使用方法
<?php 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); } }
现在我们有了这个漂亮的结果
嵌入
在 ElephantChain 中使用嵌入非常简单。嵌入函数接口意味着所有嵌入模型都提供相同的返回值,这使得它们的实现既简单又快速。
OpenAi嵌入
use Rhaymison\ElephantChain\Embeddings\OpenAIEmbeddingFunction; $chain = new OpenAIEmbeddingFunction('OPENAI_KEY'); print_r($embeddingFunction->generate(['Cristiano Ronaldo was a galactic player']));
Gemini嵌入
use Rhaymison\ElephantChain\Embeddings\GeminiEmbeddingsFunction; $chain = new GeminiEmbeddingsFunction('GEMINI_KEY'); print_r($embeddingFunction->generate(['Cristiano Ronaldo was a galactic player']));
Mistral嵌入
use Rhaymison\ElephantChain\Embeddings\MixtralEmbeddingFunction; // Mistral Model $embeddingFunction = new MixtralEmbeddingFunction('MISTRAL_KEY'); print_r($embeddingFunction->generate(['Cristiano Ronaldo was a galactic player']));
Ollama嵌入
$embeddingFunction = new OllamaEmbeddingFunction('http://127.0.0.1:11434', 'llama3'); print_r($embeddingFunction->generate(['Cristiano Ronaldo was a galactic player']));
向量数据库
ElephantVectors
ElephantVectors 类提供了存储、管理和查询矢量化数据的函数,使高效的相似性搜索和高级数据检索操作成为可能。如果你不想使用矢量数据库,可以使用 ElephantVectors,它提供文档嵌入,并允许你直接对模型进行搜索。
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);
ChromaDB
ChromaDB 类为处理矢量化数据提供了强大的功能,包括存储、管理和查询,针对高性能矢量相似性搜索进行了优化。
docker pull chromadb/chroma &&
docker run -p 6666:8000 chromadb/chroma
基本用法
use Rhaymison\ElephantChain\Databases\Chroma; use Rhaymison\ElephantChain\DocumentLoaders\TextLoaders; use Rhaymison\ElephantChain\Embeddings\OpenAIEmbeddingFunction; $chroma = new Chroma('https://', 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]);
$documents 变量有一个 3 位的数组,其中第一个是 ids,后面是元数据,第三个是 chunks。所有数组都对应。
与 OpenAI 模型的使用示例
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('https://', 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);
与 Gemini 和传递嵌入函数的使用示例
$question = 'On May 21, against rivals Chelsea, Ronaldo scored the first goal in the 26th minute, what happened next?'; $chroma = new Chroma('https://', 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);
LARAVEL 使用方法
<?php 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('https://', 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]); } }
现在我们可以与你的 VectorsDatabase 交互
<?php 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('https://', 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); } }
现在我们有了这个漂亮的结果
工具
工具是你可以添加到你的推理中的强大工具。以下你可以查看一些可用的工具 ps: 到目前为止,这些工具仅适用于简单的 Chain
维基百科
此工具在维基百科中搜索并聚集与用户问题相关的信息。构造函数中传入的参数是搜索结果的限制。
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);
DuckDuckGo
这个强大的工具在 DuckDuck 上执行高级搜索,并添加重要信息到你的提示。你只需要指定你想要进行搜索的区域。
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);
与 duckduckGo 一起的提示示例
( [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. )


