Giter Site home page Giter Site logo

ekshaks / langchoice Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 22 KB

"if-then-else" over topics made up of free-form sentences. Build conversations, not LLM chains!

License: Apache License 2.0

Python 100.00%
conversational-agents conversational-ai conversational-bots embedding-similarity flowchart gpt if-then-else large-language-models switch-case vector-database

langchoice's Introduction

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 Conversation Flow with Message-based Routing

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 states or modules
  • more control over conversation flow, without losing ability to chitchat
  • build conversations, not (worry about) LLM chains!

Check out the sales lead filtering and appointment-booking bots under examples.

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.

langchoice's People

Contributors

ekshaks avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.