<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Newton Boco</title>
    <description>The latest articles on DEV Community by Newton Boco (@nxvv).</description>
    <link>https://dev.to/nxvv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1086361%2F87c2796c-2c2e-4db1-8b1f-0a5dd2e7ad5e.png</url>
      <title>DEV Community: Newton Boco</title>
      <link>https://dev.to/nxvv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nxvv"/>
    <language>en</language>
    <item>
      <title>Build a LinkedIn Bio Generator in Laravel using the REST API of OpenAI, Livewire and Tailwind CSS.</title>
      <dc:creator>Newton Boco</dc:creator>
      <pubDate>Sun, 21 May 2023 10:48:43 +0000</pubDate>
      <link>https://dev.to/nxvv/build-a-linkedin-bio-generator-in-laravel-using-the-rest-api-of-openai-livewire-and-tailwind-css-1253</link>
      <guid>https://dev.to/nxvv/build-a-linkedin-bio-generator-in-laravel-using-the-rest-api-of-openai-livewire-and-tailwind-css-1253</guid>
      <description>&lt;p&gt;&lt;a href="https://laravel.com/"&gt;Laravel&lt;/a&gt; is a PHP framework with expressive, elegant syntax that strives to provide an amazing developer experience while providing powerful features.&lt;br&gt;
In this article, we will build a LinkedIn Bio Generator in Laravel using the REST API of OpenAI.  The Tailwind CSS and Livewire part will be for a nice graphical user interface. ✨&lt;br&gt;
At the end, you’ll have a web page where the user will provide one or more keywords that will be sent to OpenAI's API.  The API will generate a bio containing those keywords and it will be shown to the user. This web page will look like this : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RWwIEdNV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8z5pce4d84yifa5o3k6y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RWwIEdNV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8z5pce4d84yifa5o3k6y.png" alt="Image description" width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's dive in ! 🚀🚀&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;We will create our project using the latest version of Laravel (v10.3 by the time of writing this article). And for this, you should ensure that your local machine has PHP 8.1 or above and &lt;a href="https://getcomposer.org/"&gt;Composer&lt;/a&gt; installed. In addition, &lt;a href="https://nodejs.org/"&gt;Node and NPM&lt;/a&gt; should be installed too.&lt;/p&gt;
&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;Let’s create a new project with the following command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer create-project laravel/laravel linkedin-bio-generator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the project has been created, move to its root directory and run the below command to install the livewire package :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require livewire/livewire
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Livewire is a full-stack framework that makes building dynamic interfaces simple, without leaving the comfort of Laravel. It simplifies the complexity that Vue or React brings up. 🤩&lt;/p&gt;

&lt;p&gt;Next, you can install your application's frontend dependencies via NPM :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, let’s use the &lt;a href="https://github.com/openai-php/client"&gt;OpenAI PHP library&lt;/a&gt; to work with the REST API of OpenAI. This library is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API. We have to install it via composer :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require openai-php/client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need to have an API key from &lt;a href="https://platform.openai.com/"&gt;OpenAI platform&lt;/a&gt; to be able to communicate with their API. If you don’t already have an API key follow &lt;a href="https://help.socialintents.com/article/188-how-to-find-your-openai-api-key-for-chatgpt"&gt;this short tutorial&lt;/a&gt; to create an account and get it.&lt;/p&gt;

&lt;p&gt;Finally, we need to work with Tailwind CSS in our project for a good looking user interface. 😉  Follow &lt;a href="https://tailwindcss.com/docs/guides/laravel"&gt;this link&lt;/a&gt; to know every steps you need for setting up.&lt;/p&gt;

&lt;p&gt;Great ! We can now start building our &lt;a href="https://github.com/nxvv/linkedin-bio-generator"&gt;LinkedIn Bio Generator&lt;/a&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Create the user interface
&lt;/h2&gt;

&lt;p&gt;We just need a single input to capture the keywords the AI should include into the LinkedIn Bio and a place where we can show the result. &lt;br&gt;
So, inside the directory &lt;code&gt;resources/views&lt;/code&gt; let’s create a new directory named &lt;code&gt;layouts&lt;/code&gt; which will contain a file named &lt;code&gt;app.blade.php&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here’s the content for the &lt;code&gt;resources/views/layouts/app.blade.php&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&amp;gt;
    &amp;lt;title&amp;gt;LinkedIn Bio Generator&amp;lt;/title&amp;gt;

    @vite('resources/css/app.css')

    @livewireStyles

