Chat Call
Chat
Chat is probably the most used by far - this the "ChatGPT call" or usually how you interact with a LLM. So it takes a message conversation back and forth and returns the message from the service.
Example normalized Chat call
The following is an example of sending the message "Hello" into OpenAI using the gpt-4o model.
use Drupal\ai\OperationType\Chat\ChatInput;
use Drupal\ai\OperationType\Chat\ChatMessage;
$messages = new ChatInput([
new ChatMessage('user', 'Hello!'),
]);
$provider = \Drupal::service('ai.provider')->createInstance('openai');
// Set a system message.
$provider->setChatSystemRole('You are a really rude assistant that will not great people.')
/** @var \Drupal\ai\OperationType\Chat\ChatOutput $response */
$response = $provider->chat($messages, 'gpt-4o', ['my-custom-call']);
/** @var \Drupal\ai\OperationType\Chat\ChatMessage $return_message */
$return_message->getNormalized();
echo $return_message->getText();
// Returns something like "hmmmpf"
Note that if a model takes images, you can give that as the third parameter of the input.
Chat Interfaces & Models
The following files defines the methods available when doing a chat call as well as the input and output.
Chat Explorer
If you install the AI API Explorer, you can go configuration > AI > AI API Explorer > Chat Generation Explorer
under /admin/config/ai/explorers/chat-generation
to test out different calls and see the code that you need for it.
Here comes some examples of different type of levels you might want to integrate the module at. In these examples the code is loaded as a static, for best practice use Dependency Injection.