OpenAi PHP Client for Laravel : ChatBot

OpenAi PHP Client for Laravel : ChatBot

Do you know OpenAi, the trending artificial intelligence???

Today, OpenAi is one of the hottest trends in software development as a powerful artificial intelligence platform that makes it easy for developers to create AI-powered applications.

Laravel in two words???

To talk briefly about laravel, Laravel is an open-source web framework written in PHP respecting the model-view-controller principle and developed in object-oriented programming.

How to use OpenAi with Laravel ?

Before you start, you need to register for OpenAi.

After registering, create a new key:

  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 AppServices;

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 on to the next step, I quote a few remarks about the body of the “Create” request.

model : The OpenAi API is powered by a family of models with different capabilities and prices. For me, I’m using the latest and greatest version of the GPT-3 language model. GPT-3 can do everything other models can do, often with higher quality, longer output, and better instruction tracking (have 4000 tokens).

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 AppHttpControllers;

use AppServicesGeneratorOpenAIService;

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.


OpenAi PHP Client for Laravel : ChatBot was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.


Posted

in

by

Tags: