@@ -14,14 +14,14 @@ public static function extractDataFromPdf($pdfPath)
14
14
{
15
15
try {
16
16
$ parser = new Parser ;
17
- $ pdf = $ parser ->parseFile ($ pdfPath );
18
- $ text = trim ($ pdf ->getText ());
19
17
20
- if (empty ($ text )) {
18
+ $ pdfText = trim ($ parser ->parseFile ($ pdfPath )->getText ());
19
+
20
+ if (empty ($ pdfText )) {
21
21
throw new Exception ('PDF content is empty or could not be extracted. ' );
22
22
}
23
23
24
- return self ::sendLLMRequest ($ text );
24
+ return self ::sendLLMRequest ($ pdfText );
25
25
} catch (Exception $ e ) {
26
26
return ['error ' => $ e ->getMessage ()];
27
27
}
@@ -30,12 +30,13 @@ public static function extractDataFromPdf($pdfPath)
30
30
/**
31
31
* Send a request to the LLM API.
32
32
*/
33
- private static function sendLLMRequest ($ prompt )
33
+ private static function sendLLMRequest ($ prompt )
34
34
{
35
35
$ model = core ()->getConfigData ('general.magic_ai.settings.model ' );
36
36
$ apiKey = core ()->getConfigData ('general.magic_ai.settings.api_key ' );
37
+ $ apiDomain = core ()->getConfigData ('general.magic_ai.settings.api_domain ' );
37
38
38
- if (empty ( $ apiKey) || empty ( $ model) ) {
39
+ if (! $ apiKey || ! $ model ) {
39
40
return ['error ' => 'Missing API key or model configuration. ' ];
40
41
}
41
42
@@ -44,84 +45,68 @@ private static function sendLLMRequest($prompt)
44
45
}
45
46
46
47
$ apiUrlMap = [
47
- 'gpt-4o ' => 'https://api.openai.com/v1/chat/completions ' ,
48
- 'gpt-4o-mini ' => 'https://api.openai.com/v1/chat/completions ' ,
49
- 'llama3.2:latest ' => core ()->getConfigData ('general.magic_ai.settings.api_domain ' ) . '/v1/chat/completions ' ,
50
- 'deepseek-r1:8b ' => core ()->getConfigData ('general.magic_ai.settings.api_domain ' ) . '/v1/chat/completions ' ,
51
- 'gemini-2.0-flash ' => 'https://api.gemini-ai.com/v1/chat/completions ' ,
48
+ 'gpt-4o ' => 'https://api.openai.com/v1/chat/completions ' ,
49
+ 'gpt-4o-mini ' => 'https://api.openai.com/v1/chat/completions ' ,
50
+ 'llama3.2:latest ' => "$ apiDomain/v1/chat/completions " ,
51
+ 'deepseek-r1:8b ' => "$ apiDomain/v1/chat/completions " ,
52
52
];
53
53
54
54
$ apiUrl = $ apiUrlMap [$ model ] ?? 'https://api.groq.com/openai/v1/chat/completions ' ;
55
55
56
- $ data = self ::prepareRequestData ($ model , $ prompt );
57
-
58
- return self ::makeCurlRequest ($ apiUrl , $ model , json_encode ($ data ), true );
56
+ return self ::makeCurlRequest ($ apiUrl , $ model , self ::prepareRequestData ($ model , $ prompt ));
59
57
}
60
58
59
+ /**
60
+ * Send Request to Gemini AI.
61
+ */
61
62
private static function sendGeminiRequest ($ prompt , $ model )
62
63
{
63
- $ url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key= ' . core ()->getConfigData ('general.magic_ai.settings.api_key ' );
64
-
64
+ $ apiKey = core ()->getConfigData ('general.magic_ai.settings.api_key ' );
65
+
66
+ if (empty ($ apiKey )) {
67
+ return ['error ' => 'Missing Google API key. ' ];
68
+ }
69
+
70
+ $ url = "https://generativelanguage.googleapis.com/v1beta/models/ {$ model }:generateContent?key= {$ apiKey }" ;
71
+
65
72
$ data = self ::prepareRequestData ($ model , $ prompt );
66
-
67
- return self ::makeCurlRequest ($ url , $ model , json_encode ($ data ), true );
68
- }
69
73
70
- private static function makeCurlRequest ($ url , $ model , $ prompt , $ isJson = false )
71
- {
72
- $ apiKey = core ()->getConfigData ('general.magic_ai.settings.api_key ' );
74
+ $ data ['stream ' ] = false ;
73
75
74
- $ data = $ isJson ? json_decode ($ prompt , true ) : [
75
- 'model ' => $ model ,
76
- 'messages ' => [
77
- [
78
- 'role ' => 'system ' ,
79
- 'content ' => 'You are an AI assistant. You have to extract the data from the PDF file.
80
- Example Output:
81
- {
82
- "status": 1,
83
- "title": "Untitled Lead",
84
- "person": {
85
- "name": "Unknown",
86
- "email": null,
87
- "phone": null,
88
- "organization_id": null
89
- },
90
- "lead_pipeline_stage_id": null,
91
- "value": 0,
92
- "source": "AI Extracted"
93
- }
94
- Note: Only return the output, Do not return or add any comments. ' ,
95
- ],
96
- ['role ' => 'user ' , 'content ' => 'PDF:\n ' . $ prompt ],
97
- ],
98
- ];
76
+ return self ::makeCurlRequest ($ url , $ model , $ data );
77
+ }
99
78
79
+ /**
80
+ * Request data to AI using Curl API.
81
+ */
82
+ private static function makeCurlRequest ($ url , $ model , array $ data )
83
+ {
100
84
try {
101
- $ ch = curl_init ();
102
-
103
- curl_setopt ($ ch , CURLOPT_URL , $ url );
104
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
105
- curl_setopt ($ ch , CURLOPT_POST , true );
106
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ data ));
107
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
108
- 'Content-Type: application/json ' ,
109
- 'Authorization: Bearer ' . $ apiKey ,
85
+ $ ch = curl_init ($ url );
86
+
87
+ curl_setopt_array ($ ch , [
88
+ CURLOPT_RETURNTRANSFER => true ,
89
+ CURLOPT_POST => true ,
90
+ CURLOPT_POSTFIELDS => json_encode ($ data ),
91
+ CURLOPT_HTTPHEADER => [
92
+ 'Content-Type: application/json ' ,
93
+ 'Authorization: Bearer ' .core ()->getConfigData ('general.magic_ai.settings.api_key ' ),
94
+ ],
110
95
]);
111
96
112
97
$ response = curl_exec ($ ch );
113
98
$ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
114
99
115
100
if (curl_errno ($ ch )) {
116
- throw new Exception ('cURL Error: ' . curl_error ($ ch ));
101
+ throw new Exception ('cURL Error: ' . curl_error ($ ch ));
117
102
}
118
103
119
104
curl_close ($ ch );
120
105
121
106
$ decodedResponse = json_decode ($ response , true );
122
107
123
108
if ($ httpCode !== 200 || isset ($ decodedResponse ['error ' ])) {
124
- throw new Exception ('LLM API Error: ' . ($ decodedResponse ['error ' ]['message ' ] ?? 'Unknown error ' ));
109
+ throw new Exception ('LLM API Error: ' . ($ decodedResponse ['error ' ]['message ' ] ?? 'Unknown error ' ));
125
110
}
126
111
127
112
return $ decodedResponse ;
@@ -130,6 +115,9 @@ private static function makeCurlRequest($url, $model, $prompt, $isJson = false)
130
115
}
131
116
}
132
117
118
+ /**
119
+ * Prepare request data for AI.
120
+ */
133
121
private static function prepareRequestData ($ model , $ prompt )
134
122
{
135
123
return [
@@ -144,17 +132,22 @@ private static function prepareRequestData($model, $prompt)
144
132
"title": "Untitled Lead",
145
133
"person": {
146
134
"name": "Unknown",
147
- "email": null,
148
- "phone": null,
149
- "organization_id": null
135
+ "emails": {
136
+ "value": null,
137
+ "label": null
138
+ },
139
+ "contact_numbers": {
140
+ "value": null,
141
+ "label": null
142
+ }
150
143
},
151
144
"lead_pipeline_stage_id": null,
152
145
"value": 0,
153
146
"source": "AI Extracted"
154
147
}
155
148
Note: Only return the output, Do not return or add any comments. ' ,
156
149
],
157
- ['role ' => 'user ' , 'content ' => ' PDF:\n ' . $ prompt ],
150
+ ['role ' => 'user ' , 'content ' => " PDF: \n$ prompt" ],
158
151
],
159
152
];
160
153
}
0 commit comments