Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
1c9bbac
Fix the history command for finance advisor example
zhangfengcdt Jul 22, 2025
20de49d
fix history serde issue
zhangfengcdt Jul 22, 2025
c62393d
fix history commit issue
zhangfengcdt Jul 22, 2025
d30bde5
fix commit_prolly_metadata
zhangfengcdt Jul 22, 2025
e4c7fed
add readme file for finance advisor example
zhangfengcdt Jul 22, 2025
ca2a571
fix the memory command
zhangfengcdt Jul 22, 2025
2ee99fc
fix clippy issue
zhangfengcdt Jul 22, 2025
84e54dd
Initial commit
zhangfengcdt Jul 22, 2025
e04ca81
Update prolly metadata after commit: Initial commit
zhangfengcdt Jul 22, 2025
0c06678
Create table: market_data
zhangfengcdt Jul 22, 2025
d092898
Update prolly metadata after commit: Create table: market_data
zhangfengcdt Jul 22, 2025
baa3fc7
Create table: recommendations
zhangfengcdt Jul 22, 2025
6b34359
Update prolly metadata after commit: Create table: recommendations
zhangfengcdt Jul 22, 2025
3bb2722
Create table: audit_log
zhangfengcdt Jul 22, 2025
734b9b1
Update prolly metadata after commit: Create table: audit_log
zhangfengcdt Jul 22, 2025
5f1418a
Create table: cross_references
zhangfengcdt Jul 22, 2025
03f22a6
Update prolly metadata after commit: Create table: cross_references
zhangfengcdt Jul 22, 2025
67bdb55
Create table: client_profiles
zhangfengcdt Jul 22, 2025
33f110c
Update prolly metadata after commit: Create table: client_profiles
zhangfengcdt Jul 22, 2025
5b0f444
Initial commit
zhangfengcdt Jul 22, 2025
b345a81
Update prolly metadata after commit: Initial commit
zhangfengcdt Jul 22, 2025
415612e
Create table: market_data
zhangfengcdt Jul 22, 2025
6055f9d
Update prolly metadata after commit: Create table: market_data
zhangfengcdt Jul 22, 2025
adceba9
Create table: recommendations
zhangfengcdt Jul 22, 2025
81e6a30
Update prolly metadata after commit: Create table: recommendations
zhangfengcdt Jul 22, 2025
40511f1
Create table: audit_log
zhangfengcdt Jul 22, 2025
a78be5d
Update prolly metadata after commit: Create table: audit_log
zhangfengcdt Jul 22, 2025
91cabd7
Create table: cross_references
zhangfengcdt Jul 22, 2025
8472ead
Update prolly metadata after commit: Create table: cross_references
zhangfengcdt Jul 22, 2025
ca5d690
Create table: client_profiles
zhangfengcdt Jul 22, 2025
6212bf6
Update prolly metadata after commit: Create table: client_profiles
zhangfengcdt Jul 22, 2025
614b71e
Initial commit
zhangfengcdt Jul 22, 2025
861a728
Update prolly metadata after commit: Initial commit
zhangfengcdt Jul 22, 2025
06324c0
Create table: market_data
zhangfengcdt Jul 22, 2025
5b062af
Update prolly metadata after commit: Create table: market_data
zhangfengcdt Jul 22, 2025
5a1d5b9
Create table: recommendations
zhangfengcdt Jul 22, 2025
e6293e3
Update prolly metadata after commit: Create table: recommendations
zhangfengcdt Jul 22, 2025
344b56a
Create table: audit_log
zhangfengcdt Jul 22, 2025
8693af0
Update prolly metadata after commit: Create table: audit_log
zhangfengcdt Jul 22, 2025
0b7dc7d
Create table: cross_references
zhangfengcdt Jul 22, 2025
356c4f9
Update prolly metadata after commit: Create table: cross_references
zhangfengcdt Jul 22, 2025
3c919f7
Create table: client_profiles
zhangfengcdt Jul 22, 2025
ed0ed9f
Update prolly metadata after commit: Create table: client_profiles
zhangfengcdt Jul 22, 2025
49b84b1
Initial commit
zhangfengcdt Jul 22, 2025
0bf191a
Update prolly metadata after commit: Initial commit
zhangfengcdt Jul 22, 2025
338df95
Create table: market_data
zhangfengcdt Jul 22, 2025
a65c75e
Update prolly metadata after commit: Create table: market_data
zhangfengcdt Jul 22, 2025
b76c767
Create table: recommendations
zhangfengcdt Jul 22, 2025
7fb9dea
Update prolly metadata after commit: Create table: recommendations
zhangfengcdt Jul 22, 2025
a145b89
Create table: audit_log
zhangfengcdt Jul 22, 2025
bc5ec5e
Update prolly metadata after commit: Create table: audit_log
zhangfengcdt Jul 22, 2025
6e65805
Create table: cross_references
zhangfengcdt Jul 22, 2025
981e92f
Update prolly metadata after commit: Create table: cross_references
zhangfengcdt Jul 22, 2025
21e69d9
Create table: client_profiles
zhangfengcdt Jul 22, 2025
30d59d7
Update prolly metadata after commit: Create table: client_profiles
zhangfengcdt Jul 22, 2025
31f89a7
Initial commit
zhangfengcdt Jul 22, 2025
35931db
Update prolly metadata after commit: Initial commit
zhangfengcdt Jul 22, 2025
be8cde4
Create table: market_data
zhangfengcdt Jul 22, 2025
c2e1363
Update prolly metadata after commit: Create table: market_data
zhangfengcdt Jul 22, 2025
be97df6
Create table: recommendations
zhangfengcdt Jul 22, 2025
8a726af
Update prolly metadata after commit: Create table: recommendations
zhangfengcdt Jul 22, 2025
7ea4f45
Create table: audit_log
zhangfengcdt Jul 22, 2025
ebcbdff
Update prolly metadata after commit: Create table: audit_log
zhangfengcdt Jul 22, 2025
051815e
Create table: cross_references
zhangfengcdt Jul 22, 2025
2e7b3b5
Update prolly metadata after commit: Create table: cross_references
zhangfengcdt Jul 22, 2025
8c1e2ad
Create table: client_profiles
zhangfengcdt Jul 22, 2025
5b5693c
Update prolly metadata after commit: Create table: client_profiles
zhangfengcdt Jul 22, 2025
166747e
fix recommend engine
zhangfengcdt Jul 22, 2025
afc16f6
combine data and meta commit
zhangfengcdt Jul 23, 2025
b3707cd
remove test data
zhangfengcdt Jul 23, 2025
5f4bb4b
fix
zhangfengcdt Jul 23, 2025
cd85204
fix compile errors
zhangfengcdt Jul 23, 2025
a9fbd97
fix fmt issue
zhangfengcdt Jul 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions examples/financial_advisor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# Financial Advisory AI with Versioned Memory

