Skip to content

Commit

Permalink
Merge branch 'magic-ai' of https://github.com/amit-webkul/laravel-crm
Browse files Browse the repository at this point in the history
…into magic-ai
  • Loading branch information
amit-webkul committed Jan 28, 2025
2 parents 598b853 + 2830c49 commit e1e9de8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
22 changes: 11 additions & 11 deletions packages/Webkul/Admin/src/Helpers/Lead.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Webkul\Admin\Helpers;

use Smalot\PdfParser\Parser;
use Exception;
use Smalot\PdfParser\Parser;

class Lead
{
Expand All @@ -13,17 +13,17 @@ class Lead
public static function extractDataFromPdf($pdfPath)
{
try {
$parser = new Parser();
$parser = new Parser;
$pdf = $parser->parseFile($pdfPath);
$text = trim($pdf->getText());

if (empty($text)) {
throw new Exception("PDF content is empty or could not be extracted.");
throw new Exception('PDF content is empty or could not be extracted.');
}

return self::sendLLMRequest($text);
} catch (Exception $e) {
return ["error" => $e->getMessage()];
return ['error' => $e->getMessage()];
}
}

Expand All @@ -38,7 +38,7 @@ private static function sendLLMRequest($prompt)
$apiKey = core()->getConfigData('general.magic_ai.settings.api_key');

if (empty($apiKey) || empty($model)) {
return ["error" => "Missing API key or model configuration."];
return ['error' => 'Missing API key or model configuration.'];
}

$data = [
Expand Down Expand Up @@ -68,34 +68,34 @@ private static function sendLLMRequest($prompt)

try {
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer " . $apiKey
'Content-Type: application/json',
'Authorization: Bearer '.$apiKey,
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch)) {
throw new Exception("cURL Error: " . curl_error($ch));
throw new Exception('cURL Error: '.curl_error($ch));
}

curl_close($ch);

$decodedResponse = json_decode($response, true);

if ($httpCode !== 200 || isset($decodedResponse['error'])) {
throw new Exception("LLM API Error: " . ($decodedResponse['error']['message'] ?? 'Unknown error'));
throw new Exception('LLM API Error: '.($decodedResponse['error']['message'] ?? 'Unknown error'));
}

return $decodedResponse;
} catch (Exception $e) {
return ["error" => $e->getMessage()];
return ['error' => $e->getMessage()];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -666,16 +666,14 @@ private function mapAIDataToLead($aiData)
'status' => 1,
'title' => $aiData['lead_title'] ?? 'Untitled Lead',
'person' => [
'name' => $aiData['contact_name'] ?? 'Unknown',
'email' => $aiData['contact_email'] ?? null,
'phone' => $aiData['contact_phone'] ?? null,
'name' => $aiData['contact_name'] ?? 'Unknown',
'email' => $aiData['contact_email'] ?? null,
'phone' => $aiData['contact_phone'] ?? null,
'organization_id' => $aiData['organization_id'] ?? null,
],
'lead_pipeline_stage_id' => $aiData['pipeline_stage_id'] ?? null,
'value' => $aiData['lead_value'] ?? 0,
'source' => $aiData['source'] ?? 'AI Extracted',
];
}


}

0 comments on commit e1e9de8

Please sign in to comment.