{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# LangGraph-Based Systems Inspector using LangGraph\n", "\n", "## Overview\n", "The LangGraph-Based Systems Inspector is a tool designed to help developers create more secure and robust agent-based applications using LangGraph. It offers valuable insights into system architectures and helps identify potential vulnerabilities, addressing the unique challenges associated with developing LangGraph systems. By using this tool, developers can enhance the quality of their projects and ensure a more secure foundation for multi-agent applications.\n", "\n", "## Motivation\n", "The adoption of multi-agent systems with LangGraph brings opportunities and challenges, such as security concerns like prompt injection and understanding complex workflows. This project helps developers secure their systems and improve reliability by analyzing system architecture and highlighting weaknesses. \n", "\n", "This project also takes inspiration from the LangChain project [SCIPE - Systematic Chain Improvement and Problem Evaluation](https://blog.langchain.dev/scipe-systematic-chain-improvement-and-problem-evaluation/), which analyzes independent and dependent failure probabilities to identify the most impactful problematic node in the system.\n", "\n", "\n", "## Key Components\n", "1. **LangGraph and LangCHain**: Orchestrates the multi-agent systems, managing the flow of data between agents.\n", "2. **LLM model**: Generates tester agents, creates test cases, and analyzes results to ensure system robustness.\n", "3. **Pydantic**: Validates data and parses output from the LLM model, ensuring data consistency and reliability.\n", "4. **Jinja2**: Provides robust templating for prompt creation, enhancing flexibility and reusability.\n", "5. **Networkx**: Provides a simplified representation of the system, illustrating agent relationships, properties, and data flow.\n", "6. **Gradio**: Displays results through an interactive user interface, making the system accessible and easy to understand.\n", "\n", "## Method\n", "This is the general workflow of the LangGraph-Based Systems Inspector from user input to insights:\n", "\n", "1. **User Input**: The user provides:\n", " 1. LangGraph target system before compilation.\n", " 2. Description of the system's behavior.\n", " 3. Valid input sample to pass through the \"invoke\" function.\n", "\n", "2. **Gather Information**: The system extracts information from the LangGraph target system object:\n", " 1. Retrieve all nodes, edges, and tools.\n", " 2. Invoke the graph to get all node inputs and outputs.\n", " 3. Generate node descriptions.\n", "\n", "3. **Generate Tester Agents**: The system generates diverse tester agents to test the system's robustness.\n", "\n", "4. **Generate Test Cases**: Each tester agent generates test cases based on node descriptions and input/output data.\n", "\n", "5. **Run Test Cases**: Verify the test cases by running them through the system.\n", " 1. Create valid input for each test case.\n", " 2. Invoke the target system with the new valid inputs.\n", " 3. Save all thread IDs to retrieve the output later.\n", "\n", "6. **Analyze Results**: The system analyzes all outputs against acceptance criteria and creates insights.\n", "\n", "\n", "## Conclusion\n", "\n", "The LangGraph-Based Systems Inspector provides developers with an effective way to enhance the security and reliability of LangGraph-based applications. By automating system architecture analysis and identifying vulnerabilities, it helps tackle key challenges in developing robust multi-agent systems.\n", "\n", "Moving forward, this tool could be expanded to include more advanced performance optimizations, user-friendly interactions, and integration with additional AI analysis tools. As LangGraph evolves, tools like this will be essential for ensuring that complex agent-based applications are both secure and efficient." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## System Workflow\n", "\n", "
\n", "\n", "\"graph\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup and Imports\n", "\n", "Install and import necessary libraries and set up the environment." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install --quiet -U gradio networkx jinja2 langchain-core langchain-openai langgraph pydantic python-dotenv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import copy\n", "import operator\n", "import uuid\n", "import os\n", "from typing import (Annotated, Any, Dict, List, Optional, Set, Tuple, Type,\n", " Union)\n", "\n", "import gradio as gr\n", "import networkx as nx\n", "from IPython.display import Image\n", "from jinja2 import Template\n", "from langchain_core.messages import (AIMessage, ChatMessage, FunctionMessage,\n", " HumanMessage, SystemMessage, ToolMessage)\n", "from langchain_core.runnables.config import RunnableConfig\n", "from langchain_openai import ChatOpenAI\n", "from langgraph.checkpoint.memory import MemorySaver\n", "from langgraph.constants import Send\n", "from langgraph.graph import MessagesState, StateGraph\n", "from langgraph.graph.graph import CompiledGraph\n", "from langgraph.prebuilt import ToolNode, tools_condition\n", "from pydantic import BaseModel, Field, PrivateAttr\n", "from typing_extensions import TypedDict\n", "from dotenv import load_dotenv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set up LLM model\n", "- Set up API keys in a .env file\n", "\n", "- Define the LLM model for the whole system. \n", "\n", "- It must be a LangChain compatible model.\n", "\n", "- It must support pydantic output parsing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load environment variables\n", "load_dotenv()\n", "\n", "# Set OpenAI API key\n", "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY')\n", "\n", "# LLM model for all the calls\n", "llm = ChatOpenAI(model=\"gpt-4o\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Utils\n", "\n", "All utility functions needed for the system inspector." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def create_structured_llm(config: dict, structured_output: BaseModel):\n", " \"\"\"\n", " Creates a structured language model (LLM) based on the provided configuration and structured output model.\n", " Args:\n", " config (dict): A dictionary containing the configuration for the LLM. It should have a key \"configurable\" \n", " which contains another dictionary with the key \"llm\" representing the language model.\n", " structured_output (BaseModel): An instance of a BaseModel that defines the structure of the output.\n", " Returns:\n", " The structured language model configured with the provided structured output.\n", " Raises:\n", " ValueError: If the LLM model is not valid or cannot be configured with the structured output.\n", " \"\"\"\n", "\n", " try:\n", " model = config[\"configurable\"].get(\"llm\")\n", " structured_llm = model.with_structured_output(structured_output)\n", " return structured_llm\n", " except:\n", " raise ValueError(\"The llm model is not valid\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "class PromtTemplate:\n", " \"\"\"A class to render a prompt template with input variables.\"\"\"\n", " def __init__(self, template: str, input_variables: list[str]):\n", " self.template = Template(template)\n", " self.input_variables = input_variables\n", "\n", " def render(self, **kwargs):\n", " return self.template.render(**kwargs)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "class Config(TypedDict):\n", " user_id: uuid.uuid4\n", " thread_id: uuid.uuid4\n", " description: str" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wrapper function that provides error handling and configuration management around graph execution." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def invoke_graph(graph: CompiledGraph, \n", " input: Any,\n", " thread_id:Optional[str] = None,\n", " user_id:Optional[str]= None,\n", " description:str=\"\") -> tuple[Config, bool, str]:\n", " \n", "\n", " thread_id = thread_id if thread_id else str(uuid.uuid4())\n", " user_id = user_id if user_id else str(uuid.uuid4())\n", " \n", " config = Config(thread_id=thread_id,\n", " user_id=user_id,\n", " description=description)\n", " \n", " configurable = {\"configurable\": config}\n", "\n", " error = False\n", " error_message = \"\"\n", "\n", " try:\n", " graph.invoke(input, config=configurable, stream_mode=\"debug\")\n", " except Exception as e:\n", " error_message = f\"Graph execution failed: {e}\"\n", " error = True\n", "\n", " return config, error, error_message" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Custom reducer function to handle parallel execution of multiple nodes." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def reduce_valid_input(left: Any | None, right: Any | None) -> Any:\n", " if left is None:\n", " return right\n", " if right is None:\n", " return left\n", " if left == right:\n", " return left\n", " if left != right:\n", " return left" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def generate_pairs(a: list, b: list) -> list[tuple]:\n", " \"\"\"\n", " Generate all possible pairs of elements from two lists.\n", " Args:\n", " a (list): The first list of elements.\n", " b (list): The second list of elements.\n", " Returns:\n", " list[tuple]: A list of tuples, where each tuple contains one element from list 'a' and one element from list 'b'.\n", " \"\"\"\n", "\n", " result = []\n", " for node in a:\n", " for tester in b:\n", " result.append((node, tester))\n", "\n", " return result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Retrieve the annotation of any Python object. This function is used to determine the type of a generated input and check whether it is valid." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class TypeAnnotator:\n", " _iterables = [list, tuple, set, dict]\n", " _message_types = [HumanMessage, AIMessage, ToolMessage, SystemMessage, \n", " FunctionMessage, ChatMessage]\n", " _no_iterables = [int, float, str, bool] + _message_types\n", "\n", " def __init__(self, obj: Any):\n", " self.obj = obj\n", "\n", " def get_type(self) -> Type:\n", " \"\"\"Get the type annotation directly as a typing object.\"\"\"\n", " return self._infer_type(self.obj)\n", "\n", " def _infer_type(self, obj: Any) -> Type:\n", " \"\"\"Recursively determine the type annotation of a complex structure.\"\"\"\n", " # Handle message types first\n", " if any(isinstance(obj, t) for t in self._message_types):\n", " return type(obj)\n", " \n", " # Handle basic types\n", " if type(obj) in self._no_iterables:\n", " return type(obj)\n", "\n", " # Handle collections\n", " handlers = {\n", " list: self._handle_list,\n", " dict: self._handle_dict,\n", " tuple: self._handle_tuple,\n", " set: self._handle_set\n", " }\n", " return handlers.get(type(obj), lambda x: type(x))(obj)\n", "\n", " def _handle_list(self, obj: List) -> Type[List]:\n", " \"\"\"Handle list type annotation.\"\"\"\n", " if not obj:\n", " return List[Any]\n", " \n", " types = {self._infer_type(el) for el in obj}\n", " if len(types) == 1:\n", " return List[next(iter(types))]\n", " return List[Union[tuple(sorted(types, key=str))]]\n", "\n", " def _handle_dict(self, obj: Dict) -> Type[Dict]:\n", " \"\"\"Handle dict type annotation.\"\"\"\n", " if not obj:\n", " return Dict[Any, Any]\n", " \n", " key_types = {self._infer_type(k) for k in obj.keys()}\n", " value_types = {self._infer_type(v) for v in obj.values()}\n", " \n", " key_type = (Union[tuple(sorted(key_types, key=str))] \n", " if len(key_types) > 1 else next(iter(key_types)))\n", " value_type = (Union[tuple(sorted(value_types, key=str))] \n", " if len(value_types) > 1 else next(iter(value_types)))\n", " \n", " return Dict[key_type, value_type]\n", "\n", " def _handle_tuple(self, obj: Tuple) -> Type[Tuple]:\n", " \"\"\"Handle tuple type annotation.\"\"\"\n", " if not obj:\n", " return Tuple[()]\n", " return Tuple[tuple(self._infer_type(el) for el in obj)]\n", "\n", " def _handle_set(self, obj: Set) -> Type[Set]:\n", " \"\"\"Handle set type annotation.\"\"\"\n", " if not obj:\n", " return Set[Any]\n", " \n", " types = {self._infer_type(el) for el in obj}\n", " if len(types) == 1:\n", " return Set[next(iter(types))]\n", " return Set[Union[tuple(sorted(types, key=str))]]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "obj_to_str function is used to pass the inputs samples to the LLM model as strings." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "def obj_to_str(obj, max_depth=float('inf'), current_depth=0):\n", " \"\"\"\n", " Converts any Python object into a string representation that looks like the original code.\n", " \n", " Args:\n", " obj: Any Python object\n", " max_depth: Maximum depth for recursion (default: infinite)\n", " current_depth: Current recursion depth (used internally)\n", " \n", " Returns:\n", " String representation of the object that looks like code\n", " \"\"\"\n", " # Check if we've reached maximum depth\n", " if current_depth >= max_depth:\n", " return repr(obj)\n", " \n", " if isinstance(obj, dict):\n", " items = [f'\"{k}\": {obj_to_str(v, max_depth, current_depth + 1)}' for k, v in obj.items()]\n", " return '{' + ', '.join(items) + '}'\n", " elif isinstance(obj, (list, tuple)):\n", " items = [obj_to_str(item, max_depth, current_depth + 1) for item in obj]\n", " return '[' + ', '.join(items) + ']' if isinstance(obj, list) else '(' + ', '.join(items) + ')'\n", " elif isinstance(obj, str):\n", " return f'\"{obj}\"'\n", " elif isinstance(obj, (int, float, bool, type(None))):\n", " return str(obj)\n", " elif obj.__class__.__module__ == 'builtins':\n", " return repr(obj)\n", " else:\n", " # Handle custom objects by reconstructing their initialization\n", " class_name = obj.__class__.__name__\n", " \n", " # If at max_depth, just return the repr\n", " if current_depth >= max_depth:\n", " return f\"{class_name}(...)\"\n", " \n", " # Try to get the object's attributes\n", " try:\n", " # First try to get __dict__\n", " attrs = copy.copy(obj.__dict__)\n", "\n", " # more clear messages representation\n", " attrs.pop('additional_kwargs', None)\n", " attrs.pop('usage_metadata', None)\n", " attrs.pop('response_metadata', None)\n", " \n", " except AttributeError:\n", " try:\n", " # If no __dict__, try getting slots\n", " attrs = {slot: getattr(obj, slot) for slot in obj.__slots__}\n", " except AttributeError:\n", " # If neither works, just use repr\n", " return repr(obj)\n", " \n", " # Convert attributes to key=value pairs\n", " attr_strs = []\n", " for key, value in attrs.items():\n", " # Skip private attributes (starting with _)\n", " if not key.startswith('_'):\n", " attr_strs.append(f\"{key}={obj_to_str(value, max_depth, current_depth + 1)}\")\n", " \n", " return f\"{class_name}({', '.join(attr_strs)})\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Graph creation\n", "### Pydantic/TypedDict Models\n", "\n", "Models used to parse the output of the LLM model and as states. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "class Node_description(BaseModel):\n", " node_description: str = Field(description=\"Description of the node. Max 45 words\")\n", " # guesse_output: str = Field(description=\"What information would it pass to the next nodes?\")\n", "\n", "# ========================================\n", "class SuggestedTester(BaseModel):\n", " role: str = Field(\n", " description=\"Role of the tester in the context of the graph.\",\n", " )\n", " description: str = Field(\n", " description=\"Role description of the tester expertise, focus, concerns, and motives. (you are ...) \",\n", " )\n", " _id: str = PrivateAttr(default_factory=lambda: str(uuid.uuid4()))\n", "\n", " @property\n", " def id(self):\n", " return self._id\n", " \n", "class Testers(BaseModel):\n", " testers: List[SuggestedTester] = Field(\n", " description=\"Comprehensive list of testers with their roles and descriptions\",\n", " )\n", "\n", "# ========================================\n", "class TestCase(BaseModel):\n", " name: str = Field(description=\"name of the test case.\")\n", " \n", " description: str = Field(description=\"Test case description\")\n", " \n", " acceptance_criteria: str = Field(description=\"criteal to pass the test\")\n", " \n", " tester_id: str = Field(description=\"leave this field blank\", default='')\n", "\n", " _id: str = PrivateAttr(default_factory=lambda: str(uuid.uuid4()))\n", "\n", " @property\n", " def id(self):\n", " return self._id\n", " \n", "class TaseCasesList(BaseModel):\n", " test_cases: List[TestCase] = Field(description=\"Comprehensive list of test cases with their properties\")\n", "\n", "# ========================================\n", "class Input(BaseModel):\n", " new_input : str = Field(description=\"new input for the test case\")\n", " tester_id: str = Field(description=\"leave this field blank\", default='')\n", " test_case_id: str = Field(description=\"leave this field blank\", default='')\n", " actual_input: Optional[Any] = Field(description=\"leave this field blank\", default=None)\n", " is_successful: bool = Field(description=\"leave this field blank\", default=False)\n", "\n", "# ========================================\n", "class FinalOutput(BaseModel):\n", " assertion : bool = Field(description=\"Assertion result of the test case\")\n", " comments : str = Field(description=\"Comments on the test case output\")\n", " tester_id: str = Field(description=\"leave this field blank\", default='')\n", " test_case_id: str = Field(description=\"leave this field blank\", default='')\n", "\n", "# ========================================\n", " \n", "class OverallState(TypedDict):\n", " # user input\n", " user_description: str\n", " valid_input: Annotated[Any, reduce_valid_input]\n", " graph_before_compile: StateGraph\n", "\n", " # internal use\n", " compiled_graph: Annotated[CompiledGraph, reduce_valid_input]\n", " summary_graph: nx.DiGraph\n", " execution_configs: Annotated[list[Config], operator.add]\n", " testers: dict[str, SuggestedTester]\n", " node_and_tester: list[tuple]\n", " test_cases: Annotated[list[TestCase], operator.add]\n", " all_new_inputs: Annotated[list[Input], operator.add]\n", " listResults: Annotated[list[FinalOutput], operator.add]\n", "\n", "# ========================================\n", "# subgraph to create new inputs\n", "class SubGraphState(TypedDict):\n", " current_test_case: TestCase\n", " valid_input: Annotated[Any, reduce_valid_input]\n", " all_new_inputs: Annotated[list[Input], operator.add]\n", " compiled_graph: Annotated[CompiledGraph, reduce_valid_input]\n", " execution_configs: Annotated[list[Config], operator.add]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Static test node\n", "- Compile and set up a checkpoint for the graph.\n", "- Go over the graph and create a lightweight representation of the graph using Networkx." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# nodes\n", "def static_test(state: OverallState):\n", " memory = MemorySaver() # it could be a SQLite database\n", " graph_after_compile = state[\"graph_before_compile\"].compile(checkpointer=memory)\n", "\n", " graph_object = graph_after_compile.get_graph()\n", "\n", " nodes = graph_object.nodes\n", " edges = graph_object.edges\n", "\n", " graph_sumary = nx.DiGraph()\n", "\n", " for name, node in nodes.items():\n", " tools = {}\n", " type_node = type(node.data)\n", "\n", " if type_node == ToolNode:\n", " for name_tool, tool in node.data.tools_by_name.items():\n", " tools[name_tool] = tool.description\n", "\n", " graph_sumary.add_node(name, type=type_node, runnable=node.data, tools=tools, name=name) \n", "\n", " for edge in edges:\n", " graph_sumary.add_edge(edge.source, edge.target, conditional=edge.conditional)\n", "\n", " return {\"compiled_graph\": graph_after_compile,\n", " \"summary_graph\": graph_sumary,\n", " \"execution_configs\": []}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate node descriptions\n", "It will generate a description for each node base on the node's input, output, tools, and edges." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# Prompts \n", "node_description_promt = PromtTemplate(template=\"\"\"\n", "You are a workflow developer tasked with characterizating a graph. \n", "You have focused on LangChain and LangGraph frameworks in python.\n", "Using the data below, describe what a node is for:\n", "\n", "general graph description: {{graph_description}}\n", "\n", "node name: {{node_name}}\n", "type: {{type}}\n", "{% if node_description %} previous description : {{node_description}} {% endif %}\n", "\n", "income nodes: {{income_nodes}}\n", "sample_input: {{input}}\n", "\n", "outcome nodes: {{outcome_nodes}}\n", "sample_output: {{output}}\n", "\n", "{% if functions %}functions: {{functions}} {% endif %}\n", "\n", "Take your time and be clrear.\n", "\n", "First, identify the node name and its type.\n", "Then look at the input_node, sample_input, and output_node, sample_output. \n", "Explain how it could interact with neighboring nodes.\n", "Explain the input and output requirements.\n", "{% if node_description %}Combine previous description and current description.{% endif %}\n", "{% if functions %}figure out what the fuction are for in the graph context.{% endif %} \n", "Find out how the node can contribute to achieve the description. \n", "Finally, write the description of the node.\"\"\", \n", "input_variables=[\"graph_description\", \"input\", \"output\", \"node_name\", \"type\", \"functions\", \"income_nodes\", \"outcome_nodes\", \"node_description\"])\n", "\n", "\n", "# nodes\n", "def generate_node_descriptions(state: OverallState, config: RunnableConfig):\n", " structured_llm = create_structured_llm(config, Node_description)\n", "\n", " config, error, error_message = invoke_graph(graph=state[\"compiled_graph\"],\n", " input=state[\"valid_input\"])\n", " \n", " if error:\n", " raise ValueError(f\"Invalid graph input: {error}\")\n", " \n", " configurable = {\"configurable\": config}\n", "\n", " history = list(state[\"compiled_graph\"].get_state_history(configurable))\n", " history.reverse()\n", "\n", " node_name_in_tasks = [item.tasks[0].name for item in history if item.tasks]\n", " node_name_in_tasks.remove('__start__')\n", " \n", " node_tasks_in_tasks = [item.tasks[0].result for item in history if item.tasks]\n", "\n", " summary_graph = state[\"summary_graph\"]\n", "\n", " for index, node_name in enumerate(node_name_in_tasks):\n", " current_description = summary_graph.nodes[node_name].get(\"description\", None)\n", " functions = summary_graph.nodes[node_name].get(\"tools\", None)\n", "\n", " actual_input = node_tasks_in_tasks[index]\n", " actual_output = node_tasks_in_tasks[index+1]\n", "\n", " parameters = {\"graph_description\":state[\"user_description\"],\n", " \"input\":obj_to_str(actual_input),\n", " \"output\":obj_to_str(actual_output),\n", " \"node_name\":node_name,\n", " \"type\":str(summary_graph.nodes[node_name][\"type\"]),\n", " \"functions\":functions,\n", " \"income_nodes\":str(summary_graph.in_edges(node_name)),\n", " \"outcome_nodes\":str(summary_graph.out_edges(node_name)),\n", " \"node_description\":current_description}\n", " \n", " system_message = node_description_promt.render(**parameters)\n", " llm_description = structured_llm.invoke([SystemMessage(system_message)])\n", "\n", " summary_graph.nodes[node_name][\"description\"] = llm_description.node_description\n", "\n", " \n", " return {\"execution_configs\": [config],\n", " \"summary_graph\": summary_graph}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate testers \n", "It will generate several testers to test the system.s\n", "In the future, there could be human in the loop interaction to verify the testers to be created." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# promts\n", "testers_instructions = PromtTemplate(\"\"\"\n", "You are tasked with creating a set of AI tester personas. \n", "Those are going to test an agentic system in python. \n", "Those must have a grasp of the LLM and LangGraph frameworks.\n", "Follow these instructions carefully:\n", "1. First, review the general graph description:\n", "{{graph_description}}\n", " \n", "2. Examine any security team feedback that has been optionally provided to guide creation of the testers: \n", "{{human_analyst_feedback}}\n", " \n", "3. Determine the most critical kind of testing needed based upon the feedback above. Add more if needed.\n", "Max number of analysts: {{max_analysts}}\n", "\n", "5. Assign one tester to each theme. For each tester, provide the following information:\"\"\",\n", "input_variables=[\"graph_description\", \"human_analyst_feedback\", \"max_analysts\"])\n", "\n", "# Nodes\n", "def generate_testers(state: OverallState, config: RunnableConfig):\n", " structured_llm = create_structured_llm(config, Testers)\n", " \n", " parameters = {\"graph_description\":state[\"user_description\"],\n", " \"human_analyst_feedback\":\"Include: functional tester, anti injection and jailbreak LLM engeener, vulnerabilities bounty hunter\", \n", " \"max_analysts\":3}\n", "\n", " system_message = testers_instructions.render(**parameters)\n", " created_testers = structured_llm.invoke([SystemMessage(system_message)])\n", "\n", " nodes = [node_data for node_name, node_data in state[\"summary_graph\"].nodes(data=True) if node_data.get(\"description\", None)]\n", " testers = created_testers.testers\n", " \n", " return {\"testers\": {tester.id: tester for tester in created_testers.testers},\n", " \"node_and_tester\": generate_pairs(nodes, testers),\n", " \"test_cases\": []}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate Test Cases:\n", "Each tester will generate test cases for each node based on the node's properties.\n", "\n", "This node has a specific conditional edge. It will check whether the state[\"node_and_tester\"] list contains elements, which it uses to create test cases. If it does, the process will revisit the node until the list is empty. Afterward, it will use Send to parallelize the test execution. You can learn more about Send in [this excellent sample](https://github.com/langchain-ai/langchain-academy/blob/main/module-4/map-reduce.ipynb) from the LangGraph academy." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "# promts\n", "test_case_prompt = PromtTemplate(\"\"\"\n", "{{role_description}}\n", "\n", "You must test this node deeply. The below is the node information:\n", " \n", "name: {{node_name}}\n", "type: {{node_type}}\n", "description: {{node_description}}\n", "functions: {{node_functions}}\n", "sample_input: {{sample_input}}\n", "sample_output: {{sample_output}}\n", " \n", "existing test cases: {{existing_test_cases}}\n", " \n", "How would you test the node? \n", "Give at least 3 test case.\n", "AVOID [repeating the same test case, puting values in the acceptance_criteria] \n", "Take your time and think out of the box.\n", "If there is no test case neded, return and empty object.\"\"\",\n", "input_variables=[\"role_description\", \"node_name\", \"node_type\", \"node_description\", \"node_functions\", \"sample_input\", \"sample_output\", \"existing_test_cases\"])\n", "\n", "# Nodes\n", "def generate_test_cases(state: OverallState, config: RunnableConfig):\n", " structured_llm = create_structured_llm(config, TaseCasesList)\n", "\n", " current_node_and_tester = state[\"node_and_tester\"].pop(0)\n", " current_node = current_node_and_tester[0]\n", " current_tester = current_node_and_tester[1]\n", "\n", " configuration = state[\"execution_configs\"][0]\n", " configurable = {\"configurable\": configuration}\n", "\n", " history = list(state[\"compiled_graph\"].get_state_history(configurable))\n", " history.reverse()\n", "\n", " node_tasks_in_tasks = [(item.tasks[0].name, item.tasks[0].result) for item in history if item.tasks]\n", "\n", " actual_inputs = []\n", " actual_outputs = []\n", "\n", " for index, task in enumerate(node_tasks_in_tasks):\n", " if task[0] == current_node[\"name\"]:\n", " actual_inputs.append(node_tasks_in_tasks[index-1][1])\n", " actual_outputs.append(task[1])\n", "\n", " name_test_cases = [test_case.name for test_case in state[\"test_cases\"]]\n", " \n", " parameters = {\"role_description\":current_tester.description,\n", " \"node_name\":current_node[\"name\"],\n", " \"node_type\":current_node[\"type\"],\n", " \"node_description\":current_node[\"description\"],\n", " \"node_functions\":current_node[\"tools\"],\n", " \"sample_input\":obj_to_str(actual_inputs),\n", " \"sample_output\":obj_to_str(actual_outputs),\n", " \"existing_test_cases\":name_test_cases}\n", " \n", " system_message = test_case_prompt.render(**parameters)\n", " test_cases = structured_llm.invoke([SystemMessage(content=system_message)]+[HumanMessage(content=\"Generate the set of test cases.\")])\n", "\n", " for test_case in test_cases.test_cases:\n", " test_case.tester_id = current_tester.id\n", "\n", " return {\"test_cases\": test_cases.test_cases}\n", "\n", "# conditional edges\n", "def more_test_cases(state: OverallState):\n", " if state[\"node_and_tester\"]:\n", " return \"generate_test_cases\"\n", " else:\n", " routing = []\n", " valid_inpout = state[\"valid_input\"]\n", " compiled_graph = state[\"compiled_graph\"]\n", " execution_configs = state[\"execution_configs\"]\n", "\n", " for test_case in state[\"test_cases\"]:\n", " new_state = {\"current_test_case\":test_case, \n", " \"valid_input\":valid_inpout,\n", " \"compiled_graph\":compiled_graph}\n", " \n", " routing.append(Send(\"run_test_cases\",new_state))\n", "\n", " return routing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate Subgraph to Create New Inputs and Run Tests\n", "First, it will create a new input for each test case. This input is initially a string, so we need to convert it to the correct type before passing it to the invoke function in the target graph.\n", "\n", "Then, it will run the tests for each input and save the thread ID and user ID to retrieve the output later.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Promts\n", "new_input_prompt = PromtTemplate(\"\"\"\n", "You are a LangChain and LangGraph python developer. Your are focused on testing a graph of LangGraph.\n", "Some senior testers have provided you with a test case for the graph.\n", "The test case is as follows:\n", " \n", "- name: {{test_case_name}}\n", "- description: {{test_case_description}}\n", "- graph valid input: {{graph_valid_input}}\n", " \n", "you must follow this instructions:\n", "1. Review the test case description.\n", "2. Validate if the test case can be tested with an input using the valid input structure.\n", "3. If it can't be tested, return an empty string.\n", "4. If it can be tested, create a new imput for the test case.\n", "5. verify carefully the new input format. Every open bracket must have a closing bracket and so on.\n", "6. For each property in the input, you MUST make sure it is the same type as it is in valid input.\n", "7. For any message object, the content must be a string.\n", "8. Make sure the string could be passed to the 'eval' python function. For example, if the input has 'null' it should be 'None'.\n", "9. Return the new input.\"\"\",\n", "input_variables=[\"test_case_name\", \"test_case_description\", \"graph_valid_input\"])\n", "\n", "# Nodes\n", "def generate_new_inputs(state: SubGraphState, config: RunnableConfig):\n", " structured_llm = create_structured_llm(config, Input)\n", "\n", " parameters = {\"test_case_name\": state[\"current_test_case\"].name,\n", " \"test_case_description\": state[\"current_test_case\"].description,\n", " \"graph_valid_input\": obj_to_str(state[\"valid_input\"])}\n", "\n", " system_message = new_input_prompt.render(**parameters)\n", "\n", " new_input = structured_llm.invoke([SystemMessage(content=system_message)]+[HumanMessage(content=\"Generate the new input.\")])\n", " new_input.tester_id = state[\"current_test_case\"].tester_id\n", " new_input.test_case_id = state[\"current_test_case\"].id\n", "\n", " try:\n", " agent_valid_input = eval(new_input.new_input)\n", " agent_valid_input_type = TypeAnnotator(agent_valid_input).get_type()\n", " valid_input_type = TypeAnnotator(state[\"valid_input\"]).get_type()\n", "\n", " if agent_valid_input_type == valid_input_type:\n", " new_input.actual_input = agent_valid_input\n", "\n", " config, error, error_message = invoke_graph(graph=state[\"compiled_graph\"],\n", " input=agent_valid_input, \n", " description= state[\"current_test_case\"].name,\n", " thread_id=new_input.test_case_id,\n", " user_id=new_input.tester_id)\n", " new_input.is_successful = not error\n", " \n", " return {\"all_new_inputs\": [new_input], \n", " \"execution_configs\": [config]}\n", " else:\n", " raise ValueError(f\"invalid input type for {new_input.new_input}\")\n", "\n", " except Exception as e:\n", " return {\"all_new_inputs\": []}\n", "\n", "# Build the sub graph\n", "sub_builder = StateGraph(SubGraphState)\n", "\n", "sub_builder.add_node(\"generate_new_inputs\", generate_new_inputs)\n", "\n", "sub_builder.set_entry_point(\"generate_new_inputs\")\n", "sub_builder.set_finish_point(\"generate_new_inputs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyze Results:\n", "It will analyze the results of the test cases and generate insights according to the acceptance criteria. It retrieves the last state of the graph execution via target_graph.get_state(config), where the config receives the tester ID and the test case ID to identify the output of a specific test case." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# promts\n", "assertion_prompt = PromtTemplate(\"\"\"\n", "{{role_description}}\n", "A test cases has been run on the graph and here you have the results. \n", "You must validate the results using the test case description, acceptance criteria, and the output of the test case: \n", " \n", "- test case name: {{test_case_name}}\n", "- test case description: {{test_case_description}}\n", "- acceptance criteria: {{acceptance_criteria}}\n", "- output: {{output}}\n", " \n", "You must validate the output. If the output is as described in the acceptance criteria, return 'True'. Otherwise, return 'False'.\n", "Finally, write additional comments of how to solve the issue if the output is not as expected.\n", "If the output is as expected, the comments should be a description of the behavior of the graph.\n", "\"\"\", input_variables=[\"test_case_name\" ,\"role_description\", \"test_case_description\", \"acceptance_criteria\", \"output\"])\n", "\n", "# Nodes\n", "def analize_results(state: OverallState, config: RunnableConfig):\n", " structured_llm = create_structured_llm(config, FinalOutput)\n", "\n", " current_result_config = state[\"execution_configs\"].pop(0)\n", "\n", " if not current_result_config[\"description\"]:\n", " return {\"listResults\": []}\n", " \n", " for test_case in state[\"test_cases\"]:\n", " if test_case.id == current_result_config[\"thread_id\"]:\n", " current_test_case = test_case\n", " break \n", "\n", " tester = state[\"testers\"][current_result_config[\"user_id\"]] \n", "\n", " configurable = {\"configurable\": current_result_config}\n", "\n", " parameters = {\"test_case_name\" : current_test_case.name,\n", " \"role_description\":tester.description,\n", " \"test_case_description\":current_test_case.description,\n", " \"acceptance_criteria\":current_test_case.acceptance_criteria,\n", " \"output\":obj_to_str(state[\"compiled_graph\"].get_state(configurable).values)}\n", "\n", " system_message = assertion_prompt.render(**parameters)\n", " final_output = structured_llm.invoke([SystemMessage(content=system_message)]+[HumanMessage(content=\"Generate the final output.\")])\n", " final_output.tester_id = tester.id\n", " final_output.test_case_id = current_test_case.id\n", "\n", " return {\"listResults\": [final_output]}\n", "\n", "# conditional edges\n", "def more_results(state: OverallState):\n", " return bool(state[\"execution_configs\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set Up LangGraph Workflow\n", "\n", "Define the LangGraph workflow by adding nodes and edges." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "builder = StateGraph(OverallState)\n", "\n", "builder.add_node(\"static_test\", static_test)\n", "builder.add_node(\"generate_node_descriptions\", generate_node_descriptions)\n", "builder.add_node(\"generate_testers\", generate_testers)\n", "builder.add_node(\"generate_test_cases\", generate_test_cases)\n", "builder.add_node(\"run_test_cases\", sub_builder.compile())\n", "builder.add_node(\"analize_results\", analize_results)\n", "\n", "\n", "builder.set_entry_point(\"static_test\")\n", "builder.add_edge(\"static_test\", \"generate_node_descriptions\")\n", "builder.add_edge(\"generate_node_descriptions\", \"generate_testers\")\n", "builder.add_edge(\"generate_testers\", \"generate_test_cases\")\n", "builder.add_conditional_edges(\"generate_test_cases\", more_test_cases, [\"generate_test_cases\", \"run_test_cases\"])\n", "builder.add_edge(\"run_test_cases\", \"analize_results\")\n", "builder.add_conditional_edges(\"analize_results\", more_results, {True: \"analize_results\", False: \"__end__\"})\n", "\n", "graph = builder.compile()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# View\n", "# display(Image(graph.get_graph(xray=True).draw_mermaid_png()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example Usage\n", "\n", "## Simple graph for testing\n", "Very simple graph to test the system inspector. It is a react agent with only one node and one tool node." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Tools\n", "def multiply(a: float, b: float) -> float:\n", " \"\"\"Multiply a and b.\n", "\n", " Args:\n", " a: first float\n", " b: second float\n", " \"\"\"\n", " return a * b\n", "\n", "def add(a: float, b: float) -> float:\n", " \"\"\"Adds a and b.\n", "\n", " Args:\n", " a: first float\n", " b: second float\n", " \"\"\"\n", " return a + b\n", "\n", "def divide(a: float, b: float) -> float:\n", " \"\"\"Divides a by b.\n", "\n", " Args:\n", " a: first float\n", " b: second float\n", " \"\"\"\n", " return a / b\n", "\n", "tools = [add, multiply, divide]\n", "\n", "llm_with_tools = llm.bind_tools(tools)\n", "\n", "# System message\n", "sys_msg = SystemMessage(content=\"You are a helpful assistant tasked with performing arithmetic on a set of inputs.\")\n", "\n", "# Node\n", "def assistant(state: MessagesState):\n", " return {\"messages\": [llm_with_tools.invoke([sys_msg] + state[\"messages\"])]}\n", "\n", "# Graph\n", "builder_sample = StateGraph(MessagesState)\n", "\n", "builder_sample.add_node(\"assistant\", assistant)\n", "builder_sample.add_node(\"tools\", ToolNode(tools))\n", "\n", "\n", "builder_sample.set_entry_point(\"assistant\")\n", "builder_sample.add_conditional_edges(\n", " \"assistant\",\n", " # If the latest message (result) from assistant is a tool call -> tools_condition routes to tools\n", " # If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END\n", " tools_condition,\n", ")\n", "builder_sample.add_edge(\"tools\", \"assistant\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAANYAAAD5CAIAAADUe1yaAAAAAXNSR0IArs4c6QAAIABJREFUeJztnWlcU8fex+ckIWSHJOwgm+yKKyqtuFWolaoXqCtaa1tvq7V6W9cutLWL1i5a6r19alute8UVFYuCe5W6YaUKqAXZRAiEBBISsuc8L+KH0hgQNefMCZnvxxdylvn/En7MmZkz8x8Mx3GAQMCDBlsAwtlBFkRABlkQARlkQQRkkAURkEEWRECGAVvA46CUG5QyQ5vSpG41GvWOMazEcMHoDIzDp3MEDLEvk8Whw1ZEFTDH+AUCAACQ3tPe+VNdWaLmChgmI84R0Ll8BpNNA47wCRiumKrZ2NZqalMa1QoT140e0pcbPoDHE7rAlgYZx7CgQmb4/XAT3QUTejFD+nA9/F1hK3pS7t3RVBar5RKduyfz6YlihovztogcwIKXjspuF7Y+PckjrD8Pthb78+dvLb/nyEakevR92g22FjhQ3YL7vq3tO1wQFSeALYRYLufJW+WGsTO8YQuBAHUtiOP4j+9WTHrdzzeEDVsLGZReUlaVqJNf8YUthGyoa8Hvl5fPzgjmChyyz/543LqiLP5dOfk/AbCFkApFLbgvs3Z4itg32Cnqv47cKFDI6nSjp3jBFkIeVOyIXcyVxY4QOKH/AACxw904fPrNy0rYQsiDchZsbtSXF6kiB/fw/kcXDBorPLNXClsFeVDOgr/nyJ6eKIatAiYMF9rgROGlozLYQkiCWhaUVGld2bTQ2B44/vdIDB0nkl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5LizGBxXgSeH7jirjI06k1KqNun0BrVudJpHr4hHy3iJLEgVQvpyQ/py79xQlV1TlxfIRQEcg85MZzIYrgwKZizGcdykM5oMRhcmrVmiCenLDR/OC455nLSIyILUoncsr3csDwBQX6lRK0xqhVGvM2vtkejXvrhyaCwOkyPg8IV078CHDLt0DbIgRfENoeIO6kRg24JMFmamXuX/SLh5uhC2EAJhT2z/lvhCF2m1Y+dFqLyuEvv2hBVPPR7bFvTq5UrJnCfdpUWqD+7DYbigatAB6LQW9A9j/bZfQroe+3ByZ118MhV3IEc8SFf7EZdcUJQVqfqPEgu9mZ1NbqMUGpVR0WT4bZ/khYX+7t14NYSgAg/ZEruyRF10tkVSqaUzqP5gFvm6KqT60L6coePFXAHq6TsMD7FgOzoN1bekw3HA4jhAVY2worsWRCAIAlUbCMggCyIggyyIgAyyIAIyyIIIyCALIiDz/x8c2UhUcKGwAAAAAElFTkSuQmCC", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# View\n", "display(Image(builder_sample.compile().get_graph(xray=True).draw_mermaid_png()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## User input\n", "Define user input for the system inspector. Only 3 inputs are needed: the target graph, the system description, and a valid input sample." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "user_description = \"This is a react graph. It has one agent and one tool node. The agent is an assistant that can perform arithmetic operations.\"\n", "\n", "user_valid_input = {\"messages\": [HumanMessage(content=\"Add 3 and 4\")]}\n", "\n", "graph_before_compile = builder_sample" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In most cases, the recursion limit error is raised when the default value (25) is used. Therefore, it is necessary to increase the recursion limit. The LLM model is passed as a configurable parameter." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "configurations = {\"configurable\": {\"llm\": llm}, \"recursion_limit\": 50}\n", "\n", "result = graph.invoke({\"user_description\":user_description\n", " ,\"valid_input\": user_valid_input, \n", " \"graph_before_compile\": graph_before_compile},\n", " config=configurations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simple gradio interface to display the results of the system inspector." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define Unicode symbols for checkmark and cross\n", "CHECKMARK = \"\\u2705\" # ✅\n", "CROSS = \"\\u274C\" # ❌\n", "\n", "with gr.Blocks() as demo:\n", " for result_graph in result[\"listResults\"]:\n", " symbol = CHECKMARK if result_graph.assertion else CROSS\n", "\n", " for test_case in result[\"test_cases\"]:\n", " if test_case.id == result_graph.test_case_id:\n", " current_test_case = test_case\n", " break\n", "\n", " tester = result[\"testers\"][result_graph.tester_id] \n", "\n", " configurations = {\"configurable\": {\"user_id\":tester.id, \n", " \"thread_id\":current_test_case.id}}\n", "\n", " with gr.Accordion(f\"{current_test_case.name}: {symbol}\", open=False):\n", " gr.Markdown(f\"{result_graph.comments}\")\n", "\n", " with gr.Accordion(f\"Details\", open=False):\n", " gr.Markdown(f\"Tester: {tester.role}\")\n", " gr.Markdown(f\"Teste description: {current_test_case.description}\")\n", " gr.Markdown(f\"Teste assertion: {current_test_case.acceptance_criteria}\")\n", " gr.Markdown(f\"Actual output: {obj_to_str(result[\"compiled_graph\"].get_state(configurations).values)}\")\n", "\n", "demo.launch(debug=False, inbrowser=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sample results\n", "![Sample results](../images/graph_inspector_system_langgraph_result.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Final Thoughts\n", "The LangGraph-Based Systems Inspector is a tool capable of autonomously analyzing system architecture and identifying potential vulnerabilities. However, it is important to note that the tool is still in its early stages of development, and there remains significant room for improvement. For example:\n", "\n", "- The system could include human-in-the-loop interaction at several stages to supervise the process.\n", "- If a generated input is invalid, the system should be able to identify the issue and generate a new input accordingly.\n", "- By leveraging a lightweight representation of the graph, the system could answer questions like, \"What is the most used node in the system?\" or \"What is the most critical node in the system?\" in a chat interface.\n", "- The system could implement solutions and test them to determine if the system becomes more robust.\n", "- Since the system already has the ability to run individual nodes, it could isolate a node and execute it in a different environment to verify its proper functionality.\n", "- It can fetch the state history of an execution, enabling the branching of a new execution to test slight variations of the same input.\n", "- When comparing outputs against the acceptance criteria, it sometimes makes mistakes. For instance:\n", " - A target system response might differ from the expected output but still be valid. The system should be able to recognize such cases.\n", " - Some target system responses request additional information, but the system cannot interact further, leading to an incorrect classification of the response as invalid.\n", " - The system lacks tools to verify outputs for specific tasks, such as arithmetic operations." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://europe-west1-genai-agents-views-tracker.cloudfunctions.net/genai-agents-tracker?notebook=all-agents-tutorials--graph-inspector-system-langgraph)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" } }, "nbformat": 4, "nbformat_minor": 2 }