How to use the Brainglue Chain API?
Learn how to use the Brainglue Chain API and how to bring AI workflows into production quickly.
Quick Recap on How Brainglue Works
Brainglue is an AI playground that allows you to build and configure LLM prompt chains that can use different configurations and language models to complete complex generative AI tasks, such as content creation, text classification, data analysis, and more. Every Brainglue Chain has a unique API endpoint that can be used to trigger the chain, which in turn will return the outputs of every prompt in the chain so they can be used in other contexts or applications.
One of the most essential features of Brainglue is the ability to define global variables that can be used in any of the prompts in the chain. When you run a chain, Brainglue will use the default values set in the playground. However, these variables are actually dynamic variables that can be overridden when invoking the chain via its unique API endpoint.
In this guide, we will explore how we can configure the variables of a chain and use them dynamically via the Chain API.
Endpoint Usage
Every chain in Brainglue comes out-of-box with an invocation endpoint that allows you to trigger the chain and run its underlying LLM prompts from any client or application capable of interacting with a RESTful API.
You can find the endpoint URL at the top of the chain in the trigger module:

API trigger endpoints are secured with a chain secret that should be passed with every request.
To pass the secret, use any of the following:
Pass
bg_secretas a request header with your chain secret as the value.Pass
bg_secretas a query parameter with your chain secret as the value.Pass
bg_secretas a body parameter with your chain secret as the value.
As you can see, the Brainglue Chain API is extremely forgiving and allows you to set the authentication secret in multiple ways. This same principle applies to the way you can override the variables in a chain.
Chain Variables
As said before, in Brainglue, you can define global variables that can be used in the context of an LLM prompt.
Let's look at the "Synonym Poem Generator" chain example used when you create a Brainglue account.
This particular chain generates a poem about a given word by first generating a list of synonyms for that word and then using that list to write a poem and its title.
In this chain, we have a chain variable with the key word and a default value of happy

By default, this chain will run and use the default value of happy any prompt in the chain that references the variable @word. In the case of this chain, the first prompt references the @word variable to generate a list of 10 synonyms, and a subsequent prompt uses that list to write a poem.

Now, every time that we invoke this chain via the API, Brainglue will run the chain with whatever default value was set; in this case, the word happy.
However, the API allows us to override that value by simply passing a query parameter or body parameter with the name/key of the variable.
So, for example, we could invoke this same API and generate a poem with the synonyms of the word wise, by simply passing a query parameter or body parameter word with a value of wise:
NodeJS Request with Body Params
NodeJS Request with Query Params
Python Request with Body Parameters
Python Request with Query Parameters
Streamed or Buffered Responses
By default, the Brainglue Chains API uses the preferred mechanism of response for LLMs, which is streaming its response.
In the context of HTTP requests, a streamed response is sent back in chunks as they are generated and become available, allowing the client to start processing data immediately without waiting for the entire response. In contrast, a buffered response is fully received and stored in memory before it's passed along to the client for processing. Streamed responses improve the perceived experience of LLMs by allowing them to stream tokens as they are produced by the model.
We default to streamed transmission because we believe most solutions that leverage AI will benefit from having generated tokens as soon as possible.
However, we acknowledge that not all architectures or implementations need or can use a streamed response, so we also provide a buffered response by doing one of the following:
Pass
bg_streamas a request header with yourfalseas the value.Pass
bg_streamas a query parameter with yourfalseas the value.Pass
bg_streamas a body parameter with yourfalseas the value.
Once again, our goal here is to give you maximum flexibility when interacting with your chains and allow you to move rapidly to productized solutions.
Please acknowledge the following limits when deciding on a response type:
Streamed responses have a hard timeout of 15 minutes.
Buffered responses have a hard timeout of 3 minutes.
API Responses
Once you invoke the chain endpoint, you will start receiving response chunks that you can pipe into other processes or parts of your implementation. The standard streamed response looks like this:
Every chunk is an object that contains the following keys:
partcontains the identifier of the prompt that generated this output. Generally, you don't need to use this key for processing on your end, and you can safely rely on the keypromptIndexdescribed below.promptIndexcontains the index of the prompt in the chain that generated this output. You can use this key to process a respond on your end. Chunks are delivered in order of generation, so you can process chunks safely in the order in which they arrive.datacontains the actual streamed tokens that can be used to assemble the model's response.
Also, you might have noticed the last chunk is a little bit different. Brainglue will also return a chunk (or a key in a buffered response) telling you what global variables were used and where did they originated from (a default value or an API parameter).
Processing Streamed Responses
If you're wondering how to process the chunks on your end, here is a simple JavaScript implementation on how to do it.
The objective here is to create a new object where for each promptIndex, the data strings are concatenated together. Here's how you can do that:
Define an empty object that will store the results.
Iterate through the streamed response chunks.
For each chunk, if the
promptIndexdoes not exist in the result object, create it and initialize it with thedatavalue of the chunk.If the
promptIndexalready exists in the result object, simply append thedatavalue of the chunk to the existing value.Return the result object.
Here's a JavaScript function that implements the above steps:
When you run the above function with the provided example data, it should give you:
This will give you the concatenated strings for each promptIndex key in the chunks array.
Last updated