goed/cerebras-php

A PHP adapter for the Cerebras AI API

Maintainers

Package info

github.com/Good-by-WLBT/cerebras-php

pkg:composer/goed/cerebras-php

Statistics

Installs: 142

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v0.1.0 2025-10-15 15:50 UTC

This package is auto-updated.

Last update: 2026-03-28 18:08:33 UTC


README

Made with ❤️ by Goed.email

Cerebras PHP Client

A PHP client library for interacting with the Cerebras AI API, providing easy access to chat completions and other endpoints.

Installation

Install the package via Composer:

composer require goed/cerebras-php

Usage

Setup

Set your API key as an environment variable:

export CEREBRAS_API_KEY=your_api_key_here

Basic Chat Completion

Create a non-streaming chat completion:

<?php
require 'vendor/autoload.php';

use Goed\Cerebras\Client;

$client = new Client();
$response = $client->chat()->create([
    'model' => 'llama3.1-8b',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello, how are you?']
    ]
]);

echo $response['choices'][0]['message']['content'];

Streaming Chat Completion

For real-time responses, use streaming:

<?php
require 'vendor/autoload.php';

use Goed\Cerebras\Client;

$client = new Client();
$stream = $client->chat()->create([
    'model' => 'llama3.1-8b',
    'messages' => [
        ['role' => 'user', 'content' => 'Tell me a story.']
    ],
    'stream' => true
]);

foreach ($stream as $chunk) {
    echo $chunk['choices'][0]['delta']['content'] ?? '';
}

For more examples, see the examples/ directory, including basic.php, chat_page.php, and chat_page_non_streaming.php.

Browser Chat Demo

Serve the example files with PHP's built-in server for quick browser chat UIs:

php -S localhost:8080 -t examples

Then open:

  • http://localhost:8080/chat_page.php for streaming responses
  • http://localhost:8080/chat_page_non_streaming.php for non-streaming responses

Features

  • API Compatibility: Endpoints and payloads match the official Cerebras AI API. Adjust model IDs and fields as needed.
  • Retry Mechanism: Automatic retry with backoff for 429 (rate limit) and 5xx errors.
  • Streaming Support: Server-Sent Events (SSE) for real-time chat completions.
  • Helpers: Basic utility functions for common tasks.

Notes

Ensure your API key is valid and has the necessary permissions. Refer to the Cerebras AI documentation for detailed API specifications.