-
Notifications
You must be signed in to change notification settings - Fork 22
Initialize store #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Initialize store #51
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
230fe0f
Initialize store
OskarStark 3dd96a8
-
OskarStark 964c72f
-
OskarStark 3fd7742
-
OskarStark 8f52118
-
OskarStark dfff7be
-
OskarStark 9b8294d
-
OskarStark f27bf16
-
OskarStark 0daf695
Update .env
OskarStark 07a059d
fix
OskarStark 55ec22f
-
OskarStark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ AZURE_OPENAI_KEY= | |
|
||
# For using SerpApi (tool) | ||
SERP_API_KEY= | ||
|
||
# For using MongoDB Atlas (store) | ||
MONGODB_URI= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
use MongoDB\Client as MongoDBClient; | ||
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\MongoDB\Store; | ||
use PhpLlm\LlmChain\ToolBox\ChainProcessor; | ||
use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch; | ||
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer; | ||
use PhpLlm\LlmChain\ToolBox\ToolBox; | ||
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['MONGODB_URI'])) { | ||
echo 'Please set OPENAI_API_KEY and MONGODB_URI environment variables.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
// initialize the store | ||
$store = new Store( | ||
client: new MongoDBClient($_ENV['MONGODB_URI']), | ||
databaseName: 'my-database', | ||
collectionName: 'my-collection', | ||
indexName: 'my-index', | ||
vectorFieldName: 'vector', | ||
); | ||
|
||
// 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.', 'regisseur' => '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.', 'regisseur' => 'The Wachowskis'], | ||
['title' => 'The Godfather', 'description' => 'The aging patriarch of an organized crime dynasty transfers control of his empire to his reluctant son.', 'regisseur' => 'Francis Ford Coppola'], | ||
]; | ||
|
||
// create embeddings and documents | ||
foreach ($movies as $movie) { | ||
$documents[] = Document::fromText( | ||
id: Uuid::v4(), | ||
text: $movie['title'].' '.$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); | ||
|
||
// initialize the index | ||
$store->initialize(); | ||
|
||
$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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Store; | ||
|
||
interface InitializableStoreInterface extends StoreInterface | ||
{ | ||
/** | ||
* @param array<mixed> $options | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function initialize(array $options = []): void; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.