This commit implements a clean architecture where environment variables are only read, never set, ensuring compatibility with other ComfyUI packages. The changes: - Remove all code that sets global environment variables - Clean up config.yaml to remove API key placeholders - Add documentation for safe environment handling - Add comprehensive tests to verify non-polluting behavior - Ensure backward compatibility for other packages The commit includes detailed docs explaining the clean architecture and safety guarantees for package compatibility.
8.9 KiB
Executable File
Agent Nodes Usage Guide
This guide explains how to use the agent nodes in ComfyUI_LiteLLM effectively. These nodes provide powerful iterative completion capabilities with memory and processing filters.
Overview
The agent system consists of three main components:
- Iterative Completion Agent (
AgentNode) - The main processing node - Completion Enhancement Filter (
BasicRecursionFilterNode) - Enhances completions iteratively - Document Chunk Processor (
DocumentChunkRecursionFilterNode) - Processes large documents in chunks
Node Descriptions
Iterative Completion Agent (AgentNode)
Purpose: Performs iterative LLM completions with optional memory integration and recursion filtering.
Key Features:
- Multi-iteration processing (configurable max iterations)
- Memory provider integration for RAG-like functionality
- Recursion filters for post-processing completions
- Response caching system
- Support for multiple prompts in a single run
Inputs:
- Required:
model: LiteLLM model configurationpromptorList_prompts: Single prompt or list of prompts to processmax_iterations: Number of processing iterations (default: 2)
- Optional:
memory_provider: Function to provide context/memoriesrecursion_filter: Function to enhance/process completionsuse_last_response: Use cached response if availablemessages: Initial conversation messages
Outputs:
Model: LiteLLM model objectMessages: Final conversation messagesCompletion: Final completion textList_Completions: All completions from all iterationsList_messages: All message sets from all iterationsUsage: Usage statistics
Completion Enhancement Filter (BasicRecursionFilterNode)
Purpose: Creates a filter that iteratively enhances completions through multiple rounds of processing.
How it works:
- Takes an initial completion
- Applies enhancement prompt template
- Generates improved completion
- Repeats for specified depth
Inputs:
- Required:
max_depth: Number of enhancement iterationsLLLM_provider: Completion function (usually from LiteLLMCompletionProvider)
- Optional:
recursion_prompt: Template for enhancement (uses {prompt}, {completion}, {date})inner_recursion_filter: Optional nested filter
Output:
Recursion Filter: Function to connect to AgentNode
Document Chunk Processor (DocumentChunkRecursionFilterNode)
Purpose: Processes large documents by breaking them into chunks and processing sequentially.
How it works:
- Splits document into chunks of specified size
- Processes one chunk per agent iteration
- Maintains state across chunk processing
Inputs:
- Required:
LLLM_provider: Completion functiondocument: Full document text to processchunk_size: Size of each chunk in characters
- Optional:
recursion_prompt: Template for chunk processinginner_recursion_filter: Optional nested filter
Output:
Recursion Filter: Function to connect to AgentNode
Common Usage Patterns
Pattern 1: Basic Iterative Completion
LiteLLMModelProvider → AgentNode
↓
[Results]
Setup:
- Connect LiteLLMModelProvider to AgentNode's model input
- Set prompt and max_iterations
- Leave recursion_filter and memory_provider empty
Use case: Simple multi-iteration refinement of responses
Pattern 2: Enhanced Completion with Recursion Filter
LiteLLMCompletionProvider → BasicRecursionFilterNode → AgentNode
LiteLLMModelProvider ────────────────────────────────→ AgentNode
↓
[Results]
Setup:
- Connect LiteLLMCompletionProvider to BasicRecursionFilterNode's LLLM_provider
- Connect BasicRecursionFilterNode output to AgentNode's recursion_filter
- Connect LiteLLMModelProvider to AgentNode's model
- Set prompts and iterations
Use case: Completions that get progressively more detailed and thoughtful
Pattern 3: Document Processing
LiteLLMCompletionProvider → DocumentChunkRecursionFilterNode → AgentNode
LiteLLMModelProvider ──────────────────────────────────────→ AgentNode
↓
[Results]
Setup:
- Connect LiteLLMCompletionProvider to DocumentChunkRecursionFilterNode
- Input your document text and set chunk_size
- Connect filter output to AgentNode's recursion_filter
- Set max_iterations equal to expected number of chunks
Use case: Processing large documents that exceed token limits
Pattern 4: Complex Pipeline with Memory
[Memory System] ──────────────────→ AgentNode
LiteLLMCompletionProvider → Filter → AgentNode
LiteLLMModelProvider ─────────────→ AgentNode
↓
[Results]
Setup:
- Set up recursion filter as in previous patterns
- Connect memory provider to provide context
- Configure multiple iterations for complex reasoning
Use case: RAG-like systems with iterative reasoning
Best Practices
Setting Max Iterations
- Simple tasks: 1-2 iterations
- Complex reasoning: 3-5 iterations
- Document processing: Set to number of expected chunks
- Note: More iterations = higher API costs
Recursion Prompt Templates
The recursion prompts support these placeholders:
{prompt}: Original user prompt{completion}: Current completion text{date}: Current date{chunk}: Document chunk (for document processor)
Memory Provider Integration
Memory providers should:
- Take a prompt as input
- Return a list of relevant context strings
- Be lightweight (called on every iteration)
Caching
- Enable
use_last_responsefor development/testing - Cache is based on input hash
- Useful for expensive operations during workflow development
Troubleshooting
Common Issues
-
"CALLABLE" type errors
- Ensure LiteLLMCompletionProvider is connected to filter nodes
- Check that all required connections are made
-
Memory issues with large documents
- Reduce chunk_size in DocumentChunkRecursionFilterNode
- Increase max_iterations accordingly
-
High API costs
- Reduce max_iterations
- Use simpler recursion prompts
- Enable caching during development
-
Inconsistent results
- Check recursion prompt templates
- Verify model temperature settings
- Ensure proper message handling
Performance Tips
- Use smaller chunk sizes for better memory management
- Cache expensive operations during development
- Monitor API usage with complex pipelines
- Test with single iterations before scaling up
Example Workflows
Scientific Paper Analysis
- Use DocumentChunkRecursionFilterNode to process paper sections
- Set recursion prompt to analyze scientific content
- Use multiple iterations to build comprehensive analysis
- Combine with memory provider for cross-referencing
Creative Writing Enhancement
- Start with basic story outline
- Use BasicRecursionFilterNode with creativity-focused prompts
- Multiple iterations to develop characters, plot, dialogue
- Each iteration builds on previous work
Technical Documentation Review
- Process documentation with DocumentChunkRecursionFilterNode
- Use recursion prompts focused on clarity and accuracy
- Memory provider for style guide consistency
- Multiple iterations for comprehensive review
Advanced Configuration
Custom Recursion Prompts
Create specialized prompts for your use case:
- Include specific instructions for your domain
- Use the placeholder system effectively
- Test with different prompt structures
- Consider prompt engineering best practices
Chaining Multiple Filters
You can chain recursion filters by connecting the output of one filter to the inner_recursion_filter input of another:
Filter1 → inner_recursion_filter of Filter2 → AgentNode
This creates complex processing pipelines for specialized workflows.
Integration with Other Nodes
These agent nodes work well with:
- LiteLLMMessage nodes for conversation management
- ListToMessages/MessagesToList for data conversion
- ShowMessages for debugging and visualization
- Various completion nodes for preprocessing
Migration from Legacy Versions
If you're using older versions of these nodes:
- AgentNode was previously just called "Agent Node"
- BasicRecursionFilterNode provides more consistent behavior than older recursion systems
- DocumentChunkRecursionFilterNode replaces manual document splitting workflows
Update your workflows to use the new clearer naming and improved functionality.