Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

I am making a python langgraph application, and I have defined a bunch of nodes on their…

  1. User

    I am making a python langgraph application, and I have defined a bunch of nodes on their own classes but every single one of them has repeated code, how can I make a parent class that has these repeated code?

    I will paste two classes and the rest are similar:

    class TableFinder: def init(self, config, name="tablefinder"): self.prompt_template = ChatPromptTemplate.from_template(config['prompts']['tablefinder']['template']) self.llm = ChatOpenAI( model=config['nodes']['tablefinder']['model'], openai_api_key=os.environ.get("OPENROUTER_API_KEY"), openai_api_base="https://openrouter.ai/api/v1", ) self.name = name

    async def run(self, state):
        #...
    
        if "prompts" not in state:
            state["prompts"] = []
    
        if "node_name" not in state:
            state["node_name"] = []
    
        if "llm" not in state:
            state["llm"] = []
    
        if "elapsed_time" not in state:
            state["elapsed_time"] = []
    
        if "usage_metadata" not in state:
            state["usage_metadata"] = []
        
        if "custom_metadata" not in state:
            state["custom_metadata"] = {}
        
        formatted_prompt_str = "\n".join([msg.content for msg in formatted_prompt])
        state["prompts"].append(formatted_prompt_str)
        state["node_name"].append(self.name)
        state["elapsed_time"].append(elapsed_time)
        state["llm"].append(self.llm.model_name)
        state["usage_metadata"].append(response.usage_metadata)
    
        return state
    

    class RulesFinder: def init(self, config, name="rulesfinder"): self.prompt_template = ChatPromptTemplate.from_template(config['prompts']['rulesfinder']['template']) self.llm = ChatOpenAI( model=config['nodes']['rulesfinder']['model'], openai_api_key=os.environ.get("OPENROUTER_API_KEY"), openai_api_base="https://openrouter.ai/api/v1", temperature=config['nodes']['rulesfinder']['temperature'] ) self.name = name

    async def run(self, state):
        #...
        
        if "prompts" not in state:
            state["prompts"] = []
    
        if "node_name" not in state:
            state["node_name"] = []
    
        if "llm" not in state:
            state["llm"] = []
    
        if "elapsed_time" not in state:
            state["elapsed_time"] = []
    
        if "usage_metadata" not in state:
            state["usage_metadata"] = []
                
        if "custom_metadata" not in state:
            state["custom_metadata"] = {}
        
        formatted_prompt_str = "\n".join([msg.content for msg in formatted_prompt])
        state["prompts"].append(formatted_prompt_str)
        state["node_name"].append(self.name)
        state["elapsed_time"].append(elapsed_time)
        state["llm"].append(self.llm.model_name)
        state["usage_metadata"].append(response.usage_metadata)
    
        return state
    

    Graph creation

    class State(TypedDict): llm: List[str] usage_metadata: List[dict] prompts: List[str] messages: Annotated[list, add_messages] node_name: List[str] elapsed_time: List[float] custom_metadata: dict

    graph_builder = StateGraph(State)

    tablefinder_node = TableFinder(hub_config, name="tablefinder") rulesfinder_node = RulesFinder(hub_config, name="rulesfinder") #...

  2. Assistant

    It looks like you can refactor the repeated code into a parent class to reduce redundancy and make your code cleaner. Here's how you can create a parent class called BaseFinder that encapsulates the common functionality, and then inherit from this class in your TableFinder and RulesFinder classes:

    class BaseFinder:
        def __init__(self, config, node_key, name=None):
            self.prompt_template = ChatPromptTemplate.from_template(config['prompts'][node_key]['template'])
            self.llm = ChatOpenAI(
                model=config['nodes'][node_key]['model'],
                openai_api_key=os.environ.get("OPENROUTER_API_KEY"),
                openai_api_base="https://openrouter.ai/api/v1",
                temperature=config['nodes'][node_key].get('temperature', None)  # Use .get to handle optional keys
            )
            self.name = name or node_key
        
        async def run(self, state):
            # Check and initialize the state dictionary
            state.setdefault("prompts", [])
            state.setdefault("node_name", [])
            state.setdefault("llm", [])
            state.setdefault("elapsed_time", [])
            state.setdefault("usage_metadata", [])
            state.setdefault("custom_metadata", {})
    
            # Assume `formatted_prompt`, `elapsed_time`, and `response` are defined within this method
            formatted_prompt_str = "\n".join([msg.content for msg in formatted_prompt])
            state["prompts"].append(formatted_prompt_str)
            state["node_name"].append(self.name)
            state["elapsed_time"].append(elapsed_time)
            state["llm"].append(self.llm.model_name)
            state["usage_metadata"].append(response.usage_metadata)
    
            return state
    
    class TableFinder(BaseFinder):
        def __init__(self, config, name="tablefinder"):
            super().__init__(config, node_key="tablefinder", name=name)
    
    class RulesFinder(BaseFinder):
        def __init__(self, config, name="rulesfinder"):
            super().__init__(config, node_key="rulesfinder", name=name)
    
    # Graph creation
    
    class State(TypedDict):
        llm: List[str]
        usage_metadata: List[dict]
        prompts: List[str]
        messages: Annotated[list, add_messages]
        node_name: List[str]
        elapsed_time: List[float]
        custom_metadata: dict
    
    graph_builder = StateGraph(State)
    
    tablefinder_node = TableFinder(hub_config, name="tablefinder")
    rulesfinder_node = RulesFinder(hub_config, name="rulesfinder")
    

    Key Points:

    1. BaseFinder Class: This class handles the setup of prompt_template, llm, and common initialization logic. It also contains the run method which can update the state dictionary.

    2. setdefault Method: This method is used to initialize dictionary keys only if they are not already present, which simplifies the logic.

    3. Inlining Parameters: The model and temperature are accessed using the given node_key, making the class more flexible with different node configurations.

    4. super() Function: The child classes call the parent class’s __init__ method, ensuring all parent class logic is executed.

    By applying this pattern, you can easily add more finder classes that follow the same structure, without repeating the initialization and state management logic.

  3. User

    Several things, the default temperature should be 0.7 just in case doesn't give errors, another thing is the repeated code in the run method could be run once in the init instead?

  4. Assistant

    Certainly! Regarding your questions:

    1. Default Temperature: You can set a default temperature directly using the .get method or explicitly handle it.

    2. State Initialization: You can initialize the default state in the __init__ method, which should only include keys that are meant to be set up once and are not dynamically changing per execution. Although most of the run method initializes lists/collections that reset per execution, if you mean static classes can keep shared attributes, you might consider leveraging class variables or methods that encapsulate initialization logic.

    Here’s an updated version of the code with those considerations in mind:

    class BaseFinder:
        default_state = {
            "prompts": [],
            "node_name": [],
            "llm": [],
            "elapsed_time": [],
            "usage_metadata": [],
            "custom_metadata": {},
        }
    
        def __init__(self, config, node_key, name=None):
            self.prompt_template = ChatPromptTemplate.from_template(config['prompts'][node_key]['template'])
            self.llm = ChatOpenAI(
                model=config['nodes'][node_key]['model'],
                openai_api_key=os.environ.get("OPENROUTER_API_KEY"),
                openai_api_base="https://openrouter.ai/api/v1",
                temperature=config['nodes'][node_key].get('temperature', 0.7)  # Default temperature is 0.7
            )
            self.name = name or node_key
        
        def initialize_state(self, state):
            for key, default in self.default_state.items():
                state.setdefault(key, default if isinstance(default, dict) else list(default)) # Handle mutable types
    
        async def run(self, state):
            self.initialize_state(state)  # Prepare the state
    
            # Ensure these variables are defined in the context
            formatted_prompt_str = "\n".join([msg.content for msg in formatted_prompt])
            state["prompts"].append(formatted_prompt_str)
            state["node_name"].append(self.name)
            state["elapsed_time"].append(elapsed_time)
            state["llm"].append(self.llm.model_name)
            state["usage_metadata"].append(response.usage_metadata)
    
            return state
    
    class TableFinder(BaseFinder):
        def __init__(self, config, name="tablefinder"):
            super().__init__(config, node_key="tablefinder", name=name)
    
    class RulesFinder(BaseFinder):
        def __init__(self, config, name="rulesfinder"):
            super().__init__(config, node_key="rulesfinder", name=name)
    
    # Graph creation
    
    class State(TypedDict):
        llm: List[str]
        usage_metadata: List[dict]
        prompts: List[str]
        messages: Annotated[list, add_messages]
        node_name: List[str]
        elapsed_time: List[float]
        custom_metadata: dict
    
    graph_builder = StateGraph(State)
    
    tablefinder_node = TableFinder(hub_config, name="tablefinder")
    rulesfinder_node = RulesFinder(hub_config, name="rulesfinder")
    

    Key Changes:

    • Default Temperature: Specified using .get with a default of 0.7.

    • State Initialization: Moved common state setup to the initialize_state method in the BaseFinder. This method uses the default_state attribute, which is a class-level dictionary representing the initial structure of the state.

    • Immutables and Mutables: The list(default) ensures that the stored default is a new instance of a list for each key, preventing shared mutable lists between states.

    This structure provides a clearer initialization framework and clean separation of common and specialized logic, allowing you to build upon this base for additional nodes.

  5. User

    each children class has the run method with custom code, should I run the initialize_state method or create a parent run method with this can call with with the children?

  6. Assistant

    If each child class has its own custom logic in the run method but also shares common logic (like state initialization), you can use a few different approaches to maintain clean and DRY (Don't Repeat Yourself) code. Here are two main approaches:

    Approach 1: Common Initialization in a Parent Method

    You can create a utility function in the parent class (like initialize_state) and call it from each child’s overridden run method. This allows you to handle shared logic in the parent while keeping unique logic in each child.

    class BaseFinder:
        default_state = {
            "prompts": [],
            "node_name": [],
            "llm": [],
            "elapsed_time": [],
            "usage_metadata": [],
            "custom_metadata": {},
        }
    
        def __init__(self, config, node_key, name=None):
            self.prompt_template = ChatPromptTemplate.from_template(config['prompts'][node_key]['template'])
            self.llm = ChatOpenAI(
                model=config['nodes'][node_key]['model'],
                openai_api_key=os.environ.get("OPENROUTER_API_KEY"),
                openai_api_base="https://openrouter.ai/api/v1",
                temperature=config['nodes'][node_key].get('temperature', 0.7)
            )
            self.name = name or node_key
        
        def initialize_state(self, state):
            for key, default in self.default_state.items():
                state.setdefault(key, default if isinstance(default, dict) else list(default))
    
        async def common_run_logic(self, state, formatted_prompt, elapsed_time, response):
            # This method includes common logic after the child-specific logic
            formatted_prompt_str = "\n".join([msg.content for msg in formatted_prompt])
            state["prompts"].append(formatted_prompt_str)
            state["node_name"].append(self.name)
            state["elapsed_time"].append(elapsed_time)
            state["llm"].append(self.llm.model_name)
            state["usage_metadata"].append(response.usage_metadata)
    
            return state
    
    
    class TableFinder(BaseFinder):
        def __init__(self, config, name="tablefinder"):
            super().__init__(config, node_key="tablefinder", name=name)
    
        async def run(self, state):
            # Initialize state
            self.initialize_state(state)
    
            # Child-specific logic...
            # For example, set `formatted_prompt`, `elapsed_time`, and `response`
    
            # Call the common run logic
            return await self.common_run_logic(state, formatted_prompt, elapsed_time, response)
    
    
    class RulesFinder(BaseFinder):
        def __init__(self, config, name="rulesfinder"):
            super().__init__(config, node_key="rulesfinder", name=name)
    
        async def run(self, state):
            # Initialize state
            self.initialize_state(state)
    
            # Child-specific logic...
            # For example, set `formatted_prompt`, `elapsed_time`, and `response`
    
            # Call the common run logic
            return await self.common_run_logic(state, formatted_prompt, elapsed_time, response)
    

    Approach 2: Using super() and Chaining

    You could design your parent class to handle initialization and have the child classes call the parent's run method using super() if needed for common logic. Since your base class might also include key async processing, structure your calls to handle that sequence:

    class BaseFinder:
        # ... initialization and state setup as before ...
    
        async def run(self, state, formatted_prompt, elapsed_time, response):
            self.initialize_state(state)
            return await self.common_run_logic(state, formatted_prompt, elapsed_time, response)
    
    
    class TableFinder(BaseFinder):
        def __init__(self, config, name="tablefinder"):
            super().__init__(config, node_key="tablefinder", name=name)
    
        async def run(self, state):
            self.initialize_state(state)
            # Custom logic specific to TableFinder
            # Define `formatted_prompt`, `elapsed_time`, `response` if needed
    
            return await super().run(state, formatted_prompt, elapsed_time, response)
    
    
    class RulesFinder(BaseFinder):
        def __init__(self, config, name="rulesfinder"):
            super().__init__(config, node_key="rulesfinder", name=name)
    
        async def run(self, state):
            self.initialize_state(state)
            # Custom logic specific to RulesFinder
            # Define `formatted_prompt`, `elapsed_time`, `response` if needed
    
            return await super().run(state, formatted_prompt, elapsed_time, response)
    

    Choice of Structure

    • Use common utility methods (common_run_logic) if you want each child to coordinate its own logic before/after common operations.

    • Use inheritance and super() if you want to systematically ensure that both initialization and common logic are tightly coupled with the sequence of operations in each child, while still allowing modification or extension of the child's logic.

    By employing these strategies, you are ensuring that your code remains maintainable and clear. Each method has its place depending on the precision you need in your workflow and sequence logic, along with how frequently you may change common operations.

  7. User

    the default_state part is something I don't like, can you explicitly write the code instead? I am thinking more of a future proof when adding new data types

  8. Assistant

    Certainly! Instead of using a class-level default_state dictionary, you can manually set up each key in the state within the initialize_state method. This makes it clearer and more explicit to handle specific initializations and is future-proof when you need to add more keys or change existing ones.

    Here's how you can modify the base class initialize_state method to explicitly set default state values:

    class BaseFinder:
        def __init__(self, config, node_key, name=None):
            self.prompt_template = ChatPromptTemplate.from_template(config['prompts'][node_key]['template'])
            self.llm = ChatOpenAI(
                model=config['nodes'][node_key]['model'],
                openai_api_key=os.environ.get("OPENROUTER_API_KEY"),
                openai_api_base="https://openrouter.ai/api/v1",
                temperature=config['nodes'][node_key].get('temperature', 0.7)
            )
            self.name = name or node_key
        
        def initialize_state(self, state):
            # Explicit initialization
            if "prompts" not in state:
                state["prompts"] = []
    
            if "node_name" not in state:
                state["node_name"] = []
    
            if "llm" not in state:
                state["llm"] = []
    
            if "elapsed_time" not in state:
                state["elapsed_time"] = []
    
            if "usage_metadata" not in state:
                state["usage_metadata"] = []
    
            if "custom_metadata" not in state:
                state["custom_metadata"] = {}
    
        async def common_run_logic(self, state, formatted_prompt, elapsed_time, response):
            formatted_prompt_str = "\n".join([msg.content for msg in formatted_prompt])
            state["prompts"].append(formatted_prompt_str)
            state["node_name"].append(self.name)
            state["elapsed_time"].append(elapsed_time)
            state["llm"].append(self.llm.model_name)
            state["usage_metadata"].append(response.usage_metadata)
    
            return state
    
    class TableFinder(BaseFinder):
        def __init__(self, config, name="tablefinder"):
            super().__init__(config, node_key="tablefinder", name=name)
    
        async def run(self, state):
            self.initialize_state(state)
            # Child-specific logic...
            return await self.common_run_logic(state, formatted_prompt, elapsed_time, response)
    
    class RulesFinder(BaseFinder):
        def __init__(self, config, name="rulesfinder"):
            super().__init__(config, node_key="rulesfinder", name=name)
    
        async def run(self, state):
            self.initialize_state(state)
            # Child-specific logic...
            return await self.common_run_logic(state, formatted_prompt, elapsed_time, response)
    

    Key Changes and Considerations:

    • Explicit Initialization: Instead of using a dictionary, I explicitly check and initialize each key in the initialize_state method. This makes the setup easier to understand and modify when adding new types to the state structure.

    • Flexibility for Future Modifications: This format lets you add new state keys by simply adding another if block. You can also customize any default values or structures as needed.

    • Separating Concerns: By having initialize_state just for setting default state values and separate custom logic within each child class, you maintain a clear separation of responsibilities in your code.

    This approach can be easily adapted if you need to add more complex initialization logic for different state keys in the future.