Large Language Model (LLM) output
How to use the LLM step output in other steps
An LLM step produces an output based on the provided inputs and the instruction that the model was given in the prompt. It is important to know even if your instruction says “produce a list of items”, “generate a JSON object” or “write me a number between x and y” the output is always a string that only resembles a list, a JSON or a number.
LLM outputs are always of type string. So even if they resemble a list, a JSON object, a number or a Boolean, make sure to convert the output from string to the actual type when necessary.
For non-programmer readers, let us look at an example to understand the difference easier: A string is just a chain of characters with a simple underlying structure. Consider this text, for example, what we see as words does not really mean a word, it is a bunch of non-white-space characters coming after one another.
A JSON object, however, is a structured set of key values.
In the JSON object below, we can access the person’s information using the keys (Name, Last name and Age); e.g. person["Age"]
.
person = {
"Name":"Jack",
"Last name":"Smith",
"Age":28
}
Now if an LLM outputs the same object as a string, there will be no underlying structure to be able to access any of the keys or values. A string version of the above json object looks like:
'person = {\n"Name":"Jack",\n"Last name":"Smith",n"Age":28\n}'
To be able to further work with the output, you will need to convert it to the desired format.
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. All you need to do is to feed the LLM answer to the convertor component that generates the mapping JSON.
String to list
Use a coding step (JavaScript or
Python) to take care of the conversion.
All you need to do is to feed the llm answer to your code step, apply the change
(e.g. split(separator)
in JavaScript) and return the value.
String to Numbers or Booleans
Use a coding step (JavaScript or
Python) to take care of the conversion.
All you need to do is to feed the llm answer to your code step.
For numbers apply the parser (e.g. parseInt(string-value)
in JavaScript) and return
the results.
For Booleans, you can simply use a conditional statement (if ... else ...
) to return
the corresponding Boolean value (True or False).
Was this page helpful?