Skip to main content

Structured LLM Inference with Easy Parallelism

Project description

ChainFactory: Run Structured LLM Inference with Easy Parallelism (chainfactory-py 0.0.13)

Introduction

ChainFactory is a powerful system for creating complex, multi-step LLM workflows using a simple YAML-like syntax. It allows you to connect multiple prompts in a chain, with outputs from one step feeding into inputs of subsequent steps. The most important feature is the reduced reliance on exact wording of the prompts and easy parallel execution in intermediate steps.

Key Features

  • Sequential and parallel chain execution
  • Automatic prompt generation from purpose statements
  • Type-safe outputs with Pydantic models
  • Chain inheritance and reusability
  • Smart input/output mapping between chain links
  • Hash based caching for prompts generated from purpose statements and masks for convex transitions.

Basic Concepts

Chain Links

A chain link is a single unit in your workflow, defined using the @chainlink directive:

@chainlink my-first-chain
prompt: Write a story about {topic}
out:
    story: str

Sequential vs Parallel Execution

You can specify how chain links execute:

  • Sequential (-- or sequential): Links run one after another
  • Parallel (|| or parallel): Links run simultaneously for multiple inputs

Example - a 3 step chain:

@chainlink generator --     # runs once
@chainlink reviewer ||      # runs multiple times in parallel, number of runs is determined by output of the previous link
@chainlink summarizer --    # runs once to summarize the output of the previous parallel link

Cool Features

1. Purpose-Driven Prompts

Instead of writing prompts manually, let ChainFactory generate them:

@chainlink
purpose: generate creative haiku topics
in:
    num: int
out:
    topics: list[str]

The system will automatically create an optimal prompt based on the purpose and the input variales before executing the chain.

2. Chain Inheritance

Reuse existing chains with @extends:

@extends examples/base_chain.fctr
@chainlink additional_step

3. Smart Input/Output Mapping

The system automatically maps outputs to inputs between chain links using dot notation:

in:
  previous_chain.element.field: str

4. Type Safety

Define your output structures:

def:
  Haiku:
    text: str
    explanation: str?    # optional field
out:
  haikus: list[Haiku]

Real-World Examples

1. Haiku Generator and Reviewer

@chainlink haiku-generator
prompt: Write {num} haiku(s) about {topic}
out:
    haikus: list[Haiku]

@chainlink reviewer ||     # parallely review each haiku
purpose: critically analyze each haiku

2. Weird Snack Combo Generator + Parallel Filter + Email Writera

@chainlink
purpose: Generate {num} combinations of snacks that go well with each other. Generate {num} such combinations.
def:
  SnackCombo:
    items: list[str]
    comment: str?
out:
  combos: list[Combo]

@chainlink ||a # runs {num} instance in parallel
purpose: Given a snack combination, sarcastically comment on why it's the weirdest snack combination ever.
in:
  combos.element.items: str
out:
  res: list[SnackCombo]

examples/WeirdSnackCombo.fctr

@chainlink extends WeirdSnackCombos.fctr
purpose: Write a satirical article about the provided trivial subject bringing the demise of modern society.
mask:
  variables:
    - res.element.items
    - res.element.comment
out:
    article: str

examples/Article.fctra

When the above file is loaded, the only input required is the number of combos to generate. The system will automatically generate the prompt template and execute the chain - at the end, we get a satirical article about the weird snack combos.

Best Practices

  1. Use Purpose Statements When possible, let the system generate prompts using clear one-liner purpose statements.

  2. Type Everything Define input/output types for better reliability:

def:
  MyType:
    field1: str
    field2: int?
  1. Chain Structure - General Workflows
  • Start with sequential chains for initial processing.
  • Use parallel chains for whenever the order or execution is unimportant.
  • End with sequential chains for summarization and getting a final text / object output.
  1. Documentation Add field descriptions using %. This is not only for readability, but also for the LLM to understand the context of the field. It is basically a part of the prompting process.
out:
  review: str % A comprehensive analysis of the text

Advanced Features

Masks

For parallel-to-sequential transitions, use masks to format data:

mask:
  type: auto
  variables:
    - result.field1
    - result.field2

A template is automatically generated based on the supplied variables to the mask. This template is used to format the data before passing it to the final chainlink.

Caching

The system automatically caches generated prompts, and masks. Improving performance for repeated runs of the same chains.

Configuring the ChainFactory Engine

ChainFactory Engine can be configured using the ChainFactoryEngineConfig class. You can control aspects such as the language model used, caching behavior, concurrency, and execution traces. Below are the configuration options available:

  • model: Specifies the model to use (default is "gpt-4o").
  • temperature: Sets the temperature for the model, which controls the randomness of the outputs (default is 0).
  • cache: Enables caching of prompts and results (default is False).
  • provider: Defines the provider for the language model, with supported options including "openai", "anthropic", and "ollama".
  • max_tokens: Specifies the maximum tokens allowed per response (default is 1024).
  • model_kwargs: A dictionary of additional keyword arguments to pass to the model.
  • max_parallel_chains: Sets the maximum number of chains that can execute in parallel (default is 10).
  • print_trace: If True, enables printing of execution traces (default is False).
  • print_trace_for_single_chain: Similar to print_trace but for single chain execution (default is False).
  • pause_between_executions: If True, prompts for confirmation before executing the next chain (default is True).

To use these configurations, you can pass them when initializing the ChainFactoryEngine:

from chainfactory.core.engine.chainfactory_engine import ChainFactoryEngine, ChainFactoryEngineConfig

config = ChainFactoryEngineConfig(
    model="gpt-4o",
    temperature=0.7,
    cache=True,
    provider="openai",
    max_tokens=2048,
    max_parallel_chains=5,
    print_trace=True,
    prompt_between_executions=True
)

engine = ChainFactoryEngine.from_file("examples/haiku.fctr", config)

This configuration allows you to fine-tune the behavior of the ChainFactory to suit your needs.

Conclusion

ChainFactory makes it easy to create complex LLM workflows without writing code. Its simple syntax, automatic prompt generation, and smart features let you focus on what matters - designing great AI workflows.

Remember that this is just an overview - experiment with the examples to discover more possibilities!

Getting Help

  • Check the examples folder for more patterns
  • Use the built-in validation to catch errors early
  • Break complex chains into smaller, reusable pieces
  • Ping me directly on email: 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.13a0.tar.gz (17.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.13a0-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file chainfactory_py-0.0.13a0.tar.gz.

File metadata

  • Download URL: chainfactory_py-0.0.13a0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/24.2.0

File hashes

Hashes for chainfactory_py-0.0.13a0.tar.gz
Algorithm Hash digest
SHA256 ecb952a92a3f4b382131467b123b78ef3d84ccb00d176883dd98d5b6bc2831e3
MD5 9b08e01bbac05ffb8122889cd451a694
BLAKE2b-256 3b462c96f6efcd2226108107d63098f681ae70d25acf06bb664a614b990f8bd1

See more details on using hashes here.

File details

Details for the file chainfactory_py-0.0.13a0-py3-none-any.whl.

File metadata

  • Download URL: chainfactory_py-0.0.13a0-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/24.2.0

File hashes

Hashes for chainfactory_py-0.0.13a0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6218775887490c2ffe26ddb81e4bf81a1cd49a7cf93dbb20db25119ab974df7
MD5 a6216378593d04dd09110cb0cd0c1b4b
BLAKE2b-256 e04f7be34e065db6778d0f902ea4172e85320f12cfe4258d9be5e293fed5fc0b

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