|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AlvinCoded\MtnMomoAi\AI\Models; |
| 4 | + |
| 5 | +use AlvinCoded\MtnMomoAi\AI\Interfaces\LLMInterface; |
| 6 | +use DeepseekPhp\Enums\Queries\QueryRoles; |
| 7 | +use DeepseekPhp\Enums\Models; |
| 8 | +use DeepseekPhp\DeepseekClient; |
| 9 | + |
| 10 | +/** |
| 11 | + * DeepSeek AI Model Implementation for MTN MOMO API |
| 12 | + * |
| 13 | + * This class implements the LLMInterface to provide AI-powered analysis and insights |
| 14 | + * for MTN Mobile Money transactions using OpenAI's GPT models. |
| 15 | + */ |
| 16 | +class DeepSeek implements LLMInterface |
| 17 | +{ |
| 18 | + /** @var \DeepseekPhp\DeepseekClient DeepSeek API client instance */ |
| 19 | + protected $client; |
| 20 | + |
| 21 | + /** @var array Configuration settings for the Deepseek model */ |
| 22 | + protected $config; |
| 23 | + |
| 24 | + /** |
| 25 | + * Initialize DeepSeek client with configuration |
| 26 | + * |
| 27 | + * @param array $config Configuration array containing api_key |
| 28 | + */ |
| 29 | + public function __construct(array $config) |
| 30 | + { |
| 31 | + $this->client = DeepseekClient::build($config['api_key']); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Analyze transaction data across all MOMO services |
| 36 | + * |
| 37 | + * @param array $data Transaction data including status, amounts, and patterns |
| 38 | + * @return string Analysis results and insights |
| 39 | + */ |
| 40 | + public function analyze($data) |
| 41 | + { |
| 42 | + $prompt = "Analyze this MTN MOMO transaction data across collections, disbursements, and remittances. Consider transaction status, amounts, and patterns:\n\n" . json_encode($data); |
| 43 | + return $this->generateResponse($prompt); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Detect potential fraud in transaction data |
| 48 | + * |
| 49 | + * @param array $data Transaction data to evaluate for fraud indicators |
| 50 | + * @return string Fraud analysis results and risk assessment |
| 51 | + */ |
| 52 | + public function detectFraud($data) |
| 53 | + { |
| 54 | + $prompt = "Evaluate this MTN MOMO transaction for potential fraud indicators. Consider transaction type (collection/disbursement/remittance), amount patterns, and account holder behavior:\n\n" . json_encode($data); |
| 55 | + return $this->generateResponse($prompt); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Suggest retry strategy for failed transactions |
| 60 | + * |
| 61 | + * @param array $data Failed transaction data including error codes and history |
| 62 | + * @return string Recommended retry strategy |
| 63 | + */ |
| 64 | + public function suggestRetryStrategy($data) |
| 65 | + { |
| 66 | + $prompt = "Based on this failed MTN MOMO transaction data, suggest an optimal retry strategy considering the transaction type, error codes, and historical success patterns:\n\n" . json_encode($data); |
| 67 | + return $this->generateResponse($prompt); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Forecast cash flow based on historical data |
| 72 | + * |
| 73 | + * @param array $data Historical transaction data |
| 74 | + * @return string Cash flow forecast and trends analysis |
| 75 | + */ |
| 76 | + public function forecastCashFlow($data) |
| 77 | + { |
| 78 | + $prompt = "Analyze this MTN MOMO historical transaction data across collections, disbursements, and remittances to forecast future cash flow patterns and trends:\n\n" . json_encode($data); |
| 79 | + return $this->generateResponse($prompt); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Parse natural language commands into API requests |
| 84 | + * |
| 85 | + * @param string $command Natural language command to parse |
| 86 | + * @return array|string Structured API request parameters |
| 87 | + */ |
| 88 | + public function parseCommand($command) |
| 89 | + { |
| 90 | + $prompt = "Parse this natural language command into a structured MTN MOMO API request format. Consider valid transaction types (collection/disbursement/remittance) and required parameters:\n\n" . $command; |
| 91 | + return $this->generateResponse($prompt); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Generate comprehensive transaction report |
| 96 | + * |
| 97 | + * @param array $data Transaction data to analyze |
| 98 | + * @return string Detailed transaction report |
| 99 | + */ |
| 100 | + public function generateReport($data) |
| 101 | + { |
| 102 | + $prompt = "Generate a comprehensive MTN MOMO transaction report analyzing patterns across collections, disbursements, and remittances. Include transaction volumes, success rates, and notable trends:\n\n" . json_encode($data); |
| 103 | + return $this->generateResponse($prompt); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Suggest optimal disbursement timing |
| 108 | + * |
| 109 | + * @param float $amount Amount to disburse |
| 110 | + * @param string $recipient Recipient identifier |
| 111 | + * @return string Suggested optimal disbursement time |
| 112 | + */ |
| 113 | + public function suggestDisbursementTime($amount, $recipient) |
| 114 | + { |
| 115 | + $prompt = "Suggest an optimal disbursement time for this MTN MOMO transfer considering amount: $amount, recipient: $recipient. Consider historical transaction patterns and success rates."; |
| 116 | + return $this->generateResponse($prompt); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Detect transaction anomalies |
| 121 | + * |
| 122 | + * @param array $data Transaction data to analyze for anomalies |
| 123 | + * @return array|string Detected anomalies and their details |
| 124 | + */ |
| 125 | + public function detectAnomalies($data) |
| 126 | + { |
| 127 | + $prompt = "Analyze this MTN MOMO transaction data to detect anomalies across collections, disbursements, and remittances. Consider unusual patterns, amounts, frequencies, and account behaviors:\n\n" . json_encode($data); |
| 128 | + return $this->generateResponse($prompt); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Suggest optimal API call timing |
| 133 | + * |
| 134 | + * @param array $data API usage data and patterns |
| 135 | + * @return string Suggested optimal call times |
| 136 | + */ |
| 137 | + public function suggestOptimalCallTimes($data) |
| 138 | + { |
| 139 | + $prompt = "Based on this MTN MOMO API usage data, suggest optimal times for API calls considering success rates, response times, and error patterns:\n\n" . json_encode($data); |
| 140 | + return $this->generateResponse($prompt); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Explain API errors with context |
| 145 | + * |
| 146 | + * @param string $errorCode The error code to explain |
| 147 | + * @param array $context Additional context about the error |
| 148 | + * @return string Detailed error explanation and resolution steps |
| 149 | + */ |
| 150 | + public function explainError($errorCode, $context) |
| 151 | + { |
| 152 | + $prompt = "Explain this MTN MOMO API error in context of the transaction. Provide potential causes and resolution steps:\nError Code: $errorCode\nContext: " . json_encode($context); |
| 153 | + return $this->generateResponse($prompt); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Generate AI response using DeepSeek's chat completion |
| 158 | + * |
| 159 | + * @param string $prompt The prompt to send to the AI model |
| 160 | + * @return string Generated response from the AI model |
| 161 | + * @throws \Exception If the API request fails |
| 162 | + */ |
| 163 | + protected function generateResponse($prompt) |
| 164 | + { |
| 165 | + try { |
| 166 | + $response = $this->client |
| 167 | + ->query('You are an AI assistant specialized in MTN Mobile Money API operations, including collections, disbursements, and remittances. Provide detailed analysis and actionable insights.', QueryRoles::SYSTEM->value) |
| 168 | + ->query($prompt, QueryRoles::USER->value) |
| 169 | + ->withModel($this->config['model']) |
| 170 | + ->setTemperature(0.7) |
| 171 | + ->run(); |
| 172 | + |
| 173 | + return $response; |
| 174 | + } catch (\Exception $e) { |
| 175 | + throw new \Exception("DeepSeek API request failed: " . $e->getMessage()); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments