Building chains
We make it super easy to build LLM-powered features in your product
Creating your chain file
To take advantage of our automatic deployment, we recommend creating chains as files inside a chains
folder.
Let’s create: chains/pdf-qa.ts
.
Creating a chain
Start by instantiating your chain with the defineChain
method.
Define parameters
You can think of parameters like the inputs to an API endpoint. When you run this chain, you may want to pass in variables. They can (and should) be typed using JSONSchema.
For example, if you are creating a chain that asks questions of PDF’s, you will want to define a question
parameter of type string
. You’ll want to use this question in your LLM completion step.
Define steps
Steps are the sequence of transformations that your chain will perform. You define your steps within the setup
function, which receives your params
and the step
method.
The step
method is used to define a step. It receives a transformation id
as the first parameter, and an object of params
as the second parameter. You can find all transformation options from the link in the sidebar.
Note: the setup
function only allows you to declare each step of a chain. No Javascript you write outside of these steps, except for your returned output will be executed.
However, you can use the code
function provided in setup
to run custom Javascript. You can also use the runIf
function to conditionally run steps, as well as forEach
to iterate over arrays. Learn more.
Define output
Note that the setup
function returns an object.
In the above example:
This is the output of the chain. When you run this chain, only those variables will be returned (nested in an output
key).
On run, these will be accessible at:
Read more about the setup function!
Setting publiclyTriggerable
If you want to run your chain in client-side code, you should set it to be publicly triggerable.
You can now run your chain in client-side code by referencing the chain ID (file name in chains
folder).
Was this page helpful?