Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ehabafia's avatar

Looking for packages for laravel

Dears,

I am looking for composer packages for AI that can work well with Laravel. The AIs that I am looking for are:

  • OpenAI GRP-4
  • Cohere's Command Model
  • Google Gemini Pro
  • Mistral AI
  • Anthropic Claude

Any thoughts?

0 likes
2 replies
LaryAI's avatar
Level 58

As of my knowledge cutoff in early 2023, there isn't a specific Laravel package for each AI model you've listed, as many AI models are provided as APIs by their respective companies. However, you can interact with these APIs directly from your Laravel application using HTTP clients like Guzzle or Laravel's built-in HTTP client. Below are some general steps you can follow to integrate these AI services into your Laravel application:

  1. Install Laravel's HTTP Client (if you're using Laravel 7 or above, it's already included):
composer require guzzlehttp/guzzle
  1. Set up API keys and endpoints in your .env file for each service:
OPENAI_API_KEY=your_openai_api_key
COHERE_API_KEY=your_cohere_api_key
GOOGLE_GEMINI_API_KEY=your_google_gemini_api_key
MISTRAL_API_KEY=your_mistral_api_key
ANTHROPIC_API_KEY=your_anthropic_api_key
  1. Create a service class for each AI model to handle the API interactions. Here's an example for OpenAI's GPT-4 (assuming it's similar to GPT-3):
namespace App\Services;

use Illuminate\Support\Facades\Http;

class OpenAIService
{
    protected $apiKey;
    protected $baseUrl;

    public function __construct()
    {
        $this->apiKey = env('OPENAI_API_KEY');
        $this->baseUrl = 'https://api.openai.com/v1/engines/gpt-4/completions'; // Replace with the actual GPT-4 endpoint
    }

    public function generateText($prompt)
    {
        $response = Http::withHeaders([
            'Authorization' => 'Bearer ' . $this->apiKey,
            'Content-Type' => 'application/json',
        ])->post($this->baseUrl, [
            'prompt' => $prompt,
            'max_tokens' => 150,
        ]);

        return $response->json();
    }
}
  1. Use the service in your controller to interact with the AI:
namespace App\Http\Controllers;

use App\Services\OpenAIService;

class AIController extends Controller
{
    protected $openAIService;

    public function __construct(OpenAIService $openAIService)
    {
        $this->openAIService = $openAIService;
    }

    public function generateText()
    {
        $prompt = 'Your prompt here';
        $response = $this->openAIService->generateText($prompt);

        return response()->json($response);
    }
}
  1. Create routes in your web.php or api.php file to handle requests:
use App\Http\Controllers\AIController;

Route::get('/generate-text', [AIController::class, 'generateText']);

You would need to create similar service classes for Cohere, Google Gemini Pro, Mistral AI, and Anthropic Claude, using their respective API documentation to make the correct HTTP requests.

Remember to handle API rate limits and errors appropriately in your application. Always secure your API keys and do not expose them in your version control or to the frontend.

For more advanced use cases, you might want to create a unified interface or service provider that can handle multiple AI services, allowing you to switch between them easily or use them concurrently.

dualklip's avatar

Hi @ehabafia

I don't know if you are looking for a package to help you coding or you want to integrate those AI's in your application.

If you are looking for help then a good starting point for me is Laragenie. You need an account in OpenAI (ChatGPT) and Pinecone.

If you are looking for some way to integrate an AI as a service in your app I'm use directly the OpenAI SDK PHP client

The I apply something like this:

$client = OpenAI::client(env('OPENAI_KEY'));
        $messages = [
            ["role" => 'system', "content" => env('OPENAI_SYSTEM_SELF_DESCRIPTION_EN')],
        ];
        foreach ($this->chat->messages as $value) {
            $messages[] = ["role" => $value['role'], "content" => $value['text']];
        }
        $response = $client->chat()->create([
            'model' => 'gpt-3.5-turbo',
            'messages' => $messages,
            'user' => $this->chat->id,
            //'max_tokens' => 70
        ]);
        Message::create([
            'chat_id' => $this->chat->id,
            'text' => $response->choices[0]->message->content,
            'role' => $response->choices[0]->message->role
        ]);

Please or to participate in this conversation.