Skip to main content

switch-case over free-form sentences.

Project description

License PyPi

PyPI: https://pypi.org/project/langchoice

🤖 About LangChoice

switch-case, but for free-form sentences. A one-liner for if-then-else over 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 sentence match operator over and over again.

📦 Installation

pip install langchoice

🧪 Getting Started

Suppose we want to detect if an incoming user message triggered belongs to one of the following (greeting, politics) topics. Do this in a few lines using langchoice.

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
'''
  1. Set up a LangStore data container.
data = yaml.safe_load(triggers)
S = LangStore(data)
  1. On receiving a user message user_msg, simply match with topics!
match S.match(user_msg, threshold=1.2, debug=True): 
    case 'greeting', _ : #user_msg matches the greeting message cluster
        say_hello()
    case 'politics', _ : #user_msg matches the politics message cluster
        change_topic()
    case x :
        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.match returns the topic of nearest message.
  • Use S.match_centroid to instead find the nearest topic centroid .
  • (Optional) Provide thresholds for the no-topic-matches case:
    • S.match returns None if the nearest topic distance is greater than the threshold.

Debug and More!

  • Use debug=True and debug_k=5 to
    • display distance of user_msg to 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 set
  • Fine-tune embeddings to separate message clusters better.

Implement a graph-based Conversation Flow

The 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 demand
  • route user messages to owner modules
  • more control over conversation flow, without losing ability to chitchat
  • build conversations, not (worry about) LLM chains!

For example, see a sales lead filtering and appointment-booking bot under examples.

  • Specify the assistant and user messages as well as the overall conversation flow in a yaml file easily
  • Automate the conversational flow with few lines of code
  • Add messages and modify the flow effortlessly by modifying the yaml
    • Model picks up the changes instantly, no need to fine-tune models for the changes

Roadmap

  • match variants
    • hybrid match - match regex separately
    • local S.match (based on state tag)
    • match type='item' (default) | type='group' (centroid) | ...
  • allow switching under-the-hood sentence encoders
  • fine-tune based on in-topic and irrelevant messages

Author

Nishant Sinha, Founder, Consulting Scientist, OffNote Labs.

For insights on Generative AI evolution and applications, follow on Linkedin, and read our articles on Substack.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchoice-0.0.3.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

langchoice-0.0.3-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file langchoice-0.0.3.tar.gz.

File metadata

  • Download URL: langchoice-0.0.3.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.10.12 Darwin/20.6.0

File hashes

Hashes for langchoice-0.0.3.tar.gz
Algorithm Hash digest
SHA256 4bfcfda1636d8e3fa57cfb698eba7456c59400c1eba82d604e19511f3cb6eb03
MD5 cb224f20b1dc099a923b31818781e361
BLAKE2b-256 f737305e8198a3fe6c844255cf5b191d59088ce669db0782a14c5ae83e3e09d1

See more details on using hashes here.

File details

Details for the file langchoice-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: langchoice-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.10.12 Darwin/20.6.0

File hashes

Hashes for langchoice-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cb17ee1d78b37fbdd3aa47fc74c53477aeb61431a90476a3184f5bbe65067469
MD5 4608c1f4a0ed1ff7d05feb7ee4fb4423
BLAKE2b-256 2e87f024094f1c0ce6fe6b9a8e61592487b27de59133037bcf3a862280e32a8c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page