Getting Started
byjg/llm-api-objects provides a strongly-typed PHP model layer for building OpenAI Chat Completions request payloads. All models serialize to array via toApi(), ready to pass directly to openai-php/client.
It works with both OpenAI and Ollama (via the /v1 OpenAI-compatible endpoint).
Installation
composer require byjg/llm-api-objects
Requirements
- PHP 8.3+
Connecting to OpenAI
<?php
require 'vendor/autoload.php';
$client = OpenAI::factory()
->withApiKey($_ENV['OPENAI_API_KEY'])
->make();
Connecting to Ollama
Ollama exposes an OpenAI-compatible /v1 endpoint. Pass any string as the API key (Ollama does not validate it) and set the base URI:
$client = OpenAI::factory()
->withApiKey('ollama')
->withBaseUri('http://localhost:11434/v1')
->make();
Your First Request
<?php
use ByJG\LlmApiObjects\Enum\Role;
use ByJG\LlmApiObjects\Model\Chat;
use ByJG\LlmApiObjects\Model\Message;
$chat = new Chat(
model: 'gpt-4o',
messages: [
new Message(role: Role::user, message: 'What is the capital of France?'),
],
system: 'You are a helpful assistant.',
);
$result = $client->chat()->create($chat->toApi());
echo $result->choices[0]->message->content;
// Paris
tip
toApi() returns a plain PHP array. You can inspect it with print_r($chat->toApi()) before sending to debug the exact payload.
Multi-turn Conversation
Pass multiple Message objects to build a conversation history:
use ByJG\LlmApiObjects\Enum\Role;
use ByJG\LlmApiObjects\Model\Chat;
use ByJG\LlmApiObjects\Model\Message;
$chat = new Chat(
model: 'gpt-4o',
messages: [
new Message(role: Role::user, message: 'My name is Alice.'),
new Message(role: Role::assistant, message: 'Nice to meet you, Alice!'),
new Message(role: Role::user, message: 'What is my name?'),
],
);
$result = $client->chat()->create($chat->toApi());
echo $result->choices[0]->message->content;
// Your name is Alice.
Next Steps
- Chat Model — all Chat options (system, stream, parameters)
- Messages — roles, token splitting, tool result messages
- Tools — function calling
- Model Parameters — Ollama tuning parameters
- Importers — seed context from Jira exports