starlette-ai-chatbot
Adds the Asyntai AI chat assistant to a Starlette or FastAPI site. The assistant answers visitor questions from your own content, day and night, in more than 80 languages.
Free and open source. Works on every Asyntai plan, including the free plan.
- Requires Python 3.10 or newer and Starlette 0.27 or newer (any FastAPI version that uses it)
- Documentation: https://asyntai.com/documentation/integrations/starlette/
Install
pip install starlette-ai-chatbot
Starlette
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_ai_chatbot import AsyntaiWidgetMiddleware
app = Starlette(
routes=routes,
middleware=[
Middleware(AsyntaiWidgetMiddleware, widget_id="asyntai_ab12cd34"),
],
)
FastAPI
from fastapi import FastAPI
from starlette_ai_chatbot import AsyntaiWidgetMiddleware
app = FastAPI()
app.add_middleware(AsyntaiWidgetMiddleware, widget_id="asyntai_ab12cd34")
You get the widget ID from the Install page of your Asyntai dashboard. You can paste the whole snippet; the package keeps only the ID.
That is all. The chat button appears on every HTML page of the site. JSON, files, streams and websockets pass through untouched.
Options
app.add_middleware(
AsyntaiWidgetMiddleware,
widget_id="asyntai_ab12cd34", # without it: the ASYNTAI_WIDGET_ID environment variable
skip_paths=["/admin*", "/docs", "/docs/oauth2-redirect", "/redoc"], # the default
show_to_anonymous_only=False, # hide the chat from signed-in users
)
- Leave out
widget_idto read theASYNTAI_WIDGET_IDenvironment variable. It is read on every request, so you can change it without a code change. An empty value switches the chat off. skip_pathslists paths that never get the widget. A path that ends in*also matches everything below it. The default skips admin pages and the FastAPI API docs.show_to_anonymous_onlyneeds Starlette'sAuthenticationMiddleware.
The middleware only changes complete HTML pages with status 200 that are not
compressed. GZipMiddleware must run outside it, so it compresses the page
after the widget is added: in a middleware=[...] list, put GZipMiddleware
first; with app.add_middleware(...), add GZipMiddleware last.
Placing the widget by hand
With Jinja2 templates, register the helper once:
from starlette.templating import Jinja2Templates
from starlette_ai_chatbot import asyntai_widget
templates = Jinja2Templates(directory="templates")
templates.env.globals["asyntai_widget"] = asyntai_widget
Then put it before the closing body tag:
{{ asyntai_widget() }}
</body>
widget_snippet() returns the same HTML as a plain string. The middleware
never adds a second copy to a page that already has one.
How it loads
The widget script loads after the page has finished loading, so your page draws first and page speed does not change.
License
MIT
Metadata
Release files for starlette-ai-chatbot 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| starlette_ai_chatbot-1.0.0.tar.gz | 12.6 kB | Without provenance | Details for starlette_ai_chatbot-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| starlette_ai_chatbot-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for starlette_ai_chatbot-1.0.0-py3-none-any.whl |
Total release size: 21.4 kB
Release files / starlette_ai_chatbot-1.0.0.tar.gz
| Download URL | starlette_ai_chatbot-1.0.0.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cd8d53e0127d6fddc11acab9bf16ffa110a1bd83065e745f58fb50ba5621c889
|
|
BLAKE2b-256 checksum How to use checksums |
351dd4673544b229b29a2d82eb7cdc8787ea71a9ebd5f26a6726200f64a01a01
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.10
|
Release files / starlette_ai_chatbot-1.0.0-py3-none-any.whl
| Download URL | starlette_ai_chatbot-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fcd6b03aa4767bf4d05a68be154a904996f09df1aa1957e3074a90d1b04a0dba
|
|
BLAKE2b-256 checksum How to use checksums |
074782c35f37a17e4a0f4f9f304ae88746f7830760d37adda983926d4d13c27c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.10
|
Security for starlette-ai-chatbot 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.