AIModelService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Services;
  3. use App\Http\Resources\AiModelResource;
  4. use App\Models\AiModel;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Schema;
  7. class AIModelService
  8. {
  9. public function getModelsById($id)
  10. {
  11. // 添加表存在检查
  12. if (! Schema::hasTable('ai_models')) {
  13. return [];
  14. }
  15. $table = AiModel::whereIn('uid', $id);
  16. $result = $table->get();
  17. return AiModelResource::collection(resource: $result);
  18. }
  19. public function getModelById($id)
  20. {
  21. // 添加表存在检查
  22. if (! Schema::hasTable('ai_models')) {
  23. return [];
  24. }
  25. $result = AiModel::where('uid', $id)
  26. ->first();
  27. return new AiModelResource(resource: $result);
  28. }
  29. public function getSysModels($type = null)
  30. {
  31. // 添加表存在检查
  32. if (! Schema::hasTable('ai_models')) {
  33. return [];
  34. }
  35. if (empty($type)) {
  36. $types = ['wbw', 'chat', 'summarize'];
  37. } else {
  38. $types = [$type];
  39. }
  40. $sysModels = [];
  41. foreach ($types as $key => $type) {
  42. $sysModels[$type] = $this->getModelsById(Cache::get('/ai/model/system/'.$type) ?? []);
  43. }
  44. if (! empty($type)) {
  45. return $sysModels[$type];
  46. }
  47. return $sysModels;
  48. }
  49. }