Skip to content

Latest commit

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

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:

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= ...
returnresult
For 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= ...
returnresult

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

importplugbearfromasgiref.syncimportasync_to_syncPLUGBEAR_API_KEY=""PLUGBEAR_ENDPOINT="/plugbear"pb=async_to_sync(plugbear.PlugBear.init(api_key=PLUGBEAR_API_KEY))

urls.py

fromdjango.confimportsettingsfromdjango.urlsimportpathfrom . importviewsurlpatterns= [
path(settings.PLUGBEAR_ENDPOINT, views.handle_plugbear, name="plugbear"),
]

views.py

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

About

Connect a custom LLM to PlugBear using a web server.

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages