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.1.tar.gz (347.0 kB view details)

Uploaded Source

Built Distribution

hyperchain-0.1.1-py3-none-any.whl (355.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hyperchain-0.1.1.tar.gz
  • Upload date:
  • Size: 347.0 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.1.tar.gz
Algorithm Hash digest
SHA256 9e34a6334ca44c3502d9c562939778cca23c6d1f2dca97ef88762004d632b4a8
MD5 1f93b7537abb8bebc33d5a3cfddd1430
BLAKE2b-256 6c8c2303cb9f21b460f034e18c785bc66ca2829e495df0163ce4f802aa939387

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hyperchain-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 355.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 78b59d4b9701496ac73b61dadbe08a9ebe0c083aa040209805ae9eac365cf729
MD5 78a59a1f6eee066cfcd46ee0940c51d3
BLAKE2b-256 182e76bf85568d78409e6b3d6b491dd7a61a5e7d405a2df2e199193b987f0651

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