Node
class and call the API using aiohttp
.
Node
to create our new CatFactsAPITool class. The only required functions to implement are the constructor and execute
.
CatFactsAPITool
constructorNode
takes.
name
is the name of the tool. Under the hood, the nodes are identified by UUID, but have a human-readable name
for convenience.input_s
is a dictionary of the inputs that the tool expects from other Node
s as Python types. The keys are the names of the inputs, and the values are the types of the inputs.output_s
is a dictionary of the outputs that the tool produces as Python types. The keys are the names of the outputs, and the values are the types of the outputs.
In this case, we produce a single output called cat_fact_1
that is a list of dictionaries, where each dictionary has string keys and string values. This is the format that the API returns, so we’ll keep it as is.*args
and **kwargs
are used to pass arguments to the node which will be used at runtime (when Node.execute()
is called). You can set these via the constructor for convenience, or use .set_execute_args
to do this.Node
, setting these upfront will save you a lot of pain in debugging later.
For more information on Node
, visit the Reference section.
execute
execute
function is where Node
assumes your business logic will be called. For this example, the only thing
we need to do is get the arguments for calling the CatFacts API, call the API, and return the result.
.set_execute_args
. However, if you want to dynamically set these arguments (say, from the output of an LLM call executed right before), you can do so as well. These execute args will then be sent to Node.input
instead of Node.execute_args
. Despite the difference under the hood, you can use safe_get_execute_arg
(prioritizes Node.input
over Node.execute_args
) no matter how you set the argument to retrieve its value.
In this case, we only need to provide one argument: max_length
. max_length
is the maximum length of the cat facts we want to get.
aiohttp
to do this. aiohttp
is a popular asynchronous HTTP client for Python that is well-supported and easy to use.
"data"
to "cat_fact_1"
; we have done this for ease of understanding in the further steps, but doing anything here is a decision left to the user. If it remains unchanged, you’ll have to transform it later.
CatFactsAPITool
! Visit the Node reference to learn more. The final code should look like this:
LLM
nodes.