Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

feat: adding llama support via replicate and ollama #73

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ ANTHROPIC_API_KEY=
# For using Voyage
VOYAGE_API_KEY=

# For using Replicate
REPLICATE_API_KEY=

# For using Ollama
OLLAMA_HOST_URL=

# For using GPT on Azure
AZURE_OPENAI_BASEURL=
AZURE_OPENAI_DEPLOYMENT=
Expand Down
4 changes: 2 additions & 2 deletions examples/chat-claude-anthropic.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use PhpLlm\LlmChain\Anthropic\Model\Claude;
use PhpLlm\LlmChain\Anthropic\Platform\Anthropic;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Claude;
use PhpLlm\LlmChain\Platform\Anthropic;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand Down
7 changes: 3 additions & 4 deletions examples/chat-gpt-azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\Azure;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\Azure;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand All @@ -24,7 +23,7 @@
$_ENV['AZURE_OPENAI_VERSION'],
$_ENV['AZURE_OPENAI_KEY'],
);
$llm = new Gpt($platform, Version::gpt4oMini());
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);

$chain = new Chain($llm);
$messages = new MessageBag(
Expand Down
7 changes: 3 additions & 4 deletions examples/chat-gpt-openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand All @@ -18,7 +17,7 @@
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::gpt4oMini(), [
$llm = new Gpt($platform, Gpt::GPT_4O_MINI, [
'temperature' => 0.5, // default options for the model
]);

Expand Down
29 changes: 29 additions & 0 deletions examples/chat-llama-ollama.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Llama;
use PhpLlm\LlmChain\Platform\Ollama;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

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

if (empty($_ENV['OLLAMA_HOST_URL'])) {
echo 'Please set the OLLAMA_HOST_URL environment variable.'.PHP_EOL;
exit(1);
}

$platform = new Ollama(HttpClient::create(), $_ENV['OLLAMA_HOST_URL']);
$llm = new Llama($platform);

$chain = new Chain($llm);
$messages = new MessageBag(
Message::forSystem('You are a helpful assistant.'),
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'),
);
$response = $chain->call($messages);

echo $response->getContent().PHP_EOL;
29 changes: 29 additions & 0 deletions examples/chat-llama-replicate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Llama;
use PhpLlm\LlmChain\Platform\Replicate;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

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

if (empty($_ENV['REPLICATE_API_KEY'])) {
echo 'Please set the REPLICATE_API_KEY environment variable.'.PHP_EOL;
exit(1);
}

$platform = new Replicate(HttpClient::create(), $_ENV['REPLICATE_API_KEY']);
$llm = new Llama($platform);

$chain = new Chain($llm);
$messages = new MessageBag(
Message::forSystem('You are a helpful assistant.'),
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'),
);
$response = $chain->call($messages);

echo $response->getContent().PHP_EOL;
7 changes: 3 additions & 4 deletions examples/chat-o1-openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand All @@ -23,7 +22,7 @@
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::o1Preview());
$llm = new Gpt($platform, Gpt::O1_PREVIEW);

$prompt = <<<PROMPT
I want to build a Symfony app in PHP 8.2 that takes user questions and looks them
Expand Down
9 changes: 4 additions & 5 deletions examples/embeddings-openai.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

use PhpLlm\LlmChain\OpenAI\Model\Embeddings;
use PhpLlm\LlmChain\OpenAI\Model\Embeddings\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Embeddings\OpenAI as Embeddings;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI as Platform;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand All @@ -14,8 +13,8 @@
exit(1);
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$embeddings = new Embeddings($platform, Version::textEmbedding3Small());
$platform = new Platform(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$embeddings = new Embeddings($platform);

$vector = $embeddings->create(<<<TEXT
Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and rivers.
Expand Down
6 changes: 3 additions & 3 deletions examples/embeddings-voyage.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use PhpLlm\LlmChain\Voyage\Model\Voyage;
use PhpLlm\LlmChain\Voyage\Platform\Voyage as VoyagePlatform;
use PhpLlm\LlmChain\Model\Embeddings\Voyage;
use PhpLlm\LlmChain\Platform\Voyage as Platform;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand All @@ -13,7 +13,7 @@
exit(1);
}

$platform = new VoyagePlatform(HttpClient::create(), $_ENV['VOYAGE_API_KEY']);
$platform = new Platform(HttpClient::create(), $_ENV['VOYAGE_API_KEY']);
$embeddings = new Voyage($platform);

$vector = $embeddings->create(<<<TEXT
Expand Down
7 changes: 3 additions & 4 deletions examples/image-describer-binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
use PhpLlm\LlmChain\Message\Content\Image;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand All @@ -19,7 +18,7 @@
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::gpt4oMini());
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);

$chain = new Chain($llm);
$messages = new MessageBag(
Expand Down
7 changes: 3 additions & 4 deletions examples/image-describer-url.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
use PhpLlm\LlmChain\Message\Content\Image;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand All @@ -19,7 +18,7 @@
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::gpt4oMini());
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);

