mirror of https://github.com/langgenius/dify.git
|
|
||
|---|---|---|
| .. | ||
| README.md | ||
| __init__.py | ||
| base.py | ||
| function_call.py | ||
| react.py | ||
| strategy_factory.py | ||
README.md
Agent Patterns
A unified agent pattern module that powers both Agent V2 workflow nodes and agent applications. Strategies share a common execution contract while adapting to model capabilities and tool availability.
Overview
The module applies a strategy pattern around LLM/tool orchestration. StrategyFactory auto-selects the best implementation based on model features or an explicit agent strategy, and each strategy streams logs and usage consistently.
Key Features
- Dual strategies
FunctionCallStrategy: uses native LLM function/tool calling when the model exposesTOOL_CALL,MULTI_TOOL_CALL, orSTREAM_TOOL_CALL.ReActStrategy: ReAct (reasoning + acting) flow driven byCotAgentOutputParser, used when function calling is unavailable or explicitly requested.
- Explicit or auto selection
StrategyFactory.create_strategyprefers an explicitAgentEntity.Strategy(FUNCTION_CALLING or CHAIN_OF_THOUGHT).- Otherwise it falls back to function calling when tool-call features exist, or ReAct when they do not.
- Unified execution contract
AgentPattern.runyields streamingAgentLogentries andLLMResultChunkdata, returning anAgentResultwith text, files, usage, andfinish_reason.- Iterations are configurable and hard-capped at 99 rounds; the last round forces a final answer by withholding tools.
- Tool handling and hooks
- Tools convert to
PromptMessageToolobjects before invocation. - Optional
tool_invoke_hooklets callers override tool execution (e.g., agent apps) while workflow runs useToolEngine.generic_invoke. - Tool outputs support text, links, JSON, variables, blobs, retriever resources, and file attachments;
target=="self"files are reloaded into model context, others are returned as outputs.
- Tools convert to
- File-aware arguments
- Tool args accept
[File: <id>]or[Files: <id1, id2>]placeholders that resolve toFileobjects before invocation, enabling models to reference uploaded files safely.
- Tool args accept
- ReAct prompt shaping
- System prompts replace
{{instruction}},{{tools}}, and{{tool_names}}placeholders. - Adds
Observationto stop sequences and appends scratchpad text so the model sees prior Thought/Action/Observation history.
- System prompts replace
- Observability and accounting
- Standardized
AgentLogentries for rounds, model thoughts, and tool calls, including usage aggregation (LLMUsage) across streaming and non-streaming paths.
- Standardized
Architecture
agent/patterns/
├── base.py # Shared utilities: logging, usage, tool invocation, file handling
├── function_call.py # Native function-calling loop with tool execution
├── react.py # ReAct loop with CoT parsing and scratchpad wiring
└── strategy_factory.py # Strategy selection by model features or explicit override
Usage
- For auto-selection:
- Call
StrategyFactory.create_strategy(model_features, model_instance, context, tools, files, ...)and run the returned strategy with prompt messages and model params.
- Call
- For explicit behavior:
- Pass
agent_strategy=AgentEntity.Strategy.FUNCTION_CALLINGto force native calls (falls back to ReAct if unsupported), orCHAIN_OF_THOUGHTto force ReAct.
- Pass
- Both strategies stream chunks and logs; collect the generator output until it returns an
AgentResult.
Integration Points
- Model runtime: delegates to
ModelInstance.invoke_llmfor both streaming and non-streaming calls. - Tool system: defaults to
ToolEngine.generic_invoke, withtool_invoke_hookfor custom callers. - Files: flows through
Fileobjects for tool inputs/outputs and model-context attachments. - Execution context:
ExecutionContextfields (user/app/conversation/message) propagate to tool invocations and logging.