|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 83 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 111 KiB After Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 137 KiB After Width: | Height: | Size: 137 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 32 KiB |
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 113 KiB After Width: | Height: | Size: 113 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 84 KiB |
|
Before Width: | Height: | Size: 111 KiB After Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 118 KiB After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 119 KiB |
|
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
|
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 72 KiB |
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 99 KiB After Width: | Height: | Size: 99 KiB |
|
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
|
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 46 KiB |
|
Before Width: | Height: | Size: 104 KiB After Width: | Height: | Size: 104 KiB |
|
Before Width: | Height: | Size: 117 KiB After Width: | Height: | Size: 117 KiB |
|
Before Width: | Height: | Size: 118 KiB After Width: | Height: | Size: 118 KiB |
|
Before Width: | Height: | Size: 120 KiB After Width: | Height: | Size: 120 KiB |
|
Before Width: | Height: | Size: 107 KiB After Width: | Height: | Size: 107 KiB |
|
Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 119 KiB |
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 27 KiB |
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 100 KiB After Width: | Height: | Size: 100 KiB |
|
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 112 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 22 KiB |
@ -0,0 +1,178 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.controller; |
|||
|
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import dev.langchain4j.model.chat.request.ChatRequest; |
|||
import io.swagger.v3.oas.annotations.Parameter; |
|||
import io.swagger.v3.oas.annotations.media.Schema; |
|||
import jakarta.validation.Valid; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.security.access.prepost.PreAuthorize; |
|||
import org.springframework.validation.annotation.Validated; |
|||
import org.springframework.web.bind.annotation.DeleteMapping; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.PathVariable; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
import org.springframework.web.context.request.async.DeferredResult; |
|||
import org.thingsboard.server.common.data.ai.AiModel; |
|||
import org.thingsboard.server.common.data.ai.dto.TbChatRequest; |
|||
import org.thingsboard.server.common.data.ai.dto.TbChatResponse; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AiChatModelConfig; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.AiModelId; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.config.annotations.ApiOperation; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.ai.AiChatModelService; |
|||
import org.thingsboard.server.service.security.permission.Operation; |
|||
import org.thingsboard.server.service.security.permission.Resource; |
|||
|
|||
import java.time.Duration; |
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
|
|||
import static com.google.common.util.concurrent.MoreExecutors.directExecutor; |
|||
import static org.thingsboard.server.controller.ControllerConstants.AI_MODEL_TEXT_SEARCH_DESCRIPTION; |
|||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS; |
|||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION; |
|||
import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION; |
|||
import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION; |
|||
import static org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION; |
|||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH; |
|||
|
|||
@Validated |
|||
@RestController |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
@RequestMapping("/api/ai/model") |
|||
class AiModelController extends BaseController { |
|||
|
|||
private final AiChatModelService aiChatModelService; |
|||
|
|||
@ApiOperation( |
|||
value = "Create or update AI model (saveAiModel)", |
|||
notes = "Creates or updates an AI model record.\n\n" + |
|||
"• **Create:** Omit the `id` to create a new record. The platform assigns a UUID to the new record and returns it in the `id` field of the response.\n\n" + |
|||
"• **Update:** Include an existing `id` to modify that record. If no matching record exists, the API responds with **404 Not Found**.\n\n" + |
|||
"Tenant ID for the AI model will be taken from the authenticated user making the request, regardless of any value provided in the request body." + |
|||
TENANT_AUTHORITY_PARAGRAPH |
|||
) |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
@PostMapping |
|||
public AiModel saveAiModel(@RequestBody @Valid AiModel model) throws ThingsboardException { |
|||
var user = getCurrentUser(); |
|||
model.setTenantId(user.getTenantId()); |
|||
checkEntity(model.getId(), model, Resource.AI_MODEL); |
|||
return tbAiModelService.save(model, user); |
|||
} |
|||
|
|||
@ApiOperation( |
|||
value = "Get AI model by ID (getAiModelById)", |
|||
notes = "Fetches an AI model record by its `id`." + |
|||
TENANT_AUTHORITY_PARAGRAPH |
|||
) |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
@GetMapping("/{modelUuid}") |
|||
public AiModel getAiModelById( |
|||
@Parameter( |
|||
description = "ID of the AI model record", |
|||
required = true, |
|||
example = "de7900d4-30e2-11f0-9cd2-0242ac120002" |
|||
) |
|||
@PathVariable UUID modelUuid |
|||
) throws ThingsboardException { |
|||
return checkAiModelId(new AiModelId(modelUuid), Operation.READ); |
|||
} |
|||
|
|||
@ApiOperation( |
|||
value = "Get AI models (getAiModels)", |
|||
notes = "Returns a page of AI models. " + |
|||
PAGE_DATA_PARAMETERS + TENANT_AUTHORITY_PARAGRAPH |
|||
) |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
@GetMapping |
|||
public PageData<AiModel> getAiModels( |
|||
@Parameter(description = PAGE_SIZE_DESCRIPTION, required = true) |
|||
@RequestParam int pageSize, |
|||
@Parameter(description = PAGE_NUMBER_DESCRIPTION, required = true) |
|||
@RequestParam int page, |
|||
@Parameter(description = AI_MODEL_TEXT_SEARCH_DESCRIPTION) |
|||
@RequestParam(required = false) String textSearch, |
|||
@Parameter(description = SORT_PROPERTY_DESCRIPTION, schema = @Schema(allowableValues = {"createdTime", "name", "provider", "modelId"})) |
|||
@RequestParam(required = false) String sortProperty, |
|||
@Parameter(description = SORT_ORDER_DESCRIPTION, schema = @Schema(allowableValues = {"ASC", "DESC"})) |
|||
@RequestParam(required = false) String sortOrder |
|||
) throws ThingsboardException { |
|||
var user = getCurrentUser(); |
|||
accessControlService.checkPermission(user, Resource.AI_MODEL, Operation.READ); |
|||
var pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); |
|||
return aiModelService.findAiModelsByTenantId(user.getTenantId(), pageLink); |
|||
} |
|||
|
|||
@ApiOperation( |
|||
value = "Delete AI model by ID (deleteAiModelById)", |
|||
notes = "Deletes the AI model record by its `id`. " + |
|||
"If a record with the specified `id` exists, the record is deleted and the endpoint returns `true`. " + |
|||
"If no such record exists, the endpoint returns `false`." + |
|||
TENANT_AUTHORITY_PARAGRAPH |
|||
) |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
@DeleteMapping("/{modelUuid}") |
|||
public boolean deleteAiModelById( |
|||
@Parameter( |
|||
description = "ID of the AI model record", |
|||
required = true, |
|||
example = "de7900d4-30e2-11f0-9cd2-0242ac120002" |
|||
) |
|||
@PathVariable UUID modelUuid |
|||
) throws ThingsboardException { |
|||
var user = getCurrentUser(); |
|||
var modelId = new AiModelId(modelUuid); |
|||
accessControlService.checkPermission(user, Resource.AI_MODEL, Operation.DELETE); |
|||
Optional<AiModel> toDelete = aiModelService.findAiModelByTenantIdAndId(user.getTenantId(), modelId); |
|||
if (toDelete.isEmpty()) { |
|||
return false; |
|||
} |
|||
accessControlService.checkPermission(user, Resource.AI_MODEL, Operation.DELETE, modelId, toDelete.get()); |
|||
return tbAiModelService.delete(toDelete.get(), user); |
|||
} |
|||
|
|||
@ApiOperation( |
|||
value = "Send request to AI chat model (sendChatRequest)", |
|||
notes = "Submits a single prompt - made up of an optional system message and a required user message - to the specified AI chat model " + |
|||
"and returns either the generated answer or an error envelope." + |
|||
TENANT_AUTHORITY_PARAGRAPH |
|||
) |
|||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
|||
@PostMapping("/chat") |
|||
public DeferredResult<TbChatResponse> sendChatRequest(@Valid @RequestBody TbChatRequest tbChatRequest) { |
|||
ChatRequest langChainChatRequest = tbChatRequest.toLangChainChatRequest(); |
|||
AiChatModelConfig<?> chatModelConfig = tbChatRequest.chatModelConfig(); |
|||
|
|||
ListenableFuture<TbChatResponse> future = aiChatModelService.sendChatRequestAsync(chatModelConfig, langChainChatRequest) |
|||
.transform(chatResponse -> (TbChatResponse) new TbChatResponse.Success(chatResponse.aiMessage().text()), directExecutor()) |
|||
.catching(Throwable.class, ex -> new TbChatResponse.Failure(ex.getMessage()), directExecutor()); |
|||
|
|||
Integer requestTimeoutSeconds = chatModelConfig.timeoutSeconds(); |
|||
return requestTimeoutSeconds != null ? wrapFuture(future, Duration.ofSeconds(requestTimeoutSeconds).toMillis()) : wrapFuture(future); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.ai; |
|||
|
|||
import org.thingsboard.rule.engine.api.RuleEngineAiChatModelService; |
|||
|
|||
public interface AiChatModelService extends RuleEngineAiChatModelService {} |
|||
@ -0,0 +1,40 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.ai; |
|||
|
|||
import com.google.common.util.concurrent.FluentFuture; |
|||
import dev.langchain4j.model.chat.ChatModel; |
|||
import dev.langchain4j.model.chat.request.ChatRequest; |
|||
import dev.langchain4j.model.chat.response.ChatResponse; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.Langchain4jChatModelConfigurer; |
|||
|
|||
@Service |
|||
@RequiredArgsConstructor |
|||
class AiChatModelServiceImpl implements AiChatModelService { |
|||
|
|||
private final Langchain4jChatModelConfigurer chatModelConfigurer; |
|||
private final AiRequestsExecutor aiRequestsExecutor; |
|||
|
|||
@Override |
|||
public <C extends AiChatModelConfig<C>> FluentFuture<ChatResponse> sendChatRequestAsync(AiChatModelConfig<C> chatModelConfig, ChatRequest chatRequest) { |
|||
ChatModel langChainChatModel = chatModelConfig.configure(chatModelConfigurer); |
|||
return aiRequestsExecutor.sendChatRequestAsync(langChainChatModel, chatRequest); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.ai; |
|||
|
|||
import com.google.common.util.concurrent.FluentFuture; |
|||
import dev.langchain4j.model.chat.ChatModel; |
|||
import dev.langchain4j.model.chat.request.ChatRequest; |
|||
import dev.langchain4j.model.chat.response.ChatResponse; |
|||
|
|||
public interface AiRequestsExecutor { |
|||
|
|||
FluentFuture<ChatResponse> sendChatRequestAsync(ChatModel chatModel, ChatRequest chatRequest); |
|||
|
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.ai; |
|||
|
|||
import com.google.common.util.concurrent.FluentFuture; |
|||
import com.google.common.util.concurrent.ListeningExecutorService; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import dev.langchain4j.model.chat.ChatModel; |
|||
import dev.langchain4j.model.chat.request.ChatRequest; |
|||
import dev.langchain4j.model.chat.response.ChatResponse; |
|||
import jakarta.annotation.PostConstruct; |
|||
import jakarta.annotation.PreDestroy; |
|||
import jakarta.validation.constraints.Min; |
|||
import jakarta.validation.constraints.NotBlank; |
|||
import lombok.Data; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.context.annotation.Lazy; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.validation.annotation.Validated; |
|||
import org.thingsboard.common.util.ThingsBoardThreadFactory; |
|||
|
|||
import java.time.Duration; |
|||
import java.util.concurrent.Executors; |
|||
|
|||
@Lazy |
|||
@Component |
|||
@RequiredArgsConstructor |
|||
class DefaultAiRequestsExecutor implements AiRequestsExecutor { |
|||
|
|||
private final AiRequestsExecutorProperties properties; |
|||
|
|||
@Data |
|||
@Validated |
|||
@Configuration |
|||
@ConfigurationProperties(prefix = "actors.rule.ai-requests-thread-pool") |
|||
private static class AiRequestsExecutorProperties { |
|||
|
|||
@NotBlank(message = "Pool name must be not blank") |
|||
private String poolName = "ai-requests"; |
|||
|
|||
@Min(value = 1, message = "Pool size must be at least 1") |
|||
private int poolSize = 50; |
|||
|
|||
@Min(value = 1, message = "Termination timeout must be at least 1 second") |
|||
private int terminationTimeoutSeconds = 60; |
|||
|
|||
} |
|||
|
|||
private ListeningExecutorService executorService; |
|||
|
|||
@PostConstruct |
|||
private void init() { |
|||
executorService = MoreExecutors.listeningDecorator( |
|||
Executors.newFixedThreadPool(properties.getPoolSize(), ThingsBoardThreadFactory.forName(properties.getPoolName())) |
|||
); |
|||
} |
|||
|
|||
@Override |
|||
public FluentFuture<ChatResponse> sendChatRequestAsync(ChatModel chatModel, ChatRequest chatRequest) { |
|||
return FluentFuture.from(executorService.submit(() -> chatModel.chat(chatRequest))); |
|||
} |
|||
|
|||
@PreDestroy |
|||
private void destroy() { |
|||
if (executorService != null) { |
|||
MoreExecutors.shutdownAndAwaitTermination(executorService, Duration.ofSeconds(properties.getTerminationTimeoutSeconds())); |
|||
executorService = null; |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,269 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.ai; |
|||
|
|||
import com.google.api.gax.core.FixedCredentialsProvider; |
|||
import com.google.api.gax.retrying.RetrySettings; |
|||
import com.google.auth.oauth2.ServiceAccountCredentials; |
|||
import com.google.cloud.vertexai.Transport; |
|||
import com.google.cloud.vertexai.VertexAI; |
|||
import com.google.cloud.vertexai.api.GenerationConfig; |
|||
import com.google.cloud.vertexai.api.PredictionServiceClient; |
|||
import com.google.cloud.vertexai.api.PredictionServiceSettings; |
|||
import com.google.cloud.vertexai.generativeai.GenerativeModel; |
|||
import dev.langchain4j.model.anthropic.AnthropicChatModel; |
|||
import dev.langchain4j.model.azure.AzureOpenAiChatModel; |
|||
import dev.langchain4j.model.bedrock.BedrockChatModel; |
|||
import dev.langchain4j.model.chat.ChatModel; |
|||
import dev.langchain4j.model.chat.request.ChatRequestParameters; |
|||
import dev.langchain4j.model.github.GitHubModelsChatModel; |
|||
import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel; |
|||
import dev.langchain4j.model.mistralai.MistralAiChatModel; |
|||
import dev.langchain4j.model.openai.OpenAiChatModel; |
|||
import dev.langchain4j.model.vertexai.gemini.VertexAiGeminiChatModel; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AmazonBedrockChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AnthropicChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.AzureOpenAiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.GitHubModelsChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.GoogleAiGeminiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.GoogleVertexAiGeminiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.Langchain4jChatModelConfigurer; |
|||
import org.thingsboard.server.common.data.ai.model.chat.MistralAiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.model.chat.OpenAiChatModelConfig; |
|||
import org.thingsboard.server.common.data.ai.provider.AmazonBedrockProviderConfig; |
|||
import org.thingsboard.server.common.data.ai.provider.AzureOpenAiProviderConfig; |
|||
import org.thingsboard.server.common.data.ai.provider.GoogleVertexAiGeminiProviderConfig; |
|||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
|||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
|||
import software.amazon.awssdk.regions.Region; |
|||
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.IOException; |
|||
import java.time.Duration; |
|||
|
|||
@Component |
|||
class Langchain4jChatModelConfigurerImpl implements Langchain4jChatModelConfigurer { |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(OpenAiChatModelConfig chatModelConfig) { |
|||
return OpenAiChatModel.builder() |
|||
.apiKey(chatModelConfig.providerConfig().apiKey()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(AzureOpenAiChatModelConfig chatModelConfig) { |
|||
AzureOpenAiProviderConfig providerConfig = chatModelConfig.providerConfig(); |
|||
return AzureOpenAiChatModel.builder() |
|||
.endpoint(providerConfig.endpoint()) |
|||
.serviceVersion(providerConfig.serviceVersion()) |
|||
.apiKey(providerConfig.apiKey()) |
|||
.deploymentName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(GoogleAiGeminiChatModelConfig chatModelConfig) { |
|||
return GoogleAiGeminiChatModel.builder() |
|||
.apiKey(chatModelConfig.providerConfig().apiKey()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.topK(chatModelConfig.topK()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxOutputTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(GoogleVertexAiGeminiChatModelConfig chatModelConfig) { |
|||
GoogleVertexAiGeminiProviderConfig providerConfig = chatModelConfig.providerConfig(); |
|||
|
|||
// construct service account credentials using service account key JSON
|
|||
ServiceAccountCredentials serviceAccountCredentials; |
|||
try { |
|||
serviceAccountCredentials = ServiceAccountCredentials.fromStream(new ByteArrayInputStream(providerConfig.serviceAccountKey().getBytes())); |
|||
} catch (IOException e) { |
|||
throw new RuntimeException("Failed to parse service account key JSON", e); |
|||
} |
|||
|
|||
PredictionServiceSettings predictionServiceClientSettings; |
|||
try { |
|||
// create prediction service settings for REST transport with service account key credentials
|
|||
PredictionServiceSettings.Builder settingsBuilder = PredictionServiceSettings.newHttpJsonBuilder() |
|||
.setCredentialsProvider(FixedCredentialsProvider.create(serviceAccountCredentials)); |
|||
|
|||
// get the retry settings that control request timeout for generateContent RPC
|
|||
RetrySettings.Builder retrySettings = settingsBuilder |
|||
.generateContentSettings() |
|||
.getRetrySettings() |
|||
.toBuilder(); |
|||
|
|||
// set request timeout from model config
|
|||
if (chatModelConfig.timeoutSeconds() != null) { |
|||
retrySettings.setTotalTimeout(org.threeten.bp.Duration.ofSeconds(chatModelConfig.timeoutSeconds())); |
|||
} |
|||
|
|||
// set updated retry settings
|
|||
settingsBuilder.generateContentSettings().setRetrySettings(retrySettings.build()); |
|||
|
|||
// build the client settings
|
|||
predictionServiceClientSettings = settingsBuilder.build(); |
|||
} catch (IOException e) { |
|||
throw new RuntimeException("Failed to create prediction service client settings", e); |
|||
} |
|||
|
|||
// construct Vertex AI instance
|
|||
var vertexAI = new VertexAI.Builder() |
|||
.setProjectId(providerConfig.projectId()) |
|||
.setLocation(providerConfig.location()) |
|||
.setPredictionClientSupplier(() -> createPredictionServiceClient(predictionServiceClientSettings)) |
|||
.setTransport(Transport.REST) // GRPC also possible, but likely does not work with service account keys
|
|||
.build(); |
|||
|
|||
// map model config to generation config
|
|||
var generationConfigBuilder = GenerationConfig.newBuilder(); |
|||
if (chatModelConfig.temperature() != null) { |
|||
generationConfigBuilder.setTemperature(chatModelConfig.temperature().floatValue()); |
|||
} |
|||
if (chatModelConfig.topP() != null) { |
|||
generationConfigBuilder.setTopP(chatModelConfig.topP().floatValue()); |
|||
} |
|||
if (chatModelConfig.topK() != null) { |
|||
generationConfigBuilder.setTopK(chatModelConfig.topK()); |
|||
} |
|||
if (chatModelConfig.frequencyPenalty() != null) { |
|||
generationConfigBuilder.setFrequencyPenalty(chatModelConfig.frequencyPenalty().floatValue()); |
|||
} |
|||
if (chatModelConfig.frequencyPenalty() != null) { |
|||
generationConfigBuilder.setPresencePenalty(chatModelConfig.frequencyPenalty().floatValue()); |
|||
} |
|||
if (chatModelConfig.maxOutputTokens() != null) { |
|||
generationConfigBuilder.setMaxOutputTokens(chatModelConfig.maxOutputTokens()); |
|||
} |
|||
var generationConfig = generationConfigBuilder.build(); |
|||
|
|||
// construct generative model instance
|
|||
var generativeModel = new GenerativeModel(chatModelConfig.modelId(), vertexAI).withGenerationConfig(generationConfig); |
|||
|
|||
return new VertexAiGeminiChatModel(generativeModel, generationConfig, chatModelConfig.maxRetries()); |
|||
} |
|||
|
|||
private static PredictionServiceClient createPredictionServiceClient(PredictionServiceSettings settings) { |
|||
try { |
|||
return PredictionServiceClient.create(settings); |
|||
} catch (IOException e) { |
|||
throw new RuntimeException("Failed to create prediction service client", e); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(MistralAiChatModelConfig chatModelConfig) { |
|||
return MistralAiChatModel.builder() |
|||
.apiKey(chatModelConfig.providerConfig().apiKey()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(AnthropicChatModelConfig chatModelConfig) { |
|||
return AnthropicChatModel.builder() |
|||
.apiKey(chatModelConfig.providerConfig().apiKey()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.topK(chatModelConfig.topK()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(AmazonBedrockChatModelConfig chatModelConfig) { |
|||
AmazonBedrockProviderConfig providerConfig = chatModelConfig.providerConfig(); |
|||
|
|||
var credentialsProvider = StaticCredentialsProvider.create( |
|||
AwsBasicCredentials.create(providerConfig.accessKeyId(), providerConfig.secretAccessKey()) |
|||
); |
|||
|
|||
var bedrockClient = BedrockRuntimeClient.builder() |
|||
.region(Region.of(providerConfig.region())) |
|||
.credentialsProvider(credentialsProvider) |
|||
.build(); |
|||
|
|||
var defaultChatRequestParams = ChatRequestParameters.builder() |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.maxOutputTokens(chatModelConfig.maxOutputTokens()) |
|||
.build(); |
|||
|
|||
return BedrockChatModel.builder() |
|||
.client(bedrockClient) |
|||
.modelId(chatModelConfig.modelId()) |
|||
.defaultRequestParameters(defaultChatRequestParams) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
@Override |
|||
public ChatModel configureChatModel(GitHubModelsChatModelConfig chatModelConfig) { |
|||
return GitHubModelsChatModel.builder() |
|||
.gitHubToken(chatModelConfig.providerConfig().personalAccessToken()) |
|||
.modelName(chatModelConfig.modelId()) |
|||
.temperature(chatModelConfig.temperature()) |
|||
.topP(chatModelConfig.topP()) |
|||
.frequencyPenalty(chatModelConfig.frequencyPenalty()) |
|||
.presencePenalty(chatModelConfig.presencePenalty()) |
|||
.maxTokens(chatModelConfig.maxOutputTokens()) |
|||
.timeout(toDuration(chatModelConfig.timeoutSeconds())) |
|||
.maxRetries(chatModelConfig.maxRetries()) |
|||
.build(); |
|||
} |
|||
|
|||
private static Duration toDuration(Integer timeoutSeconds) { |
|||
return timeoutSeconds != null ? Duration.ofSeconds(timeoutSeconds) : null; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,150 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.stats; |
|||
|
|||
import com.google.common.util.concurrent.FutureCallback; |
|||
import com.google.common.util.concurrent.Futures; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.scheduling.annotation.Scheduled; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.kv.BasicTsKvEntry; |
|||
import org.thingsboard.server.common.data.kv.LongDataEntry; |
|||
import org.thingsboard.server.common.data.kv.TimeseriesSaveResult; |
|||
import org.thingsboard.server.common.data.kv.TsKvEntry; |
|||
import org.thingsboard.server.dao.edge.stats.EdgeStatsCounterService; |
|||
import org.thingsboard.server.dao.edge.stats.MsgCounters; |
|||
import org.thingsboard.server.dao.timeseries.TimeseriesService; |
|||
import org.thingsboard.server.queue.discovery.TopicService; |
|||
import org.thingsboard.server.queue.kafka.KafkaAdmin; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
import java.util.Collections; |
|||
import java.util.HashMap; |
|||
import java.util.HashSet; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Optional; |
|||
import java.util.concurrent.TimeUnit; |
|||
import java.util.stream.Collectors; |
|||
|
|||
import static org.thingsboard.server.dao.edge.stats.EdgeStatsKey.DOWNLINK_MSGS_ADDED; |
|||
import static org.thingsboard.server.dao.edge.stats.EdgeStatsKey.DOWNLINK_MSGS_LAG; |
|||
import static org.thingsboard.server.dao.edge.stats.EdgeStatsKey.DOWNLINK_MSGS_PERMANENTLY_FAILED; |
|||
import static org.thingsboard.server.dao.edge.stats.EdgeStatsKey.DOWNLINK_MSGS_PUSHED; |
|||
import static org.thingsboard.server.dao.edge.stats.EdgeStatsKey.DOWNLINK_MSGS_TMP_FAILED; |
|||
|
|||
@TbCoreComponent |
|||
@ConditionalOnProperty(prefix = "edges.stats", name = "enabled", havingValue = "true", matchIfMissing = false) |
|||
@RequiredArgsConstructor |
|||
@Service |
|||
@Slf4j |
|||
public class EdgeStatsService { |
|||
|
|||
private final TimeseriesService tsService; |
|||
private final EdgeStatsCounterService statsCounterService; |
|||
private final TopicService topicService; |
|||
private final Optional<KafkaAdmin> kafkaAdmin; |
|||
|
|||
@Value("${edges.stats.ttl:30}") |
|||
private int edgesStatsTtlDays; |
|||
@Value("${edges.stats.report-interval-millis:600000}") |
|||
private long reportIntervalMillis; |
|||
|
|||
|
|||
@Scheduled( |
|||
fixedDelayString = "${edges.stats.report-interval-millis:600000}", |
|||
initialDelayString = "${edges.stats.report-interval-millis:600000}" |
|||
) |
|||
public void reportStats() { |
|||
log.debug("Reporting Edge communication stats..."); |
|||
long now = System.currentTimeMillis(); |
|||
long ts = now - (now % reportIntervalMillis); |
|||
|
|||
Map<EdgeId, MsgCounters> countersByEdge = statsCounterService.getCounterByEdge(); |
|||
Map<EdgeId, Long> lagByEdgeId = kafkaAdmin.isPresent() ? getEdgeLagByEdgeId(countersByEdge) : Collections.emptyMap(); |
|||
Map<EdgeId, MsgCounters> countersByEdgeSnapshot = new HashMap<>(statsCounterService.getCounterByEdge()); |
|||
countersByEdgeSnapshot.forEach((edgeId, counters) -> { |
|||
TenantId tenantId = counters.getTenantId(); |
|||
|
|||
if (kafkaAdmin.isPresent()) { |
|||
counters.getMsgsLag().set(lagByEdgeId.getOrDefault(edgeId, 0L)); |
|||
} |
|||
List<TsKvEntry> statsEntries = List.of( |
|||
entry(ts, DOWNLINK_MSGS_ADDED.getKey(), counters.getMsgsAdded().get()), |
|||
entry(ts, DOWNLINK_MSGS_PUSHED.getKey(), counters.getMsgsPushed().get()), |
|||
entry(ts, DOWNLINK_MSGS_PERMANENTLY_FAILED.getKey(), counters.getMsgsPermanentlyFailed().get()), |
|||
entry(ts, DOWNLINK_MSGS_TMP_FAILED.getKey(), counters.getMsgsTmpFailed().get()), |
|||
entry(ts, DOWNLINK_MSGS_LAG.getKey(), counters.getMsgsLag().get()) |
|||
); |
|||
|
|||
log.trace("Reported Edge communication stats: {} tenantId - {}, edgeId - {}", statsEntries, tenantId, edgeId); |
|||
saveTs(tenantId, edgeId, statsEntries); |
|||
}); |
|||
} |
|||
|
|||
private Map<EdgeId, Long> getEdgeLagByEdgeId(Map<EdgeId, MsgCounters> countersByEdge) { |
|||
Map<EdgeId, String> edgeToTopicMap = countersByEdge.entrySet().stream() |
|||
.collect(Collectors.toMap( |
|||
Map.Entry::getKey, |
|||
e -> topicService.buildEdgeEventNotificationsTopicPartitionInfo(e.getValue().getTenantId(), e.getKey()).getTopic() |
|||
)); |
|||
|
|||
Map<String, Long> lagByTopic = kafkaAdmin.get().getTotalLagForGroupsBulk(new HashSet<>(edgeToTopicMap.values())); |
|||
|
|||
return edgeToTopicMap.entrySet().stream() |
|||
.collect(Collectors.toMap( |
|||
Map.Entry::getKey, |
|||
e -> lagByTopic.getOrDefault(e.getValue(), 0L) |
|||
)); |
|||
} |
|||
|
|||
private void saveTs(TenantId tenantId, EdgeId edgeId, List<TsKvEntry> statsEntries) { |
|||
try { |
|||
ListenableFuture<TimeseriesSaveResult> future = tsService.save( |
|||
tenantId, |
|||
edgeId, |
|||
statsEntries, |
|||
TimeUnit.DAYS.toSeconds(edgesStatsTtlDays) |
|||
); |
|||
|
|||
Futures.addCallback(future, new FutureCallback<>() { |
|||
@Override |
|||
public void onSuccess(TimeseriesSaveResult result) { |
|||
log.debug("Successfully saved edge time-series stats: {} for edge: {}", statsEntries, edgeId); |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Throwable t) { |
|||
log.warn("Failed to save edge time-series stats for edge: {}", edgeId, t); |
|||
} |
|||
}, MoreExecutors.directExecutor()); |
|||
} finally { |
|||
statsCounterService.clear(edgeId); |
|||
} |
|||
} |
|||
|
|||
private BasicTsKvEntry entry(long ts, String key, long value) { |
|||
return new BasicTsKvEntry(ts, new LongDataEntry(key, value)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.entitiy.ai; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.ai.AiModel; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.dao.ai.AiModelService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; |
|||
|
|||
import static java.util.Objects.requireNonNullElseGet; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
class DefaultTbAiModelService extends AbstractTbEntityService implements TbAiModelService { |
|||
|
|||
private final AiModelService aiModelService; |
|||
|
|||
@Override |
|||
public AiModel save(AiModel model, User user) { |
|||
var actionType = model.getId() == null ? ActionType.ADDED : ActionType.UPDATED; |
|||
|
|||
var tenantId = user.getTenantId(); |
|||
model.setTenantId(tenantId); |
|||
|
|||
AiModel savedModel; |
|||
try { |
|||
savedModel = aiModelService.save(model); |
|||
autoCommit(user, savedModel.getId()); |
|||
} catch (Exception e) { |
|||
logEntityActionService.logEntityAction(tenantId, requireNonNullElseGet(model.getId(), () -> emptyId(EntityType.AI_MODEL)), model, actionType, user, e); |
|||
throw e; |
|||
} |
|||
|
|||
logEntityActionService.logEntityAction(tenantId, savedModel.getId(), savedModel, actionType, user); |
|||
|
|||
return savedModel; |
|||
} |
|||
|
|||
@Override |
|||
public boolean delete(AiModel model, User user) { |
|||
var actionType = ActionType.DELETED; |
|||
|
|||
var tenantId = user.getTenantId(); |
|||
var modelId = model.getId(); |
|||
|
|||
boolean deleted; |
|||
try { |
|||
deleted = aiModelService.deleteByTenantIdAndId(tenantId, modelId); |
|||
} catch (Exception e) { |
|||
logEntityActionService.logEntityAction(tenantId, modelId, model, actionType, user, e, modelId.toString()); |
|||
throw e; |
|||
} |
|||
|
|||
if (deleted) { |
|||
logEntityActionService.logEntityAction(tenantId, modelId, model, actionType, user, modelId.toString()); |
|||
} |
|||
|
|||
return deleted; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.entitiy.ai; |
|||
|
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.ai.AiModel; |
|||
|
|||
public interface TbAiModelService { |
|||
|
|||
AiModel save(AiModel model, User user); |
|||
|
|||
boolean delete(AiModel model, User user); |
|||
|
|||
} |
|||