PHPackages                             aihotel/ai-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. aihotel/ai-client

ActiveLibrary[API Development](/categories/api)

aihotel/ai-client
=================

Unified PHP client for AI services (OpenAI GPT, Claude, and more)

v1.0.3(7mo ago)7141↓100%MITPHPPHP &gt;=8.1

Since Jul 31Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/dengaletin/ai-hotel)[ Packagist](https://packagist.org/packages/aihotel/ai-client)[ Docs](https://github.com/dengaletin/ai-hotel)[ RSS](/packages/aihotel-ai-client/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (3)Versions (5)Used By (0)

🤖 AiHotel - Simple AI Client for PHP
====================================

[](#-aihotel---simple-ai-client-for-php)

[![PHP Version](https://camo.githubusercontent.com/04744bae0a61d2ffe29c26f07a9612eae20445fc6feaeb77b3af1f0e9be6447c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e312d3838393242462e737667)](https://php.net/)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

Clean, simple, and powerful PHP client for OpenAI GPT. Built with modern PHP 8.1+ features, strict types, and developer happiness in mind.

✨ Why AiHotel?
--------------

[](#-why-aihotel)

- **Zero Configuration Hassle** - Just add your API key and go
- **Conversation History** - Built-in chat history management
- **Exception-Based Error Handling** - Clear exceptions with helpful messages
- **Modern PHP** - Uses latest PHP 8.1+ features with strict types
- **Clean API** - Simple, intuitive interface

🚀 Quick Start
-------------

[](#-quick-start)

### Installation

[](#installation)

```
composer require aihotel/ai-client
```

### Your First Chat

[](#your-first-chat)

```
use AiHotel\Services\AiHotelClient;
use AiHotel\Dto\Request\OpenAi\Request;
use AiHotel\Enum\OpenAi\Model;
use AiHotel\Exceptions\AiHotelException;

try {
    // Create client with your OpenAI API key
    $client = AiHotelClient::create([
        'openai_api_key' => 'sk-your-openai-api-key-here'
    ]);

    $request = new Request(
        message: 'Explain quantum computing in simple terms',
        model: Model::GPT_4
    );

    $response = $client->gpt()->chat($request);
    echo $response->getContent();

} catch (AiHotelException $e) {
    echo "Error: " . $e->getMessage();
}
```

💬 Chat with History
-------------------

[](#-chat-with-history)

```
use AiHotel\Services\Builders\OpenAi\HistoryBuilder;
use AiHotel\Enum\OpenAi\Role;

try {
    $client = AiHotelClient::create([
        'openai_api_key' => 'sk-your-openai-api-key-here'
    ]);

    // Build conversation history
    $history = HistoryBuilder::create()
        ->addMessage(Role::SYSTEM, 'You are a helpful coding assistant')
        ->addMessage(Role::USER, 'How do I create a PHP class?')
        ->addMessage(Role::ASSISTANT, 'To create a PHP class, use the `class` keyword...')
        ->addMessage(Role::USER, 'Can you show me an example?')
        ->build();

    $request = new Request(
        message: 'Make it a simple User class',
        history: $history,
        model: Model::GPT_4,
        temperature: 0.7
    );

    $response = $client->gpt()->chat($request);
    echo $response->getContent();

} catch (AiHotelException $e) {
    echo "Error: " . $e->getMessage();
}
```

📚 Usage Examples
----------------

[](#-usage-examples)

### Simple Chat

[](#simple-chat)

```
use AiHotel\Dto\Request\OpenAi\Request;
use AiHotel\Enum\OpenAi\Model;

$request = new Request(
    message: 'Write a haiku about PHP',
    model: Model::GPT_4_TURBO
);

$response = $client->gpt()->chat($request);
echo $response->getContent();
```

### Chat with System Prompt

[](#chat-with-system-prompt)

```
$request = new Request(
    message: 'How do I center a div?',
    model: Model::GPT_4,
    systemPrompt: 'You are a senior frontend developer. Be concise and practical.'
);

$response = $client->gpt()->chat($request);
echo $response->getContent();
```

### Conversation with History

[](#conversation-with-history)

```
use AiHotel\Services\Builders\OpenAi\HistoryBuilder;
use AiHotel\Enum\OpenAi\Role;

// Build conversation history
$history = (new HistoryBuilder())
    ->addMessage(Role::USER, 'My name is John')
    ->addMessage(Role::ASSISTANT, 'Nice to meet you, John!')
    ->addMessage(Role::USER, 'What programming languages do you recommend?')
    ->addMessage(Role::ASSISTANT, 'For beginners, I recommend starting with Python...')
    ->build();

// Continue the conversation
$request = new Request(
    message: 'What was my name again?',
    history: $history,
    model: Model::GPT_4
);

$response = $client->gpt()->chat($request);
// Will respond: "Your name is John!"
echo $response->getContent();
```

### Custom Parameters

[](#custom-parameters)

```
$request = new Request(
    message: 'Generate creative story ideas',
    model: Model::GPT_4,
    temperature: 1.2,  // More creative
    maxTokens: 500     // Limit response length
);

$response = $client->gpt()->chat($request);
echo $response->getContent();
```

🎯 Available Models
------------------

[](#-available-models)

```
use AiHotel\Enum\OpenAi\Model;

Model::GPT_4          // Most capable model
Model::GPT_4_TURBO    // Faster and cheaper than GPT-4
Model::GPT_3_5_TURBO  // Fast and cost-effective
```

🛡️ Exception-Based Error Handling
---------------------------------

[](#️-exception-based-error-handling)

The library uses exceptions for clean error handling:

```
use AiHotel\Exceptions\AiHotelException;

try {
    $response = $client->gpt()->chat($request);
    echo $response->getContent();

} catch (AiHotelException $e) {
    // Handle API errors
    echo "API Error: " . $e->getMessage();

} catch (Exception $e) {
    // Handle unexpected errors
    echo "Unexpected error: " . $e->getMessage();
}
```

📋 Response Information
----------------------

[](#-response-information)

Every successful response contains useful metadata:

```
$response = $client->gpt()->chat($request);

echo "Content: " . $response->getContent();
echo "Tokens used: " . $response->getTokensUsed();
echo "Model: " . $response->getModel();
```

🔧 Advanced Features
-------------------

[](#-advanced-features)

### History Builder

[](#history-builder)

Build complex conversations easily:

```
use AiHotel\Services\Builders\OpenAi\HistoryBuilder;
use AiHotel\Enum\OpenAi\Role;

$historyBuilder = new HistoryBuilder();

// Add messages fluently
$historyBuilder
    ->addMessage(Role::SYSTEM, 'You are a helpful assistant')
    ->addMessage(Role::USER, 'Hello!')
    ->addMessage(Role::ASSISTANT, 'Hi there! How can I help?')
    ->addMessage(Role::USER, 'Tell me about PHP');

// Build the history array
$history = $historyBuilder->build();

// Use in request
$request = new Request(
    message: 'What are the benefits of PHP 8.4?',
    history: $history,
    model: Model::GPT_4
);
```

### Alternative History Builder Syntax

[](#alternative-history-builder-syntax)

```
// Static factory method
$history = HistoryBuilder::create()
    ->addMessage(Role::USER, 'Start conversation')
    ->build();
```

🚀 Best Practices
----------------

[](#-best-practices)

### 1. Always Use Try-Catch

[](#1-always-use-try-catch)

```
try {
    $response = $client->gpt()->chat($request);
    // Process successful response
    echo $response->getContent();
} catch (AiHotelException $e) {
    // Log error and show user-friendly message
    error_log($e->getMessage());
    echo "Sorry, I couldn't process your request right now.";
}
```

### 2. Manage Token Usage

[](#2-manage-token-usage)

```
$response = $client->gpt()->chat($request);

// Monitor token usage for cost control
$tokensUsed = $response->getTokensUsed();
if ($tokensUsed > 1000) {
    // Log high usage or notify admin
    error_log("High token usage: {$tokensUsed} tokens");
}
```

### 3. Use System Prompts for Consistency

[](#3-use-system-prompts-for-consistency)

```
$systemPrompt = 'You are a helpful coding assistant. ' .
               'Always provide working code examples and explain your solutions clearly.';

$request = new Request(
    message: $userMessage,
    model: Model::GPT_4,
    systemPrompt: $systemPrompt
);
```

🔧 Configuration
---------------

[](#-configuration)

### Environment Variables

[](#environment-variables)

You can use environment variables for API keys:

```
// .env file
OPENAI_API_KEY=sk-your-openai-api-key-here

// PHP code
$client = AiHotelClient::create([
    'openai_api_key' => $_ENV['OPENAI_API_KEY'] ?? getenv('OPENAI_API_KEY')
]);
```

### Custom Timeout

[](#custom-timeout)

```
// Configure longer timeout for complex requests
$client = AiHotelClient::create([
    'openai_api_key' => 'sk-your-key',
    'timeout' => 60  // 60 seconds
]);
```

📖 API Reference
---------------

[](#-api-reference)

### Request Parameters

[](#request-parameters)

ParameterTypeRequiredDescription`message``string`YesThe user message to send`model``Model`NoOpenAI model to use (default: GPT\_3\_5\_TURBO)`systemPrompt``string`NoSystem prompt to set behavior`history``array`NoConversation history`temperature``float`NoCreativity level (0.0-2.0)`maxTokens``int`NoMaximum response length### Response Properties

[](#response-properties)

PropertyTypeDescription`content``string|null`The AI response content`tokensUsed``int|null`Number of tokens consumed`model``string|null`Model used for the response🤝 Contributing
--------------

[](#-contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License
---------

[](#-license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

🔗 Links
-------

[](#-links)

- [OpenAI API Documentation](https://platform.openai.com/docs)
- [PHP 8.1 Documentation](https://www.php.net/releases/8_1_x.php)

###  Health Score

37

—

LowBetter than 84% of packages

Maintenance64

Regular maintenance activity

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

4

Last Release

219d ago

PHP version history (2 changes)v1.0.0PHP &gt;=8.4

v1.0.3PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/8870d5debdf4929f1cc7f6c3a158f938f09fa934ab0998a7d985b3c6753867c6?d=identicon)[dengaletin](/maintainers/dengaletin)

---

Top Contributors

[![dengaletin](https://avatars.githubusercontent.com/u/30504327?v=4)](https://github.com/dengaletin "dengaletin (9 commits)")

---

Tags

phpapiclientaiopenaichatclaudegptartificial intelligence

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/aihotel-ai-client/health.svg)

```
[![Health](https://phpackages.com/badges/aihotel-ai-client/health.svg)](https://phpackages.com/packages/aihotel-ai-client)
```

###  Alternatives

[openai-php/laravel

OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API

3.7k7.0M62](/packages/openai-php-laravel)[deepseek-php/deepseek-php-client

deepseek PHP client is a robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities.

46554.9k5](/packages/deepseek-php-deepseek-php-client)[mozex/anthropic-php

Anthropic PHP is a supercharged community-maintained PHP API client that allows you to interact with Anthropic API.

46310.3k10](/packages/mozex-anthropic-php)[gemini-api-php/laravel

Gemini API client for Laravel

8915.0k](/packages/gemini-api-php-laravel)[claude-php/claude-php-sdk-laravel

Laravel integration for the Claude PHP SDK - Anthropic Claude API

505.5k](/packages/claude-php-claude-php-sdk-laravel)[softcreatr/php-mistral-ai-sdk

A powerful and easy-to-use PHP SDK for the Mistral AI API, allowing seamless integration of advanced AI-powered features into your PHP projects.

1516.7k](/packages/softcreatr-php-mistral-ai-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
