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


laravel, livewire

livewire important notes

Before starting your Livewire journey, here are three basic considerations about public properties: 1. The property name must not conflict with a property name reserved for Livewire (such as $rules or $message) 2. Store in Data in public properties is visible to front-end JavaScript. Therefore, you should not store sensitive data in it. 3. Properties can only be JavaScript-friendly data types (string, integer, array, boolean), or one of the following PHP types: Stringable, Collection, DateTime, Model, EloquentCollection.

docker,dlaravel

D-Laravel 1.5.5 Change Notes

D-Laravel's fpm image was rebuilt using the official dockerfile of docker php before php 7.2.1. And so I can specify that the default owner of fpm is dlaravel, --with-fpm-user=USER Set the user for php-fpm to run as. (default: nobody) --with-fpm-group=GRP Set the group for php-fpm to run as.

d-laravel,docker

D-Laravel v1.2.1 version change description (recommended update, must avoid stepping on thunder)

D-Laravel v1.2.1 revision instructions use GuzzleHttp\Client; When posting, there is an access denied problem. If you are upgrading from an older version, you may need to adjust the configuration file in etc/default-ssl.conf (if there is one) Please adjust php:9000 to web:9000, you can adjust it by executing ./console secure once. In the docker-compose configuration file, change the PHP-FPM container to network_mode: "service:web"...