-
Notifications
You must be signed in to change notification settings - Fork 8
Ability to register hand-crafted agents to the registry #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...gured-agents/src/main/java/com/phonepe/sentinelai/configuredagents/RegisterableAgent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package com.phonepe.sentinelai.configuredagents; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.phonepe.sentinelai.core.agent.Agent; | ||
| import com.phonepe.sentinelai.core.agent.AgentExtension; | ||
| import com.phonepe.sentinelai.core.agent.AgentSetup; | ||
| import com.phonepe.sentinelai.core.agent.ApproveAllToolRuns; | ||
| import com.phonepe.sentinelai.core.earlytermination.EarlyTerminationStrategy; | ||
| import com.phonepe.sentinelai.core.earlytermination.NeverTerminateEarlyStrategy; | ||
| import com.phonepe.sentinelai.core.errorhandling.DefaultErrorHandler; | ||
| import com.phonepe.sentinelai.core.errorhandling.ErrorResponseHandler; | ||
| import com.phonepe.sentinelai.core.outputvalidation.DefaultOutputValidator; | ||
| import com.phonepe.sentinelai.core.outputvalidation.OutputValidator; | ||
| import com.phonepe.sentinelai.core.tools.ExecutableTool; | ||
| import com.phonepe.sentinelai.core.tools.ToolRunApprovalSeeker; | ||
| import com.phonepe.sentinelai.core.utils.JsonUtils; | ||
| import lombok.NonNull; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * An agent that can be registered in the {@link AgentRegistry}. | ||
| * @param <T> The type of the agent extending this class. | ||
| */ | ||
| @SuppressWarnings("java:S107") | ||
| public abstract class RegisterableAgent<T extends RegisterableAgent<T>> extends Agent<String, String, T> { | ||
|
|
||
| private final AgentConfiguration agentConfiguration; | ||
|
|
||
| protected RegisterableAgent( | ||
| AgentConfiguration agentConfiguration, | ||
| @NonNull AgentSetup setup, | ||
| List<AgentExtension<String, String, T>> agentExtensions, | ||
| Map<String, ExecutableTool> knownTools) { | ||
| this(agentConfiguration, | ||
| setup, | ||
| agentExtensions, | ||
| knownTools, | ||
| new ApproveAllToolRuns<>(), | ||
| new DefaultOutputValidator<>(), | ||
| new DefaultErrorHandler<>(), | ||
| new NeverTerminateEarlyStrategy()); | ||
| } | ||
|
|
||
| protected RegisterableAgent( | ||
| AgentConfiguration agentConfiguration, | ||
| @NonNull AgentSetup setup, | ||
| List<AgentExtension<String, String, T>> agentExtensions, | ||
| Map<String, ExecutableTool> knownTools, | ||
| ToolRunApprovalSeeker<String, String, T> toolRunApprovalSeeker, | ||
| OutputValidator<String, String> outputValidator, | ||
| ErrorResponseHandler<String> errorHandler, | ||
| EarlyTerminationStrategy earlyTerminationStrategy) { | ||
| super(String.class, | ||
| agentConfiguration.getPrompt(), | ||
| setup, | ||
| agentExtensions, | ||
| knownTools, | ||
| toolRunApprovalSeeker, | ||
| outputValidator, | ||
| errorHandler, | ||
| earlyTerminationStrategy); | ||
| this.agentConfiguration = AgentConfiguration.fixConfiguration( | ||
| agentConfiguration, Objects.requireNonNullElseGet(setup.getMapper(), JsonUtils::createMapper)); | ||
| } | ||
|
|
||
| public final AgentConfiguration agentConfiguration() { | ||
| return agentConfiguration; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * You cannot override this here. Set the correct schema in the agent configuration. | ||
| * @return The output schema for this agent | ||
| */ | ||
| @Override | ||
| protected final JsonNode outputSchema() { | ||
| return agentConfiguration.getOutputSchema(); | ||
| } | ||
|
|
||
| /** | ||
| * The name of the agent as specified in the configuration. Feel free to override if needed for more exotic | ||
| * implementations | ||
| * @return The name of the agent | ||
| */ | ||
| @Override | ||
| public String name() { | ||
| return agentConfiguration.getAgentName(); | ||
| } | ||
|
|
||
| /** | ||
| * We don't do much here. Basically if the model sends out put in the required format, we just pass it through. | ||
| * You can override this if something more exotic needs to be done | ||
| * @param output The model output | ||
| * @param agentSetup The agent setup | ||
| * @return A String serialized version of the output. It is a little wasteful to serialize and deserialize again, but | ||
| * this keeps things simple. | ||
| * @throws JsonProcessingException If serialization fails | ||
| */ | ||
| @Override | ||
| protected String translateData(JsonNode output, AgentSetup agentSetup) throws JsonProcessingException { | ||
| return agentSetup.getMapper() | ||
| .writeValueAsString(output); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.