1
This commit is contained in:
@@ -41,12 +41,36 @@ public class SocialMediaLlmClient {
|
||||
@Autowired
|
||||
private OllamaClientUtil ollamaClientUtil;
|
||||
|
||||
/**
|
||||
* 使用当前激活配置(或旧版单键 / yml 默认)
|
||||
*/
|
||||
public String getResponse(String inputText) throws IOException {
|
||||
if (redisTemplate == null) {
|
||||
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) {
|
||||
return ollamaClientUtil.getResponse(inputText);
|
||||
}
|
||||
@@ -85,15 +109,7 @@ public class SocialMediaLlmClient {
|
||||
log.warn("大模型 activeId={} 在 profiles 中不存在", activeId);
|
||||
return null;
|
||||
}
|
||||
ResolvedLlm r = new ResolvedLlm();
|
||||
r.mode = trimOrNull(p.getString("mode"));
|
||||
if (r.mode == null) {
|
||||
r.mode = MODE_OLLAMA;
|
||||
}
|
||||
r.baseUrl = trimOrNull(p.getString("baseUrl"));
|
||||
r.model = trimOrNull(p.getString("model"));
|
||||
r.apiKey = trimOrNull(p.getString("apiKey"));
|
||||
return r;
|
||||
return profileJsonToResolved(p);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -102,6 +118,43 @@ public class SocialMediaLlmClient {
|
||||
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();
|
||||
r.mode = trimOrNull(p.getString("mode"));
|
||||
if (r.mode == null) {
|
||||
r.mode = MODE_OLLAMA;
|
||||
}
|
||||
r.baseUrl = trimOrNull(p.getString("baseUrl"));
|
||||
r.model = trimOrNull(p.getString("model"));
|
||||
r.apiKey = trimOrNull(p.getString("apiKey"));
|
||||
return r;
|
||||
}
|
||||
|
||||
/** 兼容尚未迁移的旧版 Redis 四键 */
|
||||
private ResolvedLlm resolveLegacySingleKeys() {
|
||||
String mode = trimOrNull(redisTemplate.opsForValue().get(KEY_MODE));
|
||||
|
||||
Reference in New Issue
Block a user