Skip to main content

LLM Chains as Configuration Without Code

Project description

ChainFactory: Mass manufacture your LLM chains.

Overview

ChainFactory is a utility to build LLM chains by configuration instead of code. The chains produces this way are reproducible and easy to manage i.e read, edit and share. The created chains can be exported as Python code and used in your projects without any mod. Additionally, you can pass the YAML configuration to ChainFactoryEngine to use them on the fly.

This allows a very interesting pattern where you can create chains during runtime and combine their outputs to do interesting things that are not possible with the standard code defined chains.

Installation

  1. Clone the repository:

    git clone https://github.com/pankajgarkoti/ChainFactory.git
    
  2. Install the required packages:

    poetry install
    
  3. Set up environment variables:

    export OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    

The ChainFactory Specification

Draft 002

Structure

A .fctr file is a YAML file with 1 major distinction - it can contain duplicate fields in the top level mapping.

  • Specify the Prompt or list the Inputs (keyword: prompts & in)
  • Define Models (keyword: def)
  • Specify the Outputs (keyword: out)

Typing

The typing system takes direct inspiration from Python's type annotations. The only difference is that the type system is stricter and more limited. The following atomic types are supported:

  • str
  • int
  • float
  • bool
  • list
  • dict

It is possible to define custom types in the def section of the .fctr file. The syntax for typing a field is as follows:

[name]: [type][?]=[default_value]

The ? symbol right after a type (without spaces) indicates that the field is optional. If a field has a RHS value that is not a valid type, ChainFactory will assume that the field type is str and the RHS is a default value.

Definitions

The def section is the part of the .fctr file that defines custom types to be used in rest of the file.

Example Usage:

def:
    Haiku:
        haiku: str
        explanation: str
        topic: str

The models defined in the def section can be used to compose the desired output structure of the chain.

Prompt

The prompt template related options can be set under this section. Attributes of the prompt section are:

  • type: template # can be template, auto. the template is generated automatically based on the purpose of the chain in the auto mode.
  • purpose: null # a string that describes the purpose of the chain. this can be used for auto generating the prompt template.
  • template: | # the template to use for the prompt.

Example Usage:

prompt:
    type: template # possible values are template, auto
    purpose: null # can be provided to auto generate the prompt template if the input variables are given
    template: | # the purpose and type fields are ignored if the template is provided
        Write a haiku about {topic}

# shorthand for the above
prompt: |
    Write a haiku about {topic}

# additionally, the following shorthand can be used for auto mode
purpose: "to generate haikus" # the file should not contain the prompt field if the purpose field is at the top level

Out

The out keyword defines part of the .fctr file that defines the output structure of the chain. The out section contains attributes for the desired output structure. Not defining the out section means that the chain output is to be a single string with no enforced structure.

Example Usage:

out:
    haikus : list[Haiku] # using the Haiku model defined in the def section

Usage

The complete .fctr file for generating haikus looks like this:

def:
    Haiku:
        haiku: str
        explanation: str
        topic: str

prompt: |
    Write {num} haiku(s) about {topic}

out:
    haikus : list[Haiku]

Here is how you can use the above file to create a chain that generates haikus:

from src.interfaces.engine import ChainFactoryEngine

engine = ChainFactoryEngine.from_file("haiku.fctr")

res = engine({ "topic": "Python", "num": 3 })

for haiku in res.haikus:
    print(haiku.haiku)
    print(haiku.explanation)
    print("\n\n")

The above code prints the following 3 haikus of questionable originality but generating haikus was not the point :)

Silent code it weaves,
Serpentine logic unfolds,
Errors shed like skin.
This haiku captures the elegance and efficiency of Python programming, likening it to a snake shedding its skin to symbolize the ease of debugging and refining code.


Indentation rules,
Whitespace guides the coder's hand,
Python's zen revealed.
This haiku highlights Python's unique use of indentation and whitespace to structure code, reflecting the language's philosophy of simplicity and readability.


Libraries abound,
Endless tools at your command,
Python's power grows.
This haiku emphasizes the vast array of libraries and tools available in Python, showcasing its versatility and the growing strength of its ecosystem.

Contact

For questions or feedback, please create an issue or contact garkotipankaj@gmail.com.

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

chainfactory_py-0.0.6.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

chainfactory_py-0.0.6-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file chainfactory_py-0.0.6.tar.gz.

File metadata

  • Download URL: chainfactory_py-0.0.6.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Darwin/23.3.0

File hashes

Hashes for chainfactory_py-0.0.6.tar.gz
Algorithm Hash digest
SHA256 0b4c8d5c9ae01f4e613798a21270b15ccd470a48548c2dbdcf96600c6735a52f
MD5 cc5e1abbfb26c95d70c1250409f50cf9
BLAKE2b-256 3675a2a8e4f4686e3a52a5c7d7024181aac3dbf938c5b676f2909b8f3430b5c3

See more details on using hashes here.

File details

Details for the file chainfactory_py-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: chainfactory_py-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Darwin/23.3.0

File hashes

Hashes for chainfactory_py-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 976f3237da8ff95f5127f4abe7e0db34b009e41acbc86dc353880cda65c6d585
MD5 af001c8226fe24b33d022ff126055357
BLAKE2b-256 58495fcfbb7b4a237737940781c9981c212aa722e7b7cd91654d01135955b803

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