model search for runware.ai implemented, added app logo and dashboard stats
This commit is contained in:
@@ -16,6 +16,9 @@ use Filament\Forms\Components\TextInput;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Tables\Actions\Action;
|
||||
use App\Models\ApiProvider;
|
||||
use App\Api\Plugins\PluginLoader;
|
||||
use App\Api\Plugins\ApiPluginInterface;
|
||||
|
||||
class AiModelResource extends Resource
|
||||
{
|
||||
@@ -27,35 +30,114 @@ class AiModelResource extends Resource
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label(__('filament.resource.ai_model.form.name'))
|
||||
->required()
|
||||
->maxLength(255),
|
||||
TextInput::make('model_id')
|
||||
->label(__('filament.resource.ai_model.form.model_id'))
|
||||
->required()
|
||||
->maxLength(255),
|
||||
TextInput::make('model_type')
|
||||
->nullable()
|
||||
->maxLength(255),
|
||||
Forms\Components\Toggle::make('enabled')
|
||||
->label(__('filament.resource.ai_model.form.enabled'))
|
||||
->default(true),
|
||||
Select::make('apiProviders')
|
||||
->relationship('apiProviders', 'name')
|
||||
->multiple()
|
||||
->preload()
|
||||
->searchable(false)
|
||||
->label(__('filament.resource.ai_model.form.api_providers')),
|
||||
Forms\Components\Textarea::make('parameters')
|
||||
->label(__('filament.resource.ai_model.form.parameters'))
|
||||
->nullable()
|
||||
->rows(15)
|
||||
->json(JSON_PRETTY_PRINT)
|
||||
->helperText(__('filament.resource.ai_model.form.parameters_help'))
|
||||
Forms\Components\Section::make()
|
||||
->schema([
|
||||
Select::make('api_provider_id')
|
||||
->label(__('filament.resource.ai_model.form.api_provider'))
|
||||
->relationship('apiProviders', 'name')
|
||||
->live()
|
||||
->nullable()
|
||||
->afterStateUpdated(function (callable $set) {
|
||||
$set('model_search_result', null);
|
||||
}),
|
||||
Select::make('model_search_result')
|
||||
->label(__('filament.resource.ai_model.form.search_model'))
|
||||
->searchable()
|
||||
->live()
|
||||
->hidden(fn (callable $get) => !static::canSearchModels($get('api_provider_id')))
|
||||
->getSearchResultsUsing(function (string $search, callable $get) {
|
||||
$apiProviderId = $get('api_provider_id');
|
||||
if (!$apiProviderId) {
|
||||
return [];
|
||||
}
|
||||
$pluginInstance = static::getPluginInstance($apiProviderId);
|
||||
if ($pluginInstance && method_exists($pluginInstance, 'searchModels')) {
|
||||
$models = $pluginInstance->searchModels($search);
|
||||
$options = [];
|
||||
foreach ($models as $model) {
|
||||
$options[json_encode(['name' => $model['name'], 'id' => $model['id'], 'type' => $model['type'] ?? null])] = $model['name'] . ' (' . $model['id'] . ')';
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
return [];
|
||||
})
|
||||
->getOptionLabelUsing(function ($value) {
|
||||
$decoded = json_decode($value, true);
|
||||
return $decoded['name'] . ' (' . $decoded['id'] . ')';
|
||||
})
|
||||
->afterStateUpdated(function (callable $set, $state) {
|
||||
if ($state) {
|
||||
$selectedModel = json_decode($state, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
return; // Stop if JSON is invalid
|
||||
}
|
||||
$set('name', $selectedModel['name']);
|
||||
$set('model_id', $selectedModel['id']);
|
||||
$set('model_type', $selectedModel['type'] ?? null);
|
||||
}
|
||||
}),
|
||||
TextInput::make('name')
|
||||
->label(__('filament.resource.ai_model.form.name'))
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->live(),
|
||||
TextInput::make('model_id')
|
||||
->label(__('filament.resource.ai_model.form.model_id'))
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->live(),
|
||||
TextInput::make('model_type')
|
||||
->nullable()
|
||||
->maxLength(255)
|
||||
->live(),
|
||||
Forms\Components\Toggle::make('enabled')
|
||||
->label(__('filament.resource.ai_model.form.enabled'))
|
||||
->default(true),
|
||||
Forms\Components\Textarea::make('parameters')
|
||||
->label(__('filament.resource.ai_model.form.parameters'))
|
||||
->nullable()
|
||||
->rows(15)
|
||||
->json(JSON_PRETTY_PRINT)
|
||||
->helperText(__('filament.resource.ai_model.form.parameters_help'))
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
protected static function canSearchModels(?int $apiProviderId): bool
|
||||
{
|
||||
if (!$apiProviderId) {
|
||||
return false;
|
||||
}
|
||||
$apiProvider = ApiProvider::find($apiProviderId);
|
||||
if (!$apiProvider || !$apiProvider->plugin) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$pluginInstance = PluginLoader::getPlugin($apiProvider->plugin, $apiProvider);
|
||||
return method_exists($pluginInstance, 'searchModels');
|
||||
} catch (\Exception $e) {
|
||||
// Log the exception if needed
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected static function getPluginInstance(?int $apiProviderId): ?ApiPluginInterface
|
||||
{
|
||||
if (!$apiProviderId) {
|
||||
return null;
|
||||
}
|
||||
$apiProvider = ApiProvider::find($apiProviderId);
|
||||
if (!$apiProvider || !$apiProvider->plugin) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return PluginLoader::getPlugin($apiProvider->plugin, $apiProvider);
|
||||
} catch (\Exception $e) {
|
||||
// Log the exception if needed
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
|
||||
Reference in New Issue
Block a user