byjg / llm-api-objects
Strongly-typed PHP objects for LLM API request payloads
Requires
- php: >=8.3
- byjg/anydataset-xml: ^6.0
Requires (Dev)
- openai-php/client: ^v0.19
- phpunit/phpunit: ^10.5|^11.5
- vimeo/psalm: ^5.9|^6.13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-25 23:50:35 UTC
README
| sidebar_key | llm-api-objects | ||
|---|---|---|---|
| sidebar_category | text-content | ||
| tags |
|
LLM API Objects
Typed PHP objects for Chat Completions requests (messages, tools, tool calls) that work with both OpenAI and Ollama.
Works with both OpenAI and Ollama (via the /v1 OpenAI-compatible endpoint) using the openai-php/client.
Why
A chat completion request is a deeply nested array: roles, content, tools with JSON-schema parameters, tool calls and their ids. Built by hand, a typo becomes an API error at runtime, and a long document has to be cut to fit the context by you.
LLM API Objects builds the request from typed classes (Chat, Message, Tool,
ToolFunction, ToolParameter), so the IDE and static analysis catch the mistakes before
the call.
- OpenAI and Ollama with the same objects, through Ollama's OpenAI-compatible endpoint.
- Long content split for you. A message is split into chunks by a delimiter and by an
approximate token limit (
maxTokens).
Features
- Strongly-typed models for Chat Completions requests
- Full support for function calling (tools):
Tool,ToolFunction,ToolParameter,ToolChoice - Multi-turn conversations with
role=toolmessages andtool_call_id - Automatic message splitting when content exceeds a token limit
- Delimiter-based message partitioning
- Compatible with OpenAI and Ollama (
/v1endpoint) - Ollama model tuning parameters via
ModelFileParameters - Jira XML/RSS importers to seed conversation context
Documentation
- Getting Started — Installation and first request
- Chat Model — Building and sending chat requests
- Messages — Roles, content,
tool_call_id,tool_calls - Tools — Function calling: Tool, ToolFunction, ToolParameter, ToolChoice
- Model Parameters — Ollama model tuning parameters
- Importers — Jira XML/RSS importers
Quick Examples
Basic chat with OpenAI
<?php require 'vendor/autoload.php'; $client = OpenAI::factory() ->withApiKey($_ENV['OPENAI_API_KEY']) ->make(); $chat = new \ByJG\LlmApiObjects\Model\Chat( model: 'gpt-4o', messages: [ new \ByJG\LlmApiObjects\Model\Message( role: \ByJG\LlmApiObjects\Enum\Role::User, message: 'What is the capital of France?', ), ], system: 'You are a helpful geography assistant.', ); $result = $client->chat()->create($chat->toApi()); echo $result->choices[0]->message->content;
Basic chat with Ollama
<?php $client = OpenAI::factory() ->withApiKey('ollama') ->withBaseUri('http://localhost:11434/v1') ->make(); $chat = new \ByJG\LlmApiObjects\Model\Chat( model: 'llama3', messages: [ new \ByJG\LlmApiObjects\Model\Message( role: \ByJG\LlmApiObjects\Enum\Role::User, message: 'Hello!', ), ], ); $result = $client->chat()->create($chat->toApi()); echo $result->choices[0]->message->content;
Function calling (tools)
<?php use ByJG\LlmApiObjects\Enum\ToolChoice; use ByJG\LlmApiObjects\Model\Chat; use ByJG\LlmApiObjects\Model\Message; use ByJG\LlmApiObjects\Enum\Role; use ByJG\LlmApiObjects\Tool\Tool; use ByJG\LlmApiObjects\Tool\ToolFunction; use ByJG\LlmApiObjects\Tool\ToolParameter; $weatherTool = new Tool( new ToolFunction( name: 'get_current_weather', description: 'Get the current weather in a given location', parameters: [ new ToolParameter(name: 'location', type: 'string', description: 'City name', required: true), new ToolParameter(name: 'unit', type: 'string', enum: ['celsius', 'fahrenheit']), ], ), ); $chat = new Chat( model: 'gpt-4o', messages: [ new Message(role: Role::User, message: "What's the weather in Paris?"), ], ); $chat->addTool($weatherTool); $chat->setToolChoice(ToolChoice::Auto); $result = $client->chat()->create($chat->toApi());
Installation
composer require byjg/llm-api-objects
Running Tests
composer test