feat: Enhance agent functionality with ModelProfile integration

- Added ModelProfile to the Agent record to allow for more detailed model configurations.
- Updated AgentExecutorService to utilize ModelProfile when fetching clients.
- Modified BaseAgent interface to include a default modelProfile method.
- Adjusted Client interface and implementations to support ModelProfile in conversation and streaming methods.
- Enhanced ClientService to retrieve clients based on ModelProfile.
- Updated CompletionInterceptor to work with ModelProfile instead of ModelCategory.
- Refactored AzureAiClient and other client implementations to handle ModelProfile correctly.
- Changed CachedClient to cache responses based on ModelProfile.
- Updated ModelCategory to a record type for better validation and immutability.
- Enhanced ModelProfile with validation checks for its fields.
- Updated ModelProfileService to support fetching profiles based on ModelCategory.
- Adjusted tests to accommodate changes in Agent and ModelProfile.
- Updated properties files to include category information for model profiles.
This commit is contained in:
extio-de
2025-10-29 12:07:36 +01:00
parent c5dca3ac07
commit 304924e223
20 changed files with 452 additions and 126 deletions

View File

@@ -6,10 +6,12 @@ import java.util.function.Function;
import de.extio.lmlib.agent.responsehandler.AgentResponseHandler;
import de.extio.lmlib.profile.ModelCategory;
import de.extio.lmlib.profile.ModelProfile;
public record Agent(String name,
AgentType agentType,
ModelCategory modelCategory,
ModelProfile modelProfile,
String systemPrompt,
String textTemplate,
AgentResponseHandler responseHandler,
@@ -44,6 +46,14 @@ public record Agent(String name,
return BaseAgent.super.modelCategory(context);
}
@Override
public ModelProfile modelProfile(final AgentContext context) {
if (modelProfile != null) {
return modelProfile;
}
return BaseAgent.super.modelProfile(context);
}
@Override
public void preProcess(final AgentContext context) {
if (preProcessor != null) {

View File

@@ -82,7 +82,13 @@ public class AgentExecutorService implements InitializingBean, DisposableBean {
Client client = null;
if (agent.agentType(context) != AgentType.PROCESSING_ONLY) {
client = this.clientService.getClient(agent.modelCategory(context));
final var modelProfile = agent.modelProfile(context);
if (modelProfile != null) {
client = this.clientService.getClient(modelProfile);
}
else {
client = this.clientService.getClient(agent.modelCategory(context));
}
}
context.setAgentContextUpdateConsumer(agentContextUpdateConsumer);

View File

@@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.slf4j.Logger;
@@ -17,11 +18,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import de.extio.lmlib.agent.responsehandler.AgentResponseHandler;
import de.extio.lmlib.agent.responsehandler.StreamedAgentResponseHandler;
import de.extio.lmlib.agent.responsehandler.TextAgentResponseHandler;
import de.extio.lmlib.client.Chunk;
import de.extio.lmlib.client.Client;
import de.extio.lmlib.client.Completion;
import de.extio.lmlib.client.Conversation;
import de.extio.lmlib.client.Conversation.TurnType;
import de.extio.lmlib.profile.ModelCategory;
import de.extio.lmlib.profile.ModelProfile;
public interface BaseAgent {
@@ -40,6 +43,10 @@ public interface BaseAgent {
default ModelCategory modelCategory(final AgentContext context) {
return ModelCategory.MEDIUM;
}
default ModelProfile modelProfile(final AgentContext context) {
return null;
}
default String systemPrompt() {
return null;
@@ -90,7 +97,8 @@ public interface BaseAgent {
split.context().getGraph().add("○");
}
else {
split.context().getGraph().add(this.modelCategory(split.context()).getShortName());
final var modelProfile = this.modelProfile(split.context());
split.context().getGraph().add(modelProfile != null ? modelProfile.category() : this.modelCategory(split.context()).getShortName());
}
split.context().getGraph().add(this.name());
@@ -102,21 +110,33 @@ public interface BaseAgent {
LOGGER.debug("Conversation: {}", conversation);
final var responseHandler = this.responseHandler();
final var modelProfile = this.modelProfile(split.context());
Completion completion = null;
if (split.context().isStreaming()) {
if (responseHandler instanceof final StreamedAgentResponseHandler streamedResponseHandler) {
streamedResponseHandler.beforeStream(split.context());
}
completion = client.streamConversation(this.modelCategory(split.context()), conversation, chunk -> {
final Consumer<Chunk> chunkConsumer = chunk -> {
if (responseHandler instanceof final StreamedAgentResponseHandler streamedResponseHandler) {
if (streamedResponseHandler.handleChunk(chunk, split.context()) && split.context().getAgentContextUpdateConsumer() != null) {
split.context().getAgentContextUpdateConsumer().accept(split.context());
}
}
});
};
if (modelProfile != null) {
completion = client.streamConversation(modelProfile, conversation, chunkConsumer);
}
else {
completion = client.streamConversation(this.modelCategory(split.context()), conversation, chunkConsumer);
}
}
else {
completion = client.conversation(this.modelCategory(split.context()), conversation);
if (modelProfile != null) {
completion = client.conversation(modelProfile, conversation);
}
else {
completion = client.conversation(this.modelCategory(split.context()), conversation);
}
}
split.context().setLastCompletion(completion);
split.context().getRequestStatistic().add(completion);