How to set up your Laravel application with OpenAi account using the API ?

Today, OpenAi is one of the most trending topics, software program improvement as an
effective synthetic intelligence platform that makes it smooth for builders to create AI-powered
applications.

Brief Explanation of Laravel in two words?

Laravel is a free, open-source PHP web framework used for web utility improvement. It is
designed to make the development method greater green by offering a stylish syntax and equipment for not unusual
place responsibilities which include routing, authentication, and caching. Laravel’s syntax is influenced by Ruby on
Rails, and it makes use of an MVC (model-view-controller) architectural pattern. It additionally consists of
integrated assist for database migrations, object-relational mapping (ORM), and a templating engine referred to as
Blade. Laravel’s recognition has grown hastily in current years and it’s now one of the most famous PHP
frameworks.

How to set up your Laravel application with an OpenAi account using the API?

Before you start, you need to register for OpenAi.After registering, create a new key:

Laravel application with an OpenAi

 

Laravel application with an OpenAi

      1. Enter your API key in your “.env” file:
        OPENAI_API_KEY= "your_key_openai"
      2. Install OpenAi PHP client (allows to interact with OpenAi API) using composer
        with this command:
        composer require openai-php/client
      3. Create a service that uses the OpenAi PHP library to communicate with the OpenAi
        service, for example:

        <?php
        namespace App\Services;
        use OpenAI;
        class GeneratorOpenAIService
        {
            private $client;
            public function __construct()
            {
                $this->client = OpenAI::client(env('OPENAI_API_KEY'));
            }
            public function generateResponseOpenAi(string $question): string
            {
                $response = $this->client->completions()->create([
                    'model' => 'text-davinci-003',
                    'temperature' => 0.9,
                    'top_p' => 1,
                    'frequency_penalty' => 0,
                    'presence_penalty' => 0,
                    'prompt' => $question,
                    'max_tokens' => 4000,
                ]);
                return $response['choices'][0]['text'];
            }
        }

        Before moving directly to the subsequent step, I quote some feedback approximately the frame of the “Create” request.

        model: The OpenAi API is powered through its own circle of relatives of fashions with distinct competencies and prices. For me, I’m using the state-of-the-art and finest model of the GPT-3 language model. GPT-3 can do the whole lot different fashions can do, frequently with better quality, longer output, and higher coaching tracking.

        temperature: 0,9 (for more creative applications)
        prompt: the text
        max_tokens: The number of tokens in your prompt plus max_tokens cannot exceed the template context length (4000 tokens).

      4. Inject the service into your controller where you want to use it and call the “generateResponseOpenAi” method

        <?php
        namespace App\Http\Controllers;
        use App\Services\GeneratorOpenAIService;
        class OpenAIController extends Controller
        {

        private $openAiService;
        public function __construct(GeneratorOpenAIService $openaiService)
        {

        $this->openAiService= $openaiService;
        }
        public function chatOpenAi(Request $request)
        {

        $question = $request->question;
        if ($question == null) {
        return back();
        }
        $response= $this->openAiService->generateResponseOpenAi($question);
        return response()->json(['response' => $response]);
        }
        }

      Conclusion

      In this tutorial, we saw the steps needed to set up your OpenAi account, use the API, and integrate it into a Laravel application.
      Hope this will allow you to use OpenAi in your Laravel application. Feel free to ask me questions if you need more details.

Leave a Reply

Your email address will not be published. Required fields are marked *