aBLT Python API wrapper
About
This is Python aBLT API wrapper. It is used to communicate with aBLT API. You may use it to create your own aBLT client using asynchronous or synchronous Python. You can find more information about aBLT API here.
At first, you need obtain aBLT API Token. You can do it here by get in touch and contact with support for paid plans. Next, you may use any existing ready-made bots or templates or create your own bot via UI interface.
Installation
API wrapper is available on PyPI. You can install it with pip (recommended to use Python 3.9+):
pip install ablt-python-api
Usage
Then you can import it and use it:
from ablt_python_api import ABLTApi
api = ABLTApi(bearer_token=YOUR_ABLT_API_TOKEN)
api = ABLTApi()
For some reason you may want to use your own logger, then you can initialize API wrapper with logger:
api = ABLTApi(logger=your_logger)
API methods
Bots
List of bots
You may get list of bots:
bots = api.api.get_bots()
Or you may use actual schema:
from ablt_python_api.schemas import BotsSchema
bots = [BotsSchema.model_validate(bot_dict) for bot_dict in api.get_bots()]
Single bot
You may get bot by UID:
bot = api.find_bot_by_uid(bot_uid='F0b98A09-c5ed-1197-90F3-BBfF1DBb28ee')
Alternatively, you may get bot by slug:
bot = api.find_bot_by_slug(bot_slug='omni')
Or by name (but it's not recommended, because name may be not unique):
bot = api.find_bot_by_name(bot_name='Miles Hiker')
In case if no bot found, then None
will be returned.
Chat
To chat with bot you may use `chat' method:
response = api.chat(bot_uid=BOT_UID, prompt='Hello, bot!')
response = api.chat(bot_slug=BOT_SLUG, prompt='Hello, bot!')
str_response = response.__next__()
Most probably, you will use messages
list instead of prompt
to save context. In this case, you may call method like
following:
messages = [
{"content": "I like to eat pizza", "role": "user"},
{"content": "Hello, I like pizza too!", "role": "assistant"},
{"content": "What do I like to eat?", "role": "user"},
]
response = api.chat(bot_uid=BOT_UID, messages=messages)
You need ensure, that your prompt is last message in messages
list.
More options
Additionally, you may extend \ override system context with passing system
instruction to bot:
messages = [
{"content": "You are a bibliophile bot, you know everything about literature", "role": "system"},
{"content": "Who is author of 'The Sirens of Titan'?", "role": "user"},
],
You may call chat
method with params to override system settings, if you want:
response = api.chat(bot_uid=BOT_UID,
prompt='Hello, bot!',
language='Arabic',
max_words=100,
user_id=42,
use_search=False)
Notes:
In general, you may to try to use unusual values for:
language
, like "Serbian", but it's not guaranteed that it will work as you expected.max_words
- you may try to use values less than 100 words to save tokens, but you may be experienced with cut-offs. For values greater than 2000 words you may be experienced with timeouts or errors for regular, not 32k or 128k models.user_id
- you may use any integer value, but it's recommended to use your own unique user ID, because it's used to split up usage statistics per user.use_search
- it's special feature for premium plans, you may try to manage it from API, and not from UI, but it's highly not recommended to use with smaller max_words
values, so, while using search, please use values at least 100 or more for max words
.
Streaming mode
By default, bots are working in streaming mode (as in UI), so, you may use chat
method to chat with bot in streaming mode, but you may to switch it off by:
response = api.chat(bot_uid=BOT_UID, prompt='Hello, bot!', stream=False)
In case if you prefer to use streaming mode, you need to get response from generator:
import sys
from ablt_python_api import DoneException
try:
for response in api.chat(bot_uid=BOT_UID, prompt='Hello, bot!', stream=True):
sys.stdout.write(response)
sys.stdout.flush()
except DoneException:
pass
Statistics
Statistics may be used to obtain data for words and tokens usage for period of time.
Full statistics
statistics_for_today = api.get_usage_statistics()
statistics_for_range = api.get_usage_statistics(start_date='2022-02-24',
end_date='2023-11-17')
statistics_for_user = api.get_usage_statistics(user_id=42)
You may use schema to validate statistics:
from ablt_python_api.schemas import StatisticsSchema
statistics = StatisticsSchema.model_validate(api.get_usage_statistics())
Statistic for a day
statistics_for_today = api.get_usage_statistics_for_day()
statistics_for_specific_day = api.get_usage_statistics_for_day(date='2022-02-24')
statistics_for_user = api.get_usage_statistics_for_day(user_id=42)
With schema:
from ablt_python_api.schemas import StatisticItemSchema
statistics = StatisticItemSchema.model_validate(api.get_usage_statistics_for_day())
Total statistics
statistics_for_today = api.get_total_usage_statistics()
statistics_for_specific_day = api.get_total_usage_statistics(date='2022-02-24')
statistics_for_user = api.get_total_usage_statistics(user_id=42)
With schema:
from ablt_python_api.schemas import StatisticTotalSchema
statistics = StatisticTotalSchema.model_validate(api.get_usage_statistics_for_day())
Troubleshooting:
You can always contact support or contact us in Discord channel.
SSL errors
In some cases, you may be experienced with SSL errors, then you may disable certificate verification:
import ssl
sslcontext = ssl.create_default_context()
sslcontext.check_hostname = False
sslcontext.verify_mode = ssl.CERT_NONE
ABLTApi_async(ssl_context=sslcontext)
ABLTApl(ssl_verify=False)
Rate limit errors
Never try to flood API with requests, because you may be experienced with rate limit errors. In this case, you need to wait for some time and retry your request. Especially, never try to flood API with simultaneous requests for more users than allowed in your plan.
Timeout errors
In some cases, you may be experienced with timeout errors, then you may decrease max_words
value or use stream = True
to get response on-the-fly.
Other errors
In vary rare cases, you may be experienced with other errors, like rebooting of API itself. To check-up API health, you may use health_check
method:
api.health_check()
Best practices
Always check-up and follow Guides, References and Examples from ABLT documentation.