Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added console command to talk with openai #279

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ APP_SECRET=cc5533299b0fa8bcc05cca4ebe21f6dd
DATABASE_URL="mysql://root:0000@mysql:3306/app"
# DATABASE_URL="postgresql://app:[email protected]:5432/app?serverVersion=15&charset=utf8"
###< doctrine/doctrine-bundle ###

OPENAI_API_KEY=""
6 changes: 6 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ services:

# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

GuzzleHttp\Client:

App\OpenAI\GuzzleOpenAIClient:
arguments:
$openaiApiKey: '%env(string:OPENAI_API_KEY)%'
47 changes: 47 additions & 0 deletions src/Console/OpenAICommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Console;

use App\OpenAI\Language;
use App\OpenAI\OpenAIClient;
use App\OpenAI\Responder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class OpenAICommand extends Command
{
public function __construct(private OpenAIClient $openAIClient)
{
parent::__construct();
}

protected function configure(): void
{
$this->setName('openai:ask');
$this->addArgument('question', InputArgument::REQUIRED, 'Your question');
$this->addOption('responder', null, InputOption::VALUE_REQUIRED, 'Who do you want to respond to you?');
$this->addOption('language', null, InputOption::VALUE_REQUIRED, 'what language do you want your answer in?');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($input->getOption('responder')) {
$this->openAIClient->setResponder(Responder::tryFrom(strtolower($input->getOption('responder'))));
}

if ($input->getOption('language')) {
$this->openAIClient->setLanguage(Language::tryFrom(strtolower($input->getOption('language'))));
}

$answer = $this->openAIClient->ask($input->getArgument('question'));

$output->writeln($answer);

return Command::SUCCESS;
}
}
69 changes: 69 additions & 0 deletions src/OpenAI/GuzzleOpenAIClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace App\OpenAI;

use GuzzleHttp\Client;

final class GuzzleOpenAIClient implements OpenAIClient
{
private const URL = 'https://api.openai.com/v1/chat/completions';
private ?Responder $responder = null;
private ?Language $language = null;

public function __construct(private readonly Client $client, private readonly string $openaiApiKey)
{
}

public function setResponder(?Responder $responder): void
{
if ($responder === null) {
return;
}

$this->responder = $responder;
}

public function setLanguage(?Language $language): void
{
if ($language === null) {
return;
}

$this->language = $language;
}

public function ask(string $prompt): string
{
$headers = [
'Authorization' => 'Bearer ' . $this->openaiApiKey,
'Accept' => 'application/json',
];

$params = [
'messages' => [],
'model' => 'gpt-3.5-turbo',
'temperature' => 0,
];

if ($this->responder) {
$params['messages'][] = ['role' => 'system', 'content' => sprintf('Pretend you are the following person: %s', $this->responder->value)];
}

if ($this->language) {
$params['messages'][] = ['role' => 'system', 'content' => sprintf('Answer in the following language: %s', $this->language->value)];
}

$params['messages'][] = ['role' => 'user', 'content' => $prompt];

$response = $this->client->post(self::URL, [
'headers' => $headers,
'json' => $params,
]);

$contents = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);

return $contents['choices'][0]['message']['content'];
}
}
13 changes: 13 additions & 0 deletions src/OpenAI/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\OpenAI;

enum Language: string
{
case ENGLISH = 'english';
case DUTCH = 'dutch';
case GERMAN = 'german';
case FRENCH = 'french';
}
14 changes: 14 additions & 0 deletions src/OpenAI/OpenAIClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\OpenAI;

interface OpenAIClient
{
public function setResponder(?Responder $responder): void;

public function setLanguage(?Language $language): void;

public function ask(string $prompt): string;
}
12 changes: 12 additions & 0 deletions src/OpenAI/Responder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\OpenAI;

enum Responder: string
{
case MACHO_MAN_RANDY_SAVAGE = 'randysavage';
case CAPTAIN_JACK_SPARROW = 'jacksparrow';
case PIKACHU = 'pikachu';
}