hch/chatgpt-integration-bundle

A Symfony bundle to integrate ChatGPT API.

v1.0.0 2024-10-25 18:46 UTC

This package is auto-updated.

Last update: 2025-04-25 19:50:19 UTC


README

This bundle provides a simple way to integrate ChatGPT API with Symfony applications.

Installation

composer require HCH/chatgpt-integration-bundle

Configuration

In your Symfony config, add your OpenAI API key, API URL, and model:

# config/packages/chatgpt_integration.yaml
chatgpt_integration:
    api_key: '%env(CHATGPT_API_KEY)%'
    api_url: 'https://api.openai.com/v1/chat/completions'
    model: 'gpt-3.5-turbo'

Usage

Inject the ChatGPTClient service wherever needed:

use HCH\ChatGPTIntegrationBundle\Service\ChatGPTClient;

class SomeService
{
    private $chatGPTClient;

    public function __construct(ChatGPTClient $chatGPTClient)
    {
        $this->chatGPTClient = $chatGPTClient;
    }

    public function getResponseFromChatGPT(string $message): string
    {
        return $this->chatGPTClient->ask($message);
    }
}

Testing

Run tests with PHPUnit:

php bin/phpunit

License

This bundle is released under the MIT License.