Skip to main content

Quill SDK for Python.

Project description

Quill Python SDK

Quickstart

First, install the quillsql package by running:

$ pip install quillsql

Then, add a /quill endpoint to your existing python server. For example, if you were running a FASTAPI app, you would just add the endpoint like this:

from quillsql import Quill

quill = Quill(
    private_key=os.getenv("QULL_PRIVATE_KEY"),
    database_connection_string=os.getenv("POSTGRES_READ"),
    database_type="postgresql"
)

security = HTTPBearer()

async def authenticate_jwt(token: str = Depends(security)):
    # Your JWT validation logic here
    # Return user object or raise HTTPException
    user = validate_jwt_token(token.credentials)
    return user

@app.post("/quill")
async def quill_post(data: Request, user: dict = Depends(authenticate_jwt)):
    # assuming user fetched via auth middleware has an userId
    user_id = user["user_id"]
    body = await data.json()
    metadata = body.get("metadata")

    result = quill.query(
        tenants=[{"tenantField": "user_id", "tenantIds": [user_id]}],
        metadata=metadata
    )
    return result

Then you can run your app like normally. Pass in this route to our react library on the frontend and you all set!

Performance Tuning And Profiling

You can tune Postgres concurrency and enable profiling with either cache config or environment variables.

quill = Quill(
    private_key=os.getenv("QULL_PRIVATE_KEY"),
    database_connection_string=os.getenv("POSTGRES_READ"),
    database_type="postgresql",
    cache={
        "postgres_pool_min": 4,
        "postgres_pool_max": 24,
        "run_query_max_workers": 8,
        "db_max_inflight": 12,
        "profile_logging": True,
    },
)

Supported environment variables:

  • QUILL_POSTGRES_POOL_MIN
  • QUILL_POSTGRES_POOL_MAX
  • QUILL_RUN_QUERY_MAX_WORKERS
  • QUILL_DB_MAX_INFLIGHT
  • QUILL_PROFILE_LOGGING

Worker behavior is adaptive for heavy dashboard/report requests:

  • report / pivot-template default to 1 worker.
  • They step up to 2 workers only when recent pool-acquire wait is low.
  • They scale to 4 only for high fanout when pool-acquire wait is very close to zero.
  • They back off aggressively when pool-acquire wait rises.

When profiling is enabled, logs include a per-request request_id so you can correlate API call timing and DB execution timing for the same SDK query.

Streaming

from quillsql import Quill
from fastapi.responses import StreamingResponse
import asyncio

quill = Quill(
    private_key=os.getenv("QULL_PRIVATE_KEY"),
    database_connection_string=os.getenv("POSTGRES_READ"),
    database_type="postgresql"
)

@app.post("/quill-stream")
async def quill_post(data: Request, user: dict = Depends(authenticate_jwt)):
    # assuming user fetched via auth middleware has an userId
    user_id = user["user_id"]
    body = await data.json()
    metadata = body.get("metadata")

    quill_stream = quill.stream(
        tenants=[{"tenantField": "user_id", "tenantIds": [user_id]}],
        metadata=metadata,
    )

    async def event_generator():
        # Full event types list: https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#data-stream-protocol
        async for event in quill_stream:
            if event["type"] == "start":
                pass
            elif event["type"] == "text-delta":
                yield event['delta']
            elif event["type"] == "finish":
                return
            elif event["type"] == "error":
                yield event['errorText']
            await asyncio.sleep(0)

    return StreamingResponse(event_generator(), media_type="text/event-stream")

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

quillsql-2.2.11.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

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

quillsql-2.2.11-py3-none-any.whl (32.0 kB view details)

Uploaded Python 3

File details

Details for the file quillsql-2.2.11.tar.gz.

File metadata

  • Download URL: quillsql-2.2.11.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for quillsql-2.2.11.tar.gz
Algorithm Hash digest
SHA256 91dbfefd391dd423906715d2e6789e32f83e6ad598cd6939f584bf281ad31f4c
MD5 4567e4da74d4dc5eb6a9f97fbe945de7
BLAKE2b-256 0a7382e6492a5918f33d08dfbd8657f2f1e796ceb7320596bcd4b8132e7ee2e8

See more details on using hashes here.

File details

Details for the file quillsql-2.2.11-py3-none-any.whl.

File metadata

  • Download URL: quillsql-2.2.11-py3-none-any.whl
  • Upload date:
  • Size: 32.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.6

File hashes

Hashes for quillsql-2.2.11-py3-none-any.whl
Algorithm Hash digest
SHA256 b48e40f9ef76115b8618726be526e0dbfa03a0932894f200027fefb3f4e6eec9
MD5 01db635714dfda2314e98ba0bdb3c4e6
BLAKE2b-256 09e6b1441b5fc30ecb9cff4089c3ea1b0ab25c553f5213258bee0cf10b3d2564

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