Skip to content

AI Models

Rumen Damyanov edited this page Jul 31, 2025 · 1 revision

AI Models

Comprehensive guide to supported AI providers and model configuration.

Table of Contents

Supported Providers

Provider Models API Key Cost Speed Quality
OpenAI GPT-4, GPT-3.5 Required $$$ Fast Excellent
Anthropic Claude 3, 3.5 Required $$$ Fast Excellent
xAI Grok-1, Grok-2 Required $$ Fast Good
Google Gemini Pro, Flash Required $$ Very Fast Excellent
Meta Llama 3, 3.1 Required $ Medium Good
DeepSeek DeepSeek V2 Required $ Fast Good
Ollama Any local model Optional Free Varies Varies
Default Built-in fallback None Free Instant Basic

OpenAI

OpenAI provides the most popular and well-tested models including GPT-4 and GPT-3.5-turbo.

Configuration

$config = [
    'model' => 'openai',
    'api_key' => env('OPENAI_API_KEY'),
    'model_name' => 'gpt-3.5-turbo', // or gpt-4, gpt-4o, etc.
    'endpoint' => 'https://api.openai.com/v1/chat/completions',
    'temperature' => 0.7,
    'max_tokens' => 1000,
];

Available Models

Model Context Cost Best For
gpt-4o 128k High Complex tasks, reasoning
gpt-4o-mini 128k Medium General purpose, cost-effective
gpt-4-turbo 128k High Advanced reasoning, analysis
gpt-3.5-turbo 16k Low Quick responses, simple tasks

Setup Steps

  1. Get API Key:

  2. Add to Environment:

    OPENAI_API_KEY=sk-your-key-here
  3. Configure Model:

    use Rumenx\PhpChatbot\Models\OpenAiModel;
    
    $model = new OpenAiModel([
        'api_key' => env('OPENAI_API_KEY'),
        'model' => 'gpt-3.5-turbo',
        'temperature' => 0.7,
    ]);

Advanced Options

$config = [
    'model' => 'openai',
    'api_key' => env('OPENAI_API_KEY'),
    'model_name' => 'gpt-4o',
    'temperature' => 0.7,      // Creativity (0.0-1.0)
    'max_tokens' => 2000,      // Response length
    'top_p' => 1.0,           // Nucleus sampling
    'frequency_penalty' => 0,  // Reduce repetition
    'presence_penalty' => 0,   // Encourage new topics
];

Anthropic

Anthropic's Claude models are known for helpful, harmless, and honest responses.

Configuration

$config = [
    'model' => 'anthropic',
    'api_key' => env('ANTHROPIC_API_KEY'),
    'model_name' => 'claude-3-sonnet-20240229',
    'endpoint' => 'https://api.anthropic.com/v1/messages',
    'temperature' => 0.7,
    'max_tokens' => 1000,
];

Available Models

Model Context Cost Best For
claude-3-opus-20240229 200k High Complex analysis, creative tasks
claude-3-sonnet-20240229 200k Medium Balanced performance
claude-3-haiku-20240307 200k Low Fast responses, simple tasks

Setup Steps

  1. Get API Key:

  2. Configure:

    ANTHROPIC_API_KEY=your-key-here
  3. Use Model:

    use Rumenx\PhpChatbot\Models\AnthropicModel;
    
    $model = new AnthropicModel([
        'api_key' => env('ANTHROPIC_API_KEY'),
        'model' => 'claude-3-sonnet-20240229',
    ]);

xAI (Grok)

xAI's Grok models provide real-time information and conversational AI.

Configuration

$config = [
    'model' => 'xai',
    'api_key' => env('XAI_API_KEY'),
    'model_name' => 'grok-beta',
    'endpoint' => 'https://api.x.ai/v1/chat/completions',
    'temperature' => 0.7,
];

Available Models

Model Features Best For
grok-beta Real-time data, web access Current events, research
grok-vision-beta Image understanding Visual analysis

Setup

XAI_API_KEY=your-xai-key-here

Google Gemini

Google's Gemini models offer fast, multimodal AI capabilities.

Configuration

$config = [
    'model' => 'gemini',
    'api_key' => env('GEMINI_API_KEY'),
    'model_name' => 'gemini-1.5-pro',
    'endpoint' => 'https://generativelanguage.googleapis.com/v1beta/models/',
    'temperature' => 0.7,
];

Available Models

Model Context Speed Best For
gemini-1.5-pro 2M Fast Complex reasoning
gemini-1.5-flash 1M Very Fast Quick responses
gemini-1.0-pro 32k Fast General purpose

