langchoice


Namelangchoice JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/ekshaks/langchoice
Summaryswitch-case over free-form sentences.
upload_time2023-09-05 09:11:58
maintainer
docs_urlNone
authorNishant Sinha
requires_python>=3.10
licenseApache License, Version 2.0
keywords llm qa ir embeddings switch-case if-then-else
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            

![License](https://img.shields.io/github/license/ekshaks/langchoice)
![PyPi](https://img.shields.io/pypi/v/langchoice)

PyPI: <a href="https://pypi.org/project/langchoice/" target="_blank">https://pypi.org/project/langchoice</a></br>


## ๐Ÿค– 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 you want to detect if the user message *triggered* one of the following (*greeting*, *politics*) categories.

First define the text messages that define each topic category (a message group).

```python
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.

```python
data = yaml.safe_load(triggers)
S = LangStore(data)
```

2. On receiving a user message `user_msg`, simply match with topics!

```python
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.')
```

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 
    - visualize distance of `user_msg` to 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


## Roadmap

- [ ] allow switching under-the-hood sentence encoders


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ekshaks/langchoice",
    "name": "langchoice",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "LLM,QA,IR,embeddings,switch-case,if-then-else",
    "author": "Nishant Sinha",
    "author_email": "nishant@offnote.co",
    "download_url": "https://files.pythonhosted.org/packages/ff/e7/2442b0d8f9560a78e87ac870ecc9df568040adce77de66503b71e0f08861/langchoice-0.0.2.tar.gz",
    "platform": null,
    "description": "\n\n![License](https://img.shields.io/github/license/ekshaks/langchoice)\n![PyPi](https://img.shields.io/pypi/v/langchoice)\n\nPyPI: <a href=\"https://pypi.org/project/langchoice/\" target=\"_blank\">https://pypi.org/project/langchoice</a></br>\n\n\n## \ud83e\udd16 About LangChoice\n\n`switch-case`, but for free-form sentences. A one-liner for `if-then-else` over similar sentences.\n\nThe 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.\n\n\n## \ud83d\udce6 Installation\n```\npip install langchoice\n```\n\n## \ud83e\uddea Getting Started\n\nSuppose you want to detect if the user message *triggered* one of the following (*greeting*, *politics*) categories.\n\nFirst define the text messages that define each topic category (a message group).\n\n```python\ntriggers =\\\n'''\ngreeting:\n    - hello\n    - hi\n    - what's up?\n\npolitics:\n    - what are your political beliefs?\n    - thoughts on the president?\n    - left wing\n    - right wing\n'''\n```\n\n1. Set up a `LangStore` data container.\n\n```python\ndata = yaml.safe_load(triggers)\nS = LangStore(data)\n```\n\n2. On receiving a user message `user_msg`, simply match with topics!\n\n```python\nmatch S.match(user_msg, threshold=1.2, debug=True): \n    case 'greeting', _ : #user_msg matches the greeting message cluster\n        say_hello()\n    case 'politics', _ : #user_msg matches the politics message cluster\n        change_topic()\n    case x :\n        print(x)\n        print(f'No defined triggers detected. Ask an LLM for response.')\n```\n\nSupports multiple matching modes:\n\n* `S.match` returns the topic of nearest message.\n* Use `S.match_centroid` to instead find the nearest topic *centroid* .\n* (Optional) Provide thresholds for the *no-topic-matches* case:\n    - `S.match` returns `None` if the nearest topic distance is greater than the threshold.\n\n## Debug and More!\n\n* Use `debug=True` and `debug_k=5` to \n    - visualize distance of `user_msg` to topics.\n    - Get match scores for each message group to debug what went wrong.\n\n\nComing Soon!\n- In built assertions, which fail if the execution fails to match the expected topic / group.\n* Compute the thresholds automatically for pre-specified message groups against a query evaluation set\n* Fine-tune embeddings to *separate* message clusters better.\n\n\n### Implement a graph-based Conversation Flow\n\n\n## Roadmap\n\n- [ ] allow switching under-the-hood sentence encoders\n\n",
    "bugtrack_url": null,
    "license": "Apache License, Version 2.0",
    "summary": "switch-case over free-form sentences.",
    "version": "0.0.2",
    "project_urls": {
        "Homepage": "https://github.com/ekshaks/langchoice",
        "Repository": "https://github.com/ekshaks/langchoice"
    },
    "split_keywords": [
        "llm",
        "qa",
        "ir",
        "embeddings",
        "switch-case",
        "if-then-else"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a2b42a3a3d5f7a7473846133c47f5a5283604464665e0e1a0b2b296ebf2c25b9",
                "md5": "3618dc00c52f7072381bc9ef06447b7e",
                "sha256": "fe16b01613dda722fe17331703d090ba35589efa0d4a3c304194cd009bb78955"
            },
            "downloads": -1,
            "filename": "langchoice-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3618dc00c52f7072381bc9ef06447b7e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 12195,
            "upload_time": "2023-09-05T09:11:57",
            "upload_time_iso_8601": "2023-09-05T09:11:57.124863Z",
            "url": "https://files.pythonhosted.org/packages/a2/b4/2a3a3d5f7a7473846133c47f5a5283604464665e0e1a0b2b296ebf2c25b9/langchoice-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ffe72442b0d8f9560a78e87ac870ecc9df568040adce77de66503b71e0f08861",
                "md5": "e28d4793df47194d3cfcdb13ae2c200f",
                "sha256": "edc1aba5cb1290b858d5eaf745d18c0829cfb69ae1266805d65bafaa1d53bb96"
            },
            "downloads": -1,
            "filename": "langchoice-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "e28d4793df47194d3cfcdb13ae2c200f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 10802,
            "upload_time": "2023-09-05T09:11:58",
            "upload_time_iso_8601": "2023-09-05T09:11:58.857654Z",
            "url": "https://files.pythonhosted.org/packages/ff/e7/2442b0d8f9560a78e87ac870ecc9df568040adce77de66503b71e0f08861/langchoice-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-05 09:11:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ekshaks",
    "github_project": "langchoice",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "langchoice"
}
        
Elapsed time: 0.12819s