&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

    &amp;lt;nav class="bg-gray-600 px-2 sm:px-4 py-2.5"&amp;gt;
        &amp;lt;div class="container flex flex-wrap items-center justify-between mx-auto"&amp;gt;
            &amp;lt;a href="/" class="flex items-center text-white text-2xl"&amp;gt;
                LinkedIn Bio Generator
        &amp;lt;/a&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/nav&amp;gt;

    &amp;lt;div class="mx-auto max-w-2xl"&amp;gt;

        @livewire('show-form')

    &amp;lt;/div&amp;gt;

    @livewireScripts

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we are adding two blade directives (&lt;code&gt;@livewireStyles&lt;/code&gt; and &lt;code&gt;@livewireScripts&lt;/code&gt;) to include the Livewire styles and scripts on the page. &lt;br&gt;
We are also rendering a Livewire component using this instruction: &lt;code&gt;@livewire('show-form')&lt;/code&gt;. &lt;br&gt;
Don't worry, we will create this component right after 😉.&lt;br&gt;
Finally, we are including in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;  Tailwind’s utility classes to style our content with this instruction : &lt;code&gt;@vite('resources/css/app.css')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can now run the following command to generate our Livewire component called &lt;code&gt;showForm&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:livewire showForm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running this command will generate the following two files : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;app/Http/Livewire/ShowForm.php&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;resources/views/livewire/show-form.blade.php&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s update the &lt;code&gt;resources/views/livewire/show-form.blade.php&lt;/code&gt; file by the following content :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="mt-8"&amp;gt;
    &amp;lt;h1 class="text-4xl font-bold tracking-tight text-zinc-800 sm:text-5xl"&amp;gt;
        LinkedIn Bio Generator
    &amp;lt;/h1&amp;gt;
    &amp;lt;p class="mt-6 text-base text-zinc-60"&amp;gt;
        Save time. &amp;amp;#9203;
        Generate a LinkedIn bio by Artificial Intelligence ! &amp;amp;#128523;
    &amp;lt;/p&amp;gt;
    &amp;lt;form class="mt-12" wire:submit.prevent="getBio"&amp;gt;
        &amp;lt;div class="sm:flex sm:w-full sm:max-w-lg"&amp;gt;
            &amp;lt;div class="min-w-0 flex-1"&amp;gt;
                &amp;lt;div&amp;gt;
                    &amp;lt;div class="relative rounded-md shadow-sm"&amp;gt;
                        &amp;lt;input type="text" wire:model.defer="keywords" name="keywords" id="keywords" 
                            class="block w-full rounded-md border 
                            border-gray-300 px-5 py-3 text-base  
                            text-gray-900 placeholder-gray-700 
                            shadow-sm focus:border-teal-500 
                            focus:ring-teal-500 " 
                            placeholder="Type keywords (PHP, Developer, ...)"
                        &amp;gt;
                    &amp;lt;/div&amp;gt;
                    &amp;lt;p class="mt-2 text-sm text-red-600" id="keywords-error"&amp;gt;&amp;lt;/p&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;div class="mt-4 sm:mt-0 sm:ml-3"&amp;gt;
                &amp;lt;button type="submit" class="block w-full rounded-md border border-transparent
                    bg-gray-600 px-5 py-3 text-base font-medium text-white shadow 
                    hover:bg-emerald-500 focus:outline-none focus:ring-2 focus:ring-teal-500 
                    focus:ring-offset-2 sm:px-10"
                &amp;gt;
                    Get my biooo &amp;amp;#127870; &amp;amp;#127870;
                &amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/form&amp;gt;

    &amp;lt;div&amp;gt;
        @if ($bio)
            &amp;lt;h2 class="mt-4 mb-1 text-2xl font-medium"&amp;gt;Your awesome bio!&amp;lt;/h2&amp;gt;
            &amp;lt;div class="mb-12 bg-gray-100 text-grey-900 p-4 rounded-lg"&amp;gt;
                {{$bio}}
            &amp;lt;/div&amp;gt;
        @endif

        &amp;lt;div wire:loading.flex class="hidden p-6 justify-center"&amp;gt;
            &amp;lt;div role="status"&amp;gt;
                &amp;lt;svg class="inline mr-2 w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-gray-600 dark:fill-gray-300" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"&amp;gt;
                    &amp;lt;path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"&amp;gt;&amp;lt;/path&amp;gt;
                    &amp;lt;path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"&amp;gt;&amp;lt;/path&amp;gt;
                &amp;lt;/svg&amp;gt;
                &amp;lt;span class="sr-only"&amp;gt;Loading...&amp;lt;/span&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we just created a single input to capture the keywords the AI should include into the LinkedIn Bio and a place where we can show the result. &lt;/p&gt;

