Skip to main content

Framework to procedurally query LLMs

Project description

Langworks

Langworks is an open-source framework build on top of Pypeworks to procedurally query LLM's in Python. It offers the following features:

  • Chained prompting with conditional pathways
  • Templatable prompts and responses using Jinja
  • Guided generation constrained by regex and context-free grammar
  • Mixing and matching of LLM's and LLM servers
  • Fine-tuning of sampling parameters per prompt

Install

Langworks is available through the PyPI repository and can be installed using pip:

pip install langworks

Quickstart

A Langwork represents a specialised pipework consisting of Query and Node objects. A Query object encapsulates a templatable prompt that may be passed to a LLM, as well as any guidance on how that LLM may process that prompt. Nodes serve as processing units, to be used to prepare input for Query objects, or to process output received from these objects.

Assuming a vLLM-backend running Llama-3 is available, a Langwork may be instantiated as follows:

from langworks import (
    Connection,
    Langwork,
    Query
)

from pypeworks import (
    Node
)

from langworks.middleware.vllm import (
    SamplingParams,
    vLLM
)

langwork = Langwork(

    # Configuration
    middleware = vLLM(
        url          = "http://127.0.0.1:4000/v1",
        model        = "meta-llama/Meta-Llama-3-8B-Instruct",
        params       = SamplingParams(temperature = 0.3)
    ),

    system_msg = [
        {
            "role": "system",
            "content": (
                "You are a helpful assistant knowledgable about popular trivia."
            )
        }
    ],

    # Logic
    query = Query(

        query = (
            "Who or what is more popular: {{ input[0] }} or {{ input[1] }}? Think step-by-step"
            " before stating your final answer, either '{{ input[0] }}' or '{{ input[1] }}'"
            ", delimited by triple asterisks (i.e. ***{{ input[0] }}*** or"
            " ***{{ input[1] }}***)."
        ),

        guidance = (
            'Let\'s think step-by-step'
            '{% gen params = Params(stop = ["***"], include_stop = True) %}'
            '{% choice input, var = "hit" %, params = Params(temperature = 0)}'
            '***'
        )

    ),

    extract = Node(
        lambda context, history: context.get("hit", None)
    ),

    # Connections
    connections = [

        Connection("enter"   , "query"),
        Connection("query"   , "extract"),
        Connection("extract" , "exit")
        
    ]

)

for result in langwork(
    iter([("cats", "dogs"), ("werewolves", "vampires"), ("rock", "pop")])
):
    
    print(result)

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

langworks-0.1.1.tar.gz (33.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langworks-0.1.1-py3-none-any.whl (41.2 kB view details)

Uploaded Python 3

File details

Details for the file langworks-0.1.1.tar.gz.

File metadata

  • Download URL: langworks-0.1.1.tar.gz
  • Upload date:
  • Size: 33.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for langworks-0.1.1.tar.gz
Algorithm Hash digest
SHA256 60dbce74282bd40c89c342f9bbdfb0a2a3e97647978216d2072798a273f29ad0
MD5 9e91bb3a6f59c39f493ad51abddf9a58
BLAKE2b-256 36b2fe6651eaab3a339934b60cbd551315808c8238070c903061f1d137b82833

See more details on using hashes here.

File details

Details for the file langworks-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: langworks-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 41.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for langworks-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d455d51cc32db6247e5f7f898097b31b01e7ac31400878207496cb689dc72a48
MD5 ee86925b69ec6901d84d1cdb0155db0a
BLAKE2b-256 bb1c82f9746f8695a54fbf3f6810ed56409f8e35363e5d6fed9df67260ef6bce

See more details on using hashes here.

Supported by

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