Integrate OpenAI

Edit on GitHub

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

Install feature core

Follow the steps below to install the OpenAi module core.

Prerequisites

Install the required features:

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

1) Install the required modules

Run the following command to 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

Using the data from your OpenAI account, make sure the OPEN_AI_API_TOKEN env variable contains the OpenAI secret key and add the following configuration:

config/Shared/config_default.php


use SprykerEco\Shared\OpenAi\OpenAiConstants;

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

3) Set up transfer objects

Run the following command to 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

To validate the whole integration add and run the following code in your application, the $openAiChatResponseTransfer should contain the OpenAI response:


<?php

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

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