mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 05:49:20 +08:00
feat: 公司支持多模型提供商与会话消歧选型
This commit is contained in:
+45
-11
@@ -2,7 +2,9 @@ package top.xyquan.ecomdmbbackend.modules.agent.llm.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -10,15 +12,18 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.CreateAgentLlmProviderDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.ProbeAgentLlmModelsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpsertAgentLlmSettingsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpdateAgentLlmProviderDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpsertAgentLlmProviderModelsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmEnabledModelsVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProbeModelsResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmSettingsVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProviderListVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProviderVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.service.AgentCompanyLlmSettingsService;
|
||||
|
||||
/**
|
||||
* 公司模型 API 配置(浏览器 JWT)。
|
||||
* 公司多提供商模型 API(浏览器 JWT)。
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/company")
|
||||
@@ -27,16 +32,45 @@ public class AgentCompanyLlmSettingsController {
|
||||
|
||||
private final AgentCompanyLlmSettingsService settingsService;
|
||||
|
||||
@GetMapping("/agent-llm-settings")
|
||||
@RequirePermission(value = "agent:llm-settings", description = "查看模型配置", allowSuper = true)
|
||||
public ApiResponse<AgentLlmSettingsVO> get() {
|
||||
return ApiResponse.success(settingsService.getForCurrentCompany());
|
||||
@GetMapping("/agent-llm-providers")
|
||||
@RequirePermission(value = "agent:llm-settings", description = "查看模型提供商", allowSuper = true)
|
||||
public ApiResponse<AgentLlmProviderListVO> listProviders() {
|
||||
return ApiResponse.success(settingsService.listProvidersForCurrentCompany());
|
||||
}
|
||||
|
||||
@PutMapping("/agent-llm-settings")
|
||||
@RequirePermission(value = "agent:llm-settings", description = "更新模型配置", allowSuper = true)
|
||||
public ApiResponse<AgentLlmSettingsVO> upsert(@Valid @RequestBody UpsertAgentLlmSettingsDTO dto) {
|
||||
return ApiResponse.success("保存成功", settingsService.upsertForCurrentCompany(dto));
|
||||
@GetMapping("/agent-llm-providers/{id}")
|
||||
@RequirePermission(value = "agent:llm-settings", description = "查看模型提供商", allowSuper = true)
|
||||
public ApiResponse<AgentLlmProviderVO> getProvider(@PathVariable("id") Long id) {
|
||||
return ApiResponse.success(settingsService.getProviderForCurrentCompany(id));
|
||||
}
|
||||
|
||||
@PostMapping("/agent-llm-providers")
|
||||
@RequirePermission(value = "agent:llm-settings", description = "创建模型提供商", allowSuper = true)
|
||||
public ApiResponse<AgentLlmProviderVO> createProvider(@Valid @RequestBody CreateAgentLlmProviderDTO dto) {
|
||||
return ApiResponse.success("创建成功", settingsService.createProvider(dto));
|
||||
}
|
||||
|
||||
@PutMapping("/agent-llm-providers/{id}")
|
||||
@RequirePermission(value = "agent:llm-settings", description = "更新模型提供商", allowSuper = true)
|
||||
public ApiResponse<AgentLlmProviderVO> updateProvider(
|
||||
@PathVariable("id") Long id,
|
||||
@RequestBody UpdateAgentLlmProviderDTO dto) {
|
||||
return ApiResponse.success("保存成功", settingsService.updateProvider(id, dto));
|
||||
}
|
||||
|
||||
@DeleteMapping("/agent-llm-providers/{id}")
|
||||
@RequirePermission(value = "agent:llm-settings", description = "删除模型提供商", allowSuper = true)
|
||||
public ApiResponse<Void> deleteProvider(@PathVariable("id") Long id) {
|
||||
settingsService.deleteProvider(id);
|
||||
return ApiResponse.success("删除成功", null);
|
||||
}
|
||||
|
||||
@PutMapping("/agent-llm-providers/{id}/models")
|
||||
@RequirePermission(value = "agent:llm-settings", description = "更新启用模型", allowSuper = true)
|
||||
public ApiResponse<AgentLlmProviderVO> upsertModels(
|
||||
@PathVariable("id") Long id,
|
||||
@Valid @RequestBody UpsertAgentLlmProviderModelsDTO dto) {
|
||||
return ApiResponse.success("保存成功", settingsService.upsertProviderModels(id, dto));
|
||||
}
|
||||
|
||||
@PostMapping("/agent-llm-settings/probe-models")
|
||||
|
||||
+2
-1
@@ -30,13 +30,14 @@ public class AgentInternalLlmController {
|
||||
@GetMapping("/llm-runtime")
|
||||
public ApiResponse<AgentLlmRuntimeVO> runtime(
|
||||
@RequestParam("companyId") Long companyId,
|
||||
@RequestParam(value = "providerId", required = false) Long providerId,
|
||||
@RequestParam(value = "model", required = false) String model,
|
||||
HttpServletRequest request) {
|
||||
assertAgentBearer(request);
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("companyId 不能为空");
|
||||
}
|
||||
return ApiResponse.success(settingsService.getRuntime(companyId, model));
|
||||
return ApiResponse.success(settingsService.getRuntime(companyId, providerId, model));
|
||||
}
|
||||
|
||||
private void assertAgentBearer(HttpServletRequest request) {
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 创建提供商。
|
||||
*/
|
||||
@Data
|
||||
public class CreateAgentLlmProviderDTO {
|
||||
|
||||
@NotBlank(message = "baseUrl 不能为空")
|
||||
private String baseUrl;
|
||||
|
||||
@NotBlank(message = "apiKey 不能为空")
|
||||
private String apiKey;
|
||||
|
||||
private String displayName;
|
||||
}
|
||||
+5
-3
@@ -8,15 +8,17 @@ import lombok.Data;
|
||||
@Data
|
||||
public class ProbeAgentLlmModelsDTO {
|
||||
|
||||
/** 必填;探测用的 Base URL */
|
||||
/** 探测用的 Base URL;useStoredKey 且传 providerId 时可空(用库中 URL) */
|
||||
private String baseUrl;
|
||||
|
||||
/**
|
||||
* 临时 Key;与 useStoredKey 二选一。
|
||||
* 留空且 useStoredKey=true 时用库中密钥。
|
||||
*/
|
||||
private String apiKey;
|
||||
|
||||
/** 为 true 时使用已保存的 apiKey(须已配置) */
|
||||
/** 为 true 时使用已保存的 apiKey(须指定 providerId) */
|
||||
private Boolean useStoredKey;
|
||||
|
||||
/** 使用已存密钥时指定提供商 */
|
||||
private Long providerId;
|
||||
}
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 更新提供商凭证(apiKey 留空则不改)。
|
||||
*/
|
||||
@Data
|
||||
public class UpdateAgentLlmProviderDTO {
|
||||
|
||||
/** 非空则更新 Base URL */
|
||||
private String baseUrl;
|
||||
|
||||
/** 非空且非掩码则更新 Key */
|
||||
private String apiKey;
|
||||
|
||||
/** 传 null 表示不改;传空串可清空备注 */
|
||||
private String displayName;
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 某提供商下启用模型列表(增量 upsert,按 model 字符串匹配)。
|
||||
*/
|
||||
@Data
|
||||
public class UpsertAgentLlmProviderModelsDTO {
|
||||
|
||||
@NotEmpty(message = "至少启用一个模型")
|
||||
@Valid
|
||||
private List<AgentLlmModelItemDTO> models;
|
||||
}
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 更新公司 LLM:凭证 + 启用模型列表(全量覆盖模型)。
|
||||
*/
|
||||
@Data
|
||||
public class UpsertAgentLlmSettingsDTO {
|
||||
|
||||
@NotBlank(message = "baseUrl 不能为空")
|
||||
private String baseUrl;
|
||||
|
||||
/** 首次必填;后续留空表示不改 Key */
|
||||
private String apiKey;
|
||||
|
||||
private String displayName;
|
||||
|
||||
@NotEmpty(message = "至少启用一个模型")
|
||||
@Valid
|
||||
private List<AgentLlmModelItemDTO> models;
|
||||
}
|
||||
+3
@@ -21,6 +21,9 @@ public class AgentCompanyLlmModelPO {
|
||||
@TableField("company_id")
|
||||
private Long companyId;
|
||||
|
||||
@TableField("provider_id")
|
||||
private Long providerId;
|
||||
|
||||
@TableField("model")
|
||||
private String model;
|
||||
|
||||
|
||||
+3
-1
@@ -4,10 +4,12 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 已启用模型条目(对外)。
|
||||
* 已启用模型条目(对外;含提供商消歧)。
|
||||
*/
|
||||
@Data
|
||||
public class AgentLlmModelVO {
|
||||
private Long providerId;
|
||||
private String providerDisplayName;
|
||||
private String model;
|
||||
private String label;
|
||||
private Integer contextWindow;
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 公司提供商列表。
|
||||
*/
|
||||
@Data
|
||||
public class AgentLlmProviderListVO {
|
||||
private List<AgentLlmProviderVO> items = new ArrayList<>();
|
||||
}
|
||||
+3
-2
@@ -6,10 +6,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 对公司管理员可见的模型配置(Key 仅掩码)。
|
||||
* 提供商(Key 仅掩码)+ 其下启用模型。
|
||||
*/
|
||||
@Data
|
||||
public class AgentLlmSettingsVO {
|
||||
public class AgentLlmProviderVO {
|
||||
private Long id;
|
||||
private boolean configured;
|
||||
private String baseUrl;
|
||||
private String displayName;
|
||||
+1
@@ -9,6 +9,7 @@ import java.util.Map;
|
||||
*/
|
||||
@Data
|
||||
public class AgentLlmRuntimeVO {
|
||||
private Long providerId;
|
||||
private String baseUrl;
|
||||
private String apiKey;
|
||||
private String model;
|
||||
|
||||
+21
-8
@@ -1,30 +1,43 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.agent.llm.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.CreateAgentLlmProviderDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.ProbeAgentLlmModelsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpsertAgentLlmSettingsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpdateAgentLlmProviderDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpsertAgentLlmProviderModelsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmEnabledModelsVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProbeModelsResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProviderListVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProviderVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmRuntimeVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmSettingsVO;
|
||||
|
||||
/**
|
||||
* 公司模型 API:凭证 + 多模型勾选。
|
||||
* 公司多提供商模型 API:凭证 CRUD + 按提供商增量 upsert 启用模型。
|
||||
*/
|
||||
public interface AgentCompanyLlmSettingsService {
|
||||
|
||||
AgentLlmSettingsVO getForCurrentCompany();
|
||||
AgentLlmProviderListVO listProvidersForCurrentCompany();
|
||||
|
||||
AgentLlmSettingsVO upsertForCurrentCompany(UpsertAgentLlmSettingsDTO dto);
|
||||
AgentLlmProviderVO getProviderForCurrentCompany(Long providerId);
|
||||
|
||||
AgentLlmProviderVO createProvider(CreateAgentLlmProviderDTO dto);
|
||||
|
||||
AgentLlmProviderVO updateProvider(Long providerId, UpdateAgentLlmProviderDTO dto);
|
||||
|
||||
void deleteProvider(Long providerId);
|
||||
|
||||
/** 按 (provider_id, model) 增量 upsert,不删再建仍保留的行 */
|
||||
AgentLlmProviderVO upsertProviderModels(Long providerId, UpsertAgentLlmProviderModelsDTO dto);
|
||||
|
||||
AgentLlmProbeModelsResultVO probeModels(ProbeAgentLlmModelsDTO dto);
|
||||
|
||||
/** 聊天用:启用模型列表(无 Key) */
|
||||
/** 聊天用:启用模型列表(无 Key,含 providerId) */
|
||||
AgentLlmEnabledModelsVO listEnabledModelsForChat();
|
||||
|
||||
/**
|
||||
* Agent 内部:运行时明文。
|
||||
*
|
||||
* @param model 空则默认模型;非空须在启用列表中
|
||||
* @param providerId 与 model 同时为空 → 公司默认;仅 model 时若唯一匹配则用之,否则须带 providerId
|
||||
* @param model 空则取默认(或该 provider 下默认/首个)
|
||||
*/
|
||||
AgentLlmRuntimeVO getRuntime(Long companyId, String model);
|
||||
AgentLlmRuntimeVO getRuntime(Long companyId, Long providerId, String model);
|
||||
}
|
||||
|
||||
+293
-115
@@ -1,6 +1,7 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.agent.llm.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -14,16 +15,18 @@ import top.xyquan.ecomdmbbackend.modules.agent.llm.config.AgentLlmProperties;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.mapper.AgentCompanyLlmModelMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.mapper.AgentCompanyLlmProviderMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.AgentLlmModelItemDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.CreateAgentLlmProviderDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.ProbeAgentLlmModelsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpsertAgentLlmSettingsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpdateAgentLlmProviderDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.dto.UpsertAgentLlmProviderModelsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.po.AgentCompanyLlmModelPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.po.AgentCompanyLlmProviderPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmEnabledModelsVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmModelVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProbeModelsResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProbedModelVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProviderListVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmProviderVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmRuntimeVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.model.vo.AgentLlmSettingsVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.service.AgentCompanyLlmSettingsService;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.support.AgentLlmCrypto;
|
||||
import top.xyquan.ecomdmbbackend.modules.agent.llm.support.OpenAiCompatibleModelsProbe;
|
||||
@@ -34,12 +37,15 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 公司模型 API:MySQL 存密文与启用模型;Redis 按模型缓存明文供 Agent。
|
||||
* 公司多提供商模型 API:MySQL 存密文;Redis 按 company+provider+model 缓存明文。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -58,101 +64,177 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
private final OpenAiCompatibleModelsProbe modelsProbe;
|
||||
|
||||
@Override
|
||||
public AgentLlmSettingsVO getForCurrentCompany() {
|
||||
public AgentLlmProviderListVO listProvidersForCurrentCompany() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
return toPublicVo(findProvider(companyId), listModels(companyId));
|
||||
AgentLlmProviderListVO vo = new AgentLlmProviderListVO();
|
||||
for (AgentCompanyLlmProviderPO p : listProviders(companyId)) {
|
||||
vo.getItems().add(toProviderVo(p, listModelsByProvider(companyId, p.getId())));
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AgentLlmProviderVO getProviderForCurrentCompany(Long providerId) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
AgentCompanyLlmProviderPO provider = requireOwnedProvider(companyId, providerId);
|
||||
return toProviderVo(provider, listModelsByProvider(companyId, providerId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public AgentLlmSettingsVO upsertForCurrentCompany(UpsertAgentLlmSettingsDTO dto) {
|
||||
public AgentLlmProviderVO createProvider(CreateAgentLlmProviderDTO dto) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
|
||||
String baseUrl = dto.getBaseUrl().trim().replaceAll("/+$", "");
|
||||
List<AgentLlmModelItemDTO> modelItems = normalizeModelItems(dto.getModels());
|
||||
|
||||
AgentCompanyLlmProviderPO existing = findProvider(companyId);
|
||||
String encrypted;
|
||||
String suffix;
|
||||
String apiKey = dto.getApiKey();
|
||||
if (StringUtils.hasText(apiKey) && !apiKey.trim().startsWith("****")) {
|
||||
String plain = apiKey.trim();
|
||||
encrypted = crypto.encrypt(plain);
|
||||
suffix = plain.length() >= 4 ? plain.substring(plain.length() - 4) : plain;
|
||||
} else if (existing != null) {
|
||||
encrypted = existing.getApiKeyEncrypted();
|
||||
suffix = existing.getApiKeySuffix();
|
||||
} else {
|
||||
throw new BusinessException("首次配置必须提供 apiKey");
|
||||
String plain = dto.getApiKey().trim();
|
||||
if (!StringUtils.hasText(plain) || plain.startsWith("****")) {
|
||||
throw new BusinessException("创建提供商必须填写有效 apiKey");
|
||||
}
|
||||
|
||||
if (existing == null) {
|
||||
AgentCompanyLlmProviderPO po = new AgentCompanyLlmProviderPO();
|
||||
po.setCompanyId(companyId);
|
||||
po.setBaseUrl(baseUrl);
|
||||
po.setApiKeyEncrypted(encrypted);
|
||||
po.setApiKeySuffix(suffix);
|
||||
po.setDisplayName(trimToNull(dto.getDisplayName()));
|
||||
po.setCreateBy(userId);
|
||||
po.setUpdateBy(userId);
|
||||
providerMapper.insert(po);
|
||||
existing = po;
|
||||
} else {
|
||||
existing.setBaseUrl(baseUrl);
|
||||
existing.setApiKeyEncrypted(encrypted);
|
||||
existing.setApiKeySuffix(suffix);
|
||||
if (dto.getDisplayName() != null) {
|
||||
existing.setDisplayName(trimToNull(dto.getDisplayName()));
|
||||
}
|
||||
existing.setUpdateBy(userId);
|
||||
providerMapper.updateById(existing);
|
||||
}
|
||||
|
||||
modelMapper.delete(new LambdaQueryWrapper<AgentCompanyLlmModelPO>()
|
||||
.eq(AgentCompanyLlmModelPO::getCompanyId, companyId));
|
||||
for (AgentLlmModelItemDTO item : modelItems) {
|
||||
AgentCompanyLlmModelPO m = new AgentCompanyLlmModelPO();
|
||||
m.setCompanyId(companyId);
|
||||
m.setModel(item.getModel().trim());
|
||||
m.setLabel(trimToNull(item.getLabel()));
|
||||
m.setContextWindow(normalizeContextWindow(item.getContextWindow()));
|
||||
m.setIsDefault(Boolean.TRUE.equals(item.getIsDefault()));
|
||||
m.setCreateBy(userId);
|
||||
m.setUpdateBy(userId);
|
||||
modelMapper.insert(m);
|
||||
}
|
||||
|
||||
List<AgentCompanyLlmModelPO> savedModels = listModels(companyId);
|
||||
AgentCompanyLlmProviderPO po = new AgentCompanyLlmProviderPO();
|
||||
po.setCompanyId(companyId);
|
||||
po.setBaseUrl(baseUrl);
|
||||
po.setApiKeyEncrypted(crypto.encrypt(plain));
|
||||
po.setApiKeySuffix(plain.length() >= 4 ? plain.substring(plain.length() - 4) : plain);
|
||||
po.setDisplayName(trimToNull(dto.getDisplayName()));
|
||||
po.setCreateBy(userId);
|
||||
po.setUpdateBy(userId);
|
||||
providerMapper.insert(po);
|
||||
evictRuntimeCache(companyId);
|
||||
try {
|
||||
for (AgentCompanyLlmModelPO m : savedModels) {
|
||||
cacheRuntime(companyId, buildRuntimeForModel(existing, m));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("预热 LLM 运行时缓存失败 companyId={}", companyId, e);
|
||||
return toProviderVo(po, List.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public AgentLlmProviderVO updateProvider(Long providerId, UpdateAgentLlmProviderDTO dto) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
AgentCompanyLlmProviderPO existing = requireOwnedProvider(companyId, providerId);
|
||||
|
||||
if (StringUtils.hasText(dto.getBaseUrl())) {
|
||||
existing.setBaseUrl(dto.getBaseUrl().trim().replaceAll("/+$", ""));
|
||||
}
|
||||
return toPublicVo(existing, savedModels);
|
||||
if (StringUtils.hasText(dto.getApiKey()) && !dto.getApiKey().trim().startsWith("****")) {
|
||||
String plain = dto.getApiKey().trim();
|
||||
existing.setApiKeyEncrypted(crypto.encrypt(plain));
|
||||
existing.setApiKeySuffix(plain.length() >= 4 ? plain.substring(plain.length() - 4) : plain);
|
||||
}
|
||||
if (dto.getDisplayName() != null) {
|
||||
existing.setDisplayName(trimToNull(dto.getDisplayName()));
|
||||
}
|
||||
existing.setUpdateBy(userId);
|
||||
providerMapper.updateById(existing);
|
||||
evictRuntimeCache(companyId);
|
||||
warmCacheForProvider(existing);
|
||||
return toProviderVo(existing, listModelsByProvider(companyId, providerId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteProvider(Long providerId) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
requireOwnedProvider(companyId, providerId);
|
||||
boolean hadDefault = listModelsByProvider(companyId, providerId).stream()
|
||||
.anyMatch(m -> Boolean.TRUE.equals(m.getIsDefault()));
|
||||
// 模型 FK CASCADE
|
||||
providerMapper.deleteById(providerId);
|
||||
if (hadDefault) {
|
||||
ensureCompanyHasDefault(companyId, currentUserContext.getCurrentUserId());
|
||||
}
|
||||
evictRuntimeCache(companyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public AgentLlmProviderVO upsertProviderModels(Long providerId, UpsertAgentLlmProviderModelsDTO dto) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
AgentCompanyLlmProviderPO provider = requireOwnedProvider(companyId, providerId);
|
||||
|
||||
List<AgentLlmModelItemDTO> items = normalizeModelItemsForProvider(dto.getModels());
|
||||
Map<String, AgentCompanyLlmModelPO> existingByModel = listModelsByProvider(companyId, providerId)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(AgentCompanyLlmModelPO::getModel, m -> m, (a, b) -> a, LinkedHashMap::new));
|
||||
|
||||
Set<String> keep = new HashSet<>();
|
||||
boolean requestSetsDefault = items.stream().anyMatch(i -> Boolean.TRUE.equals(i.getIsDefault()));
|
||||
Long newDefaultModelRowId = null;
|
||||
|
||||
for (AgentLlmModelItemDTO item : items) {
|
||||
String modelId = item.getModel().trim();
|
||||
keep.add(modelId);
|
||||
AgentCompanyLlmModelPO row = existingByModel.get(modelId);
|
||||
if (row == null) {
|
||||
row = new AgentCompanyLlmModelPO();
|
||||
row.setCompanyId(companyId);
|
||||
row.setProviderId(providerId);
|
||||
row.setModel(modelId);
|
||||
row.setLabel(trimToNull(item.getLabel()));
|
||||
row.setContextWindow(normalizeContextWindow(item.getContextWindow()));
|
||||
row.setIsDefault(false);
|
||||
row.setCreateBy(userId);
|
||||
row.setUpdateBy(userId);
|
||||
modelMapper.insert(row);
|
||||
} else {
|
||||
row.setLabel(trimToNull(item.getLabel()));
|
||||
row.setContextWindow(normalizeContextWindow(item.getContextWindow()));
|
||||
row.setUpdateBy(userId);
|
||||
modelMapper.updateById(row);
|
||||
}
|
||||
if (Boolean.TRUE.equals(item.getIsDefault())) {
|
||||
newDefaultModelRowId = row.getId();
|
||||
}
|
||||
}
|
||||
|
||||
for (AgentCompanyLlmModelPO old : existingByModel.values()) {
|
||||
if (!keep.contains(old.getModel())) {
|
||||
modelMapper.deleteById(old.getId());
|
||||
}
|
||||
}
|
||||
|
||||
if (requestSetsDefault && newDefaultModelRowId != null) {
|
||||
clearCompanyDefaults(companyId);
|
||||
modelMapper.update(null, new LambdaUpdateWrapper<AgentCompanyLlmModelPO>()
|
||||
.eq(AgentCompanyLlmModelPO::getId, newDefaultModelRowId)
|
||||
.set(AgentCompanyLlmModelPO::getIsDefault, true)
|
||||
.set(AgentCompanyLlmModelPO::getUpdateBy, userId));
|
||||
} else {
|
||||
ensureCompanyHasDefault(companyId, userId);
|
||||
}
|
||||
|
||||
List<AgentCompanyLlmModelPO> saved = listModelsByProvider(companyId, providerId);
|
||||
evictRuntimeCache(companyId);
|
||||
warmCacheForProvider(provider);
|
||||
return toProviderVo(provider, saved);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AgentLlmProbeModelsResultVO probeModels(ProbeAgentLlmModelsDTO dto) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (dto == null || !StringUtils.hasText(dto.getBaseUrl())) {
|
||||
throw new BusinessException("baseUrl 不能为空");
|
||||
if (dto == null) {
|
||||
throw new BusinessException("请求体不能为空");
|
||||
}
|
||||
String baseUrl = dto.getBaseUrl().trim().replaceAll("/+$", "");
|
||||
|
||||
String baseUrl;
|
||||
String apiKey;
|
||||
if (Boolean.TRUE.equals(dto.getUseStoredKey())) {
|
||||
AgentCompanyLlmProviderPO provider = findProvider(companyId);
|
||||
if (provider == null) {
|
||||
throw new BusinessException("尚未保存 API Key,请先填写密钥或关闭「使用已存密钥」");
|
||||
if (dto.getProviderId() == null) {
|
||||
throw new BusinessException("使用已存密钥时须指定 providerId");
|
||||
}
|
||||
AgentCompanyLlmProviderPO provider = requireOwnedProvider(companyId, dto.getProviderId());
|
||||
baseUrl = StringUtils.hasText(dto.getBaseUrl())
|
||||
? dto.getBaseUrl().trim().replaceAll("/+$", "")
|
||||
: provider.getBaseUrl();
|
||||
apiKey = crypto.decrypt(provider.getApiKeyEncrypted());
|
||||
} else if (StringUtils.hasText(dto.getApiKey()) && !dto.getApiKey().trim().startsWith("****")) {
|
||||
apiKey = dto.getApiKey().trim();
|
||||
} else {
|
||||
throw new BusinessException("请填写 apiKey,或勾选使用已保存的密钥");
|
||||
if (!StringUtils.hasText(dto.getBaseUrl())) {
|
||||
throw new BusinessException("baseUrl 不能为空");
|
||||
}
|
||||
baseUrl = dto.getBaseUrl().trim().replaceAll("/+$", "");
|
||||
if (!StringUtils.hasText(dto.getApiKey()) || dto.getApiKey().trim().startsWith("****")) {
|
||||
throw new BusinessException("请填写 apiKey,或勾选使用已保存的密钥");
|
||||
}
|
||||
apiKey = dto.getApiKey().trim();
|
||||
}
|
||||
|
||||
AgentLlmProbeModelsResultVO vo = new AgentLlmProbeModelsResultVO();
|
||||
@@ -163,44 +245,34 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
@Override
|
||||
public AgentLlmEnabledModelsVO listEnabledModelsForChat() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Map<Long, AgentCompanyLlmProviderPO> providers = listProviders(companyId).stream()
|
||||
.collect(Collectors.toMap(AgentCompanyLlmProviderPO::getId, p -> p));
|
||||
AgentLlmEnabledModelsVO vo = new AgentLlmEnabledModelsVO();
|
||||
List<AgentLlmModelVO> items = new ArrayList<>();
|
||||
for (AgentCompanyLlmModelPO m : listModels(companyId)) {
|
||||
items.add(toModelVo(m));
|
||||
items.add(toModelVo(m, providers.get(m.getProviderId())));
|
||||
}
|
||||
vo.setItems(items);
|
||||
return vo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AgentLlmRuntimeVO getRuntime(Long companyId, String model) {
|
||||
public AgentLlmRuntimeVO getRuntime(Long companyId, Long providerId, String model) {
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("companyId 不能为空");
|
||||
}
|
||||
AgentCompanyLlmProviderPO provider = findProvider(companyId);
|
||||
if (provider == null) {
|
||||
throw new BusinessException("公司尚未配置模型 API");
|
||||
}
|
||||
List<AgentCompanyLlmModelPO> models = listModels(companyId);
|
||||
if (models.isEmpty()) {
|
||||
throw new BusinessException("公司尚未启用任何模型");
|
||||
}
|
||||
|
||||
AgentCompanyLlmModelPO selected;
|
||||
if (StringUtils.hasText(model)) {
|
||||
String want = model.trim();
|
||||
selected = models.stream()
|
||||
.filter(m -> want.equals(m.getModel()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new BusinessException("模型未启用或不存在: " + want));
|
||||
} else {
|
||||
selected = models.stream()
|
||||
.filter(m -> Boolean.TRUE.equals(m.getIsDefault()))
|
||||
.findFirst()
|
||||
.orElse(models.get(0));
|
||||
AgentCompanyLlmModelPO selected = resolveModelRow(models, providerId, model);
|
||||
AgentCompanyLlmProviderPO provider = providerMapper.selectById(selected.getProviderId());
|
||||
if (provider == null || !Objects.equals(provider.getCompanyId(), companyId)) {
|
||||
throw new BusinessException("提供商不存在");
|
||||
}
|
||||
|
||||
AgentLlmRuntimeVO cached = readRuntimeCache(companyId, selected.getModel());
|
||||
AgentLlmRuntimeVO cached = readRuntimeCache(companyId, provider.getId(), selected.getModel());
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
@@ -209,7 +281,49 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
return runtime;
|
||||
}
|
||||
|
||||
private List<AgentLlmModelItemDTO> normalizeModelItems(List<AgentLlmModelItemDTO> raw) {
|
||||
private AgentCompanyLlmModelPO resolveModelRow(
|
||||
List<AgentCompanyLlmModelPO> models, Long providerId, String model) {
|
||||
boolean hasModel = StringUtils.hasText(model);
|
||||
String want = hasModel ? model.trim() : null;
|
||||
|
||||
if (providerId != null && hasModel) {
|
||||
return models.stream()
|
||||
.filter(m -> providerId.equals(m.getProviderId()) && want.equals(m.getModel()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new BusinessException(
|
||||
"模型未启用或不存在: providerId=" + providerId + ", model=" + want));
|
||||
}
|
||||
if (providerId != null) {
|
||||
List<AgentCompanyLlmModelPO> under = models.stream()
|
||||
.filter(m -> providerId.equals(m.getProviderId()))
|
||||
.toList();
|
||||
if (under.isEmpty()) {
|
||||
throw new BusinessException("该提供商尚未启用任何模型");
|
||||
}
|
||||
return under.stream()
|
||||
.filter(m -> Boolean.TRUE.equals(m.getIsDefault()))
|
||||
.findFirst()
|
||||
.orElse(under.get(0));
|
||||
}
|
||||
if (hasModel) {
|
||||
List<AgentCompanyLlmModelPO> matched = models.stream()
|
||||
.filter(m -> want.equals(m.getModel()))
|
||||
.toList();
|
||||
if (matched.isEmpty()) {
|
||||
throw new BusinessException("模型未启用或不存在: " + want);
|
||||
}
|
||||
if (matched.size() > 1) {
|
||||
throw new BusinessException("存在同名模型,请指定 providerId: " + want);
|
||||
}
|
||||
return matched.get(0);
|
||||
}
|
||||
return models.stream()
|
||||
.filter(m -> Boolean.TRUE.equals(m.getIsDefault()))
|
||||
.findFirst()
|
||||
.orElse(models.get(0));
|
||||
}
|
||||
|
||||
private List<AgentLlmModelItemDTO> normalizeModelItemsForProvider(List<AgentLlmModelItemDTO> raw) {
|
||||
if (raw == null || raw.isEmpty()) {
|
||||
throw new BusinessException("至少启用一个模型");
|
||||
}
|
||||
@@ -237,9 +351,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
if (out.isEmpty()) {
|
||||
throw new BusinessException("至少启用一个模型");
|
||||
}
|
||||
if (defaultIdx < 0) {
|
||||
out.get(0).setIsDefault(true);
|
||||
} else {
|
||||
if (defaultIdx >= 0) {
|
||||
for (int i = 0; i < out.size(); i++) {
|
||||
out.get(i).setIsDefault(i == defaultIdx);
|
||||
}
|
||||
@@ -247,9 +359,33 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
return out;
|
||||
}
|
||||
|
||||
private void clearCompanyDefaults(Long companyId) {
|
||||
modelMapper.update(null, new LambdaUpdateWrapper<AgentCompanyLlmModelPO>()
|
||||
.eq(AgentCompanyLlmModelPO::getCompanyId, companyId)
|
||||
.eq(AgentCompanyLlmModelPO::getIsDefault, true)
|
||||
.set(AgentCompanyLlmModelPO::getIsDefault, false));
|
||||
}
|
||||
|
||||
private void ensureCompanyHasDefault(Long companyId, Long userId) {
|
||||
List<AgentCompanyLlmModelPO> all = listModels(companyId);
|
||||
if (all.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
boolean has = all.stream().anyMatch(m -> Boolean.TRUE.equals(m.getIsDefault()));
|
||||
if (has) {
|
||||
return;
|
||||
}
|
||||
AgentCompanyLlmModelPO first = all.get(0);
|
||||
modelMapper.update(null, new LambdaUpdateWrapper<AgentCompanyLlmModelPO>()
|
||||
.eq(AgentCompanyLlmModelPO::getId, first.getId())
|
||||
.set(AgentCompanyLlmModelPO::getIsDefault, true)
|
||||
.set(AgentCompanyLlmModelPO::getUpdateBy, userId));
|
||||
}
|
||||
|
||||
private AgentLlmRuntimeVO buildRuntimeForModel(
|
||||
AgentCompanyLlmProviderPO provider, AgentCompanyLlmModelPO model) {
|
||||
AgentLlmRuntimeVO vo = new AgentLlmRuntimeVO();
|
||||
vo.setProviderId(provider.getId());
|
||||
vo.setBaseUrl(provider.getBaseUrl());
|
||||
vo.setApiKey(crypto.decrypt(provider.getApiKeyEncrypted()));
|
||||
vo.setModel(model.getModel());
|
||||
@@ -258,6 +394,17 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
return vo;
|
||||
}
|
||||
|
||||
private void warmCacheForProvider(AgentCompanyLlmProviderPO provider) {
|
||||
try {
|
||||
for (AgentCompanyLlmModelPO m : listModelsByProvider(provider.getCompanyId(), provider.getId())) {
|
||||
cacheRuntime(provider.getCompanyId(), buildRuntimeForModel(provider, m));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("预热 LLM 运行时缓存失败 companyId={} providerId={}",
|
||||
provider.getCompanyId(), provider.getId(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Integer normalizeContextWindow(Integer raw) {
|
||||
if (raw == null || raw <= 0) {
|
||||
return null;
|
||||
@@ -285,21 +432,48 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
}
|
||||
}
|
||||
|
||||
private AgentCompanyLlmProviderPO findProvider(Long companyId) {
|
||||
return providerMapper.selectOne(new LambdaQueryWrapper<AgentCompanyLlmProviderPO>()
|
||||
private AgentCompanyLlmProviderPO requireOwnedProvider(Long companyId, Long providerId) {
|
||||
if (providerId == null) {
|
||||
throw new BusinessException("providerId 不能为空");
|
||||
}
|
||||
AgentCompanyLlmProviderPO provider = providerMapper.selectById(providerId);
|
||||
if (provider == null || !Objects.equals(provider.getCompanyId(), companyId)) {
|
||||
throw new BusinessException("提供商不存在");
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
|
||||
private List<AgentCompanyLlmProviderPO> listProviders(Long companyId) {
|
||||
return providerMapper.selectList(new LambdaQueryWrapper<AgentCompanyLlmProviderPO>()
|
||||
.eq(AgentCompanyLlmProviderPO::getCompanyId, companyId)
|
||||
.last("LIMIT 1"));
|
||||
.orderByAsc(AgentCompanyLlmProviderPO::getId));
|
||||
}
|
||||
|
||||
private List<AgentCompanyLlmModelPO> listModels(Long companyId) {
|
||||
return modelMapper.selectList(new LambdaQueryWrapper<AgentCompanyLlmModelPO>()
|
||||
.eq(AgentCompanyLlmModelPO::getCompanyId, companyId)
|
||||
.orderByDesc(AgentCompanyLlmModelPO::getIsDefault)
|
||||
.orderByAsc(AgentCompanyLlmModelPO::getProviderId)
|
||||
.orderByAsc(AgentCompanyLlmModelPO::getId));
|
||||
}
|
||||
|
||||
private AgentLlmModelVO toModelVo(AgentCompanyLlmModelPO m) {
|
||||
private List<AgentCompanyLlmModelPO> listModelsByProvider(Long companyId, Long providerId) {
|
||||
return modelMapper.selectList(new LambdaQueryWrapper<AgentCompanyLlmModelPO>()
|
||||
.eq(AgentCompanyLlmModelPO::getCompanyId, companyId)
|
||||
.eq(AgentCompanyLlmModelPO::getProviderId, providerId)
|
||||
.orderByDesc(AgentCompanyLlmModelPO::getIsDefault)
|
||||
.orderByAsc(AgentCompanyLlmModelPO::getId));
|
||||
}
|
||||
|
||||
private AgentLlmModelVO toModelVo(AgentCompanyLlmModelPO m, AgentCompanyLlmProviderPO provider) {
|
||||
AgentLlmModelVO mv = new AgentLlmModelVO();
|
||||
mv.setProviderId(m.getProviderId());
|
||||
if (provider != null) {
|
||||
mv.setProviderDisplayName(
|
||||
StringUtils.hasText(provider.getDisplayName())
|
||||
? provider.getDisplayName()
|
||||
: provider.getBaseUrl());
|
||||
}
|
||||
mv.setModel(m.getModel());
|
||||
mv.setLabel(m.getLabel());
|
||||
mv.setContextWindow(m.getContextWindow());
|
||||
@@ -307,13 +481,10 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
return mv;
|
||||
}
|
||||
|
||||
private AgentLlmSettingsVO toPublicVo(
|
||||
private AgentLlmProviderVO toProviderVo(
|
||||
AgentCompanyLlmProviderPO provider, List<AgentCompanyLlmModelPO> models) {
|
||||
AgentLlmSettingsVO vo = new AgentLlmSettingsVO();
|
||||
if (provider == null) {
|
||||
vo.setConfigured(false);
|
||||
return vo;
|
||||
}
|
||||
AgentLlmProviderVO vo = new AgentLlmProviderVO();
|
||||
vo.setId(provider.getId());
|
||||
vo.setConfigured(true);
|
||||
vo.setBaseUrl(provider.getBaseUrl());
|
||||
vo.setDisplayName(provider.getDisplayName());
|
||||
@@ -323,19 +494,20 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
}
|
||||
List<AgentLlmModelVO> modelVos = new ArrayList<>();
|
||||
for (AgentCompanyLlmModelPO m : models) {
|
||||
modelVos.add(toModelVo(m));
|
||||
modelVos.add(toModelVo(m, provider));
|
||||
}
|
||||
vo.setModels(modelVos);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private static String redisKey(Long companyId, String model) {
|
||||
return "agent:llm:rt:" + companyId + ":" + model;
|
||||
private static String redisKey(Long companyId, Long providerId, String model) {
|
||||
return "agent:llm:rt:" + companyId + ":" + providerId + ":" + model;
|
||||
}
|
||||
|
||||
private void cacheRuntime(Long companyId, AgentLlmRuntimeVO runtime) {
|
||||
try {
|
||||
Map<String, Object> payload = new HashMap<>();
|
||||
payload.put("provider_id", runtime.getProviderId());
|
||||
payload.put("base_url", runtime.getBaseUrl());
|
||||
payload.put("api_key", runtime.getApiKey());
|
||||
payload.put("model", runtime.getModel());
|
||||
@@ -343,7 +515,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
payload.put("extra_headers",
|
||||
runtime.getExtraHeaders() == null ? Map.of() : runtime.getExtraHeaders());
|
||||
stringRedisTemplate.opsForValue().set(
|
||||
redisKey(companyId, runtime.getModel()),
|
||||
redisKey(companyId, runtime.getProviderId(), runtime.getModel()),
|
||||
objectMapper.writeValueAsString(payload),
|
||||
Duration.ofSeconds(Math.max(60, llmProperties.getRuntimeCacheTtlSeconds())));
|
||||
} catch (Exception e) {
|
||||
@@ -351,15 +523,21 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
}
|
||||
}
|
||||
|
||||
private AgentLlmRuntimeVO readRuntimeCache(Long companyId, String model) {
|
||||
private AgentLlmRuntimeVO readRuntimeCache(Long companyId, Long providerId, String model) {
|
||||
try {
|
||||
String raw = stringRedisTemplate.opsForValue().get(redisKey(companyId, model));
|
||||
String raw = stringRedisTemplate.opsForValue().get(redisKey(companyId, providerId, model));
|
||||
if (!StringUtils.hasText(raw)) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> map = objectMapper.readValue(raw, new TypeReference<>() {
|
||||
});
|
||||
AgentLlmRuntimeVO vo = new AgentLlmRuntimeVO();
|
||||
Object pid = map.get("provider_id");
|
||||
if (pid instanceof Number n) {
|
||||
vo.setProviderId(n.longValue());
|
||||
} else {
|
||||
vo.setProviderId(providerId);
|
||||
}
|
||||
vo.setBaseUrl(String.valueOf(map.get("base_url")));
|
||||
vo.setApiKey(String.valueOf(map.get("api_key")));
|
||||
vo.setModel(String.valueOf(map.get("model")));
|
||||
|
||||
Reference in New Issue
Block a user