PlugBear Python SDK
Connect a custom LLM to PlugBear through a web server.
To install the PlugBear Python SDK, run the following command:
pip install 'plugbear[fastapi]'Here's a simple example to get you started:
importcontextlibimportplugbear.fastapifromfastapiimportFastAPIPLUGBEAR_API_KEY=""PLUGBEAR_ENDPOINT="/plugbear"@contextlib.asynccontextmanagerasyncdeflifespan(app: FastAPI):
awaitplugbear.fastapi.register(
app,
llm_func=some_llm,
api_key=PLUGBEAR_API_KEY,
endpoint=PLUGBEAR_ENDPOINT,
)
yieldapp=FastAPI(lifespan=lifespan)
asyncdefsome_llm(context: plugbear.fastapi.Request) ->str:
# template prompt using `context` to your own LLM# and return resultresult: str= ...
returnresultFor versions below 0.93.0
importplugbear.fastapifromfastapiimportFastAPIapp=FastAPI()
PLUGBEAR_API_KEY=""PLUGBEAR_ENDPOINT="/plugbear"@app.on_event("startup")asyncdef_startup():
awaitplugbear.fastapi.register(
app,
llm_func=some_llm,
api_key=PLUGBEAR_API_KEY,
endpoint=PLUGBEAR_ENDPOINT,
)
asyncdefsome_llm(context: plugbear.fastapi.Request) ->str:
# template prompt using `context` to your own LLM# and return resultresult: str= ...
returnresultAlthough it does not yet provide an interface as convenient as FastAPI, it can be directly linked to PlugBear.
pip install plugbearTaking Django as an example, it looks like this:
importplugbearfromasgiref.syncimportasync_to_syncPLUGBEAR_API_KEY=""PLUGBEAR_ENDPOINT="/plugbear"pb=async_to_sync(plugbear.PlugBear.init(api_key=PLUGBEAR_API_KEY))fromdjango.confimportsettingsfromdjango.urlsimportpathfrom . importviewsurlpatterns= [
path(settings.PLUGBEAR_ENDPOINT, views.handle_plugbear, name="plugbear"),
]importplugbearfromdaciteimportfrom_dictfromdjango.httpimportHttpResponsedefhandle_plugbear(request):
context=from_dict(data_class=plugbear.Request, data=request.data)
result=some_llm(context)
returnHttpResponse(result)
defsome_llm(context: plugbear.Request) ->str:
# template prompt using `context` to your own LLM# and return resultresult: str= ...
returnresult