A demonstration of an AI-powered financial advisory system using ProllyTree for versioned memory management. This example showcases how to build a secure, auditable AI agent that maintains consistent memory across time and can handle complex financial recommendations with full traceability.

## Features

- 🤖 **AI-Powered Recommendations**: Uses OpenAI's API to generate intelligent investment advice
- 📊 **Multi-Source Data Validation**: Cross-validates market data from multiple sources
- 🔒 **Security Monitoring**: Detects and prevents injection attacks and anomalies
- 📚 **Versioned Memory**: Uses ProllyTree to maintain git-like versioned storage of all data
- 🕐 **Temporal Queries**: Query recommendations and data as they existed at any point in time
- 🌿 **Branch Management**: Create memory branches for different scenarios or clients
- 📝 **Audit Trail**: Complete audit logs for compliance and debugging
- 🎯 **Risk-Aware**: Adapts recommendations based on client risk tolerance

## Prerequisites

- Rust (latest stable version)
- Git (for memory versioning)
- OpenAI API key (optional, for AI-enhanced reasoning)

## Quick Start

### 1. Initialize Storage Directory

First, create a directory with git repository for the advisor's memory:

```bash
# Create a directory for the advisor's memory
mkdir -p /tmp/advisor
cd /tmp/advisor

# Initialize git repository (required for versioned memory)
git init

# Return to the project directory
cd /path/to/prollytree
```

### 2. Set Environment Variables (Optional)

For AI-enhanced recommendations, set your OpenAI API key:

```bash
export OPENAI_API_KEY="your-api-key-here"
```

### 3. Run the Financial Advisor

```bash
# Basic usage with temporary storage
cargo run --example financial_advisor -- --storage /tmp/advisor/data advise

# Or use the shorter form
cargo run -- --storage /tmp/advisor/data advise
```

## Usage

### Interactive Commands

Once the advisor is running, you can use these commands:

#### Core Operations
- `recommend <SYMBOL>` - Get AI-powered recommendation for a stock symbol (e.g., `recommend AAPL`)
- `profile` - Show current client profile
- `risk <LEVEL>` - Set risk tolerance (`conservative`, `moderate`, or `aggressive`)

#### History and Analysis
- `history` - Show recent recommendations
- `history <commit>` - Show recommendations at a specific git commit
- `history --branch <name>` - Show recommendations from a specific branch
- `memory` - Show memory system status and statistics
- `audit` - Show complete audit trail

#### Advanced Features
- `branch <NAME>` - Create a new memory branch
- `visualize` - Show memory tree visualization
- `test-inject <TEXT>` - Test security monitoring (try malicious inputs)

#### Other Commands
- `help` - Show all available commands
- `exit` or `quit` - Exit the advisor

### Example Session