&lt;p&gt;Finally, add a &lt;code&gt;resources/views/home.blade.php&lt;/code&gt; file with this content : &lt;code&gt;@extends('layouts.app')&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the AI power
&lt;/h2&gt;

&lt;p&gt;Open the &lt;code&gt;app/Http/Livewire/ShowForm.php&lt;/code&gt; file and update it with the following code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Http\Livewire;

use Livewire\Component;

use OpenAI;

class ShowForm extends Component
{
    public $bio;
    public $keywords;

    public function render()
    {
        return view('livewire.show-form');
    }

    public function getBio()
    {
        $key = config('services.open_ai.api_key');
        $client = OpenAI::client($key);

        $result = $client-&amp;gt;completions()-&amp;gt;create([
            'model' =&amp;gt; 'text-davinci-003',
           'prompt' =&amp;gt; 'Write a LinkedIn bio including the keywords: ' . $this-&amp;gt;keywords,
            'max_tokens' =&amp;gt; 256,
            'temperature' =&amp;gt; 1 
        ]);

        $this-&amp;gt;bio = $result-&amp;gt;choices[0]-&amp;gt;text;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in this file, we added the &lt;code&gt;getBio()&lt;/code&gt;  method to our component and here are the explanations : &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Get the value of the API key&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$key = config('services.open_ai.api_key');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this, let’s add in the &lt;code&gt;.env&lt;/code&gt; file a new line providing the OpenAI API key :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OPENAI_API_KEY="xxxxxxxxxxxx"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in the &lt;code&gt;config/services.php&lt;/code&gt; we also need some new lines :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'open_ai' =&amp;gt; [
        'api_key' =&amp;gt; env('OPENAI_API_KEY')
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Create a client that will interact with the OpenAI API&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$client = OpenAI::client($key);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Call the OpenAI completions endpoint&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        $result = $client-&amp;gt;completions()-&amp;gt;create([
            'model' =&amp;gt; 'text-davinci-003',
           'prompt' =&amp;gt; 'Write a LinkedIn bio including the keywords: ' . $this-&amp;gt;keywords,
            'max_tokens' =&amp;gt; 256,
            'temperature' =&amp;gt; 1 
        ]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a completion for the provided prompt and parameters.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;model&lt;/code&gt; parameter specifies the ID of the model to use. &lt;br&gt;
The OpenAI API is powered by a diverse set of models with different capabilities. Here, we are using  &lt;code&gt;davinci&lt;/code&gt; which is the most capable GPT-3 model.&lt;/p&gt;

&lt;p&gt;Then we create a &lt;code&gt;prompt&lt;/code&gt; which we pass to the AI to complete. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;max_tokens&lt;/code&gt; is the maximum number of tokens to generate in the completion. 256 tokens lead to a maximal length of around 1000 characters. To learn more about the tokens see the &lt;a href="https://platform.openai.com/tokenizer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;temperature&lt;/code&gt;  provides the sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random/creative, while lower values like 0.2 will make it more focused and deterministic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Get the result&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$this-&amp;gt;bio = $result-&amp;gt;choices[0]-&amp;gt;text;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We finally get our LinkedIn bio text generated by OpenAI API. 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the project
&lt;/h2&gt;

&lt;p&gt;Before running the project, let’s register a web route to our nice page. So, open the &lt;code&gt;/routes/web.php&lt;/code&gt;  and add the following lines of code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('/get-bio', function () {
    return view('home');
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Everything is finally ready to run our application.🥳 &lt;/p&gt;

&lt;p&gt;Then start the server with the following command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan serve  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And run &lt;code&gt;npm run dev&lt;/code&gt; for the Tailwind CSS build process. &lt;/p&gt;

&lt;p&gt;Now, we can access our application in the browser at &lt;code&gt;http://127.0.0.1:8000/get-bio&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qbsa4x7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvcggyk013dc9xbhw7hq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qbsa4x7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvcggyk013dc9xbhw7hq.png" alt="Image description" width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we’ve learned how to build a LinkedIn Bio Generator in Laravel using the REST API of OpenAI, Livewire and Tailwind CSS.&lt;br&gt;
And as every article can be made better so your suggestions or questions are welcome in the comment section. 😉&lt;br&gt;
Check the code of this tutorial &lt;a href="https://github.com/nxvv/linkedin-bio-generator"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>openai</category>
      <category>laravel</category>
      <category>tailwindcss</category>
      <category>livewire</category>
    </item>
  </channel>
</rss>
