Skip to content

Commit

Permalink
docs: add pinecone example (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-hertel authored Oct 3, 2024
1 parent d252ecf commit 1bf42d7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ SERP_API_KEY=

# For using MongoDB Atlas (store)
MONGODB_URI=

# For using Pinecone (store)
PINECONE_API_KEY=
PINECONE_HOST=
68 changes: 68 additions & 0 deletions examples/store-pinecone-similarity-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Document\Document;
use PhpLlm\LlmChain\Document\Metadata;
use PhpLlm\LlmChain\DocumentEmbedder;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Embeddings;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Store\Pinecone\Store;
use PhpLlm\LlmChain\ToolBox\ChainProcessor;
use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\ToolBox\ToolBox;
use Probots\Pinecone\Pinecone;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Uid\Uuid;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (empty($_ENV['OPENAI_API_KEY']) || empty($_ENV['PINECONE_API_KEY']) || empty($_ENV['PINECONE_HOST'])) {
echo 'Please set OPENAI_API_KEY, PINECONE_API_KEY and PINECONE_HOST environment variables.'.PHP_EOL;
exit(1);
}

// initialize the store
$store = new Store(Pinecone::client($_ENV['PINECONE_API_KEY'], $_ENV['PINECONE_HOST']));

// our data
$movies = [
['title' => 'Inception', 'description' => 'A skilled thief is given a chance at redemption if he can successfully perform inception, the act of planting an idea in someone\'s subconscious.', 'director' => 'Christopher Nolan'],
['title' => 'The Matrix', 'description' => 'A hacker discovers the world he lives in is a simulated reality and joins a rebellion to overthrow its controllers.', 'director' => 'The Wachowskis'],
['title' => 'The Godfather', 'description' => 'The aging patriarch of an organized crime dynasty transfers control of his empire to his reluctant son.', 'director' => 'Francis Ford Coppola'],
];

// create embeddings and documents
foreach ($movies as $movie) {
$documents[] = Document::fromText(
id: Uuid::v4(),
text: 'Title: '.$movie['title'].PHP_EOL.'Director: '.$movie['director'].PHP_EOL.$movie['description'],
metadata: new Metadata($movie),
);
}

// create embeddings for documents
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$embedder = new DocumentEmbedder($embeddings = new Embeddings($platform), $store);
$embedder->embed($documents);

$llm = new Gpt($platform, Version::gpt4oMini());

$similaritySearch = new SimilaritySearch($embeddings, $store);
$toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]);
$processor = new ChainProcessor($toolBox);
$chain = new Chain($llm, [$processor], [$processor]);

$messages = new MessageBag(
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
Message::ofUser('Which movie fits the theme of the mafia?')
);
$response = $chain->call($messages);

echo $response.PHP_EOL;
3 changes: 2 additions & 1 deletion src/Store/Pinecone/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Probots\Pinecone\Client;
use Probots\Pinecone\Resources\Data\VectorResource;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Uid\Uuid;

final readonly class Store implements VectorStoreInterface
Expand All @@ -20,7 +21,7 @@
*/
public function __construct(
private Client $pinecone,
private LoggerInterface $logger,
private LoggerInterface $logger = new NullLogger(),
private ?string $namespace = null,
private array $filter = [],
private int $topK = 3,
Expand Down

0 comments on commit 1bf42d7

Please sign in to comment.