Skip to main content

Link multiple GPT prompts into a chain of responses.

Project description

markshust/gptchain

gptchain is a Python library designed to facilitate the creation of a sequence of GPT prompts that build upon each other's responses. This tool allows users to generate complex, multistep conversations or processes with AI models by linking prompts into a cohesive chain.

It differs from tools such as langchain in that it's far easier to use. It lacks many of the features of langchain, as it simply interacts with GPT by executing a list of prompts in sequential order.

Features

  • Easy installation and setup.
  • Customizable prompt chains for complex AI-driven workflows.
  • Support for custom command-line arguments to tailor the AI's output.
  • Ability to override default OpenAI configuration for model specifications.

Installation

Install the gptchain library using pip with the following command in your terminal:

pip install gptchain

This command will download and install gptchain along with any necessary dependencies.

Usage

To use gptchain, you'll need to write a Python script that imports the library and defines the prompt chain and any custom arguments.

OpenAI Setup

Be sure to expose your OpenAI API Key as an environment variable by adding the following to your ~/.bash_profile or ~/.zshrc file:

export OPENAI_API_KEY="your-key"

Defining Custom Arguments

Custom arguments allow you to pass dynamic content to the AI prompts. These arguments are defined as a list of dictionaries, where each dictionary represents a command-line flag.

custom_args = [
    {'flag': '--idea', 'help': 'The name of the idea to discuss', 'required': True},
    {'flag': '--timeframe', 'help': 'The timeframe for the business concept', 'required': True},
    # Add more custom arguments as needed
]

Creating a Message Chain

The message chain is a list of prompt stages, where each stage is a list of system and user messages. The chain defines the flow of conversation or analysis.

Custom arguments may be included in messages by using the {argName} format as shown below:

message_chain = [
    [
        {"role": "system", "content": "You are a business analyst."},
        {"role": "user", "content": "Create 5 potential business initiatives for the idea: {idea}"}
    ],
    [
        {"role": "system", "content": "You are a technical architect."},
        {"role": "user", "content": "Pick the best idea from the previous list, and list out any potential technical limitations that could come up for it."}
    ],
    [
        {"role": "system", "content": "You are a product manager."},
        {"role": "user", "content": "List out any potential impediments for this idea that would prevent it from launching in {timeframe}."}
    ]
]

When the script is executed, the first array in the message chain is sent to GPT and the result is returned. The response is merged with the second message in the chain as an "assistant" response, like so:

[
    {"role": "system", "content": "You are a technical architect."},
    {"role": "assistant", "content": "These are the 5 business initiatives for the idea..."},
    {"role": "user", "content": "Pick the best idea from the previous list, and list out any potential technical limitations that could come up for it."}
]

And this process continues with each subsequent prompt. The previous results are included in order to create a cohesive chain of prompts. This allows you to automate a chain of prompts in a successive order without any human intervention.

Execute the Chain

Once you have defined your custom arguments and message chain, use the gptchain function to execute the chain.

from gptchain import gptchain
    gptchain(custom_args, message_chain)

Configuration

You can also customize the default OpenAI configuration by providing additional parameters such as the model, temperature, and max tokens.

# Define custom OpenAI configuration values
model = 'gpt-4-1106-preview'  # Replace with the model you want to use
temperature = 0.7  # Set the temperature for the conversation
max_tokens = 1000  # Set the maximum number of tokens per response

# Call the main function with the custom configuration values
gptchain(custom_args, message_chain, model, temperature, max_tokens)

Example of a Complete Script

Below is an example of a completed script that defines custom arguments, defines a message chain, and executes the chain with custom configuration values.

from gptchain import gptchain

# Define your custom arguments
custom_args = [
    {'flag': '--idea', 'help': 'The name of the idea to discuss', 'required': True},
    {'flag': '--timeframe', 'help': 'The timeframe for the business concept', 'required': True},
    # Add more custom arguments as needed
]

# Define your chain of messages
message_chain = [
    [
        {"role": "system", "content": "You are a business analyst."},
        {"role": "user", "content": "Create 5 potential business initiatives for the idea: {idea}"}
    ],
    [
        {"role": "system", "content": "You are a technical architect."},
        {"role": "user", "content": "Pick the best idea from the previous list, and list out any potential technical limitations that could come up for it."}
    ],
    [
        {"role": "system", "content": "You are a product manager."},
        {"role": "user", "content": "List out any potential impediments for this idea that would prevent it from launching in {timeframe}."}
    ]
]

if __name__ == '__main__':
    # Define custom OpenAI configuration values
    model = 'gpt-4-1106-preview'  # Replace with the model you want to use
    temperature = 0.7  # Set the temperature for the conversation
    max_tokens = 1000  # Set the maximum number of tokens per response

    # Call the main function with the custom configuration values
    gptchain(custom_args, message_chain, model, temperature, max_tokens)

Command-Line Interface (CLI) Execution

When you execute your script from command line, be sure to pass in any custom arguments that were defined. In the example above, idea and timeframe were defined arguments, and you can pass in values with quoted strings, like so:

python usage.py --idea "deploy drones to feed the poor" --timeframe "1 month"

This command will start the gptchain process using the provided idea and timeframe, executing the linked prompts in sequence.

Output

Each message chain is output to the terminal as it is executed, along with the response after it has been executed. When the script has completed in its entirety, it is written to a CSV file using the naming convention:

conversation-{arg1value}-{arg2value}-{etc}.csv

In the above example, this file will be named:

conversation-deploy drones to feed the poor-1 month.csv

License

MIT

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

gptchain-1.0.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

gptchain-1.0.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file gptchain-1.0.0.tar.gz.

File metadata

  • Download URL: gptchain-1.0.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for gptchain-1.0.0.tar.gz
Algorithm Hash digest
SHA256 84f351e4cb2d2ddb31536f1434db67d2f0c0a514f9ba3eefc4ccc5265b29f716
MD5 c80d26d66966b20c8ab43b3bb0ed2ea5
BLAKE2b-256 3ff5ab32d77d5f646d9b52a2c26b1998d6a0f186628c021b709f1e5d249859f9

See more details on using hashes here.

File details

Details for the file gptchain-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: gptchain-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for gptchain-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fc10d73b72d8e00e27afea593fa697341d170a6b6a461926f1adacb5745a346
MD5 767b598eef6a570c3cb913a36a296b25
BLAKE2b-256 f1298de649d8b9f7f45c3488a93571fed458686255f22f151689db9535270a06

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