Skip to content

Aczeko/livewire-ai-chat

Repository files navigation

Livewire-AI-Chatbot

Table of Contents


🔎 Overview

This is an AI chatbot using Livewire and ChatGPT.

It's using wire:stream to stream in ChatGPT responses as they arrive, by building a chat interface with Livewire.

Every message that is sent and every response is shown in the chat history. It also remembers the conversation context, though nothing is saved in a database. It's using the OpenAI PHP client, that allows you to interact with the OpenAI API.

Since it is interacting with the OpenAI API, you will need to create an account at OpenAI and generate an API Key.

⚙️ Installation

  1. Clone this repository:

    git clone git@github.com:Aczeko/livewire-ai-chat.git
  2. Navigate into the directory:

    cd yourproject
  3. Install dependencies:

    npm install
    composer install 
  4. Duplicate the .env.example file and save it as .env

    • here you can enter your OpenAI API Key
    OPENAI_API_KEY=
    
  5. Generate a new Application Key:

    php artisan key:generate
  6. Migrate the tables

    php artisan migrate
  7. Run your build process with:

    npm run dev

🛠️ Usage

Model

Depending on which ChatGPT model you are using, you'll need to change it in the code. Currently, it is set to GPT-3.5 Turbo, since you're given a limited amount of free credits, so you can test out the bot, without having to upgrade to one of the paid plans. Though you should keep in mind, that OpenAI might have restrictions or changes in the free tier that apply to your account, limiting the number of requests you can make.

If you do want to change the model, you can do that in the ChatResponse.php file:

$stream = app('openai')->chat()->createStreamed([
            'model' => 'gpt-3.5-turbo', // here you can change the model to your preference
            'messages' => $this->messages,
        ]);

Role

You can set up what role the bot should have and how it should communicate with you. In the code, the bot is set as a "friendly web developer".

To change the bot's role, you need to change the content inside the mount()method in theChat.php` file:

public function mount()
    {
        $this->messages[] = ['role' => 'system', 'content' => 'You are a friendly web developer here to help.']; // Change content to your liking
    }

If you want the bot to always start the conversation with a certain word or message, you can also add that to the content inside the mount() method:

Example

public function mount()
    {
        $this->messages[] = ['role' => 'system', 'content' => 'You are a friendly web developer here to help. Always start the conversation with Hey!']; // Change content to your liking
    }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages