Skip to main content

Database connector & database logging for LM-Proxy

Project description

LM Proxy DB Connector

Coverage License: MIT Python Versions Code Style Build Status

A minimalistic SQLAlchemy-based database connector for 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

pip install lm-proxy-db-connector

Quick Start

Using with LM-Proxy

Add the DB component to your LM-Proxy configuration:

# 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

# 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

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

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

# 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

# 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 file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lm_proxy_db_connector-1.0.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lm_proxy_db_connector-1.0.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file lm_proxy_db_connector-1.0.0.tar.gz.

File metadata

  • Download URL: lm_proxy_db_connector-1.0.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for lm_proxy_db_connector-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0c2a30176d44929f951a4e5184461fff2beb3d698fcaf587cad43953e74f50e7
MD5 cdae2d8601a0305ba8cf5a135bd59a3a
BLAKE2b-256 66c31ea6ba89c851521b90eefe9fcc397ddc264f8d9002bc10dbce2e3980ae2a

See more details on using hashes here.

File details

Details for the file lm_proxy_db_connector-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lm_proxy_db_connector-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b91fae432ce81d2cb816b47e70858bc7ce89c06efdd5cbc9b995d5d5d90ce38
MD5 a74c4606fa6485dd748827ff42a53e6c
BLAKE2b-256 e32516d6df77e15d96bcb3eb5a9beaba3a555e1db54d87599e233ef6e2f96e72

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page