Core Features: - Multi-provider support (OpenAI, Anthropic, DeepSeek, Gemini, Mistral) - Provider service architecture with abstract base class - Dynamic model discovery from provider APIs - Encrypted per-user provider credentials storage Admin Interface: - Complete admin panel with Livewire components - User management with CRUD operations - API key management with testing capabilities - Budget system with limits and reset schedules - Usage logs with filtering and CSV export - Model pricing management with cost calculator - Dashboard with Chart.js visualizations Database Schema: - MariaDB migrations for all tables - User provider credentials (encrypted) - LLM request logging - Budget tracking and rate limiting - Model pricing configuration API Implementation: - OpenAI-compatible endpoints - Budget checking middleware - Rate limit enforcement - Request logging jobs - Cost calculation service Testing: - Unit tests for all provider services - Provider factory tests - Cost calculator tests Documentation: - Admin user seeder - Model pricing seeder - Configuration files
88 lines
2.9 KiB
PHP
88 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\LLM\Providers;
|
|
|
|
use App\Models\ModelPricing;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class DeepSeekProvider extends AbstractProvider
|
|
{
|
|
protected string $baseUrl = 'https://api.deepseek.com/v1';
|
|
|
|
protected function buildRequest(array $messages, array $options): array
|
|
{
|
|
return array_filter([
|
|
'model' => $options['model'] ?? 'deepseek-chat',
|
|
'messages' => $messages,
|
|
'temperature' => $options['temperature'] ?? 0.7,
|
|
'max_tokens' => $options['max_tokens'] ?? null,
|
|
'top_p' => $options['top_p'] ?? null,
|
|
'frequency_penalty' => $options['frequency_penalty'] ?? null,
|
|
'presence_penalty' => $options['presence_penalty'] ?? null,
|
|
'stop' => $options['stop'] ?? null,
|
|
'stream' => false,
|
|
], fn($value) => $value !== null);
|
|
}
|
|
|
|
protected function getAuthHeaders(): array
|
|
{
|
|
return [
|
|
'Authorization' => 'Bearer ' . $this->apiKey,
|
|
'Content-Type' => 'application/json',
|
|
];
|
|
}
|
|
|
|
public function chatCompletion(array $messages, array $options = []): array
|
|
{
|
|
$data = $this->buildRequest($messages, $options);
|
|
return $this->makeRequest('/chat/completions', $data);
|
|
}
|
|
|
|
public function normalizeResponse(array $response): array
|
|
{
|
|
return [
|
|
'id' => $response['id'] ?? null,
|
|
'model' => $response['model'] ?? null,
|
|
'content' => $response['choices'][0]['message']['content'] ?? '',
|
|
'role' => $response['choices'][0]['message']['role'] ?? 'assistant',
|
|
'finish_reason' => $response['choices'][0]['finish_reason'] ?? null,
|
|
'usage' => [
|
|
'prompt_tokens' => $response['usage']['prompt_tokens'] ?? 0,
|
|
'completion_tokens' => $response['usage']['completion_tokens'] ?? 0,
|
|
'total_tokens' => $response['usage']['total_tokens'] ?? 0,
|
|
],
|
|
'raw_response' => $response,
|
|
];
|
|
}
|
|
|
|
public function calculateCost(int $promptTokens, int $completionTokens, string $model): float
|
|
{
|
|
$cacheKey = "pricing:deepseek:{$model}";
|
|
|
|
$pricing = Cache::remember($cacheKey, 3600, function () use ($model) {
|
|
return ModelPricing::where('provider', 'deepseek')
|
|
->where('model', $model)
|
|
->where('is_active', true)
|
|
->first();
|
|
});
|
|
|
|
if (!$pricing) {
|
|
return 0.0;
|
|
}
|
|
|
|
$promptCost = ($promptTokens / 1_000_000) * $pricing->input_price_per_million;
|
|
$completionCost = ($completionTokens / 1_000_000) * $pricing->output_price_per_million;
|
|
|
|
return round($promptCost + $completionCost, 6);
|
|
}
|
|
|
|
public function getSupportedModels(): array
|
|
{
|
|
return [
|
|
'deepseek-chat',
|
|
'deepseek-coder',
|
|
'deepseek-reasoner',
|
|
];
|
|
}
|
|
}
|