Skip to content

Commit 1bf42d7

Browse files
authored
docs: add pinecone example (#109)
1 parent d252ecf commit 1bf42d7

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ SERP_API_KEY=
2020

2121
# For using MongoDB Atlas (store)
2222
MONGODB_URI=
23+
24+
# For using Pinecone (store)
25+
PINECONE_API_KEY=
26+
PINECONE_HOST=
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Chain;
4+
use PhpLlm\LlmChain\Document\Document;
5+
use PhpLlm\LlmChain\Document\Metadata;
6+
use PhpLlm\LlmChain\DocumentEmbedder;
7+
use PhpLlm\LlmChain\Message\Message;
8+
use PhpLlm\LlmChain\Message\MessageBag;
9+
use PhpLlm\LlmChain\OpenAI\Model\Embeddings;
10+
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
11+
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
12+
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
13+
use PhpLlm\LlmChain\Store\Pinecone\Store;
14+
use PhpLlm\LlmChain\ToolBox\ChainProcessor;
15+
use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch;
16+
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
17+
use PhpLlm\LlmChain\ToolBox\ToolBox;
18+
use Probots\Pinecone\Pinecone;
19+
use Symfony\Component\Dotenv\Dotenv;
20+
use Symfony\Component\HttpClient\HttpClient;
21+
use Symfony\Component\Uid\Uuid;
22+
23+
require_once dirname(__DIR__).'/vendor/autoload.php';
24+
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
25+
26+
if (empty($_ENV['OPENAI_API_KEY']) || empty($_ENV['PINECONE_API_KEY']) || empty($_ENV['PINECONE_HOST'])) {
27+
echo 'Please set OPENAI_API_KEY, PINECONE_API_KEY and PINECONE_HOST environment variables.'.PHP_EOL;
28+
exit(1);
29+
}
30+
31+
// initialize the store
32+
$store = new Store(Pinecone::client($_ENV['PINECONE_API_KEY'], $_ENV['PINECONE_HOST']));
33+
34+
// our data
35+
$movies = [
36+
['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'],
37+
['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'],
38+
['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'],
39+
];
40+
41+
// create embeddings and documents
42+
foreach ($movies as $movie) {
43+
$documents[] = Document::fromText(
44+
id: Uuid::v4(),
45+
text: 'Title: '.$movie['title'].PHP_EOL.'Director: '.$movie['director'].PHP_EOL.$movie['description'],
46+
metadata: new Metadata($movie),
47+
);
48+
}
49+
50+
// create embeddings for documents
51+
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
52+
$embedder = new DocumentEmbedder($embeddings = new Embeddings($platform), $store);
53+
$embedder->embed($documents);
54+
55+
$llm = new Gpt($platform, Version::gpt4oMini());
56+
57+
$similaritySearch = new SimilaritySearch($embeddings, $store);
58+
$toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]);
59+
$processor = new ChainProcessor($toolBox);
60+
$chain = new Chain($llm, [$processor], [$processor]);
61+
62+
$messages = new MessageBag(
63+
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
64+
Message::ofUser('Which movie fits the theme of the mafia?')
65+
);
66+
$response = $chain->call($messages);
67+
68+
echo $response.PHP_EOL;

src/Store/Pinecone/Store.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Probots\Pinecone\Client;
1212
use Probots\Pinecone\Resources\Data\VectorResource;
1313
use Psr\Log\LoggerInterface;
14+
use Psr\Log\NullLogger;
1415
use Symfony\Component\Uid\Uuid;
1516

1617
final readonly class Store implements VectorStoreInterface
@@ -20,7 +21,7 @@
2021
*/
2122
public function __construct(
2223
private Client $pinecone,
23-
private LoggerInterface $logger,
24+
private LoggerInterface $logger = new NullLogger(),
2425
private ?string $namespace = null,
2526
private array $filter = [],
2627
private int $topK = 3,

0 commit comments

Comments
 (0)