LLM Output

Large Language Model (LLM) Output

How to use the LLM step output in other steps

Understanding LLM Output

An LLM step produces an output based on the provided inputs and the instructions given in the prompt. It is important to note that even if your instruction specifies to "produce a list of items," "generate a JSON object," or "write a number between x and y," the output is always a string that only resembles a list, a JSON object, or a number.

LLM Output is Always a String

LLM outputs are always of type string. Therefore, even if they resemble a list, a JSON object, a number, or a Boolean, you must convert the output from a string to the actual type when necessary.

Example Explanation

For non-programmers, consider this example to understand the difference more easily: A string is just a chain of characters with a simple underlying structure. For instance, the text you are reading now is composed of characters forming words and sentences, but it is just a continuous sequence of characters.

A JSON object, however, is a structured set of key-value pairs. In the JSON object below, you can access the person’s information using the keys (Name, Last name, and Age):

{
    "Name": "Jack",
    "Last name": "Smith",
    "Age": 28
}

If an LLM outputs the same object as a string, there will be no underlying structure to access any of the keys or values. A string version of the above JSON object might look like this:

'{"Name":"Jack","Last name":"Smith","Age":28}'

To be able to work with the output further, you will need to convert it to the desired format.

Converting LLM Output

String to JSON

When instructing an LLM to generate a JSON object, it is recommended to also set up the is_json validator. Next, use a "Convert string to JSON" action. Feed the LLM output to the converter component, which will generate the corresponding JSON object.

Example

// JavaScript example
const jsonString = '{"Name":"Jack","Last name":"Smith","Age":28}';
const jsonObject = JSON.parse(jsonString);
console.log(jsonObject.Name); // Output: Jack

String to List

Use a coding step (JavaScript or Python) to handle the conversion. Feed the LLM output to your code step, apply the transformation (e.g., split(separator) in JavaScript), and return the value.

Example

// JavaScript example
const listString = "apple,banana,orange";
const listArray = listString.split(",");
console.log(listArray); // Output: ["apple", "banana", "orange"]

String to Numbers or Booleans

Use a coding step (JavaScript or Python) to handle the conversion. For numbers, apply the parser (e.g., parseInt(stringValue) in JavaScript) and return the results. For Booleans, use a conditional statement (if ... else ...) to return the corresponding Boolean value (true or false).

Example

// JavaScript example for number
const numberString = "123";
const number = parseInt(numberString);
console.log(number); // Output: 123

// JavaScript example for Boolean
const booleanString = "true";
const booleanValue = (booleanString === "true");
console.log(booleanValue); // Output: true

By following these guidelines, you can effectively handle and convert LLM outputs to the desired formats, ensuring smooth integration and processing within your workflows.

Last updated