mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:读取智能体模型配置
This commit is contained in:
parent
325c570f2b
commit
34526e2b1b
19 changed files with 215 additions and 27 deletions
|
|
@ -71,6 +71,14 @@ public class SwaggerConfig {
|
|||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GroupedOpenApi configApi() {
|
||||
return GroupedOpenApi.builder()
|
||||
.group("config")
|
||||
.pathsToMatch("/config/**")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OpenAPI customOpenAPI() {
|
||||
return new OpenAPI().info(new Info()
|
||||
|
|
|
|||
|
|
@ -38,7 +38,14 @@ public class RedisKeys {
|
|||
* 模型名称的Key
|
||||
*/
|
||||
public static String getModelNameById(String id) {
|
||||
return "sys:model:name:" + id;
|
||||
return "model:name:" + id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模型配置的Key
|
||||
*/
|
||||
public static String getModelConfigById(String id) {
|
||||
return "model:data:" + id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -54,4 +61,5 @@ public class RedisKeys {
|
|||
public static String getAgentDeviceCountById(String id) {
|
||||
return "agent:device:count:" + id;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -191,4 +191,5 @@ public class AgentController {
|
|||
.list(new QueryWrapper<AgentTemplateEntity>().orderByAsc("sort"));
|
||||
return new Result<List<AgentTemplateEntity>>().ok(list);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -42,4 +42,12 @@ public interface AgentService extends BaseService<AgentEntity> {
|
|||
* @return 设备数量
|
||||
*/
|
||||
Integer getDeviceCountByAgentId(String agentId);
|
||||
|
||||
/**
|
||||
* 根据设备MAC地址获取智能体模型配置
|
||||
*
|
||||
* @param macAddress 设备MAC地址
|
||||
* @return 模型配置信息
|
||||
*/
|
||||
Map<String, Object> getAgentModelsByMac(String macAddress);
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package xiaozhi.modules.agent.service.impl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
|
@ -13,6 +14,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.common.page.PageData;
|
||||
import xiaozhi.common.redis.RedisKeys;
|
||||
import xiaozhi.common.redis.RedisUtils;
|
||||
|
|
@ -21,6 +23,9 @@ import xiaozhi.modules.agent.dao.AgentDao;
|
|||
import xiaozhi.modules.agent.dto.AgentDTO;
|
||||
import xiaozhi.modules.agent.entity.AgentEntity;
|
||||
import xiaozhi.modules.agent.service.AgentService;
|
||||
import xiaozhi.modules.device.entity.DeviceEntity;
|
||||
import xiaozhi.modules.device.service.DeviceService;
|
||||
import xiaozhi.modules.model.entity.ModelConfigEntity;
|
||||
import xiaozhi.modules.model.service.ModelConfigService;
|
||||
import xiaozhi.modules.timbre.service.TimbreService;
|
||||
|
||||
|
|
@ -37,6 +42,9 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
|||
@Autowired
|
||||
private RedisUtils redisUtils;
|
||||
|
||||
@Autowired
|
||||
private DeviceService deviceService;
|
||||
|
||||
public AgentServiceImpl(AgentDao agentDao) {
|
||||
this.agentDao = agentDao;
|
||||
}
|
||||
|
|
@ -130,4 +138,42 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
|||
|
||||
return deviceCount != null ? deviceCount : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAgentModelsByMac(String macAddress) {
|
||||
|
||||
// 根据MAC地址查找设备
|
||||
DeviceEntity device = deviceService.getDeviceByMacAddress(macAddress);
|
||||
if (device == null) {
|
||||
throw new RenException("设备未找到");
|
||||
}
|
||||
|
||||
// 获取智能体信息
|
||||
AgentEntity agent = getAgentById(device.getAgentId());
|
||||
if (agent == null) {
|
||||
throw new RenException("智能体未找到");
|
||||
}
|
||||
|
||||
// 构建模块选择
|
||||
Map<String, String> selectedModule = new HashMap<>();
|
||||
|
||||
// 构建返回数据
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("prompt", agent.getSystemPrompt());
|
||||
|
||||
String[] modelTypes = { "VAD", "Intent", "Memory", "ASR", "LLM", "TTS" };
|
||||
String[] modelIds = { agent.getVadModelId(), agent.getIntentModelId(), agent.getMemModelId(),
|
||||
agent.getAsrModelId(), agent.getLlmModelId(), agent.getTtsModelId() };
|
||||
|
||||
for (int i = 0; i < modelIds.length; i++) {
|
||||
ModelConfigEntity model = modelConfigService.getModelById(modelIds[i]);
|
||||
Map<String, Object> typeConfig = new HashMap<>();
|
||||
typeConfig.put(model.getModelCode(), model.getConfigJson());
|
||||
result.put(modelTypes[i], typeConfig);
|
||||
selectedModule.put(modelTypes[i], model.getModelCode());
|
||||
}
|
||||
result.put("selected_module", selectedModule);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package xiaozhi.modules.config.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
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.RestController;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.AllArgsConstructor;
|
||||
import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.common.utils.Result;
|
||||
import xiaozhi.common.validator.ValidatorUtils;
|
||||
import xiaozhi.modules.agent.service.AgentService;
|
||||
import xiaozhi.modules.config.dto.AgentModelsDTO;
|
||||
import xiaozhi.modules.sys.dto.ConfigSecretDTO;
|
||||
import xiaozhi.modules.sys.service.SysParamsService;
|
||||
|
||||
/**
|
||||
* xiaozhi-server 配置获取
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("config")
|
||||
@Tag(name = "参数管理")
|
||||
@AllArgsConstructor
|
||||
public class ConfigController {
|
||||
private final SysParamsService sysParamsService;
|
||||
private final AgentService agentService;
|
||||
|
||||
@PostMapping("server-base")
|
||||
@Operation(summary = "获取配置")
|
||||
public Result<Object> getConfig(@RequestBody ConfigSecretDTO dto) {
|
||||
// 效验数据
|
||||
ValidatorUtils.validateEntity(dto);
|
||||
checkSecret(dto.getSecret());
|
||||
Object config = sysParamsService.getConfig();
|
||||
return new Result<Object>().ok(config);
|
||||
}
|
||||
|
||||
@PostMapping("agent-models")
|
||||
@Operation(summary = "获取智能体模型")
|
||||
public Result<Object> getAgentModels(@RequestBody AgentModelsDTO dto) {
|
||||
// 效验数据
|
||||
ValidatorUtils.validateEntity(dto);
|
||||
checkSecret(dto.getSecret());
|
||||
Map<String, Object> models = agentService.getAgentModelsByMac(dto.getMacAddress());
|
||||
return new Result<Object>().ok(models);
|
||||
}
|
||||
|
||||
private void checkSecret(String secret) {
|
||||
String secretParam = sysParamsService.getValue(Constant.SERVER_SECRET, true);
|
||||
// 验证密钥
|
||||
if (StringUtils.isBlank(secret) || !secret.equals(secretParam)) {
|
||||
throw new RenException("密钥错误");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package xiaozhi.modules.config.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Schema(description = "获取智能体模型配置DTO")
|
||||
public class AgentModelsDTO {
|
||||
@NotBlank(message = "密钥不能为空")
|
||||
@Schema(description = "密钥")
|
||||
private String secret;
|
||||
|
||||
@NotBlank(message = "设备MAC地址不能为空")
|
||||
@Schema(description = "设备MAC地址")
|
||||
private String macAddress;
|
||||
|
||||
@NotBlank(message = "客户端ID不能为空")
|
||||
@Schema(description = "客户端ID")
|
||||
private String clientId;
|
||||
}
|
||||
|
|
@ -66,4 +66,12 @@ public interface DeviceService {
|
|||
* @return 用户列表分页数据
|
||||
*/
|
||||
PageData<UserShowDeviceListVO> page(DevicePageUserDTO dto);
|
||||
|
||||
/**
|
||||
* 根据MAC地址获取设备信息
|
||||
*
|
||||
* @param macAddress MAC地址
|
||||
* @return 设备信息
|
||||
*/
|
||||
DeviceEntity getDeviceByMacAddress(String macAddress);
|
||||
}
|
||||
|
|
@ -240,6 +240,16 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
|||
return new PageData<>(list, page.getTotal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeviceEntity getDeviceByMacAddress(String macAddress) {
|
||||
if (StringUtils.isBlank(macAddress)) {
|
||||
return null;
|
||||
}
|
||||
QueryWrapper<DeviceEntity> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("mac_address", macAddress);
|
||||
return baseDao.selectOne(wrapper);
|
||||
}
|
||||
|
||||
private DeviceReportRespDTO.ServerTime buildServerTime() {
|
||||
DeviceReportRespDTO.ServerTime serverTime = new DeviceReportRespDTO.ServerTime();
|
||||
TimeZone tz = TimeZone.getDefault();
|
||||
|
|
|
|||
|
|
@ -28,4 +28,12 @@ public interface ModelConfigService extends BaseService<ModelConfigEntity> {
|
|||
* @return 模型名称
|
||||
*/
|
||||
String getModelNameById(String id);
|
||||
|
||||
/**
|
||||
* 根据ID获取模型配置
|
||||
*
|
||||
* @param id 模型ID
|
||||
* @return 模型配置实体
|
||||
*/
|
||||
ModelConfigEntity getModelById(String id);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -125,4 +125,20 @@ public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, Mode
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelConfigEntity getModelById(String id) {
|
||||
if (StringUtils.isBlank(id)) {
|
||||
return null;
|
||||
}
|
||||
ModelConfigEntity cachedConfig = (ModelConfigEntity) redisUtils.get(RedisKeys.getModelConfigById(id));
|
||||
if (cachedConfig != null) {
|
||||
return ConvertUtils.sourceToTarget(cachedConfig, ModelConfigEntity.class);
|
||||
}
|
||||
ModelConfigEntity entity = modelConfigDao.selectById(id);
|
||||
if (entity != null) {
|
||||
redisUtils.set(RedisKeys.getModelConfigById(id), entity);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,7 +77,8 @@ public class ShiroConfig {
|
|||
filterMap.put("/user/captcha", "anon");
|
||||
filterMap.put("/user/login", "anon");
|
||||
filterMap.put("/user/register", "anon");
|
||||
filterMap.put("/admin/params/config", "anon");
|
||||
filterMap.put("/config/server-base", "anon");
|
||||
filterMap.put("/config/agent-models", "anon");
|
||||
filterMap.put("/**", "oauth2");
|
||||
shiroFilter.setFilterChainDefinitionMap(filterMap);
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ import xiaozhi.common.validator.ValidatorUtils;
|
|||
import xiaozhi.common.validator.group.AddGroup;
|
||||
import xiaozhi.common.validator.group.DefaultGroup;
|
||||
import xiaozhi.common.validator.group.UpdateGroup;
|
||||
import xiaozhi.modules.sys.dto.ConfigSecretDTO;
|
||||
import xiaozhi.modules.sys.dto.SysParamsDTO;
|
||||
import xiaozhi.modules.sys.service.SysParamsService;
|
||||
|
||||
|
|
@ -108,11 +107,4 @@ public class SysParamsController {
|
|||
|
||||
return new Result<Void>();
|
||||
}
|
||||
|
||||
@PostMapping("config")
|
||||
@Operation(summary = "获取配置")
|
||||
public Result<Object> getConfig(@RequestBody ConfigSecretDTO dto) {
|
||||
Object config = sysParamsService.getConfig(dto.getSecret());
|
||||
return new Result<Object>().ok(config);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
package xiaozhi.modules.sys.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Schema(description = "配置密钥DTO")
|
||||
public class ConfigSecretDTO {
|
||||
@Schema(description = "密钥")
|
||||
@NotBlank(message = "密钥不能为空")
|
||||
private String secret;
|
||||
}
|
||||
|
|
@ -57,8 +57,7 @@ public interface SysParamsService extends BaseService<SysParamsEntity> {
|
|||
/**
|
||||
* 获取配置
|
||||
*
|
||||
* @param secret 密钥
|
||||
* @return 配置信息
|
||||
*/
|
||||
Object getConfig(String secret);
|
||||
Object getConfig();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -201,14 +201,8 @@ public class SysParamsServiceImpl extends BaseServiceImpl<SysParamsDao, SysParam
|
|||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object getConfig(String secret) {
|
||||
String secretParam = getValue(Constant.SERVER_SECRET, true);
|
||||
// 验证密钥
|
||||
if (StringUtils.isBlank(secret) || !secret.equals(secretParam)) {
|
||||
throw new RenException("密钥错误");
|
||||
}
|
||||
|
||||
// 查询所有非系统参数
|
||||
public Object getConfig() {
|
||||
// 查询所有系统参数
|
||||
QueryWrapper<SysParamsEntity> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("param_type", 1);
|
||||
List<SysParamsEntity> paramsList = baseDao.selectList(wrapper);
|
||||
|
|
|
|||
|
|
@ -189,7 +189,6 @@ class ConnectionHandler:
|
|||
self.config["delete_audio"],
|
||||
)
|
||||
self.tts = _tts
|
||||
print("初始化组件_tts")
|
||||
_llm = llm.create_instance(
|
||||
(
|
||||
self.config["selected_module"]["LLM"]
|
||||
|
|
@ -200,7 +199,6 @@ class ConnectionHandler:
|
|||
self.config["LLM"][self.config["selected_module"]["LLM"]],
|
||||
)
|
||||
self.llm = _llm
|
||||
print("初始化组件_llm")
|
||||
_intent = intent.create_instance(
|
||||
(
|
||||
self.config["selected_module"]["Intent"]
|
||||
|
|
@ -213,7 +211,6 @@ class ConnectionHandler:
|
|||
self.config["Intent"][self.config["selected_module"]["Intent"]],
|
||||
)
|
||||
self.intent = _intent
|
||||
print("初始化组件_intent")
|
||||
_memory = memory.create_instance(
|
||||
(
|
||||
self.config["selected_module"]["Memory"]
|
||||
|
|
@ -226,12 +223,9 @@ class ConnectionHandler:
|
|||
self.config["Memory"][self.config["selected_module"]["Memory"]],
|
||||
)
|
||||
self.memory = _memory
|
||||
print("初始化组件_memory")
|
||||
|
||||
def _initialize_components(self):
|
||||
print("初始化组件11")
|
||||
self._initialize_modules()
|
||||
print("初始化组件22")
|
||||
"""加载提示词"""
|
||||
self.prompt = self.config["prompt"]
|
||||
if self.private_config:
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import os
|
|||
import random
|
||||
import time
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
||||
WAKEUP_CONFIG = {
|
||||
|
|
@ -69,6 +70,14 @@ def getWakeupWordFile(file_name):
|
|||
|
||||
|
||||
async def wakeupWordsResponse(conn):
|
||||
wait_max_time = 5
|
||||
while conn.llm is None or not conn.llm.response_no_stream:
|
||||
await asyncio.sleep(1)
|
||||
wait_max_time -= 1
|
||||
if wait_max_time <= 0:
|
||||
logger.bind(tag=TAG).error("连接对象没有llm")
|
||||
return
|
||||
|
||||
"""唤醒词响应"""
|
||||
wakeup_word = random.choice(WAKEUP_CONFIG["words"])
|
||||
result = conn.llm.response_no_stream(conn.config["prompt"], wakeup_word)
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ def read_config(config_path):
|
|||
if "你" in secret:
|
||||
raise Exception("请先配置manager-api的secret")
|
||||
response = requests.post(
|
||||
f"{api_url}/admin/params/config", json={"secret": secret}
|
||||
f"{api_url}/config/server-base", json={"secret": secret}
|
||||
)
|
||||
if response.status_code == 200:
|
||||
config = response.json()["data"]
|
||||
|
|
|
|||
Loading…
Reference in a new issue