Nous.Plugins.InputGuard.Strategy behaviour (nous v0.13.3)
View SourceBehaviour for input guard detection strategies.
Implement this behaviour to create custom strategies for detecting malicious or unwanted input. Each strategy receives the user input text, its own configuration, and the full agent context.
Creating Your Own Strategy
Here's a complete example of a custom blocklist strategy:
defmodule MyApp.InputGuard.Blocklist do
@behaviour Nous.Plugins.InputGuard.Strategy
alias Nous.Plugins.InputGuard.Result
@impl true
def check(input, config, _ctx) do
blocklist = Keyword.get(config, :words, [])
downcased = String.downcase(input)
case Enum.find(blocklist, &String.contains?(downcased, &1)) do
nil ->
{:ok, %Result{severity: :safe}}
word ->
{:ok, %Result{
severity: :blocked,
reason: "Blocklisted word: #{word}",
strategy: __MODULE__
}}
end
end
endThen use it in your agent configuration:
agent = Nous.new("openai:gpt-4",
plugins: [Nous.Plugins.InputGuard]
)
{:ok, result} = Nous.run(agent, "Hello",
deps: %{
input_guard_config: %{
strategies: [
{MyApp.InputGuard.Blocklist, words: ["hack", "exploit"]}
]
}
}
)
Summary
Callbacks
Check input text for malicious content.
Callbacks
@callback check(input :: String.t(), config :: keyword(), ctx :: Nous.Agent.Context.t()) :: {:ok, Nous.Plugins.InputGuard.Result.t()} | {:error, term()}
Check input text for malicious content.
Returns {:ok, result} with a Result struct indicating the severity,
or {:error, reason} if the check itself failed.
Parameters
input— The user's input text to checkconfig— Strategy-specific configuration from the{Module, opts}tuplectx— The full agent context for access to message history, deps, etc.