Plugbear easily integrates LLM into various channels.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
plugbear-0.1.0.tar.gz
(3.3 kB
view details)
Built Distribution
File details
Details for the file plugbear-0.1.0.tar.gz
.
File metadata
- Download URL: plugbear-0.1.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98ac9698bc6c0977876c2f2b34ac5f74a5b21106866c05cc8c568b83c5a1638d |
|
MD5 | d31c81e096b600fb1b06cda047f20a80 |
|
BLAKE2b-256 | 9bdbcac722d8da9bbebd3d003a8d08a1974058bd67dcefab19f3242e0aa47a5f |
File details
Details for the file plugbear-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: plugbear-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aadc40f2d3f34679f56edb9f7ce3b5fbe05eeeffab8eb4c278f3d29d1e6fbf26 |
|
MD5 | 8670586a1d10a02cf0695587e95414ee |
|
BLAKE2b-256 | a85267442a68db4fa229c6055c6d9cb4390ac9ab56870f19c33fac0c46773bb3 |