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.
import { defineChain } from '@relevanceai/chain';
const chain = defineChain({
title: 'PDF Q&A',
});
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.
const chain = defineChain({
title: 'PDF Q&A',
params: {
question: {
type: 'string',
},
},
});
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.
const chain = defineChain({
title: 'PDF Q&A',
params: {
question: {
type: 'string',
},
},
setup({ params, steps }) {
const { question } = params;
const pdf_url = 'https://www.mypdf.com/doc.pdf';
const { text } = step('pdf_to_text', {
pdf_url,
});
const { chunks } = step('split_text', {
method: 'tokens',
num_tokens: 250,
text,
});
const { results: search_results } = step('search_array', {
array: chunks,
query: question,
});
const { answer } = step('prompt_completion', {
prompt: `${search_results}
Based off the above context answer the question below:
Question:
${question}
Answer:`,
});
return { answer };
},
});
Define output
Note that the setup
function returns an object.
In the above example:
return {
answer,
};
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:
{
output: {
answer: '...',
pdf_url: '...'
}
}
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.
const chain = defineChain({
title: 'PDF Q&A',
publiclyTriggerable: true,
});
You can now run your chain in client-side code by referencing the chain ID (file name in chains
folder).
import { Client } from '@relevance-ai/chain';
// we can import the type for the chain to get type safety (optional)
import type pdfQa from './chains/pdf-qa.ts';
const client = new Client({ regionId, projectId });
// run the chain by its ID
const output = await client.runChain<typeof pdfQa>('pdf-qa');
Was this page helpful?