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

duyn's avatar
Level 7

Laravel Inertia Vue packages for OpenAI Assistants API?

Are there any good packages for integrating the OpenAI Assistants API in a Laravel Inertia Vue project? I'm aware of the AI SDK, and was wondering if others have had a good experience just using that toolkit as the front-end with a Laravel backend.

0 likes
1 reply
LaryAI's avatar
Level 58

To integrate the OpenAI API in a Laravel Inertia Vue project, you can follow these steps. While there might not be a specific package that combines all three technologies, you can use existing tools and libraries to achieve the integration.

Step 1: Set Up Laravel Backend

  1. Install OpenAI PHP Client: Use a package like openai-php/client to interact with the OpenAI API from your Laravel backend.

    composer require openai-php/client
    
  2. Configure Environment Variables: Add your OpenAI API key to your .env file.

    OPENAI_API_KEY=your_openai_api_key
    
  3. Create a Service Class: Create a service class to handle requests to the OpenAI API.

    <?php
    
    namespace App\Services;
    
    use OpenAI\Client;
    
    class OpenAIService
    {
        protected $client;
    
        public function __construct()
        {
            $this->client = new Client([
                'api_key' => env('OPENAI_API_KEY'),
            ]);
        }
    
        public function getResponse($prompt)
        {
            return $this->client->completions()->create([
                'model' => 'text-davinci-003',
                'prompt' => $prompt,
                'max_tokens' => 150,
            ]);
        }
    }
    
  4. Create a Controller: Create a controller to handle API requests from the front-end.

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Services\OpenAIService;
    
    class OpenAIController extends Controller
    {
        protected $openAIService;
    
        public function __construct(OpenAIService $openAIService)
        {
            $this->openAIService = $openAIService;
        }
    
        public function getResponse(Request $request)
        {
            $prompt = $request->input('prompt');
            $response = $this->openAIService->getResponse($prompt);
    
            return response()->json($response);
        }
    }
    
  5. Define Routes: Add a route to handle the request.

    use App\Http\Controllers\OpenAIController;
    
    Route::post('/api/openai', [OpenAIController::class, 'getResponse']);
    

Step 2: Set Up Inertia Vue Frontend

  1. Create a Vue Component: Create a Vue component to interact with the backend.

  2. Add the Component to Your Page: Use the component in your Inertia page.

    <template>
      <div>
        <OpenAIComponent />
      </div>
    </template>
    
    <script>
    import OpenAIComponent from '@/Components/OpenAIComponent.vue';
    
    export default {
      components: {
        OpenAIComponent,
      },
    };
    </script>
    

Conclusion

By following these steps, you can integrate the OpenAI API into your Laravel Inertia Vue project. This setup allows you to handle API requests on the backend with Laravel and display the results on the frontend using Vue.

Please or to participate in this conversation.