by Devin Yang
(This article was automatically translated.)

Published - 2 years ago ( Updated - 2 years ago )

It seems that everyone is talking about OpenAI recently. If you want to play, you can write a simple API test in Laravel's API.
Because you can use curl to call, you can call through guzzle.

If you log in to the openai website, you can see the official teaching instructions here
https://beta.openai.com/docs/quickstart/build-your-application

API KEY application, just in There is a button at the bottom of this page

+ Create new secret key


Then add the key to Laravel's .env, here I named it
OPENAI_KEY=* 100017*
Add /openai function in routes/api.php

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
Route::get('/openai', function (Request $r) {
    if(empty($r->text)){
        return sprintf("Call API:
%s/api/openapi?text=question",env("APP_URL")); } $client = new Client; $api_url = "https://api.openai.com/v1/completions"; $json = <<text", "temperature": 0.9, "max_tokens": 150, "top_p": 1, "frequency_penalty": 0.0, "presence_penalty": 0.6, "stop": ["Human:", "AI:"] } JSON; $json = json_decode(preg_replace('/[\x00-\x1F]/', '', $json), true); try { $r = $client->request('POST', $api_url, [ 'headers' => [ 'Authorization' => 'Bearer ' .env("OPENAI_KEY") ], 'json' => $json ]); } catch (ClientException $e) { return json_decode($e->getResponse()->getBody()->getContents(), true); } return $r; });


Then open the /api/openai?text= of your Laravel project and you can enter the test

There are a lot of examples on the official website for reference. How to set the Json
https://beta.openai.com/examples/

Tags: openapi ai laravel

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


dlaravel

How to set the external network IP to the Project on D-Laravel?

This video introduces how we assign Public Ip (public IP) to a specific Project in the D-Laravel environment. Whether it is the setting of the external production environment or the demo of your own project on the intranet, you can refer to the setting method of this video. In the video, although I used the external network IP of PPPoE dial-up to illustrate, we can also use the local network IP on our own computer if we change it. In this way, colleagues can connect to our D-Laravel Project.

dlaravel

Use D-Laravel to build your own dedicated php fpm image.

D-Laravel has provided a built php image, if you need to adjust it yourself and build your own dedicated image is quite simple. 1. First, enter dockerfiles/fpm in D-Laravel, and select the PHP version you want to build, such as 7.2. The command is as follows...

laravel storage,sftp

Use tinker to test Laravel's sftp custom Storage::path and Storage::url

Today, I will test the use of sftp driver on Laravel. If you have never used it, come and see the results of my test. By the way, it is very convenient for us to perform Storage functions in the tinker environment of Laravel, whether it is local or remote. After adjusting the settings, remember to leave and enter again.