parent
b51bc2093b
commit
d185180430
@ -1,429 +0,0 @@
|
|||||||
# 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/)
|
|
||||||
|
|
||||||
@ -1,194 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
# vLLM Integration Guide
|
|
||||||
|
|
||||||
!!! info "Overview"
|
|
||||||
vLLM is a high-performance and easy-to-use library for LLM inference and serving. This guide explains how to integrate vLLM with Swarms for efficient, production-grade language model deployment.
|
|
||||||
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
!!! note "Prerequisites"
|
|
||||||
Before you begin, make sure you have Python 3.8+ installed on your system.
|
|
||||||
|
|
||||||
=== "pip"
|
|
||||||
```bash
|
|
||||||
pip install -U vllm swarms
|
|
||||||
```
|
|
||||||
|
|
||||||
=== "poetry"
|
|
||||||
```bash
|
|
||||||
poetry add vllm swarms
|
|
||||||
```
|
|
||||||
|
|
||||||
## Basic Usage
|
|
||||||
|
|
||||||
Here's a simple example of how to use vLLM with Swarms:
|
|
||||||
|
|
||||||
```python title="basic_usage.py"
|
|
||||||
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.",
|
|
||||||
temperature=0.7,
|
|
||||||
max_tokens=4000
|
|
||||||
)
|
|
||||||
|
|
||||||
# Run inference
|
|
||||||
response = vllm.run("What is the capital of France?")
|
|
||||||
print(response)
|
|
||||||
```
|
|
||||||
|
|
||||||
## VLLMWrapper Class
|
|
||||||
|
|
||||||
!!! abstract "Class Overview"
|
|
||||||
The `VLLMWrapper` class provides a convenient interface for working with vLLM models.
|
|
||||||
|
|
||||||
### Key Parameters
|
|
||||||
|
|
||||||
| Parameter | Type | Description | Default |
|
|
||||||
|-----------|------|-------------|---------|
|
|
||||||
| `model_name` | str | Name of the model to use | "meta-llama/Llama-2-7b-chat-hf" |
|
|
||||||
| `system_prompt` | str | System prompt to use | None |
|
|
||||||
| `stream` | bool | Whether to stream the output | False |
|
|
||||||
| `temperature` | float | Sampling temperature | 0.5 |
|
|
||||||
| `max_tokens` | int | Maximum number of tokens to generate | 4000 |
|
|
||||||
|
|
||||||
### Example with Custom Parameters
|
|
||||||
|
|
||||||
```python title="custom_parameters.py"
|
|
||||||
vllm = VLLMWrapper(
|
|
||||||
model_name="meta-llama/Llama-2-13b-chat-hf",
|
|
||||||
system_prompt="You are an expert in artificial intelligence.",
|
|
||||||
temperature=0.8,
|
|
||||||
max_tokens=2000
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Integration with Agents
|
|
||||||
|
|
||||||
You can easily integrate vLLM with Swarms agents for more complex workflows:
|
|
||||||
|
|
||||||
```python title="agent_integration.py"
|
|
||||||
from swarms import Agent
|
|
||||||
from swarms.utils.vllm_wrapper import VLLMWrapper
|
|
||||||
|
|
||||||
# Initialize vLLM
|
|
||||||
vllm = VLLMWrapper(
|
|
||||||
model_name="meta-llama/Llama-2-7b-chat-hf",
|
|
||||||
system_prompt="You are a helpful assistant."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create an agent with vLLM
|
|
||||||
agent = Agent(
|
|
||||||
agent_name="Research-Agent",
|
|
||||||
agent_description="Expert in conducting research and analysis",
|
|
||||||
system_prompt="""You are an expert research agent. Your tasks include:
|
|
||||||
1. Analyzing complex topics
|
|
||||||
2. Providing detailed summaries
|
|
||||||
3. Making data-driven recommendations""",
|
|
||||||
llm=vllm,
|
|
||||||
max_loops=1
|
|
||||||
)
|
|
||||||
|
|
||||||
# Run the agent
|
|
||||||
response = agent.run("Research the impact of AI on healthcare")
|
|
||||||
```
|
|
||||||
|
|
||||||
## Advanced Features
|
|
||||||
|
|
||||||
### Batch Processing
|
|
||||||
|
|
||||||
!!! tip "Performance Optimization"
|
|
||||||
Use batch processing for efficient handling of multiple tasks simultaneously.
|
|
||||||
|
|
||||||
```python title="batch_processing.py"
|
|
||||||
tasks = [
|
|
||||||
"What is machine learning?",
|
|
||||||
"Explain neural networks",
|
|
||||||
"Describe deep learning"
|
|
||||||
]
|
|
||||||
|
|
||||||
results = vllm.batched_run(tasks, batch_size=3)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Error Handling
|
|
||||||
|
|
||||||
!!! warning "Error Management"
|
|
||||||
Always implement proper error handling in production environments.
|
|
||||||
|
|
||||||
```python title="error_handling.py"
|
|
||||||
from loguru import logger
|
|
||||||
|
|
||||||
try:
|
|
||||||
response = vllm.run("Complex task")
|
|
||||||
except Exception as error:
|
|
||||||
logger.error(f"Error occurred: {error}")
|
|
||||||
```
|
|
||||||
|
|
||||||
## Best Practices
|
|
||||||
|
|
||||||
!!! success "Recommended Practices"
|
|
||||||
=== "Model Selection"
|
|
||||||
- Choose appropriate model sizes based on your requirements
|
|
||||||
- Consider the trade-off between model size and inference speed
|
|
||||||
|
|
||||||
=== "System Resources"
|
|
||||||
- Ensure sufficient GPU memory for your chosen model
|
|
||||||
- Monitor resource usage during batch processing
|
|
||||||
|
|
||||||
=== "Prompt Engineering"
|
|
||||||
- Use clear and specific system prompts
|
|
||||||
- Structure user prompts for optimal results
|
|
||||||
|
|
||||||
=== "Error Handling"
|
|
||||||
- Implement proper error handling and logging
|
|
||||||
- Set up monitoring for production deployments
|
|
||||||
|
|
||||||
=== "Performance"
|
|
||||||
- Use batch processing for multiple tasks
|
|
||||||
- Adjust max_tokens based on your use case
|
|
||||||
- Fine-tune temperature for optimal output quality
|
|
||||||
|
|
||||||
## Example: Multi-Agent System
|
|
||||||
|
|
||||||
Here's an example of creating a multi-agent system using vLLM:
|
|
||||||
|
|
||||||
```python title="multi_agent_system.py"
|
|
||||||
from swarms import Agent, ConcurrentWorkflow
|
|
||||||
from swarms.utils.vllm_wrapper import VLLMWrapper
|
|
||||||
|
|
||||||
# Initialize vLLM
|
|
||||||
vllm = VLLMWrapper(
|
|
||||||
model_name="meta-llama/Llama-2-7b-chat-hf",
|
|
||||||
system_prompt="You are a helpful assistant."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create specialized agents
|
|
||||||
research_agent = Agent(
|
|
||||||
agent_name="Research-Agent",
|
|
||||||
agent_description="Expert in research",
|
|
||||||
system_prompt="You are a research expert.",
|
|
||||||
llm=vllm
|
|
||||||
)
|
|
||||||
|
|
||||||
analysis_agent = Agent(
|
|
||||||
agent_name="Analysis-Agent",
|
|
||||||
agent_description="Expert in analysis",
|
|
||||||
system_prompt="You are an analysis expert.",
|
|
||||||
llm=vllm
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create a workflow
|
|
||||||
agents = [research_agent, analysis_agent]
|
|
||||||
workflow = ConcurrentWorkflow(
|
|
||||||
name="Research-Analysis-Workflow",
|
|
||||||
description="Comprehensive research and analysis workflow",
|
|
||||||
agents=agents
|
|
||||||
)
|
|
||||||
|
|
||||||
# Run the workflow
|
|
||||||
result = workflow.run("Analyze the impact of renewable energy")
|
|
||||||
```
|
|
||||||
@ -1,214 +0,0 @@
|
|||||||
from swarms import Agent, ConcurrentWorkflow
|
|
||||||
from swarms.utils.vllm_wrapper import VLLMWrapper
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
# 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"
|
|
||||||
)
|
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
from swarms.utils.vllm_wrapper import VLLMWrapper
|
||||||
|
|
||||||
|
# Initialize the vLLM wrapper
|
||||||
|
vllm = VLLMWrapper(
|
||||||
|
model_name="gpt-4o-mini",
|
||||||
|
system_prompt="You are a helpful assistant.",
|
||||||
|
temperature=0.7,
|
||||||
|
max_tokens=4000
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run inference
|
||||||
|
response = vllm.run("What is the capital of France?")
|
||||||
|
print(response)
|
||||||
Loading…
Reference in new issue