Skip to main content

A high-performance LLM chaining framework for python

Project description

HyperChain

HyperChain is an easy-to-use and efficient Python library that simplifies interacting with various Large Language Models. It allows for asynchronous execution and chaining of the LLMs with out-of-order execution using customizable prompt templates.

Installation

To install HyperChain directly from the GitHub repository, execute the following commands:

git clone https://github.com/PyRepair/hyperchain.git
cd hyperchain
pip install .

Usage

Below are some simple examples to get you started with HyperChain. More detailed examples can be found in the examples folder.

Prompt Templates

HyperChain offers templates to create prompts easily for different LLM applications. These templates can be combined using the '+' operator.

StringTemplate

The StringTemplate is mainly used with completion models and takes a formatable string as input. A python string will automatically be wrapped in a StringTemplate by the LLMChain.

from hyperchain.prompt_templates import StringTemplate

template = StringTemplate("Answer the following question: {question}\n")

ChatTemplate

The ChatTemplate is used with chat models and takes a list of formatable messages as input. This is illustrated with OpenAI's chat models in example_chat_chain.py.

from hyperchain.prompt_templates import ChatTemplate

template = ChatTemplate(
    [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "{question}"},
    ],
)

MaskToSentinelTemplate

The MaskToSentinelTemplate simplifies converting a masked prompt into one that uses sentinel tokens, necessary for some Seq2Seq models like T5. This avoids manual tracking of sentinel tokens and their order, as demonstrated with the CodeT5 model in example_masked_code.py.

from hyperchain.prompt_templates import MaskToSentinelTemplate

template = MaskToSentinelTemplate("{masked_code}")

print(template.format(masked_code="def greet_user(<mask>: User):\n  print('Hi,' + <mask>)\n"))

LLMRunner

An LLMRunner is used to communicate with a specific LLM inside a chain and provide it with error handlers for different LLM-specific exceptions.

from hyperchain.llm_runners import OpenAIRunner

llm_runner = OpenAIRunner(
    model="MODEL",
    api_key="OPENAI_API_KEY",
    model_params={"max_tokens": 500},
)

LLMChain

LLMChain allows using templates to create prompts and send them to a chosen Large-Language Model. It supports asynchronous execution and includes error handling for exceptions identified in each LLMRunner.

from hyperchain.chain import LLMChain

chain = LLMChain(
   template=template,
   llm_runner=llm_runner,
   output_name="answer",
)

The output_name argument allows the output from one chain to be used as the input for the next, under a specified name as seen in example_chain.py

ChainSequence is automatically created when Chain instances are added together with the '+' operator. It leverages required_keys and output_keys instance variables of each chain to calculate dependencies and execute the sequence out-of-order where possible.

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

hyperchain-0.1.2.tar.gz (347.5 kB view details)

Uploaded Source

Built Distribution

hyperchain-0.1.2-py3-none-any.whl (355.7 kB view details)

Uploaded Python 3

File details

Details for the file hyperchain-0.1.2.tar.gz.

File metadata

  • Download URL: hyperchain-0.1.2.tar.gz
  • Upload date:
  • Size: 347.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for hyperchain-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f8e193d7acbcf8be544507f5a564702978d336970a380541f15ebec15c5936ed
MD5 8cc6e98e7d1066447a4a72d975e92dce
BLAKE2b-256 80a7ee8b442de4d30112cae840c3a08b300edefd3bc892f2a02a210a12ad78a4

See more details on using hashes here.

File details

Details for the file hyperchain-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: hyperchain-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 355.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for hyperchain-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dca1ed190453ddf9455be8f9ccd760a098737f26132457439626f0f9e4e3b697
MD5 bff058d3d73b9af5ae3560946b4509b8
BLAKE2b-256 258ed67453d3cdc8f2edb93270dddf615a757135f46279a00a135826ee8a9c99

See more details on using hashes here.

Supported by

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