|
1 | | -# Portfolio Commentary Copilot |
2 | | - |
3 | | -This is a FastAPI-based copilot service that provides portfolio commentary and analysis capabilities. The service utilizes DeepSeek for reasoning and Perplexity for web search based on user requests. It is designed to work with OpenBB Workspace and can be deployed using Docker or run locally. |
4 | | - |
5 | | -## Architecture |
6 | | - |
7 | | -```sh |
8 | | -┌─────────────────────┐ ┌─────────────────────┐ |
9 | | -│ │ │ │ |
10 | | -│ OpenBB Workspace │ ───────────> │ Portfolio │ |
11 | | -│ (Frontend) │ HTTP │ Commentary │ |
12 | | -│ │ Request │ Copilot │ |
13 | | -│ │ │ (Backend) │ |
14 | | -│ │ <───────────-│ │ |
15 | | -│ │ SSE │ │ |
16 | | -└─────────────────────┘ └─────────────────────┘ |
17 | | -``` |
18 | | - |
19 | | -The architecture consists of two main components: |
20 | | - |
21 | | -1. **OpenBB Workspace (Frontend)**: The user interface where queries are entered |
22 | | -2. **Portfolio Commentary Copilot (Backend)**: A FastAPI service that processes queries and returns analysis |
| 1 | +# Portfolio Commentary Agent |
23 | 2 |
|
24 | | -## Project Structure |
25 | | - |
26 | | -``` |
27 | | -portfolio-commentary/ |
28 | | -├── portfolio_commentary/ |
29 | | -│ ├── main.py # Main FastAPI application |
30 | | -│ ├── functions.py # Core functionality |
31 | | -│ ├── prompts.py # LLM prompts |
32 | | -│ └── agents.json # Copilot configuration |
33 | | -├── common/ # Shared utilities |
34 | | -├── pyproject.toml # Project dependencies |
35 | | -├── Dockerfile # Docker configuration |
36 | | -└── fly.toml # Fly.io deployment config |
37 | | -``` |
| 3 | +A sophisticated AI-powered portfolio analysis agent built for OpenBB Workspace. This agent provides intelligent commentary and insights on portfolio performance, allocation, and risk metrics by analyzing widget data from your dashboard. |
38 | 4 |
|
39 | 5 | ## Getting Started |
40 | 6 |
|
41 | | -Here's how to get your copilot up and running: |
42 | | - |
43 | 7 | ### Prerequisites |
44 | 8 |
|
45 | 9 | - Python 3.10 or higher |
46 | | -- Poetry for dependency management |
| 10 | +- Poetry for dependency management (or pip) |
47 | 11 | - Docker (optional, for containerized deployment) |
48 | | -- Your OpenRouter API key |
| 12 | +- OpenRouter API key for AI model access |
49 | 13 |
|
50 | | -### Installation and Running |
| 14 | +### Environment Setup |
51 | 15 |
|
52 | | -1. Clone this repository to your local machine. |
| 16 | +1. **Clone and navigate to the project:** |
| 17 | +```bash |
| 18 | +cd 99-advanced-examples/70-portfolio-commentary |
| 19 | +``` |
53 | 20 |
|
54 | | -2. Create and activate a virtual environment: |
55 | | -```sh |
56 | | -python -m venv .venv |
57 | | -source .venv/bin/activate # On Windows: .venv\Scripts\activate |
| 21 | +2. **Create environment file:** |
| 22 | +Create a `.env` file with the following variables: |
| 23 | +```env |
| 24 | +OPENROUTER_API_KEY=your_openrouter_api_key_here |
| 25 | +OPENROUTER_MODEL=deepseek/deepseek-chat-v3-0324 |
| 26 | +OPENROUTER_MODEL_PERPLEXITY=perplexity/sonar |
58 | 27 | ``` |
59 | 28 |
|
60 | | -3. Install dependencies using Poetry: |
61 | | -```sh |
| 29 | +### Local Development |
| 30 | + |
| 31 | +**Option 1: Using Poetry** |
| 32 | +```bash |
| 33 | +# Install dependencies |
62 | 34 | poetry install --no-root |
63 | | -``` |
64 | 35 |
|
65 | | -4. Set up environment variables: |
66 | | -Create a `.env` file in the root directory with the following content: |
67 | | -``` |
68 | | -OPENROUTER_API_KEY=<your-api-key> |
| 36 | +# Run the server |
| 37 | +poetry run uvicorn portfolio_commentary.main:app --reload --port 7777 |
69 | 38 | ``` |
70 | 39 |
|
71 | | -5. Run the development server: |
72 | | -```sh |
73 | | -cd 70-portfolio-commentary |
74 | | -poetry run uvicorn portfolio_commentary.main:app --reload --port 7777 |
| 40 | +**Option 2: Using pip** |
| 41 | +```bash |
| 42 | +# Create virtual environment |
| 43 | +python -m venv .venv |
| 44 | +source .venv/bin/activate # On Windows: .venv\Scripts\activate |
| 45 | + |
| 46 | +# Install dependencies |
| 47 | +pip install fastapi uvicorn python-dotenv httpx sse-starlette openbb-ai |
| 48 | + |
| 49 | +# Run the server |
| 50 | +uvicorn portfolio_commentary.main:app --reload --port 7777 |
75 | 51 | ``` |
76 | 52 |
|
77 | 53 | ### Docker Deployment |
78 | 54 |
|
79 | | -1. Build the Docker image: |
80 | | -```sh |
| 55 | +```bash |
| 56 | +# Build the image |
81 | 57 | docker build -t portfolio-commentary . |
82 | | -``` |
83 | 58 |
|
84 | | -2. Run the container: |
85 | | -```sh |
| 59 | +# Run the container |
86 | 60 | docker run -p 7777:7777 --env-file .env portfolio-commentary |
87 | 61 | ``` |
88 | 62 |
|
89 | | -### Fly.io Deployment |
| 63 | +## Configuration |
| 64 | + |
| 65 | +### OpenBB Workspace Integration |
| 66 | + |
| 67 | +The agent automatically registers with OpenBB Workspace through the `/agents.json` endpoint. It supports: |
| 68 | + |
| 69 | +- **Streaming responses** for real-time analysis |
| 70 | +- **Widget dashboard selection** for data source integration |
| 71 | +- **Perplexity search feature** as an optional enhancement |
| 72 | + |
| 73 | +### Feature Toggles |
| 74 | + |
| 75 | +**Perplexity Search** (`perplexity-search`): |
| 76 | +- Enables real-time web search capabilities |
| 77 | +- Provides current market context and news |
| 78 | +- Uses Perplexity model via OpenRouter |
| 79 | +- Default: Disabled |
| 80 | + |
| 81 | +## Usage |
| 82 | + |
| 83 | +### 1. **Add Portfolio Widgets** |
| 84 | +- Add relevant widgets to your OpenBB Workspace dashboard |
| 85 | +- Common widgets: Performance charts, Holdings tables, Allocation breakdowns, Risk metrics |
| 86 | + |
| 87 | +### 2. **Select Widget Data** |
| 88 | +- Use the widget selection feature in OpenBB Workspace |
| 89 | +- The agent will automatically detect and process the selected widget data |
90 | 90 |
|
91 | | -The project includes a `fly.toml` configuration for deployment to Fly.io. To deploy: |
| 91 | +### 3. **Enable Features (Optional)** |
| 92 | +- Toggle "Perplexity for Search" for enhanced market context |
| 93 | +- This adds real-time research capabilities to your analysis |
92 | 94 |
|
93 | | -0. Go into agents.json and change `"query": "https://portfolio-commentary.fly.dev/v1/query"` to `"query": "https://<YOUR-FLY-IO-APP-NAME>.fly.dev/v1/query"`. This will make sure that OpenBB workspace utilizes this endpoint. |
94 | | -1. Install the Fly CLI |
95 | | -2. Run `fly launch` to create a new app |
96 | | -3. Set your environment variables using `fly secrets set` |
97 | | -4. Deploy with `fly deploy` |
| 95 | +### 4. **Request Analysis** |
| 96 | +- Ask questions about your portfolio |
| 97 | +- Examples: |
| 98 | + - "Provide a commentary on my portfolio performance" |
| 99 | + - "What are the key risks in my current allocation?" |
| 100 | + - "How has my portfolio performed relative to benchmarks?" |
98 | 101 |
|
99 | | -### Accessing the Documentation |
| 102 | +## API Endpoints |
| 103 | + |
| 104 | +- **GET `/agents.json`**: Agent configuration for OpenBB Workspace |
| 105 | +- **POST `/v1/query`**: Main query endpoint for portfolio analysis |
| 106 | +- **GET `/docs`**: Interactive API documentation (FastAPI Swagger UI) |
| 107 | + |
| 108 | +## Technical Details |
| 109 | + |
| 110 | +### AI Models |
| 111 | +- **Default**: DeepSeek Chat v3 (cost-effective, high-quality analysis) |
| 112 | +- **Enhanced**: Perplexity Sonar (when search feature is enabled) |
| 113 | +- **Streaming**: Real-time response delivery via Server-Sent Events |
| 114 | + |
| 115 | +### Dependencies |
| 116 | +- **FastAPI**: Web framework and API server |
| 117 | +- **OpenBB AI**: Widget data integration and processing |
| 118 | +- **OpenRouter**: AI model access and management |
| 119 | +- **httpx**: HTTP client for external API calls |
| 120 | + |
| 121 | +### Dynamic Prompt System |
| 122 | +The agent uses intelligent prompt generation that adapts based on: |
| 123 | +- Available widget data and context |
| 124 | +- Enabled features (Perplexity search) |
| 125 | +- Analysis requirements and user requests |
| 126 | + |
| 127 | +## Development |
| 128 | + |
| 129 | +### Local Testing |
| 130 | +```bash |
| 131 | +# Access API documentation |
| 132 | +http://localhost:7777/docs |
| 133 | + |
| 134 | +# Test agent configuration |
| 135 | +curl http://localhost:7777/agents.json |
| 136 | +``` |
100 | 137 |
|
101 | | -Once the API server is running, you can view the documentation and interact with |
102 | | -the API by visiting: http://localhost:7777/docs |
| 138 | +### Debugging |
| 139 | +The application logs detailed information about: |
| 140 | +- Widget data processing |
| 141 | +- AI model selection (DeepSeek vs Perplexity) |
| 142 | +- API calls and responses |
| 143 | +- Error handling and fallbacks |
103 | 144 |
|
104 | 145 | ## License |
105 | 146 |
|
106 | | -This project is licensed under the MIT License - see the LICENSE file for details. |
| 147 | +This project is licensed under the MIT License. |
0 commit comments