Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions a2a-examples/observability-a2a-agent
Submodule observability-a2a-agent added at 88ad36
1 change: 1 addition & 0 deletions agent-examples/observability-gpt-agent
Submodule observability-gpt-agent added at 8451d8
14 changes: 14 additions & 0 deletions financial-agent-py/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key_here

# Nevermined Configuration (for protected agent)
BUILDER_NVM_API_KEY=your_builder_api_key_here
SUBSCRIBER_NVM_API_KEY=your_subscriber_api_key_here
NVM_ENVIRONMENT=staging_sandbox
NVM_AGENT_ID=your_agent_did_here
NVM_PLAN_ID=your_plan_did_here
NVM_AGENT_HOST=http://localhost:8000

# Server Configuration
PORT=8000
AGENT_URL=http://localhost:8000
161 changes: 161 additions & 0 deletions financial-agent-py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# macOS
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Dependency directories
node_modules/
136 changes: 136 additions & 0 deletions financial-agent-py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Financial Agent (Python)

A Python implementation of the FinGuide financial education agent using FastAPI, OpenAI, and Nevermined Payments.

## Features

- **Two Agent Versions**:
- `index_unprotected.py` - Free access financial education agent
- `index_nevermined.py` - Protected agent with Nevermined payment integration

- **Dynamic Credit System** (Protected version only):
- Credits charged based on actual token usage
- Formula: `10 * (actual_tokens / max_tokens)`, minimum 1 credit
- Rewards concise responses with lower costs

- **Session Management**: Maintains conversation context across multiple questions
- **Loading Animation**: Visual feedback during API calls
- **Educational Focus**: Provides financial education rather than investment advice

## Setup

1. **Install dependencies**:
```bash
pip install -r requirements.txt
```

2. **Environment Configuration**:
```bash
cp .env.example .env
# Edit .env with your API keys and configuration
```

3. **Required Environment Variables**:
```
OPENAI_API_KEY=your_openai_api_key_here

# For protected agent only:
BUILDER_NVM_API_KEY=your_builder_api_key_here
SUBSCRIBER_NVM_API_KEY=your_subscriber_api_key_here
NVM_ENVIRONMENT=staging_sandbox
NVM_AGENT_ID=your_agent_did_here
NVM_PLAN_ID=your_plan_did_here
```

## Running the Agent

### Unprotected Agent (Free Access)
```bash
cd agent
python index_unprotected.py
# Server starts on http://localhost:8001
```

### Protected Agent (Nevermined Integration)
```bash
cd agent
python index_nevermined.py
# Server starts on http://localhost:8000
```

## Running the Client

### Test Unprotected Agent
```bash
cd client
python index_unprotected.py
```

### Test Protected Agent
```bash
cd client
python index_nevermined.py
```

## API Endpoints

### POST /ask
Send a financial question to FinGuide.

**Request Body**:
```json
{
"input_query": "What is diversification?",
"sessionId": "optional-session-id"
}
```

**Response** (Unprotected):
```json
{
"output": "Diversification is like not putting all your eggs in one basket...",
"sessionId": "generated-or-provided-session-id"
}
```

**Response** (Protected):
```json
{
"output": "Diversification is like not putting all your eggs in one basket...",
"sessionId": "generated-or-provided-session-id",
"redemptionResult": {
"creditsRedeemed": 3,
"error": null
}
}
```

### GET /health
Health check endpoint.

**Response**:
```json
{
"status": "ok"
}
```

## Architecture

- **FastAPI**: Modern Python web framework with automatic OpenAPI docs
- **OpenAI**: GPT-4o-mini for natural language responses
- **Nevermined Payments**: Payment and observability integration (protected version)
- **Session Management**: In-memory conversation history
- **Async/Await**: Non-blocking request handling

## Key Differences from TypeScript Version

1. **FastAPI** instead of Express.js
2. **Pydantic models** for request/response validation
3. **Async/await** pattern throughout
4. **Python-style** naming conventions (snake_case)
5. **Local payments-py** library integration

## Development

The Python implementation maintains feature parity with the TypeScript version while following Python best practices and idioms.
Loading