PHP code example of chubes4 / ai-http-client

1. Go to this page and download the library: Download chubes4/ai-http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chubes4 / ai-http-client example snippets


// OLD (v1.x)
apply_filters('ai_providers', [])
apply_filters('ai_provider_api_keys', null)
apply_filters('ai_models', $provider)
apply_filters('ai_tools', [])
apply_filters('ai_request', $request)
apply_filters('ai_file_to_base64', '', $path)
apply_filters('ai_http', [], $method, $url, $args)

// NEW (v2.0)
apply_filters('chubes_ai_providers', [])
apply_filters('chubes_ai_provider_api_keys', null)
apply_filters('chubes_ai_models', $provider)
apply_filters('chubes_ai_tools', [])
apply_filters('chubes_ai_request', $request)
apply_filters('chubes_ai_file_to_base64', '', $path)
apply_filters('chubes_ai_http', [], $method, $url, $args)

// Composer: Auto-loads via Composer (no includes needed)

// Git Subtree/Manual: Include in your plugin

$response = apply_filters('chubes_ai_request', [
    'messages' => [['role' => 'user', 'content' => 'Hello AI!']]
], 'openai'); // Provider name is now 

// Specific provider (ters('chubes_ai_request', $request, 'anthropic');

// With streaming callback
$response = apply_filters('chubes_ai_request', $request, 'openai', $streaming_callback);

// With function calling tools
$response = apply_filters('chubes_ai_request', $request, 'openai', null, $tools);

// With conversation continuation
$response = apply_filters('chubes_ai_request', $request, 'openai', null, $tools, $conversation_data);

// Provider Discovery
$providers = apply_filters('chubes_ai_providers', []);

// API Keys Management
$keys = apply_filters('chubes_ai_provider_api_keys', null); // Get all keys
apply_filters('chubes_ai_provider_api_keys', $new_keys);     // Update all keys

// Dynamic Model Fetching (with 24-hour cache)
$models = apply_filters('chubes_ai_models', $provider_name, $config);

// AI Tools Registration
$tools = apply_filters('chubes_ai_tools', []);

// File Operations
$base64 = apply_filters('chubes_ai_file_to_base64', '', $file_path, $options);

// HTTP Requests (internal use)
$result = apply_filters('chubes_ai_http', [], 'POST', $url, $args, 'Context');

// Configure API keys via REST API
wp_remote_post('/wp-json/ai-http-client/v1/api-keys/openai', [
    'body' => wp_json_encode([
        'api_key' => 'your-api-key'
    ]),
    'headers' => [
        'Content-Type' => 'application/json',
        'X-WP-Nonce' => wp_create_nonce('wp_rest')
    ]
]);

// Get provider configuration
$config = wp_remote_get('/wp-json/ai-http-client/v1/api-keys/openai', [
    'headers' => [
        'X-WP-Nonce' => wp_create_nonce('wp_rest')
    ]
]);

// Get available models for a provider
$models = wp_remote_get('/wp-json/ai-http-client/v1/models/openai', [
    'headers' => [
        'X-WP-Nonce' => wp_create_nonce('wp_rest')
    ]
]);

// Get all available providers
$providers = wp_remote_get('/wp-json/ai-http-client/v1/providers', [
    'headers' => [
        'X-WP-Nonce' => wp_create_nonce('wp_rest')
    ]
]);

// WordPress option: 'chubes_ai_http_shared_api_keys'
$shared_keys = apply_filters('chubes_ai_provider_api_keys', null);
// Returns: ['openai' => 'sk-...', 'anthropic' => 'sk-ant-...', ...]

// Each provider accepts configuration in constructor
$provider = new AI_HTTP_OpenAI_Provider([
    'api_key' => 'sk-...',
    'organization' => 'org-...',
    'base_url' => 'https://api.openai.com/v1' // Optional custom endpoint
]);

add_filter('chubes_ai_tools', function($tools) {
    $tools['file_processor'] = [
        'class' => 'FileProcessor_Tool',
        'category' => 'file_handling',
        'description' => 'Process files and extract content',
        'parameters' => [
            'file_path' => [
                'type' => 'string',
                '

// Get all registered tools
$all_tools = apply_filters('chubes_ai_tools', []);

// Pass tools to AI request
$response = apply_filters('chubes_ai_request', $request, 'openai', null, $tools);
// Note: Tool execution is handled by consuming plugins

class AI_HTTP_MyProvider {
    public function __construct($config = []) { /* Provider setup */ }
    public function is_configured() { /* Check if ready */ }
    public function request($standard_request) { /* Standard → Provider → Standard */ }
    public function streaming_request($standard_request, $callback) { /* Streaming support */ }
    public function get_normalized_models() { /* Get models for UI */ }
    public function get_raw_models() { /* Get raw API response */ }
}

// Self-register via filter
add_filter('chubes_ai_providers', function($providers) {
    $providers['myprovider'] = [
        'class' => 'AI_HTTP_MyProvider',
        'type' => 'llm',
        'name' => 'My Provider'
    ];
    return $providers;
});

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);