Skip to content

Commit

Permalink
📝 Fix the ChatGPT command example
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Feb 7, 2024
1 parent 59d5dbe commit 3646b12
Showing 1 changed file with 95 additions and 7 deletions.
102 changes: 95 additions & 7 deletions content/docs/chatgpt.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ priority: 1
group: Examples
---

Creating a Discord command to interact with OpenAI's API is quite easy thanks to the official [Laravel OpenAI package](https://github.com/openai-php/laravel).
Creating a Discord command to interact with OpenAI's API is quite easy thanks to the official [OpenAI PHP package](https://github.com/openai-php/client).

> #### Note
>
> We are unable to use `openai-php/laravel` due to it requiring `laravel/framework` as a dependency which conflicts with Laracord.
In this example, we will create a simple `!chat` command to ask ChatGPT questions through the Discord bot.

## Requirements

Before we get started, make sure you have an [OpenAI API Key](https://platform.openai.com/api-keys) on hand.

Once you have your API key, install the OpenAI package using Composer:
Once you have your API key, install the OpenAI Client package using Composer:

```sh
$ composer require openai-php/laravel
$ composer require openai-php/client
```

and then add your API key to `.env`:
Expand All @@ -38,6 +42,7 @@ $ php laracord make:command Chat

I won't bore you with a breakdown of the code, but it achieves the following goals:

- Initializes the OpenAI client using the provided key in **.env**.
- Requires an argument (message) to be passed.
- Trims incoming messages to 384 characters to prevent abuse.
- Caches the ongoing conversation for 1 minute.
Expand All @@ -50,7 +55,7 @@ Here's how it is done:
namespace App\Commands;

use Laracord\Commands\Command;
use OpenAI\Laravel\Facades\OpenAI;
use OpenAI;

class Chat extends Command
{
Expand Down Expand Up @@ -83,11 +88,21 @@ class Chat extends Command
protected $apiKey = '';

/**
* The OpenAI prompt.
* The OpenAI client instance.
*
* @var string
* @var \OpenAI\Client
*/
protected $client;

/**
* The OpenAI API key.
*/
protected $prompt = 'You only reply with 1-2 sentences at a time as if responding to a chat message.';
protected string $apiKey = '';

/**
* The OpenAI system prompt.
*/
protected string $prompt = 'You only reply with 1-2 sentences at a time as if responding to a chat message.';

/**
* Handle the command.
Expand Down Expand Up @@ -132,5 +147,78 @@ class Chat extends Command
->send($message);
});
}

/**
* Execute the Discord command.
*
* @param \Discord\Parts\Channel\Message $message
* @param array $args
* @return mixed
*/
public function handle($message, $args)
{
$input = trim(
implode(' ', $args ?? [])
);

if (! $input) {
return $this
->message('You must provide a message.')
->title('Chat')
->error()
->send($message);
}

$message->channel->broadcastTyping()->done(function () use ($message, $input) {
$key = "{$message->channel->id}.chat.responses";
$input = Str::limit($input, 384);

$messages = cache()->get($key, [['role' => 'system', 'content' => $this->prompt]]);
$messages[] = ['role' => 'user', 'content' => $input];

$result = $this->client()->chat()->create([
'model' => 'gpt-3.5-turbo',
'messages' => $messages,
]);

$response = $result->choices[0]->message->content;

$messages[] = ['role' => 'assistant', 'content' => $response];

cache()->put($key, $messages, now()->addMinute());

return $this
->message($response)
->send($message);
});
}

/**
* Retrieve the OpenAPI client instance.
*
* @return \OpenAI\Client
*/
protected function client()
{
if ($this->client) {
return $this->client;
}

return $this->client = OpenAI::client($this->apiKey());
}

/**
* Retrieve the OpenAPI API key.
*
* @return string
*/
protected function apiKey()
{
if ($this->apiKey) {
return $this->apiKey;
}

return $this->apiKey = env('OPENAI_API_KEY', $this->apiKey);
}
}
```

0 comments on commit 3646b12

Please sign in to comment.