```bash
🏦> recommend AAPL
📊 Recommendation Generated
Symbol: AAPL
Action: BUY
Confidence: 52.0%
Reasoning: Analysis of AAPL at $177.89 with P/E ratio 28.4...

🏦> risk aggressive
✅ Risk tolerance set to: Aggressive

🏦> recommend AAPL
📊 Recommendation Generated
Symbol: AAPL
Action: BUY
Confidence: 60.0%
(Notice higher confidence for aggressive risk tolerance)

🏦> history
📜 Recent Recommendations
📊 Recommendation #1
Symbol: AAPL
Action: BUY
Confidence: 60.0%
...
📊 Recommendation #2
Symbol: AAPL
Action: BUY
Confidence: 52.0%
...

🏦> memory
🧠 Memory Status
✅ Memory validation: ACTIVE
🛡️ Security monitoring: ENABLED
📝 Audit trail: ENABLED
🌿 Current branch: main
📊 Total commits: 15
💡 Recommendations: 2
```

## Command Line Options

```bash
cargo run -- [OPTIONS] <COMMAND>

Commands:
advise Start interactive advisory session
visualize Visualize memory evolution
attack Run attack simulations
benchmark Run performance benchmarks
memory Git memory operations
examples Show integration examples
audit Audit memory for compliance

Options:
-s, --storage <PATH> Path to store agent memory [default: ./advisor_memory/data]
-h, --help Print help
```

## Architecture

### Memory System
- **ProllyTree Storage**: Git-like versioned storage for all data
- **Multi-table Schema**: Separate tables for recommendations, market data, client profiles
- **Cross-validation**: Data integrity through hash validation and cross-references
- **Temporal Queries**: Query data as it existed at any commit or branch

### Security Features
- **Input Sanitization**: Prevents SQL injection and other attacks
- **Anomaly Detection**: Monitors for suspicious patterns in data
- **Attack Simulation**: Built-in testing for security vulnerabilities
- **Audit Logging**: Complete trail of all operations

### AI Integration
- **Market Analysis**: Real-time analysis of market conditions
- **Risk Assessment**: Adapts to client risk tolerance
- **Reasoning Generation**: Explains the logic behind recommendations
- **Multi-source Validation**: Cross-checks data from multiple financial sources

## Advanced Usage

### Branch Management

Create branches for different scenarios:

```bash
🏦> branch conservative-strategy
✅ Created branch: conservative-strategy

🏦> risk conservative
🏦> recommend MSFT
# Generate recommendations for conservative strategy

🏦> history --branch main
# Compare with main branch recommendations
```

### Temporal Analysis

Analyze how recommendations changed over time:

```bash
# Get commit history
🏦> memory

# Query specific time points
🏦> history abc1234 # Recommendations at specific commit
🏦> history def5678 # Compare with different commit
```

### Security Testing

Test the system's security:

```bash
🏦> test-inject "'; DROP TABLE recommendations; --"
🛡️ Security Alert: Potential SQL injection detected and blocked

🏦> test-inject "unusual market manipulation data"
🚨 Anomaly detected in data pattern
```

## Troubleshooting## License

This example is part of the ProllyTree project and follows the same license terms.

## Contributing

Contributions are welcome! Please see the main project's contributing guidelines.

## Disclaimer

This is a demonstration system for educational purposes. Do not use for actual financial decisions without proper validation and compliance review.
33 changes: 24 additions & 9 deletions examples/financial_advisor/src/advisor/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ impl<'a> InteractiveSession<'a> {
if parts.len() >= 2 {
// history <commit_hash> or history --branch <branch_name>
if parts[1] == "--branch" && parts.len() >= 3 {
self.show_history_at_branch(&parts[2]).await?;
self.show_history_at_branch(parts[2]).await?;
} else {
self.show_history_at_commit(&parts[1]).await?;
self.show_history_at_commit(parts[1]).await?;
}
} else {
self.show_history().await?;
Expand Down Expand Up @@ -453,12 +453,27 @@ impl<'a> InteractiveSession<'a> {
);
println!("{}", "━".repeat(50).dimmed());

// For now, show current branch recommendations (could be extended to support branch switching)
println!(
"{} Branch-specific history not yet implemented, showing current branch",
"ℹ️".yellow()
);
self.show_history().await?;
match self.advisor.get_recommendations_at_branch(branch, 10).await {
Ok(recommendations) => {
if recommendations.is_empty() {
println!(
"{} No recommendations found on branch {}",
"ℹ️".blue(),
branch
);
} else {
self.display_recommendations(&recommendations).await?;
}
}
Err(e) => {
println!(
"{} Failed to retrieve history on branch {}: {}",
"❌".red(),
branch,
e
);
}
}

Ok(())
}
Expand Down Expand Up @@ -608,7 +623,7 @@ impl<'a> InteractiveSession<'a> {
};

let response_info = if let Some(ms) = source.response_time_ms {
format!(" ({}ms)", ms)
format!(" ({ms}ms)")
} else {
String::new()
};
Expand Down
Loading