Skip to content

Commit 908fe04

Browse files
committed
refactor: remove MagicAI package and integrate lead extraction functionality into LeadService
1 parent 1e05c67 commit 908fe04

File tree

12 files changed

+279
-526
lines changed

12 files changed

+279
-526
lines changed

config/app.php

-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@
213213
Webkul\Marketing\Providers\MarketingServiceProvider::class,
214214
Webkul\Installer\Providers\InstallerServiceProvider::class,
215215
Webkul\Lead\Providers\LeadServiceProvider::class,
216-
Webkul\MagicAI\Providers\MagicAIServiceProvider::class,
217216
Webkul\Product\Providers\ProductServiceProvider::class,
218217
Webkul\Quote\Providers\QuoteServiceProvider::class,
219218
Webkul\Tag\Providers\TagServiceProvider::class,

packages/Webkul/Admin/src/Helpers/Lead.php

-195
This file was deleted.

packages/Webkul/Admin/src/Http/Controllers/Lead/LeadController.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\View\View;
1212
use Prettus\Repository\Criteria\RequestCriteria;
1313
use Webkul\Admin\DataGrids\Lead\LeadDataGrid;
14-
use Webkul\Admin\Helpers\Lead;
1514
use Webkul\Admin\Http\Controllers\Controller;
1615
use Webkul\Admin\Http\Requests\LeadForm;
1716
use Webkul\Admin\Http\Requests\MassDestroyRequest;
@@ -27,6 +26,7 @@
2726
use Webkul\Lead\Repositories\SourceRepository;
2827
use Webkul\Lead\Repositories\StageRepository;
2928
use Webkul\Lead\Repositories\TypeRepository;
29+
use Webkul\Lead\Services\LeadService;
3030
use Webkul\Tag\Repositories\TagRepository;
3131
use Webkul\User\Repositories\UserRepository;
3232

@@ -641,7 +641,7 @@ public function createByAI(LeadForm $request)
641641
], 400);
642642
}
643643

644-
$extractedData = Lead::extractDataFromPdf($pdfFile->getPathName());
644+
$extractedData = LeadService::extractDataFromPdf($pdfFile->getPathName());
645645

646646
if (! empty($extractedData['error'])) {
647647
return response()->json([
@@ -652,7 +652,10 @@ public function createByAI(LeadForm $request)
652652

653653
$leadData = $this->mapAIDataToLead($extractedData);
654654

655-
if (! empty($leadData['status']) && $leadData['status'] === 'error') {
655+
if (
656+
! empty($leadData['status'])
657+
&& $leadData['status'] === 'error'
658+
) {
656659
return response()->json([
657660
'status' => 'error',
658661
'message' => $leadData['message'],
@@ -667,7 +670,13 @@ public function createByAI(LeadForm $request)
667670
*/
668671
private function mapAIDataToLead($aiData)
669672
{
670-
$content = $aiData['choices'][0]['message']['content'] ?? '';
673+
$model = core()->getConfigData('general.magic_ai.settings.model');
674+
675+
if (str_contains($model, 'gemini-1.5-flash')) {
676+
$content = $aiData['candidates'][0]['content']['parts'][0]['text'] ?? '';
677+
} else {
678+
$content = $aiData['choices'][0]['message']['content'] ?? '';
679+
}
671680

672681
$content = strip_tags($content);
673682

@@ -780,10 +789,6 @@ private function leadCreate($data)
780789
],
781790
]);
782791

783-
if (in_array($stage->code, ['won', 'lost'])) {
784-
$data['closed_at'] = Carbon::now();
785-
}
786-
787792
$lead = $this->leadRepository->create($data);
788793

789794
Event::dispatch('lead.create.after', $lead);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Webkul\Lead\Services;
4+
5+
use Exception;
6+
7+
class GeminiService
8+
{
9+
/**
10+
* Send Request to Gemini AI.
11+
*/
12+
public static function ask($prompt, $model, $apiKey)
13+
{
14+
$url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent?key={$apiKey}";
15+
16+
return self::curlRequest($url, self::prepareRequestData($prompt));
17+
}
18+
19+
/**
20+
* Request data to AI using Curl API.
21+
*/
22+
private static function curlRequest($url, array $data)
23+
{
24+
try {
25+
$ch = curl_init($url);
26+
27+
curl_setopt_array($ch, [
28+
CURLOPT_RETURNTRANSFER => true,
29+
CURLOPT_POST => true,
30+
CURLOPT_POSTFIELDS => json_encode($data),
31+
CURLOPT_HTTPHEADER => [
32+
'Content-Type: application/json',
33+
],
34+
]);
35+
36+
$response = curl_exec($ch);
37+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
38+
39+
if (curl_errno($ch)) {
40+
throw new Exception('cURL Error: '.curl_error($ch));
41+
}
42+
43+
curl_close($ch);
44+
45+
$decodedResponse = json_decode($response, true);
46+
47+
if ($httpCode !== 200 || isset($decodedResponse['error'])) {
48+
throw new Exception('LLM API Error: '.($decodedResponse['error']['message'] ?? 'Unknown error'));
49+
}
50+
51+
return $decodedResponse;
52+
} catch (Exception $e) {
53+
return ['error' => $e->getMessage()];
54+
}
55+
}
56+
57+
/**
58+
* Prepare request data for AI.
59+
*/
60+
private static function prepareRequestData($prompt)
61+
{
62+
return [
63+
'contents' => [
64+
[
65+
'parts' => [
66+
[
67+
'text' => "You are an AI assistant. Extract data from the provided PDF text.
68+
69+
Example Output:
70+
{
71+
\"status\": 1,
72+
\"title\": \"Untitled Lead\",
73+
\"person\": {
74+
\"name\": \"Unknown\",
75+
\"emails\": {
76+
\"value\": null,
77+
\"label\": null
78+
},
79+
\"contact_numbers\": {
80+
\"value\": null,
81+
\"label\": null
82+
}
83+
},
84+
\"lead_pipeline_stage_id\": null,
85+
\"lead_value\": 0,
86+
\"source\": \"AI Extracted\"
87+
}
88+
89+
Note: Only return the output. Do not return or add any comments.
90+
91+
PDF Content:
92+
$prompt",
93+
],
94+
],
95+
'role' => 'user',
96+
],
97+
],
98+
'generationConfig' => [
99+
'temperature' => 0.2,
100+
'topK' => 30,
101+
'topP' => 0.8,
102+
'maxOutputTokens' => 512,
103+
],
104+
];
105+
}
106+
}

0 commit comments

Comments
 (0)