Metadata-Version: 2.3
Name: lm-proxy-db-connector
Version: 1.0.0
Summary: Database connector & database logging for LM-Proxy
License: MIT License
         
         Copyright (c) 2025 Vitalii Stepanenko
         
         Permission is hereby granted, free of charge, to any person obtaining a copy
         of this software and associated documentation files (the "Software"), to deal
         in the Software without restriction, including without limitation the rights
         to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
         copies of the Software, and to permit persons to whom the Software is
         furnished to do so, subject to the following conditions:
         
         The above copyright notice and this permission notice shall be included in all
         copies or substantial portions of the Software.
         
         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
         IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
         FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
         AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
         LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
         OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
         SOFTWARE.
Keywords: llm,large language models,ai,gpt,openai,proxy,http,proxy-server,database,logging,RDBMS,PostgreSQL,DB,SQLAlchemy,MySQL,SQLite
Author: Vitalii Stepanenko
Author-email: mail@vitalii.in
Maintainer: Vitalii Stepanenko
Maintainer-email: mail@vitalii.in
Requires-Python: >=3.11,<4
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: License :: OSI Approved :: MIT License
Requires-Dist: SQLAlchemy (>=2.0.0,<2.1.0)
Requires-Dist: lm-proxy (>=2.0.0)
Project-URL: Source Code, https://github.com/Nayjest/lm-proxy-db-connector
Description-Content-Type: text/markdown

# LM Proxy DB Connector

![Coverage](coverage.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python Versions](https://img.shields.io/badge/python-3.11%20|%203.12%20|%203.13-blue)](https://www.python.org/)
[![Code Style](https://github.com/Nayjest/lm-proxy-db-connector/actions/workflows/code-style.yml/badge.svg)](https://github.com/Nayjest/lm-proxy-db-connector/actions)
[![Build Status](https://github.com/Nayjest/lm-proxy-db-connector/actions/workflows/tests.yml/badge.svg)](https://github.com/Nayjest/lm-proxy-db-connector/actions)

A minimalistic SQLAlchemy-based database connector for [LM-Proxy](https://github.com/Nayjest/lm-proxy).

## Features

- 📊 Database connection management with SQLAlchemy
- 📝 Includes a component for logging of LLM requests and responses to various databases
- 🔄 Support for SQLite, PostgreSQL, MySQL, and other SQLAlchemy-supported databases
- 🛡️ Thread-safe implementation

## Installation

```bash
pip install lm-proxy-db-connector
```

## Quick Start

### Using with LM-Proxy

Add the DB component to your LM-Proxy configuration:

```toml
# config.toml
[components.db]
dsn = "postgresql+psycopg2://user:password@localhost:5432/mydb"
class = "lm_proxy_db_connector.Component"

# Add database logging
[[loggers]]
class = "lm_proxy.loggers.BaseLogger"
[loggers.log_writer]
class = "lm_proxy_db_connector.logging.DBLogWriter"
table_name = "llm_logs"
```

### Using with YAML config

```yaml
# config.yml
components:
  db:
    class: "lm_proxy_db_connector.init_db"
    db_url: "postgresql+psycopg2://user:password@localhost:5432/mydb"

loggers:
  - class: "lm_proxy_db_connector.logging.DBLogger"
    table_name: "llm_logs"
```

### Supported Database URLs

The connector uses SQLAlchemy's URL format:

- SQLite: `sqlite:///path/to/database.db` or `sqlite:///:memory:` (in-memory)
- PostgreSQL: `postgresql+psycopg2://user:password@localhost:5432/dbname`
- MySQL: `mysql+pymysql://user:password@localhost:3306/dbname`

### Database Session Usage

```python
from lm_proxy_db_connector import db_session
# DB initialization is handled by the LM Proxy component

# Use a session
with db_session() as session:
    result = session.execute("SELECT * FROM users")
    # Session is automatically committed or rolled back
```

### Database Logger

```python
from lm_proxy_db_connector.logging import DBLogger

# Create a logger that logs to a database table
logger = DBLogger(
    table_name="llm_logs",
    schema="public",  # Optional
    # Define column structure and mapping
    columns={
        "id": {"type": "string", "primary_key": True, "length": 36},
        "request": {"type": "json", "src": "request.messages"},
        "response": {"type": "text"},
        "created_at": {"type": "datetime", "default": "now"},
        # Completion tokens extracted from response.usage
        "completion_tokens": {"type": "integer", "src": "response.usage.completion_tokens"}
    }
)
```

## Development

### Setup Development Environment

```bash
# Clone the repository
git clone https://github.com/Nayjest/lm-proxy-db-connector.git
cd lm-proxy-db-connector

# Install in development mode
pip install -e .
```

### Running Tests

```bash
# Start test databases (PostgreSQL and MySQL)
docker compose up -d

# Run tests
pytest
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
