Chris Padilla/Blog

My passion project! Posts spanning music, art, software, books, and more
You can follow by Newsletter or RSS! (What's RSS?) Full archive here.

    The Retrieval-Augmented Generation Pattern for AI Development

    Yes ladies and gentleman, a post about developing with AI!

    If you're team is looking to incorporate an LLM into your services, the first challenge to overcome is how to do so in a cost effective way.

    Chances are, your business is already focused on a specific product domain, with resources targeted towards building that solution. This already is going to point you towards finding an off the shelf solution to integrate with an API.

    With your flavor of LLM picked, the next set of challenges center around getting it to respond to questions in a way that meaningfully provides answers from your business data. LLM's need to be informed on how to respond to requests, what data to utilize when considering their answers, and even what to do if they're tempted to guess.

    The way forward is through prompt engineering, with the help of Retrieval-Augmented Generation

    Retrieval-Augmented Generation

    The simplified procedure for RAG goes as follows:

    1. Request is made to your app with the message "How many Tex-Mex Restaurants are in Dallas?"
    2. Your application gathers context. For example, we may make a query to our DB for a summary of all restaurants in the area.
    3. We'll provide a summary of the context and instructions to the LLM with a prompt.
    4. We send along the response to the user.

    That's an overly simplified walk through, but it should already get you thinking about the details involved in those steps depending on your use case.

    Another benefit to this is that requests to an API are not inherently stateful. The chat window of an AI app will remember our previous messages. But my API request to that third party does not automatically. We have to store and retrieve that context.

    AI Agents

    It's worth noting that step 2 may even require an LLM to parse the question and then interact with an API to gather data. There's a fair amount of complexity to still parse in developing these solutions. This is where you may be leaning on an AI Agent. An agent is an LLM that will parse a result and determine if a tool is required, such as pinging your internal APIs.

    Prompt Engineering is emerging as a role and craft all of it's own, and there are many nuances to doing it well.

    LangChain

    The workflow is already so common that there's a framework at the ready to spin up and take care of the heavy lifting for you. LangChain (stylized as 🦜⛓️‍💥) is just that tool.

    For a hands on experience building a RAG application on rails, their docs on building a chatbot are a good starting place.

    For a more complex agentive tool, LangGraph opens up the hood on LangChain for more control and plays nicely with LangChain when needed.

    Campfire Folk Intro

    Listen on Youtube

    Gather round, and listen to this tale...

    Just a bit of noodling between practicing longer pieces.

    Calm Sky

    🦋

    It's grey out this time of year. But behind the clouds, there's always a blue sky. 🌤️

    Extending Derived Class Methods in Python

    Polymorphism! A sturdy pillar in the foundation of Object Oriented Programming. At it's simplest, it's the ability to change the implementation of specific methods on derived classes.

    At face value, that could mean entirely rewriting the method. But what if we want a bit more nuance? What if we want to extend instead of replace the method entirely.

    I'll continue on my Vehicle example from my previous post on polymorphism, this time in Python:

    from abc import ABC
    
    class Vehicle(ABC):
        def __init__(self, color: str):
        
            if not color:
                raise ValueError("Color string cannot be null")
                
            self._passengers = []
            self.color = color
    
        def load_passenger(self, passenger: str):
            # Logic to load passenger
    
        def move(self):
            # Some default code for moving
            print("Moving 1 mile North")

    I've created an Abstract Base Class that serves as a starting point for any derived classes. Within it, I've defined a method move() that moves the vehicle North by 1 mile. All children will have this class available automatically.

    Now, if I want to override that method, it's as simple as declaring a method of the same name in my child classes:

    class Car(Vehicle):
        def move(self):
            print("Driving 1 mile North")
    
    
    class Boat(Vehicle):
        def move(self):
            print("Sailing 1 mile North")

    In the case that I want to extend the functionality, we can use Super() to do so:

    class Car(Vehicle):
        def move(self):
            super().move()
            print("Pedal to metal!")
    
    
    class Boat(Vehicle):
        def move(self):
            super().move()
            print("Raising the sail!")

    The benefit here is I can pass all the same arguments I'm receiving in the method call on either child instance to the default implementation in the parent. They can then be used in my own custom implementation in the child class.

    car = Car()
    car.move()
    # Moving 1 mile North
    # Pedal to metal!

    Angel Eyes

    Listen on Youtube

    'Scuse me while I disappear~ 🌫️