Skip to main content

PlugBear Python SDK

Connect a custom LLM to PlugBear through a web server.

FastAPI

Installation

To install the PlugBear Python SDK, run the following command:

pip install 'plugbear[fastapi]'

Usage

Here's a simple example to get you started:

import contextlib

import plugbear.fastapi
from fastapi import FastAPI

PLUGBEAR_API_KEY = ""
PLUGBEAR_ENDPOINT = "/plugbear"


@contextlib.asynccontextmanager
async def lifespan(app: FastAPI):
    await plugbear.fastapi.register(
        app,
        llm_func=some_llm,
        api_key=PLUGBEAR_API_KEY,
        endpoint=PLUGBEAR_ENDPOINT,
    )
    yield


app = FastAPI(lifespan=lifespan)


async def some_llm(context: plugbear.fastapi.Request) -> str:
    # template prompt using `context` to your own LLM
    # and return result
    result: str = ...
    return result
For versions below 0.93.0
import plugbear.fastapi
from fastapi import FastAPI

app = FastAPI()
PLUGBEAR_API_KEY = ""
PLUGBEAR_ENDPOINT = "/plugbear"


@app.on_event("startup")
async def _startup():
    await plugbear.fastapi.register(
        app,
        llm_func=some_llm,
        api_key=PLUGBEAR_API_KEY,
        endpoint=PLUGBEAR_ENDPOINT,
    )


async def some_llm(context: plugbear.fastapi.Request) -> str:
    # template prompt using `context` to your own LLM
    # and return result
    result: str = ...
    return result

Other Web server frameworks

Although it does not yet provide an interface as convenient as FastAPI, it can be directly linked to PlugBear.

Installation

pip install plugbear

Usage

Taking Django as an example, it looks like this:

settings.py

import plugbear
from asgiref.sync import async_to_sync

PLUGBEAR_API_KEY = ""
PLUGBEAR_ENDPOINT = "/plugbear"

pb = async_to_sync(plugbear.PlugBear.init(api_key=PLUGBEAR_API_KEY))

urls.py

from django.conf import settings
from django.urls import path

from . import views

urlpatterns = [
    path(settings.PLUGBEAR_ENDPOINT, views.handle_plugbear, name="plugbear"),
]

views.py

import plugbear
from dacite import from_dict
from django.http import HttpResponse


def handle_plugbear(request):
    context = from_dict(data_class=plugbear.Request, data=request.data)
    result = some_llm(context)
    return HttpResponse(result)

def some_llm(context: plugbear.Request) -> str:
    # template prompt using `context` to your own LLM
    # and return result
    result: str = ...
    return result

Metadata

Release files for plugbear 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for plugbear 0.1.0
File Size Uploaded
plugbear-0.1.0.tar.gz 3.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for plugbear 0.1.0
File Interpreter ABI Platform
plugbear-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.0 kB

Release files / plugbear-0.1.0.tar.gz

Download URL plugbear-0.1.0.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
98ac9698bc6c0977876c2f2b34ac5f74a5b21106866c05cc8c568b83c5a1638d
BLAKE2b-256 checksum
How to use checksums
9bdbcac722d8da9bbebd3d003a8d08a1974058bd67dcefab19f3242e0aa47a5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / plugbear-0.1.0-py3-none-any.whl

Download URL plugbear-0.1.0-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aadc40f2d3f34679f56edb9f7ce3b5fbe05eeeffab8eb4c278f3d29d1e6fbf26
BLAKE2b-256 checksum
How to use checksums
a85267442a68db4fa229c6055c6d9cb4390ac9ab56870f19c33fac0c46773bb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page