Package: ablt-python-api
aBLT Python API wrapperAboutThis 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 APIhere.At first, you need obtain aBLT API Token. You can do ithereby 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.InstallationAPI wrapper is available on PyPI. You can install it with pip (recommended to use Python 3.9+):pipinstallablt-python-apiUsageThen you can import it and use it:# for asynchronous API wrapper use ABLTApi_asyncfromablt_python_apiimportABLTApi# this is synchronous API wrapper# Init with explicit tokenapi=ABLTApi(bearer_token=YOUR_ABLT_API_TOKEN)# Init with environment variable, use ABLT_BEARER_TOKENapi=ABLTApi()For some reason you may want to use your own logger, then you can initialize API wrapper with logger:# logger it's pre-configured instance of logging.loggerapi=ABLTApi(logger=your_logger)API methodsBotsList of botsYou may get list of bots:# will return list of bots, see schema docs: https://docs.ablt.ai/api_docs/bots/get#Response-bodybots=api.api.get_bots()Or you may use actual schema:fromablt_python_api.schemasimportBotsSchema# Use await for asynchronous API wrapperbots=[BotsSchema.model_validate(bot_dict)forbot_dictinapi.get_bots()]Single botYou 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, thenNonewill be returned.ChatTo chat with bot you may use `chat' method:# Specify bot UID, will return generator with bot responses.response=api.chat(bot_uid=BOT_UID,prompt='Hello, bot!')# Or specify bot slug, return generator with bot responses.response=api.chat(bot_slug=BOT_SLUG,prompt='Hello, bot!')# To get response as string, you may use loop or extract just first response from generatorstr_response=response.__next__()# Or use __anext__ for async modeMost probably, you will usemessageslist instead ofpromptto 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"},]# Will return generator with bot responseresponse=api.chat(bot_uid=BOT_UID,messages=messages)You need ensure, that your prompt is last message inmessageslist.More optionsAdditionally, you may extend \ override system context with passingsysteminstruction to bot:# Use with caution, it may conflict or replace all settings stored in UI# and may lead to unexpected resultsmessages=[{"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 callchatmethod with params to override system settings, if you want:response=api.chat(bot_uid=BOT_UID,prompt='Hello, bot!',language='Arabic',# may be: "Arabic", "French", "English", "Spanish", "Russian"max_words=100,# any integer, values less than 100 and greater than 2000 not recommendeduser_id=42,# unique user ID, used to split up usage statistics per useruse_search=False)# use search mode, if True, then bot will try to find answer in internetNotes:
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 smallermax_wordsvalues, so, while using search, please use values at least 100 or more formax words.Streaming modeBy default, bots are working in streaming mode (as in UI), so, you may usechatmethod 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:importsysfromablt_python_apiimportDoneExceptiontry:forresponseinapi.chat(bot_uid=BOT_UID,prompt='Hello, bot!',stream=True):# I use direct stdout output to make output be printed on-the-flysys.stdout.write(response)# To get typewriter effect I forcefully flush output each timesys.stdout.flush()exceptDoneException:pass# DoneException is raised when bot finished conversationStatisticsStatistics may be used to obtain data for words and tokens usage for period of time.Full statistics# Will return statistics for current date for default user = -1statistics_for_today=api.get_usage_statistics()# Will return statistics for date rangestatistics_for_range=api.get_usage_statistics(start_date='2022-02-24',end_date='2023-11-17')# Will return statistics for user with ID 42statistics_for_user=api.get_usage_statistics(user_id=42)You may use schema to validate statistics:fromablt_python_api.schemasimportStatisticsSchemastatistics=StatisticsSchema.model_validate(api.get_usage_statistics())Statistic for a day# Will return statistics for current date for user = -1statistics_for_today=api.get_usage_statistics_for_day()# Will return statistics for specified datestatistics_for_specific_day=api.get_usage_statistics_for_day(date='2022-02-24')# Will return statistics for user with ID 42statistics_for_user=api.get_usage_statistics_for_day(user_id=42)With schema:fromablt_python_api.schemasimportStatisticItemSchemastatistics=StatisticItemSchema.model_validate(api.get_usage_statistics_for_day())Total statistics# Will return statistics for current date for user = -1statistics_for_today=api.get_total_usage_statistics()# Well return statistics for specified datestatistics_for_specific_day=api.get_total_usage_statistics(date='2022-02-24')# Will return statistics for user with ID 42statistics_for_user=api.get_total_usage_statistics(user_id=42)With schema:fromablt_python_api.schemasimportStatisticTotalSchemastatistics=StatisticTotalSchema.model_validate(api.get_usage_statistics_for_day())Troubleshooting:You can alwayscontact supportor contact us inDiscord channel.SSL errorsIn some cases, you may be experienced with SSL errors, then you may disable certificate verification:importsslsslcontext=ssl.create_default_context()sslcontext.check_hostname=Falsesslcontext.verify_mode=ssl.CERT_NONEABLTApi_async(ssl_context=sslcontext)# for asyncABLTApl(ssl_verify=False)# for syncRate limit errorsNever 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 errorsIn some cases, you may be experienced with timeout errors, then you may decreasemax_wordsvalue or usestream = Trueto get response on-the-fly.Other errorsIn vary rare cases, you may be experienced with other errors, like rebooting of API itself. To check-up API health, you may usehealth_checkmethod:# will return True if API is healthy, otherwise Falseapi.health_check()Best practicesAlways check-up and followGuides,ReferencesandExamplesfrom ABLT documentation.