diff --git a/.env b/.env index 45ebf2ea..6f06a602 100644 --- a/.env +++ b/.env @@ -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= diff --git a/README.md b/README.md index 0e1ab167..0f8fb583 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/chat-llama-azure.php b/examples/chat-llama-azure.php new file mode 100644 index 00000000..bd7cbb85 --- /dev/null +++ b/examples/chat-llama-azure.php @@ -0,0 +1,31 @@ +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; diff --git a/src/Bridge/Azure/Meta/LlamaHandler.php b/src/Bridge/Azure/Meta/LlamaHandler.php new file mode 100644 index 00000000..aa835a99 --- /dev/null +++ b/src/Bridge/Azure/Meta/LlamaHandler.php @@ -0,0 +1,60 @@ +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']); + } +} diff --git a/src/Bridge/Azure/Meta/PlatformFactory.php b/src/Bridge/Azure/Meta/PlatformFactory.php new file mode 100644 index 00000000..e6889d1a --- /dev/null +++ b/src/Bridge/Azure/Meta/PlatformFactory.php @@ -0,0 +1,23 @@ +