Package: gptchain
markshust/gptchaingptchainis 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 aslangchainin 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.FeaturesEasy 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.InstallationInstall thegptchainlibrary using pip with the following command in your terminal:pipinstallgptchainThis command will download and installgptchainalong with any necessary dependencies.UsageTo usegptchain, you'll need to write a Python script that imports the library and defines the prompt chain and any custom arguments.OpenAI SetupBe sure to expose your OpenAI API Key as an environment variable by adding the following to your~/.bash_profileor~/.zshrcfile:export OPENAI_API_KEY="your-key"Defining Custom ArgumentsCustom 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 ChainThe 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 ChainOnce you have defined your custom arguments and message chain, use thegptchainfunction to execute the chain.fromgptchainimportgptchaingptchain(custom_args,message_chain)ConfigurationYou can also customize the default OpenAI configuration by providing additional parameters such as the model, temperature, and max tokens.# Define custom OpenAI configuration valuesmodel='gpt-4-1106-preview'# Replace with the model you want to usetemperature=0.7# Set the temperature for the conversationmax_tokens=1000# Set the maximum number of tokens per response# Call the main function with the custom configuration valuesgptchain(custom_args,message_chain,model,temperature,max_tokens)Example of a Complete ScriptBelow is an example of a completed script that defines custom arguments, defines a message chain, and executes the chain with custom configuration values.fromgptchainimportgptchain# Define your custom argumentscustom_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 messagesmessage_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 valuesmodel='gpt-4-1106-preview'# Replace with the model you want to usetemperature=0.7# Set the temperature for the conversationmax_tokens=1000# Set the maximum number of tokens per response# Call the main function with the custom configuration valuesgptchain(custom_args,message_chain,model,temperature,max_tokens)Command-Line Interface (CLI) ExecutionWhen you execute your script from command line, be sure to pass in any custom arguments that were defined. In the example above,ideaandtimeframewere defined arguments, and you can pass in values with quoted strings, like so:pythonusage.py--idea"deploy drones to feed the poor"--timeframe"1 month"This command will start thegptchainprocess using the provided idea and timeframe, executing the linked prompts in sequence.OutputEach 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}.csvIn the above example, this file will be named:conversation-deploy drones to feed the poor-1 month.csvLicenseMIT