mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(framework): make AbstractAgentModule sealed with class-based module contracts and streamline ActivateModel model access
This commit is contained in:
@@ -3,12 +3,10 @@ package work.slhaf.partner.module.modules.core;
|
|||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||||
import work.slhaf.partner.api.agent.factory.module.abstracts.AbstractAgentRunningModule;
|
import work.slhaf.partner.api.agent.factory.module.abstracts.AbstractAgentModule;
|
||||||
import work.slhaf.partner.api.agent.factory.module.abstracts.ActivateModel;
|
import work.slhaf.partner.api.agent.factory.module.abstracts.ActivateModel;
|
||||||
import work.slhaf.partner.api.agent.factory.module.annotation.CoreModule;
|
|
||||||
import work.slhaf.partner.api.agent.factory.module.annotation.Init;
|
import work.slhaf.partner.api.agent.factory.module.annotation.Init;
|
||||||
import work.slhaf.partner.api.chat.ChatClient;
|
import work.slhaf.partner.api.chat.ChatClient;
|
||||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||||
@@ -29,10 +27,8 @@ import static work.slhaf.partner.common.util.ExtractUtil.extractJson;
|
|||||||
|
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
@Slf4j
|
public class CoreModel extends AbstractAgentModule.Running<PartnerRunningFlowContext> implements ActivateModel {
|
||||||
@CoreModule
|
|
||||||
public class CoreModel extends AbstractAgentRunningModule<PartnerRunningFlowContext> implements ActivateModel {
|
|
||||||
|
|
||||||
@InjectCapability
|
@InjectCapability
|
||||||
private CognationCapability cognationCapability;
|
private CognationCapability cognationCapability;
|
||||||
private List<Message> appendedMessages = new ArrayList<>();
|
private List<Message> appendedMessages = new ArrayList<>();
|
||||||
@@ -252,4 +248,9 @@ public class CoreModel extends AbstractAgentRunningModule<PartnerRunningFlowCont
|
|||||||
.build();
|
.build();
|
||||||
appendedMessages.add(startMessage);
|
appendedMessages.add(startMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int order() {
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package work.slhaf.partner.api.agent.factory.module.abstracts
|
package work.slhaf.partner.api.agent.factory.module.abstracts
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityHolder
|
import work.slhaf.partner.api.agent.factory.capability.annotation.CapabilityHolder
|
||||||
import work.slhaf.partner.api.agent.factory.module.annotation.Init
|
import work.slhaf.partner.api.agent.factory.module.annotation.Init
|
||||||
import work.slhaf.partner.api.agent.runtime.config.AgentConfigManager
|
import work.slhaf.partner.api.agent.runtime.config.AgentConfigManager
|
||||||
@@ -13,57 +15,58 @@ import work.slhaf.partner.api.chat.pojo.Message
|
|||||||
* 模块基类
|
* 模块基类
|
||||||
*/
|
*/
|
||||||
@CapabilityHolder
|
@CapabilityHolder
|
||||||
abstract class AbstractAgentModule {
|
sealed class AbstractAgentModule {
|
||||||
|
|
||||||
var moduleName: String = javaClass.simpleName
|
var moduleName: String = javaClass.simpleName
|
||||||
|
|
||||||
interface Running<T : RunningFlowContext> {
|
@JvmField
|
||||||
|
val log: Logger = LoggerFactory.getLogger(javaClass.simpleName)
|
||||||
|
|
||||||
fun execute(context: T)
|
abstract class Running<T : RunningFlowContext> : AbstractAgentModule() {
|
||||||
|
|
||||||
fun order(): Int
|
abstract fun execute(context: T)
|
||||||
|
|
||||||
|
abstract fun order(): Int
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Sub<I, O> {
|
abstract class Sub<I, O> : AbstractAgentModule() {
|
||||||
fun execute(input: I): O
|
abstract fun execute(input: I): O
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Standalone
|
abstract class Standalone
|
||||||
|
|
||||||
// TODO 后续于此处扩展生命周期内容
|
// TODO 后续于此处扩展生命周期内容
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ActivateModel {
|
interface ActivateModel {
|
||||||
|
|
||||||
|
val model: Model
|
||||||
|
get() = modelMap.computeIfAbsent(modelKey()) {
|
||||||
|
buildModel()
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val modelMap: MutableMap<String, Model> = mutableMapOf()
|
val modelMap: MutableMap<String, Model> = mutableMapOf()
|
||||||
private val configManager: AgentConfigManager = AgentConfigManager.INSTANCE
|
private val configManager: AgentConfigManager = AgentConfigManager.INSTANCE
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getModel(): Model {
|
|
||||||
fun buildModel(): Model {
|
|
||||||
val modelConfig = configManager.loadModelConfig(modelKey())
|
|
||||||
val chatClient = ChatClient(modelConfig.baseUrl, modelConfig.apikey, modelConfig.model)
|
|
||||||
val model = Model(chatClient)
|
|
||||||
|
|
||||||
val baseMessages = if (withBasicPrompt()) {
|
|
||||||
loadSpecificPromptAndBasicPrompt(modelKey())
|
|
||||||
} else {
|
|
||||||
configManager.loadModelPrompt(modelKey())
|
|
||||||
}
|
|
||||||
model.baseMessages.addAll(baseMessages)
|
|
||||||
return model
|
|
||||||
}
|
|
||||||
|
|
||||||
val model = modelMap.computeIfAbsent(modelKey()) {
|
|
||||||
buildModel()
|
|
||||||
}
|
|
||||||
return model
|
|
||||||
}
|
|
||||||
|
|
||||||
@Init(order = -1)
|
@Init(order = -1)
|
||||||
fun modelSettings() {
|
fun modelSettings() {
|
||||||
val model = getModel()
|
modelMap[modelKey()] = buildModel()
|
||||||
modelMap[modelKey()] = model
|
}
|
||||||
|
|
||||||
|
fun buildModel(): Model {
|
||||||
|
val modelConfig = configManager.loadModelConfig(modelKey())
|
||||||
|
val chatClient = ChatClient(modelConfig.baseUrl, modelConfig.apikey, modelConfig.model)
|
||||||
|
val model = Model(chatClient)
|
||||||
|
|
||||||
|
val baseMessages = if (withBasicPrompt()) {
|
||||||
|
loadSpecificPromptAndBasicPrompt(modelKey())
|
||||||
|
} else {
|
||||||
|
configManager.loadModelPrompt(modelKey())
|
||||||
|
}
|
||||||
|
model.baseMessages.addAll(baseMessages)
|
||||||
|
return model
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadSpecificPromptAndBasicPrompt(modelKey: String): MutableList<Message> {
|
private fun loadSpecificPromptAndBasicPrompt(modelKey: String): MutableList<Message> {
|
||||||
@@ -74,7 +77,6 @@ interface ActivateModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun chat(): ChatResponse {
|
fun chat(): ChatResponse {
|
||||||
val model = this.getModel()
|
|
||||||
val temp = ArrayList<Message?>()
|
val temp = ArrayList<Message?>()
|
||||||
temp.addAll(model.baseMessages)
|
temp.addAll(model.baseMessages)
|
||||||
temp.addAll(model.chatMessages)
|
temp.addAll(model.chatMessages)
|
||||||
@@ -82,14 +84,12 @@ interface ActivateModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun singleChat(input: String): ChatResponse {
|
fun singleChat(input: String): ChatResponse {
|
||||||
val model = this.getModel()
|
|
||||||
val temp = ArrayList<Message>(model.baseMessages)
|
val temp = ArrayList<Message>(model.baseMessages)
|
||||||
temp.add(Message(ChatConstant.Character.USER, input))
|
temp.add(Message(ChatConstant.Character.USER, input))
|
||||||
return model.chatClient.runChat(temp)
|
return model.chatClient.runChat(temp)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateChatClientSettings() {
|
fun updateChatClientSettings() {
|
||||||
val model = this.getModel()
|
|
||||||
model.chatClient.temperature = 0.4
|
model.chatClient.temperature = 0.4
|
||||||
model.chatClient.top_p = 0.8
|
model.chatClient.top_p = 0.8
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user