Przeglądaj źródła

Change text for chatgpt

Alex 8 miesięcy temu
rodzic
commit
9764748552

+ 18 - 1
src/app/Http/Controllers/IndexController.php

@@ -3,11 +3,13 @@
 namespace App\Http\Controllers;
 
 use App\Http\Requests\ClientRequest;
+use App\Models\Client;
 use App\Services\ChatGPT\ChatGPT;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 use App\Jobs\SummariesJob;
 use App\Models\Summaries;
+use Illuminate\Support\Facades\Storage;
 
 class IndexController extends Controller
 {
@@ -19,9 +21,24 @@ class IndexController extends Controller
 
     public function clientInfo(ClientRequest $request) {
         $data = $request->validated();
-        $chatResponse = ChatGPT::getClientInfo($data);
+        $client = Client::where('email', $data['email'])->first();
 
+        try{
+            $chatResponse = ChatGPT::getClientInfo($data['prompt'], $client);
+            $images = [];
+            foreach ($chatResponse['images'] as $value) {
+                try{
+                    $images[] = Storage::disk('public')->url($client->photos->pluck('path')[$value - 1]);
+                } catch (\Exception $exception){
+                    continue;
+                }
+            }
+            $chatResponse['images'] = $images;
 
+            return response()->json($chatResponse);
+        } catch (\Throwable $th) {
+            return response()->json($th->getMessage(), 500);
+        }
 
     }
 

+ 9 - 25
src/app/Services/ChatGPT/ChatGPT.php

@@ -20,29 +20,22 @@ class ChatGPT {
         return $response->json()['choices'][0]['message']['content'] ?? false;
     }
 
-    public static function getClientInfo($data) {
-        if ($client = Client::where('email', $data['email'])->first()) {
+    public static function getClientInfo($prompt, Client $client) {
+        if ($client) {
+//            return ['images' => [1,2], 'explanation' => 'test'];
             try {
                 $request = [
                     [
                         'role' => 'system',
-                        'content' => $data['prompt'] . ' . Please respond with the image numbers separated by comma and explanation in two rows separated by the new line. If there won\'t be any images matching the request please put null for the "images" key'
+                        'content' => $prompt . ' . Please respond with the image numbers separated by comma and explanation, JSON only, without any extra formatting, such as triple backticks or quotation marks. Just the raw JSON as text. If there won\'t be any images matching the request please put null for the "images" key'
                     ],
                     [
                         "role" => "user",
-//                        "content" => [
-//                            [
-//                                'type' => 'text',
-//                                'text' => ''
-//                            ]
-//                        ]
                     ]
                 ];
 
-                $photos = [];
                 if ($client->photos) {
                     foreach ($client->photos as $photo) {
-                        $photos[] = Storage::disk('public')->get($photo->path);
                         $request[1]['content'][] = [
                             "type" => "image_url",
                             "image_url" => [
@@ -53,26 +46,17 @@ class ChatGPT {
                 }
 
                 $response = ChatGPT::processRequest($request);
-                dump($response);
-                die;
 
-                if ($response) {
-                    $data = json_decode($response, true);
-
-                    dd($data);
-
-//                    if ($data['images']) {
-//                    }
-//
-//                    return ;
+                if (json_validate($response)) {
+                    return json_decode($response, true);
                 }
 
                  throw new \Exception('Can\'t process your request');
-            } catch (\Exception $e) {
-
+            } catch (\Exception $th) {
+                throw new \Exception($e->getMessage());
             }
         }
 
         return false;
     }
-}
+}