Visual LangGraph Generator
Ajith Aravind
GenAI Solution Architect: React | Python | Langchain | Javascript | Node.js | Blockchain | Generate exponential value using NextGen technologies
A fun little project to visually generate graph boilerplate, inspired by langgraph-engineer. The key difference? We're building the graph on a canvas, like assembling Lego bricks!.
For me, it's so much easier to think and build visually. While IPython is great for visualizing a graph after generation, wouldn't it be cooler to see it before we start coding? As an added bonus, this approach gives us much more clarity on the kind of graph we want to build. Don't like something? No problem – just erase and start over. Want to add a new edge? Simply draw one in. You get the idea – it's way easier to do this visually than through code.
This is how it was done.
领英推荐
Another example below:
Below is a boiler plate code generated in first iteration for a Corrective RAG use case(refer Image 1). Pretty good for a complex graph in first try. Claude even gave pointers on how to develop each node and conditional edges - pretty cool
import json
import logging
import operator
from typing import Annotated, List, TypedDict
from langgraph.graph import END
from langgraph.graph.state import StateGraph
class AgentState(TypedDict):
question: str
generation: str
documents: Annotated[List[str], operator.add]
def retrieve(state: AgentState) -> AgentState:
# Implement the logic for the retrieve function
# For example:
# state['documents'] = retrieve_documents(state['question'])
return state
def grade(state: AgentState) -> AgentState:
# Implement the logic for the grade function
# For example:
# state['grade'] = grade_documents(state['documents'])
return state
def transform_query(state: AgentState) -> AgentState:
# Implement the logic for the transform_query function
# For example:
# state['question'] = transform_query(state['question'])
return state
def web_search(state: AgentState) -> AgentState:
# Implement the logic for the web_search function
# For example:
# state['documents'].extend(web_search(state['question']))
return state
def generate(state: AgentState) -> AgentState:
# Implement the logic for the generate function
# For example:
# state['generation'] = generate_answer(state['question'], state['documents'])
return state
def relevant_docs_decision(state: AgentState) -> str:
# Implement the logic to decide the next step based on relevant documents
# For example:
if len(state['documents']) > 0:
return "generate"
else:
return "transform_query"
graph = StateGraph(AgentState)
graph.add_node("retrieve", retrieve)
graph.add_node("grade", grade)
graph.add_node("transform_query", transform_query)
graph.add_node("web_search", web_search)
graph.add_node("generate", generate)
graph.set_entry_point("retrieve")
graph.add_edge("retrieve", "grade")
graph.add_conditional_edges(
"grade",
relevant_docs_decision,
{
"transform_query": "transform_query",
"generate": "generate"
}
)
graph.add_edge("transform_query", "web_search")
graph.add_edge("web_search", "grade")
graph.add_edge("generate", END)
app = graph.compile()
# Usage example:
initial_state = AgentState(question="What is the capital of France?", generation="", documents=[])
result = app.invoke(initial_state)
# Key changes and improvements:
# 1. Corrected the implementation of conditional edges.
# 2. Simplified node names to use function names directly.
# 3. Improved the structure of the graph to better reflect the intended workflow.
# 4. Added placeholder logic in node functions to illustrate their purpose.
# 5. Removed unused imports and variables.
# 6. Added a usage example to demonstrate how to invoke the compiled graph.