$chain = new Chain($llm);
$messages = new MessageBag(
Expand Down
11 changes: 5 additions & 6 deletions examples/store-mongodb-similarity-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
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\Model\Embeddings\OpenAI as Embeddings;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI as Platform;
use PhpLlm\LlmChain\Store\MongoDB\Store;
use PhpLlm\LlmChain\ToolBox\ChainProcessor;
use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch;
Expand Down Expand Up @@ -54,14 +53,14 @@
}

// create embeddings for documents
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$platform = new Platform(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());
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);

$similaritySearch = new SimilaritySearch($embeddings, $store);
$toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]);
Expand Down
11 changes: 5 additions & 6 deletions examples/store-pinecone-similarity-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
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\Model\Embeddings\OpenAI as Embeddings;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI as Platform;
use PhpLlm\LlmChain\Store\Pinecone\Store;
use PhpLlm\LlmChain\ToolBox\ChainProcessor;
use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch;
Expand Down Expand Up @@ -48,11 +47,11 @@
}

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

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

$similaritySearch = new SimilaritySearch($embeddings, $store);
$toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]);
Expand Down
4 changes: 2 additions & 2 deletions examples/stream-claude-anthropic.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use PhpLlm\LlmChain\Anthropic\Model\Claude;
use PhpLlm\LlmChain\Anthropic\Platform\Anthropic;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Claude;
use PhpLlm\LlmChain\Platform\Anthropic;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

Expand Down
7 changes: 3 additions & 4 deletions examples/stream-gpt-openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\EventSourceHttpClient;

Expand All @@ -18,7 +17,7 @@
}

$platform = new OpenAI(new EventSourceHttpClient(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::gpt4oMini());
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);

$chain = new Chain($llm);
$messages = new MessageBag(
Expand Down
7 changes: 3 additions & 4 deletions examples/structured-output-clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use PhpLlm\LlmChain\StructuredOutput\ChainProcessor as StructuredOutputProcessor;
use PhpLlm\LlmChain\StructuredOutput\ResponseFormatFactory;
use PhpLlm\LlmChain\ToolBox\ChainProcessor as ToolProcessor;
Expand All @@ -28,7 +27,7 @@
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::gpt4oMini());
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);

$clock = new Clock(new SymfonyClock());
$toolBox = new ToolBox(new ToolAnalyzer(), [$clock]);
Expand Down
7 changes: 3 additions & 4 deletions examples/structured-output-math.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use PhpLlm\LlmChain\StructuredOutput\ChainProcessor;
use PhpLlm\LlmChain\StructuredOutput\ResponseFormatFactory;
use PhpLlm\LlmChain\Tests\Fixture\StructuredOutput\MathReasoning;
Expand All @@ -24,7 +23,7 @@
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::gpt4oMini());
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);

$processor = new ChainProcessor(new ResponseFormatFactory(), $serializer);
Expand Down
7 changes: 3 additions & 4 deletions examples/toolbox-clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use PhpLlm\LlmChain\ToolBox\ChainProcessor;
use PhpLlm\LlmChain\ToolBox\Tool\Clock;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
Expand All @@ -23,7 +22,7 @@
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::gpt4oMini());
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);

$clock = new Clock(new SymfonyClock());
$toolBox = new ToolBox(new ToolAnalyzer(), [$clock]);
Expand Down
Loading