You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
429 lines
13 KiB
429 lines
13 KiB
# VLLM Swarm Agents
|
|
|
|
!!! tip "Quick Summary"
|
|
This guide demonstrates how to create a sophisticated multi-agent system using VLLM and Swarms for comprehensive stock market analysis. You'll learn how to configure and orchestrate multiple AI agents working together to provide deep market insights.
|
|
|
|
## Overview
|
|
|
|
The example showcases how to build a stock analysis system with 5 specialized agents:
|
|
|
|
- Technical Analysis Agent
|
|
- Fundamental Analysis Agent
|
|
- Market Sentiment Agent
|
|
- Quantitative Strategy Agent
|
|
- Portfolio Strategy Agent
|
|
|
|
Each agent has specific expertise and works collaboratively through a concurrent workflow.
|
|
|
|
## Prerequisites
|
|
|
|
!!! warning "Requirements"
|
|
Before starting, ensure you have:
|
|
|
|
- Python 3.7 or higher
|
|
- The Swarms package installed
|
|
- Access to VLLM compatible models
|
|
- Sufficient compute resources for running VLLM
|
|
|
|
## Installation
|
|
|
|
!!! example "Setup Steps"
|
|
|
|
1. Install the Swarms package:
|
|
```bash
|
|
pip install swarms
|
|
```
|
|
|
|
2. Install VLLM dependencies (if not already installed):
|
|
```bash
|
|
pip install vllm
|
|
```
|
|
|
|
## Basic Usage
|
|
|
|
Here's a complete example of setting up the stock analysis swarm:
|
|
|
|
```python
|
|
from swarms import Agent, ConcurrentWorkflow
|
|
from swarms.utils.vllm_wrapper import VLLMWrapper
|
|
|
|
# Initialize the VLLM wrapper
|
|
vllm = VLLMWrapper(
|
|
model_name="meta-llama/Llama-2-7b-chat-hf",
|
|
system_prompt="You are a helpful assistant.",
|
|
)
|
|
```
|
|
|
|
!!! note "Model Selection"
|
|
The example uses Llama-2-7b-chat, but you can use any VLLM-compatible model. Make sure you have the necessary permissions and resources to run your chosen model.
|
|
|
|
## Agent Configuration
|
|
|
|
### Technical Analysis Agent
|
|
|
|
```python
|
|
technical_analyst = Agent(
|
|
agent_name="Technical-Analysis-Agent",
|
|
agent_description="Expert in technical analysis and chart patterns",
|
|
system_prompt="""You are an expert Technical Analysis Agent specializing in market technicals and chart patterns. Your responsibilities include:
|
|
|
|
1. PRICE ACTION ANALYSIS
|
|
- Identify key support and resistance levels
|
|
- Analyze price trends and momentum
|
|
- Detect chart patterns (e.g., head & shoulders, triangles, flags)
|
|
- Evaluate volume patterns and their implications
|
|
|
|
2. TECHNICAL INDICATORS
|
|
- Calculate and interpret moving averages (SMA, EMA)
|
|
- Analyze momentum indicators (RSI, MACD, Stochastic)
|
|
- Evaluate volume indicators (OBV, Volume Profile)
|
|
- Monitor volatility indicators (Bollinger Bands, ATR)
|
|
|
|
3. TRADING SIGNALS
|
|
- Generate clear buy/sell signals based on technical criteria
|
|
- Identify potential entry and exit points
|
|
- Set appropriate stop-loss and take-profit levels
|
|
- Calculate position sizing recommendations
|
|
|
|
4. RISK MANAGEMENT
|
|
- Assess market volatility and trend strength
|
|
- Identify potential reversal points
|
|
- Calculate risk/reward ratios for trades
|
|
- Suggest position sizing based on risk parameters
|
|
|
|
Your analysis should be data-driven, precise, and actionable. Always include specific price levels, time frames, and risk parameters in your recommendations.""",
|
|
max_loops=1,
|
|
llm=vllm,
|
|
)
|
|
```
|
|
|
|
!!! tip "Agent Customization"
|
|
Each agent can be customized with different:
|
|
|
|
- System prompts
|
|
|
|
- Temperature settings
|
|
|
|
- Max token limits
|
|
|
|
- Response formats
|
|
|
|
## Running the Swarm
|
|
|
|
To execute the swarm analysis:
|
|
|
|
```python
|
|
swarm = ConcurrentWorkflow(
|
|
name="Stock-Analysis-Swarm",
|
|
description="A swarm of agents that analyze stocks and provide comprehensive analysis.",
|
|
agents=stock_analysis_agents,
|
|
)
|
|
|
|
# Run the analysis
|
|
response = swarm.run("Analyze the best etfs for gold and other similar commodities in volatile markets")
|
|
```
|
|
|
|
|
|
|
|
## Full Code Example
|
|
|
|
```python
|
|
from swarms import Agent, ConcurrentWorkflow
|
|
from swarms.utils.vllm_wrapper import VLLMWrapper
|
|
|
|
# Initialize the VLLM wrapper
|
|
vllm = VLLMWrapper(
|
|
model_name="meta-llama/Llama-2-7b-chat-hf",
|
|
system_prompt="You are a helpful assistant.",
|
|
)
|
|
|
|
# Technical Analysis Agent
|
|
technical_analyst = Agent(
|
|
agent_name="Technical-Analysis-Agent",
|
|
agent_description="Expert in technical analysis and chart patterns",
|
|
system_prompt="""You are an expert Technical Analysis Agent specializing in market technicals and chart patterns. Your responsibilities include:
|
|
|
|
1. PRICE ACTION ANALYSIS
|
|
- Identify key support and resistance levels
|
|
- Analyze price trends and momentum
|
|
- Detect chart patterns (e.g., head & shoulders, triangles, flags)
|
|
- Evaluate volume patterns and their implications
|
|
|
|
2. TECHNICAL INDICATORS
|
|
- Calculate and interpret moving averages (SMA, EMA)
|
|
- Analyze momentum indicators (RSI, MACD, Stochastic)
|
|
- Evaluate volume indicators (OBV, Volume Profile)
|
|
- Monitor volatility indicators (Bollinger Bands, ATR)
|
|
|
|
3. TRADING SIGNALS
|
|
- Generate clear buy/sell signals based on technical criteria
|
|
- Identify potential entry and exit points
|
|
- Set appropriate stop-loss and take-profit levels
|
|
- Calculate position sizing recommendations
|
|
|
|
4. RISK MANAGEMENT
|
|
- Assess market volatility and trend strength
|
|
- Identify potential reversal points
|
|
- Calculate risk/reward ratios for trades
|
|
- Suggest position sizing based on risk parameters
|
|
|
|
Your analysis should be data-driven, precise, and actionable. Always include specific price levels, time frames, and risk parameters in your recommendations.""",
|
|
max_loops=1,
|
|
llm=vllm,
|
|
)
|
|
|
|
# Fundamental Analysis Agent
|
|
fundamental_analyst = Agent(
|
|
agent_name="Fundamental-Analysis-Agent",
|
|
agent_description="Expert in company fundamentals and valuation",
|
|
system_prompt="""You are an expert Fundamental Analysis Agent specializing in company valuation and financial metrics. Your core responsibilities include:
|
|
|
|
1. FINANCIAL STATEMENT ANALYSIS
|
|
- Analyze income statements, balance sheets, and cash flow statements
|
|
- Calculate and interpret key financial ratios
|
|
- Evaluate revenue growth and profit margins
|
|
- Assess company's debt levels and cash position
|
|
|
|
2. VALUATION METRICS
|
|
- Calculate fair value using multiple valuation methods:
|
|
* Discounted Cash Flow (DCF)
|
|
* Price-to-Earnings (P/E)
|
|
* Price-to-Book (P/B)
|
|
* Enterprise Value/EBITDA
|
|
- Compare valuations against industry peers
|
|
|
|
3. BUSINESS MODEL ASSESSMENT
|
|
- Evaluate competitive advantages and market position
|
|
- Analyze industry dynamics and market share
|
|
- Assess management quality and corporate governance
|
|
- Identify potential risks and growth opportunities
|
|
|
|
4. ECONOMIC CONTEXT
|
|
- Consider macroeconomic factors affecting the company
|
|
- Analyze industry cycles and trends
|
|
- Evaluate regulatory environment and compliance
|
|
- Assess global market conditions
|
|
|
|
Your analysis should be comprehensive, focusing on both quantitative metrics and qualitative factors that impact long-term value.""",
|
|
max_loops=1,
|
|
llm=vllm,
|
|
)
|
|
|
|
# Market Sentiment Agent
|
|
sentiment_analyst = Agent(
|
|
agent_name="Market-Sentiment-Agent",
|
|
agent_description="Expert in market psychology and sentiment analysis",
|
|
system_prompt="""You are an expert Market Sentiment Agent specializing in analyzing market psychology and investor behavior. Your key responsibilities include:
|
|
|
|
1. SENTIMENT INDICATORS
|
|
- Monitor and interpret market sentiment indicators:
|
|
* VIX (Fear Index)
|
|
* Put/Call Ratio
|
|
* Market Breadth
|
|
* Investor Surveys
|
|
- Track institutional vs retail investor behavior
|
|
|
|
2. NEWS AND SOCIAL MEDIA ANALYSIS
|
|
- Analyze news flow and media sentiment
|
|
- Monitor social media trends and discussions
|
|
- Track analyst recommendations and changes
|
|
- Evaluate corporate insider trading patterns
|
|
|
|
3. MARKET POSITIONING
|
|
- Assess hedge fund positioning and exposure
|
|
- Monitor short interest and short squeeze potential
|
|
- Track fund flows and asset allocation trends
|
|
- Analyze options market sentiment
|
|
|
|
4. CONTRARIAN SIGNALS
|
|
- Identify extreme sentiment readings
|
|
- Detect potential market turning points
|
|
- Analyze historical sentiment patterns
|
|
- Provide contrarian trading opportunities
|
|
|
|
Your analysis should combine quantitative sentiment metrics with qualitative assessment of market psychology and crowd behavior.""",
|
|
max_loops=1,
|
|
llm=vllm,
|
|
)
|
|
|
|
# Quantitative Strategy Agent
|
|
quant_analyst = Agent(
|
|
agent_name="Quantitative-Strategy-Agent",
|
|
agent_description="Expert in quantitative analysis and algorithmic strategies",
|
|
system_prompt="""You are an expert Quantitative Strategy Agent specializing in data-driven investment strategies. Your primary responsibilities include:
|
|
|
|
1. FACTOR ANALYSIS
|
|
- Analyze and monitor factor performance:
|
|
* Value
|
|
* Momentum
|
|
* Quality
|
|
* Size
|
|
* Low Volatility
|
|
- Calculate factor exposures and correlations
|
|
|
|
2. STATISTICAL ANALYSIS
|
|
- Perform statistical arbitrage analysis
|
|
- Calculate and monitor pair trading opportunities
|
|
- Analyze market anomalies and inefficiencies
|
|
- Develop mean reversion strategies
|
|
|
|
3. RISK MODELING
|
|
- Build and maintain risk models
|
|
- Calculate portfolio optimization metrics
|
|
- Monitor correlation matrices
|
|
- Analyze tail risk and stress scenarios
|
|
|
|
4. ALGORITHMIC STRATEGIES
|
|
- Develop systematic trading strategies
|
|
- Backtest and validate trading algorithms
|
|
- Monitor strategy performance metrics
|
|
- Optimize execution algorithms
|
|
|
|
Your analysis should be purely quantitative, based on statistical evidence and mathematical models rather than subjective opinions.""",
|
|
max_loops=1,
|
|
llm=vllm,
|
|
)
|
|
|
|
# Portfolio Strategy Agent
|
|
portfolio_strategist = Agent(
|
|
agent_name="Portfolio-Strategy-Agent",
|
|
agent_description="Expert in portfolio management and asset allocation",
|
|
system_prompt="""You are an expert Portfolio Strategy Agent specializing in portfolio construction and management. Your core responsibilities include:
|
|
|
|
1. ASSET ALLOCATION
|
|
- Develop strategic asset allocation frameworks
|
|
- Recommend tactical asset allocation shifts
|
|
- Optimize portfolio weightings
|
|
- Balance risk and return objectives
|
|
|
|
2. PORTFOLIO ANALYSIS
|
|
- Calculate portfolio risk metrics
|
|
- Monitor sector and factor exposures
|
|
- Analyze portfolio correlation matrix
|
|
- Track performance attribution
|
|
|
|
3. RISK MANAGEMENT
|
|
- Implement portfolio hedging strategies
|
|
- Monitor and adjust position sizing
|
|
- Set stop-loss and rebalancing rules
|
|
- Develop drawdown protection strategies
|
|
|
|
4. PORTFOLIO OPTIMIZATION
|
|
- Calculate efficient frontier analysis
|
|
- Optimize for various objectives:
|
|
* Maximum Sharpe Ratio
|
|
* Minimum Volatility
|
|
* Maximum Diversification
|
|
- Consider transaction costs and taxes
|
|
|
|
Your recommendations should focus on portfolio-level decisions that optimize risk-adjusted returns while meeting specific investment objectives.""",
|
|
max_loops=1,
|
|
llm=vllm,
|
|
)
|
|
|
|
# Create a list of all agents
|
|
stock_analysis_agents = [
|
|
technical_analyst,
|
|
fundamental_analyst,
|
|
sentiment_analyst,
|
|
quant_analyst,
|
|
portfolio_strategist
|
|
]
|
|
|
|
swarm = ConcurrentWorkflow(
|
|
name="Stock-Analysis-Swarm",
|
|
description="A swarm of agents that analyze stocks and provide a comprehensive analysis of the current trends and opportunities.",
|
|
agents=stock_analysis_agents,
|
|
)
|
|
|
|
swarm.run("Analyze the best etfs for gold and other similiar commodities in volatile markets")
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
!!! success "Optimization Tips"
|
|
1. **Agent Design**
|
|
- Keep system prompts focused and specific
|
|
|
|
- Use clear role definitions
|
|
|
|
- Include error handling guidelines
|
|
|
|
2. **Resource Management**
|
|
|
|
- Monitor memory usage with large models
|
|
|
|
- Implement proper cleanup procedures
|
|
|
|
- Use batching for multiple queries
|
|
|
|
3. **Output Handling**
|
|
|
|
- Implement proper logging
|
|
|
|
- Format outputs consistently
|
|
|
|
- Include error checking
|
|
|
|
## Common Issues and Solutions
|
|
|
|
!!! warning "Troubleshooting"
|
|
Common issues you might encounter:
|
|
|
|
1. **Memory Issues**
|
|
|
|
- *Problem*: VLLM consuming too much memory
|
|
|
|
- *Solution*: Adjust batch sizes and model parameters
|
|
|
|
2. **Agent Coordination**
|
|
|
|
- *Problem*: Agents providing conflicting information
|
|
|
|
- *Solution*: Implement consensus mechanisms or priority rules
|
|
|
|
3. **Performance**
|
|
|
|
- *Problem*: Slow response times
|
|
|
|
- *Solution*: Use proper batching and optimize model loading
|
|
|
|
## FAQ
|
|
|
|
??? question "Can I use different models for different agents?"
|
|
Yes, you can initialize multiple VLLM wrappers with different models for each agent. However, be mindful of memory usage.
|
|
|
|
??? question "How many agents can run concurrently?"
|
|
The number depends on your hardware resources. Start with 3-5 agents and scale based on performance.
|
|
|
|
??? question "Can I customize agent communication patterns?"
|
|
Yes, you can modify the ConcurrentWorkflow class or create custom workflows for specific communication patterns.
|
|
|
|
## Advanced Configuration
|
|
|
|
!!! example "Extended Settings"
|
|
```python
|
|
vllm = VLLMWrapper(
|
|
model_name="meta-llama/Llama-2-7b-chat-hf",
|
|
system_prompt="You are a helpful assistant.",
|
|
temperature=0.7,
|
|
max_tokens=2048,
|
|
top_p=0.95,
|
|
)
|
|
```
|
|
|
|
## Contributing
|
|
|
|
!!! info "Get Involved"
|
|
We welcome contributions! Here's how you can help:
|
|
|
|
1. Report bugs and issues
|
|
2. Submit feature requests
|
|
3. Contribute to documentation
|
|
4. Share example use cases
|
|
|
|
## Resources
|
|
|
|
!!! abstract "Additional Reading"
|
|
- [VLLM Documentation](https://docs.vllm.ai/en/latest/)
|
|
|