198 words
1 minutes
Optimize ChatGPT API Calls with a Caching System in Symfony 6

As mentioned in the previous article, using the ChatGPT API can quickly become expensive, especially if you make the same request repeatedly. To prevent this, we will implement a caching system to store data.


We will use the Filesystem Cache Adapter, which stores data on your hard drive. The advantage is that it is very simple to implement:

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
...
$cache = new FilesystemAdapter();

Applying it to ChatGPT Code#

If we apply this principle to our previous ChatGPT code, it would look like this:

$sortedClientComments = $cache->get('sorted_client_comments', function (ItemInterface $item) use ($clientComments) {
    $item->expiresAfter(3600);

    $result = $this->openai->completions()->create([
        'model' => 'text-davinci-003',
        'prompt' => 'Sort these restaurant customer comments from nicest to meanest:

' . implode("
", $clientComments),
        'max_tokens' => 1000,
    ]);

    return explode("
", trim($result['choices'][0]['text']));
});

Here, we query ChatGPT only if 3600 seconds have passed since the data was cached.

Bonus: Checking Data Expiry#

You can also check when a piece of data will expire:

$item = $cache->getItem('sorted_client_comments');
$metadata = $item->getMetadata();
$expirationTimeInMinutes = round(($metadata['expiry'] - time()) / 60);
$output->writeln([
    '============',
    'This data will expire in ' . $expirationTimeInMinutes . ' minutes'
]);

Conclusion#

With this caching system, you can significantly reduce the cost of ChatGPT API calls while optimizing performance.

You can find the complete code on GitHub: https://github.com/gponty/sf6-chatGPT