The chains folder

Create all of your chains in a chains folder at the root of your project. Each chain should be a separate file and export your chain instance. The name of the file will be the ID of the chain.

For example, if you create a file called chains/pdf-qa.ts, the chain ID will be pdf-qa.

You use the ID for running the chain from your client side application.

// in chains/hello-world.ts
const chain = defineChain({
    title: 'Hello world',
    setup({ step }) {
        const { answer } = step('prompt_completion', {
            prompt: 'Say hello world.',
        });

        return {
            answer,
        };
    },
});

export default chain;

// in your frontend
const { answer } = await chain.runChain('hello-world');

Deployment

To deploy your chains, simply run relevance deploy in your terminal. This will deploy all chains exported in the chains folder. It’s as simple as that! You can now run your chains from your application.

Server vs. client side Deployment

When running your chain on the server, all you have to do is import your chain file. You will have complete type safety.

import helloWorld from './chains/hello-world.ts';
const { output } = await HelloWorld.run();

However, if running the SDK on the client-side, you will likely want to set your chain to publiclyTriggerable and use the runChain method. This will protect the internals of the chain from client side access.

In your chain definition, add:

const chain = defineChain({
    title: 'Hello world',
    publiclyTriggerable: true,
    setup({ step }) {
        const { answer } = step('prompt_completion', {
            prompt: 'Say hello world.',
        });

        return {
            answer,
        };
    },
});

And now in your frontend code:

import { Client } from '@relevance-ai/chain';

// we can import the type for the chain to get type safety (optional)
import type helloWorld from './chains/hello-world.ts';

const client = new Client({ regionId, projectId });

// run the chain by its ID
const output = await client.runChain<typeof helloWorld>('hello-world');