Skip to content

feat: bring in Llama 3.3 support with Azure #276

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 1 commit into from
Apr 12, 2025
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
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ AZURE_OPENAI_DEPLOYMENT=
AZURE_OPENAI_VERSION=
AZURE_OPENAI_KEY=

# For using Llama on Azure
AZURE_LLAMA_BASEURL=
AZURE_LLAMA_KEY=

# For using OpenRouter
OPENROUTER_KEY=

Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ $embeddings = new Embeddings();
* Language Models
* [OpenAI's GPT](https://platform.openai.com/docs/models/overview) with [OpenAI](https://platform.openai.com/docs/overview) and [Azure](https://learn.microsoft.com/azure/ai-services/openai/concepts/models) as Platform
* [Anthropic's Claude](https://www.anthropic.com/claude) with [Anthropic](https://www.anthropic.com/) as Platform
* [Meta's Llama](https://www.llama.com/) with [Ollama](https://ollama.com/) and [Replicate](https://replicate.com/) as Platform
* [Google's Gemini](https://gemini.google.com/) with [Google](https://ai.google.dev/) as Platform
* [Google's Gemini](https://gemini.google.com/) with [OpenRouter](https://www.openrouter.com/) as Platform
* [Meta's Llama](https://www.llama.com/) with [Azure](https://learn.microsoft.com/azure/machine-learning/how-to-deploy-models-llama), [Ollama](https://ollama.com/) and [Replicate](https://replicate.com/) as Platform
* [Google's Gemini](https://gemini.google.com/) with [Google](https://ai.google.dev/) and [OpenRouter](https://www.openrouter.com/) as Platform
* [DeepSeek's R1](https://www.deepseek.com/) with [OpenRouter](https://www.openrouter.com/) as Platform
* Embeddings Models
* [OpenAI's Text Embeddings](https://platform.openai.com/docs/guides/embeddings/embedding-models) with [OpenAI](https://platform.openai.com/docs/overview) and [Azure](https://learn.microsoft.com/azure/ai-services/openai/concepts/models) as Platform
Expand Down
31 changes: 31 additions & 0 deletions examples/chat-llama-azure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use PhpLlm\LlmChain\Bridge\Azure\Meta\PlatformFactory;
use PhpLlm\LlmChain\Bridge\Meta\Llama;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;

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

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

$platform = PlatformFactory::create($_ENV['AZURE_LLAMA_BASEURL'], $_ENV['AZURE_LLAMA_KEY']);
$llm = new Llama(Llama::LLAMA_3_3_70B_INSTRUCT);

$chain = new Chain($platform, $llm);
$messages = new MessageBag(Message::ofUser('I am going to Paris, what should I see?'));
$response = $chain->call($messages, [
'max_tokens' => 2048,
'temperature' => 0.8,
'top_p' => 0.1,
'presence_penalty' => 0,
'frequency_penalty' => 0,
]);

echo $response->getContent().PHP_EOL;
60 changes: 60 additions & 0 deletions src/Bridge/Azure/Meta/LlamaHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Bridge\Azure\Meta;

use PhpLlm\LlmChain\Bridge\Meta\Llama;
use PhpLlm\LlmChain\Exception\RuntimeException;
use PhpLlm\LlmChain\Model\Message\MessageBagInterface;
use PhpLlm\LlmChain\Model\Model;
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
use PhpLlm\LlmChain\Model\Response\TextResponse;
use PhpLlm\LlmChain\Platform\ModelClient;
use PhpLlm\LlmChain\Platform\ResponseConverter;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Webmozart\Assert\Assert;

final readonly class LlamaHandler implements ModelClient, ResponseConverter
{
public function __construct(
private HttpClientInterface $httpClient,
private string $baseUrl,
#[\SensitiveParameter] private string $apiKey,
) {
}

public function supports(Model $model, object|array|string $input): bool
{
return $model instanceof Llama && $input instanceof MessageBagInterface;
}

public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface
{
Assert::isInstanceOf($input, MessageBagInterface::class);
$url = sprintf('https://%s/chat/completions', $this->baseUrl);

return $this->httpClient->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => $this->apiKey,
],
'json' => array_merge($options, [
'model' => $model->getVersion(),
'messages' => $input,
]),
]);
}

public function convert(ResponseInterface $response, array $options = []): LlmResponse
{
$data = $response->toArray();

if (!isset($data['choices'][0]['message']['content'])) {
throw new RuntimeException('Response does not contain output');
}

return new TextResponse($data['choices'][0]['message']['content']);
}
}
23 changes: 23 additions & 0 deletions src/Bridge/Azure/Meta/PlatformFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Bridge\Azure\Meta;

use PhpLlm\LlmChain\Platform;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final readonly class PlatformFactory
{
public static function create(
string $baseUrl,
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
): Platform {
$modelClient = new LlamaHandler($httpClient ?? HttpClient::create(), $baseUrl, $apiKey);

return new Platform([$modelClient], [$modelClient]);
}
}
1 change: 1 addition & 0 deletions src/Bridge/Meta/Llama.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

final readonly class Llama implements LanguageModel
{
public const LLAMA_3_3_70B_INSTRUCT = 'llama-3.3-70B-Instruct';
public const LLAMA_3_2_90B_VISION_INSTRUCT = 'llama-3.2-90b-vision-instruct';
public const LLAMA_3_2_11B_VISION_INSTRUCT = 'llama-3.2-11b-vision-instruct';
public const LLAMA_3_2_3B = 'llama-3.2-3b';
Expand Down