Integrate OpenAI

Edit on GitHub

This document describes how to integrate OpenAI. This integration enables other functionalities to use AI.

Prerequisites

Install the required features:

NAME VERSION INSTALLATION GUIDE
Spryker Core 202410.0 Install the Spryker Core feature

1) Install the required modules

Install the required module:

composer require spryker-eco/open-ai:"^0.1.1" --update-with-dependencies
Verification

Make sure the following modules have been installed:

MODULE EXPECTED DIRECTORY
OpenAi vendor/spryker-eco/open-ai

2) Set up the configuration

  1. Using the data from your OpenAI account, add the OpenAI secret key as a OPEN_AI_API_TOKEN env variable to the needed configuration file. For an example, see Migrate to cloud: Define environment variables

  2. Add the following configuration:

CONFIGURATION SPECIFICATION NAMESPACE
OpenAiConstants::API_TOKEN Specifies the OpenAI API token. SprykerEco\Shared\OpenAi

config/Shared/config_default.php*


use SprykerEco\Shared\OpenAi\OpenAiConstants;

$config[OpenAiConstants::API_TOKEN] = getenv('OPEN_AI_API_TOKEN');

3) Set up transfer objects

Generate transfer changes:

console transfer:generate
Verification

Make sure the following changes have been applied in transfer objects:

TRANSFER TYPE EVENT PATH
OpenAiChatRequest class created src/Generated/Shared/Transfer/OpenAiChatRequestTransfer
OpenAiChatResponse class created src/Generated/Shared/Transfer/OpenAiChatResponseTransfer
Verification

Add and run the following code in your application:


<?php

use Generated\Shared\Transfer\OpenAiChatRequestTransfer;
use SprykerEco\Client\OpenAi\OpenAiClient;

$openAiClient = new OpenAiClient();
$openAiChatResponseTransfer = $openAiClient->chat((new OpenAiChatRequestTransfer())->setMessage('Hello'));

Make sure $openAiChatResponseTransfer contains the OpenAI response.