openai integration with laravel

 controller - 


use Illuminate\Support\Facades\Http;

class chatController extends Controller

{

    function generate(Request $request){

        $res = Http::withToken(env('OPENAI_KEY'))->post('https://api.openai.com/v1/chat/completions', [

            'model' => 'gpt-3.5-turbo',

            'messages' => [

                [

                    "role" => "system",

                    "content" => "You are a helpful assistant"

                ],

                [

                    "role" => "user",

                    "content" => "tell me about india bollywood celebs"

                ],

            ],

        ])->json();

        $totalTokens = $res['usage']['total_tokens'];

        $content = $res['choices'][0]['message']['content'];

        

        return view('chatbot',['content'=>$content]);

    }

}

--------------------------------------------------------------------------------------------------------------------------

view - 

   <font>

            {{$content}}

        </font>


        <br>

        <br>


        <!--  this basically breaks newline characters into <br> tag -->

        <b>

            {!! nl2br($content) !!}

        </b>

Popular posts from this blog

MCSL 216 MCA NEW Practical ~ common questions suggestions

dev chaeatsheet

STRAPI