Agent pipeline¶
BasePipelineElement
¶
Bases: ABC
Base class for agent pipeline elements.
Subclasses must implement the query
method.
query
abstractmethod
¶
query(
query: str,
runtime: FunctionsRuntime,
env: Env = EmptyEnv(),
messages: Sequence[ChatMessage] = [],
extra_args: dict = {},
) -> tuple[
str, FunctionsRuntime, Env, Sequence[ChatMessage], dict
]
Executes the element of the pipeline on the given query, runtime, environment, and messages.
Must be implemented by the subclass.
Parameters:
-
query
(str
) –the query to execute.
-
runtime
(FunctionsRuntime
) –the runtime that can be used by the agent to execute tools.
-
env
(Env
, default:EmptyEnv()
) –the environment on which the agent is operating.
-
messages
(Sequence[ChatMessage]
, default:[]
) –the list of chat messages exchanged between the agent, the user, and the tools.
-
extra_args
(dict
, default:{}
) –extra arguments that can be used by the agent to execute the query and passed around among different pipeline elements.
Source code in src/agentdojo/agent_pipeline/base_pipeline_element.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Base elements¶
AgentPipeline
¶
AgentPipeline(elements: Iterable[BasePipelineElement])
Bases: BasePipelineElement
Executes a sequence of BasePipelineElement
s in order.
Parameters:
-
elements
(Iterable[BasePipelineElement]
) –the elements of the pipeline to execute.
Source code in src/agentdojo/agent_pipeline/agent_pipeline.py
174 175 |
|
from_config
classmethod
¶
from_config(config: PipelineConfig) -> Self
Creates a pipeline for a given model and defense.
Source code in src/agentdojo/agent_pipeline/agent_pipeline.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
PipelineConfig
¶
Bases: BaseModel
llm
instance-attribute
¶
llm: str | BasePipelineElement
Which LLM to use. One of the models in ModelsEnum
or a custom object which inherits from BasePipelineElement
and implements calls to an LLM.
defense
instance-attribute
¶
defense: str | None
Which defense to use. One of the defenses in DEFENSES
.
InitQuery
¶
Bases: BasePipelineElement
Initializes the pipeline execution with the user query, by adding a
ChatUserMessage
to the messages list.
GroundTruthPipeline
¶
GroundTruthPipeline(
task: BaseUserTask | BaseInjectionTask | None,
)
Bases: BasePipelineElement
Pipeline that executes a task's ground_truth
.
It is used to identify which injection placeholders are seen by the agent during the task execution.
Source code in src/agentdojo/agent_pipeline/ground_truth_pipeline.py
17 18 |
|
SystemMessage
¶
SystemMessage(system_message: str)
Bases: BasePipelineElement
Adds a system message to the messages list (as a ChatSystemMessage
).
Parameters:
-
system_message
(str
) –the content of the system message.
Source code in src/agentdojo/agent_pipeline/basic_elements.py
35 36 |
|
Function execution elements¶
ToolsExecutor
¶
ToolsExecutor(
tool_output_formatter: Callable[
[FunctionReturnType], str
] = tool_result_to_str
)
Bases: BasePipelineElement
Executes the tool calls in the last messages for which tool execution is required.
Parameters:
-
tool_output_formatter
(Callable[[FunctionReturnType], str]
, default:tool_result_to_str
) –a function that converts a tool's output into plain text to be fed to the model. It should take as argument the tool output, and convert it into a string. The default converter converts the output to structured YAML.
Source code in src/agentdojo/agent_pipeline/tool_execution.py
46 47 |
|
ToolsExecutionLoop
¶
ToolsExecutionLoop(
elements: Sequence[BasePipelineElement],
max_iters: int = 15,
)
Bases: BasePipelineElement
Executes in loop a sequence of pipeline elements related to tool execution until the LLM does not return any tool calls.
Parameters:
-
elements
(Sequence[BasePipelineElement]
) –a sequence of pipeline elements to be executed in loop. One of them should be an LLM, and one of them should be a ToolsExecutor (or something that behaves similarly by executing function calls). You can find an example usage of this class here.
-
max_iters
(int
, default:15
) –maximum number of iterations to execute the pipeline elements in loop.
Source code in src/agentdojo/agent_pipeline/tool_execution.py
115 116 117 |
|
tool_result_to_str
¶
tool_result_to_str(
tool_result: FunctionReturnType,
dump_fn: Callable[[dict | list[dict]], str] = safe_dump,
) -> str
Basic tool output formatter with YAML dump by default. Could work with json.dumps
as
dump_fn
.
Source code in src/agentdojo/agent_pipeline/tool_execution.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Exceptions¶
AbortAgentError
¶
AbortAgentError(
message: str,
messages: list[ChatMessage],
task_environment: TaskEnvironment,
)
Bases: Exception
Checking tools can raise this exception to abort the execution of the agent pipeline (e.g. when a prompt injection is detected).
Source code in src/agentdojo/agent_pipeline/errors.py
13 14 15 16 17 18 19 20 21 22 23 24 |
|
Available models and defenses¶
ModelsEnum
¶
Bases: StrEnum
Currently supported models.
GPT_4O_2024_05_13
class-attribute
instance-attribute
¶
GPT_4O_2024_05_13 = 'gpt-4o-2024-05-13'
GPT-4o
GPT_4O_MINI_2024_07_18
class-attribute
instance-attribute
¶
GPT_4O_MINI_2024_07_18 = 'gpt-4o-mini-2024-07-18'
GPT-4o Mini
GPT_4_0125_PREVIEW
class-attribute
instance-attribute
¶
GPT_4_0125_PREVIEW = 'gpt-4-0125-preview'
GPT-4 Turbo Preview 0125
GPT_3_5_TURBO_0125
class-attribute
instance-attribute
¶
GPT_3_5_TURBO_0125 = 'gpt-3.5-turbo-0125'
GPT-3.5 Turbo 0125
GPT_4_TURBO_2024_04_09
class-attribute
instance-attribute
¶
GPT_4_TURBO_2024_04_09 = 'gpt-4-turbo-2024-04-09'
GPT-4 turbo
CLAUDE_3_OPUS_20240229
class-attribute
instance-attribute
¶
CLAUDE_3_OPUS_20240229 = 'claude-3-opus-20240229'
Claude 3 opus
CLAUDE_3_SONNET_20240229
class-attribute
instance-attribute
¶
CLAUDE_3_SONNET_20240229 = 'claude-3-sonnet-20240229'
Claude 3 sonnet
CLAUDE_3_5_SONNET_20240620
class-attribute
instance-attribute
¶
CLAUDE_3_5_SONNET_20240620 = 'claude-3-5-sonnet-20240620'
Claude 3.5 sonnet
CLAUDE_3_5_SONNET_20241022
class-attribute
instance-attribute
¶
CLAUDE_3_5_SONNET_20241022 = 'claude-3-5-sonnet-20241022'
Claude 3.5 sonnet October
CLAUDE_3_HAIKU_20240307
class-attribute
instance-attribute
¶
CLAUDE_3_HAIKU_20240307 = 'claude-3-haiku-20240307'
Claude 3 haiku
COMMAND_R_PLUS
class-attribute
instance-attribute
¶
COMMAND_R_PLUS = 'command-r-plus'
Command R Plus
MISTRALAI_MIXTRAL_8X7B_INSTRUCT_V0_1
class-attribute
instance-attribute
¶
MISTRALAI_MIXTRAL_8X7B_INSTRUCT_V0_1 = (
"mistralai/Mixtral-8x7B-Instruct-v0.1"
)
MistralAI Mixtral 8x7B Instruct v0.1
GEMINI_1_5_PRO_002
class-attribute
instance-attribute
¶
GEMINI_1_5_PRO_002 = 'gemini-1.5-pro-002'
Gemini 1.5 Pro 002
GEMINI_1_5_PRO_001
class-attribute
instance-attribute
¶
GEMINI_1_5_PRO_001 = 'gemini-1.5-pro-001'
Gemini 1.5 Pro 001
GEMINI_1_5_FLASH_002
class-attribute
instance-attribute
¶
GEMINI_1_5_FLASH_002 = 'gemini-1.5-flash-002'
Gemini 1.5 Flash 002
GEMINI_1_5_FLASH_001
class-attribute
instance-attribute
¶
GEMINI_1_5_FLASH_001 = 'gemini-1.5-flash-001'
Gemini 1.5 Flash 001
DEFENSES
module-attribute
¶
DEFENSES = [
"tool_filter",
"transformers_pi_detector",
"spotlighting_with_delimiting",
"repeat_user_prompt",
]
Available defenses.