Basic Chains
This page will tell you to use chains to combine prompt templates with models.
Text-to-Image Models
At the very basic level, we can use chains to utilize prompt templates with text-to-iamge models. An example of this is shown below. First, let’s create a prompt template below:
Next, let’s get our model. Let’s use Stable Diffusion XL.
Finally, let’s create the chain:
Let’s dive a bit deeper into the formatting of chains. Each part of the chain for text-to-text or text-to-image models can be simplified into the following format:
where MODEL_OUTPUT
is the output of each model, MODEL
is the model object that we are running, PROMPT
is the prompt we pass into each model, and PARAMETERS
is an optional parameters for any other parameters we want to specify. In this case, PARAMETERS
is equivalent to a dictionary that represents the optional parameters of text-to-image models.
Additionally, as there is only one model in this chain, MODEL_OUTPUT
is the final output of the chain.
We can run the chain as below:
Note how we can specify the animal parameter within this statement due to the prompt template we passed in. This will give us the following output:
Text-to-Text Models
Text-to-Text models have the same format as text-to-image models, except that their output is strictly text and they do not have any additional parameters.
An simple example of this is given below:
Now, if we call this chain, we’ll simply get:
Image-to-Image Models
Unlike text-to-image models, image-to-image models take in a prompt and an image as input. We’ll look at both situations below.
Text as Input
If we are taking text as input, the format is the same as text-to-image models. The only difference is that we
include an image in our PARAMETERS
dictionary:
Image as Input
If we are taking text as input, the format is a bit different. Rather than passing in a string or PromptTemplate
into the
PROMPT
section, we pass in an image url. Therefore, each part of the chain for text-to-image models can be simplified into the following format:
where IMAGE
is the image that we pass into the image-to-image model. Therefore, now our PARAMETERS
dictionary contains a
prompt rather than an image. An example of this is shown below with the PromptTemplate
from the first example.
Upscalers
We put upscalers in a separate section from image-to-image models because, although they do take in images as input and return images as output, they do not take in text. They additionally do not have any other customizable parameters after their initialization. Therefore, their format is:
An example of this is given below: