#308: Overwrite | to Simulate UNIX Pipes
In our LangChain posts we used this handy way to create a chain of the various parts that we need to interact with an LLM:
This mimics the pipelines in UNIX/LINUX, where we can chain command line tools together to create powerful operations. But how does this work behind the scenes? Let us find out how we can create such a behaviour on our own.
The syntactical sugar of |
When we write a | b, the Python interpreter rewrites this statement into this method call:
Methods with double underscores before and after their name are called dunder methods (short for double-underscore methods), or special methods. They let us tell Python how our objects should behave in different built-in situations and offer us a way to hook into the language itself to modify that behaviour.
We did this a few months back with the __call__() method to turn an instance into a callable function. The | operator is intended for bitwise operations, but we can overwrite the __or__() method to do whatever we want, like chaining objects together.
Overwrite the __or__ method
Since __or__() is a method, we can overwrite it in our classes. To create a useful example, we also need to overwrite the __call__() method:
Chain things together
With our Step class in place, we can build our own pipeline. We now need code that we want to run and put that into our steps:
This example uses lambdas, but you could extend the Step class and put your logic there. To run our pipeline, we need to use it in a function call and pass a string to it:
The pipeline will get rid of whitespaces and tabs at the beginning and at the end, removes spaces in between the words, turns it into upper case and adds an exclamation mark at the end.
Next
With this little behind the scenes post we now should know the magic behind chains in LangChain. We can create this functionality on our own by overwriting the __or__() method and then use it to save a lot of explicit method calls. Next week we explore the helpful features of the itertools module.