1
This commit is contained in:
@@ -41,12 +41,36 @@ public class SocialMediaLlmClient {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private OllamaClientUtil ollamaClientUtil;
|
private OllamaClientUtil ollamaClientUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用当前激活配置(或旧版单键 / yml 默认)
|
||||||
|
*/
|
||||||
public String getResponse(String inputText) throws IOException {
|
public String getResponse(String inputText) throws IOException {
|
||||||
if (redisTemplate == null) {
|
if (redisTemplate == null) {
|
||||||
return ollamaClientUtil.getResponse(inputText);
|
return ollamaClientUtil.getResponse(inputText);
|
||||||
}
|
}
|
||||||
|
return executeWithResolved(inputText, resolveConfig());
|
||||||
|
}
|
||||||
|
|
||||||
ResolvedLlm cfg = resolveConfig();
|
/**
|
||||||
|
* 使用指定 profileId 的配置做单次调用(用于调试,不改变激活项)
|
||||||
|
*
|
||||||
|
* @param profileId 为空时与 {@link #getResponse(String)} 相同
|
||||||
|
*/
|
||||||
|
public String getResponse(String inputText, String profileId) throws IOException {
|
||||||
|
if (redisTemplate == null) {
|
||||||
|
return ollamaClientUtil.getResponse(inputText);
|
||||||
|
}
|
||||||
|
if (StrUtil.isBlank(profileId)) {
|
||||||
|
return getResponse(inputText);
|
||||||
|
}
|
||||||
|
ResolvedLlm cfg = resolveProfileById(profileId.trim());
|
||||||
|
if (cfg == null) {
|
||||||
|
throw new IOException("配置不存在或 Redis 中无该 profileId: " + profileId);
|
||||||
|
}
|
||||||
|
return executeWithResolved(inputText, cfg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String executeWithResolved(String inputText, ResolvedLlm cfg) throws IOException {
|
||||||
if (cfg == null) {
|
if (cfg == null) {
|
||||||
return ollamaClientUtil.getResponse(inputText);
|
return ollamaClientUtil.getResponse(inputText);
|
||||||
}
|
}
|
||||||
@@ -85,6 +109,41 @@ public class SocialMediaLlmClient {
|
|||||||
log.warn("大模型 activeId={} 在 profiles 中不存在", activeId);
|
log.warn("大模型 activeId={} 在 profiles 中不存在", activeId);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return profileJsonToResolved(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("解析多套 LLM 配置失败,尝试旧版单键: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
return resolveLegacySingleKeys();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResolvedLlm resolveProfileById(String profileId) {
|
||||||
|
try {
|
||||||
|
String raw = redisTemplate.opsForValue().get(LLM_PROFILES_STORE_KEY);
|
||||||
|
if (StrUtil.isBlank(raw)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
JSONObject store = JSON.parseObject(raw);
|
||||||
|
if (store == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
JSONObject profiles = store.getJSONObject("profiles");
|
||||||
|
if (profiles == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
JSONObject p = profiles.getJSONObject(profileId);
|
||||||
|
if (p == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return profileJsonToResolved(p);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("按 id 读取 profile 失败: {}", e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ResolvedLlm profileJsonToResolved(JSONObject p) {
|
||||||
ResolvedLlm r = new ResolvedLlm();
|
ResolvedLlm r = new ResolvedLlm();
|
||||||
r.mode = trimOrNull(p.getString("mode"));
|
r.mode = trimOrNull(p.getString("mode"));
|
||||||
if (r.mode == null) {
|
if (r.mode == null) {
|
||||||
@@ -95,12 +154,6 @@ public class SocialMediaLlmClient {
|
|||||||
r.apiKey = trimOrNull(p.getString("apiKey"));
|
r.apiKey = trimOrNull(p.getString("apiKey"));
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("解析多套 LLM 配置失败,尝试旧版单键: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
return resolveLegacySingleKeys();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 兼容尚未迁移的旧版 Redis 四键 */
|
/** 兼容尚未迁移的旧版 Redis 四键 */
|
||||||
private ResolvedLlm resolveLegacySingleKeys() {
|
private ResolvedLlm resolveLegacySingleKeys() {
|
||||||
|
|||||||
Reference in New Issue
Block a user