Package: langchoice
PyPI:https://pypi.org/project/langchoice🤖 About LangChoiceswitch-case, but for free-form sentences. A one-liner forif-then-elseover similar sentences.The LangChoice library allows you to condition your structured programs over natural language sentence predicates or triggers. Makes it easy to define conditional flows over user inputs without implementing the sentencematchoperator over and over again.📦 Installationpip install langchoice🧪 Getting StartedSuppose we want to detect if an incoming user messagetriggeredbelongs to one of the following (greeting,politics) topics. Do this in a few lines usinglangchoice.First define the text messages that define each topic category (a message group).triggers=\'''greeting:- hello- hi- what's up?politics:- what are your political beliefs?- thoughts on the president?- left wing- right wing'''Set up aLangStoredata container.data=yaml.safe_load(triggers)S=LangStore(data)On receiving a user messageuser_msg, simply match with topics!matchS.match(user_msg,threshold=1.2,debug=True):case'greeting',_:#user_msg matches the greeting message clustersay_hello()case'politics',_:#user_msg matches the politics message clusterchange_topic()casex:print(x)print(f'No defined triggers detected. Ask an LLM for response.')Add or remove sentences from each topic or introduce new topics. Works on-the-fly!Supports multiple matching modes:S.matchreturns the topic of nearest message.UseS.match_centroidto instead find the nearest topiccentroid.(Optional) Provide thresholds for theno-topic-matchescase:S.matchreturnsNoneif the nearest topic distance is greater than the threshold.Debug and More!Usedebug=Trueanddebug_k=5todisplay distance ofuser_msgto different topics.Get match scores for each message group to debug what went wrong.Coming Soon!In built assertions, which fail if the execution fails to match the expected topic / group.Compute the thresholds automatically for pre-specified message groups against a query evaluation setFine-tune embeddings toseparatemessage clusters better.Implement a graph-based Conversation FlowThe langchoice package enables you to make controlled chatbot flows as well as build guardrails very quickly.The key motivation is to allow users to have maximal control when designing the bot:add/update messages on demandroute user messages to owner modulesmore control over conversation flow, without losing ability to chitchatbuild conversations, not (worry about) LLM chains!For example, see a sales lead filtering and appointment-booking bot underexamples.Specify the assistant and user messages as well as the overall conversation flow in a yaml file easilyAutomate the conversational flow with few lines of codeAdd messages and modify the flow effortlessly by modifying the yamlModel picks up the changes instantly, no need to fine-tune models for the changesRoadmapmatch variantshybrid match - match regex separatelylocal S.match (based on state tag)match type='item' (default) | type='group' (centroid) | ...allow switching under-the-hood sentence encodersfine-tune based on in-topic and irrelevant messagesAuthorNishant Sinha, Founder, Consulting Scientist, OffNote Labs.For insights on Generative AI evolution and applications, follow onLinkedin, and read our articles onSubstack.