Setup

  1. Get API Key:

  2. Configure:

    GEMINI_API_KEY=your-gemini-key-here

Meta (Llama)

Meta's Llama models via hosted providers.

Configuration

$config = [
    'model' => 'meta',
    'api_key' => env('META_API_KEY'),
    'model_name' => 'llama-3.1-70b-instruct',
    'endpoint' => 'https://api.together.xyz/v1/chat/completions',
];

Available Models

Model Size Performance
llama-3.1-405b-instruct 405B Best
llama-3.1-70b-instruct 70B Excellent
llama-3.1-8b-instruct 8B Good

Ollama (Local)

Run AI models locally using Ollama.

Prerequisites

  1. Install Ollama:

    # macOS
    brew install ollama
    
    # Linux
    curl -fsSL https://ollama.ai/install.sh | sh
    
    # Windows
    # Download from https://ollama.ai/download
  2. Pull a Model:

    ollama pull llama3.2
    ollama pull mistral
    ollama pull phi3

Configuration

$config = [
    'model' => 'ollama',
    'base_url' => 'http://localhost:11434',
    'model_name' => 'llama3.2',
    'timeout' => 30,
];

Popular Models

Model Size Best For
llama3.2 3B/1B General purpose
mistral 7B Code generation
phi3 3.8B Reasoning
codellama 7B/13B/34B Programming

Custom Endpoint

For remote Ollama instances:

$config = [
    'model' => 'ollama',
    'base_url' => 'https://your-ollama-server.com',
    'api_key' => 'optional-auth-key',
    'model_name' => 'llama3.2',
];

DeepSeek

DeepSeek provides cost-effective AI models.

Configuration

$config = [
    'model' => 'deepseek',
    'api_key' => env('DEEPSEEK_API_KEY'),
    'model_name' => 'deepseek-chat',
    'endpoint' => 'https://api.deepseek.com/v1/chat/completions',
];

Setup

DEEPSEEK_API_KEY=your-deepseek-key-here

Custom Models

Implement your own AI model by creating a class that implements AiModelInterface.

Create Custom Model

<?php
namespace App\Models;

use Rumenx\PhpChatbot\Contracts\AiModelInterface;

class CustomAiModel implements AiModelInterface
{
    private $apiKey;
    private $endpoint;
    
    public function __construct(array $config = [])
    {
        $this->apiKey = $config['api_key'] ?? '';
        $this->endpoint = $config['endpoint'] ?? '';
    }
    
    public function sendMessage(string $message, array $context = []): string
    {
        // Your custom implementation
        $response = $this->callCustomAPI($message, $context);
        return $response;
    }
    
    public function getModel(): string
    {
        return $this->model ?? 'custom-model';
    }
    
    public function setModel(string $model): void
    {
        $this->model = $model;
    }
    
    private function callCustomAPI(string $message, array $context): string
    {
        // Implement your API call logic
        // Return the AI response
    }
}

Register Custom Model

use App\Models\CustomAiModel;
use Rumenx\PhpChatbot\PhpChatbot;

$config = [
    'model' => CustomAiModel::class,
    'api_key' => 'your-api-key',
    'endpoint' => 'https://your-api.com/chat',
];

$model = new CustomAiModel($config);
$chatbot = new PhpChatbot($model, $config);

Model Factory Registration

// In your config file
return [
    'model' => 'custom',
    'models' => [
        'custom' => [
            'class' => App\Models\CustomAiModel::class,
            'api_key' => env('CUSTOM_API_KEY'),
            'endpoint' => 'https://api.custom.com/v1/chat',
        ],
    ],
];

Model Selection Tips

For Development

  • Default Model: Free, instant responses for testing
  • Ollama: Local development, no API costs

For Production

  • OpenAI GPT-3.5: Best balance of cost/performance
  • Gemini Flash: Fastest responses, good quality
  • Claude Haiku: Safety-focused, reasonable cost

For Specific Use Cases

  • Code Generation: OpenAI GPT-4, Codellama
  • Creative Writing: Claude Opus, GPT-4
  • Real-time Info: Grok
  • Cost-sensitive: DeepSeek, Gemini Flash

Performance Comparison

Provider Avg Response Time Reliability Context Window
OpenAI 1-3s 99.9% 128k
Anthropic 2-4s 99.8% 200k
Google 0.5-2s 99.5% 2M
xAI 2-5s 99.0% 131k
Ollama 5-30s Local Varies

Next: Framework Integration

Clone this wiki locally