File size: 9,338 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | <?php
namespace App\Http\Controllers;
use App\Helpers\Classes\Helper;
use App\Services\Assistant\AssistantService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
class AssistantController extends Controller
{
public function __construct(protected AssistantService $service) {}
/**
* Display a listing of the resource.
*
* @throws GuzzleException
*/
public function index(): Factory|Application|View|\Illuminate\Contracts\Foundation\Application
{
$assistants = $this->service->listAssistant();
$assistantsCollection = collect($assistants);
$perPage = 10;
$currentPage = Paginator::resolveCurrentPage() ?: 1;
$currentItems = $assistantsCollection->slice(($currentPage - 1) * $perPage, $perPage)->all();
$paginatedItems = new LengthAwarePaginator($currentItems, $assistantsCollection->count(), $perPage, $currentPage, [
'path' => Paginator::resolveCurrentPath(),
]);
return view('panel.admin.assistant.index', ['assistants' => $paginatedItems]);
}
/**
* Show the form for creating a new resource.
*
* @throws GuzzleException
*/
public function create(): Factory|Application|View|\Illuminate\Contracts\Foundation\Application
{
$assistant = [];
$models = $this->service->listModels();
return view('panel.admin.assistant.create', compact('models', 'assistant'));
}
/**
* Store a newly created resource in storage.
*
* @throws GuzzleException
*/
public function store(Request $request): JsonResponse|RedirectResponse
{
if (Helper::appIsDemo()) {
return response()->json([
'status' => 'error',
'message' => trans('This feature is disabled in demo mode.'),
]);
}
$request->validate([
'name' => 'required',
'model' => 'required',
'instructions' => 'required',
'files.*' => 'mimes:pdf,doc,docx',
]);
if ($request->get('file_search') === '1') {
$tools[] = ['type' => 'file_search'];
}
if ($request->get('code_interpreter') === '1') {
$tools[] = ['type' => 'code_interpreter'];
}
if ($request->has('files')) {
$fileIds = [];
foreach ($request->file('files') as $file) {
$file = $this->service->createFile($file);
if (isset($file['id'])) {
$fileIds[] = $file['id'];
}
}
$vectorId = $this->service->createVectorStore()['id'];
foreach ($fileIds as $fileId) {
$this->service->createVectorStoreFiles($vectorId, $fileId);
}
$resources = ['file_search' => ['vector_store_ids' => [$vectorId]]];
}
$response = $this->service->createAssistant(
$request->get('instructions'),
$request->get('name'),
$request->get('model'),
$resources ?? null,
$tools ?? null,
$request->get('description'),
$request->get('temperature'),
$request->get('top_p'),
);
if (isset($response['id'])) {
return redirect()->route('dashboard.admin.ai-assistant.index')
->with(['message' => __('Assistant Created Successfully'), 'type' => 'success']);
} else {
return redirect()->back()->with(['message' => $response['message'], 'type' => 'error']);
}
}
/**
* Show the form for editing the specified resource.
*
* @throws GuzzleException
*/
public function edit(string $id): Factory|Application|View|\Illuminate\Contracts\Foundation\Application
{
$models = $this->service->listModels();
$assistant = $this->service->showAssistant($id);
$existingFiles = [];
if (isset($assistant['tool_resources']['file_search']['vector_store_ids'][0])) {
$vectorId = $assistant['tool_resources']['file_search']['vector_store_ids'][0];
$vector = $this->service->listVectorStoreFiles($vectorId);
if (isset($vector['data'])) {
$vectorFiles = $vector['data'];
foreach ($vectorFiles as $vectorFile) {
$existingFiles[] = $this->service->showFiles($vectorFile['id']);
}
}
}
return view('panel.admin.assistant.edit',
['assistant' => $assistant, 'models' => $models, 'existingFiles' => $existingFiles]);
}
/**
* Update the specified resource in storage.
*
* @throws GuzzleException
*/
public function update(Request $request, string $id): JsonResponse|RedirectResponse
{
if (Helper::appIsDemo()) {
return response()->json([
'status' => 'error',
'message' => trans('This feature is disabled in demo mode.'),
]);
}
$request->validate([
'name' => 'sometimes|required',
'model' => 'sometimes|required',
'instructions' => 'sometimes|required',
]);
$tools = [];
if ($request->get('file_search') === '1') {
$tools[] = ['type' => 'file_search'];
}
if ($request->get('code_interpreter') === '1') {
$tools[] = ['type' => 'code_interpreter'];
}
$assistant = $this->service->showAssistant($id);
$vectorId = $assistant['tool_resources']['file_search']['vector_store_ids'][0] ?? null;
$resources = [];
if ($request->has('existingfiles') && $vectorId) {
$this->syncExistingFiles($vectorId, $request->get('existingfiles'));
$resources = ['file_search' => ['vector_store_ids' => [$vectorId]]];
} else {
$this->clearVectorStoreFiles($vectorId);
if ($vectorId) {
$resources = ['file_search' => ['vector_store_ids' => [$vectorId]]];
}
}
if ($request->has('files')) {
$vectorId = $vectorId ?? $this->service->createVectorStore()['id'];
$this->uploadNewFiles($request->file('files'), $vectorId);
$resources = ['file_search' => ['vector_store_ids' => [$vectorId]]];
}
$response = $this->service->updateAssistant(
$assistant['id'],
$request->get('instructions'),
$request->get('name'),
$request->get('model'),
$resources,
$tools,
$request->get('description'),
$request->get('temperature'),
$request->get('top_p')
);
if (isset($response['id'])) {
return redirect()->back()->with(['message' => __('Assistant Trained Successfully'), 'type' => 'success']);
} else {
return redirect()->back()->with(['message' => $response['message'], 'type' => 'error']);
}
}
/**
* @throws GuzzleException
*/
private function syncExistingFiles(string $vectorId, array $existingFiles): void
{
$vectors = $this->service->listVectorStoreFiles($vectorId)['data'];
$vectorFiles = collect($vectors)->pluck('id')->toArray();
$removedFiles = array_diff($vectorFiles, $existingFiles);
foreach ($removedFiles as $removedFile) {
$this->service->deleteVectorStoreFiles($vectorId, $removedFile);
}
}
/**
* @throws GuzzleException
*/
private function clearVectorStoreFiles(?string $vectorId): void
{
if ($vectorId) {
$vectors = $this->service->listVectorStoreFiles($vectorId)['data'];
foreach ($vectors as $vector) {
$this->service->deleteVectorStoreFiles($vectorId, $vector['id']);
}
}
}
/**
* @throws GuzzleException
*/
private function uploadNewFiles(array $files, string $vectorId): void
{
$fileIds = [];
foreach ($files as $file) {
$file = $this->service->createFile($file);
if (isset($file['id'])) {
$fileIds[] = $file['id'];
}
}
foreach ($fileIds as $fileId) {
$this->service->createVectorStoreFiles($vectorId, $fileId);
}
}
/**
* Remove the specified resource from storage.
*
* @throws GuzzleException
*/
public function destroy(string $id): JsonResponse
{
if (Helper::appIsDemo()) {
return response()->json([
'status' => 'error',
'message' => trans('This feature is disabled in demo mode.'),
]);
}
$this->service->deleteAssistant($id);
return response()->json([
'message' => trans('Assistant Deleted Successfully'),
'reload' => true,
'setTimeOut' => 1000,
]);
}
}
|