Skip to content

Instantly share code, notes, and snippets.

View kennethleungty's full-sized avatar
🎯
Focusing

Kenneth Leung kennethleungty

🎯
Focusing
View GitHub Profile
def supervisor_conditional_node(state: SupervisorState) -> dict:
messages = state["messages"]
# Call supervisor with full conversation history
response = supervisor_llm.invoke([SystemMessage(content=SUPERVISOR_PROMPT)] + messages)
return {"messages": [AIMessage(content=response.content, name="supervisor")]}
def supervisor_command_node(
state: SupervisorState,
) -> Command[Literal["transaction_history_agent", "property_profile_agent"]]:
"""Supervisor node using Command for routing + state updates.
Uses structured output to:
1. Determine which agent to route to
2. Extract property_name from the query
3. Update state via Command.update
def router(state):
# Read user intent from the current graph state.
user_intent = state.get("intent")
# Decide which agent to call next based on simple logic.
# This demonstrates how you choose a target node.
if user_intent == "search":
return Command(
update={"task": "run_search"}, # Update part of the state
goto="search_agent" # Explicitly route to this node
def should_continue(
state: SupervisorState,
) -> Literal["transaction_history_agent", "property_profile_agent", "end"]:
"""Extract routing decision from supervisor's message.
Looks for agent names in the supervisor's latest message to determine routing.
Returns "end" if supervisor provides a direct response (no routing needed).
"""
# Find latest supervisor message
for msg in reversed(state["messages"]):
if (
graph.add_conditional_edges(
source="supervisor",
path=should_continue,
path_map={
"transaction_history_agent": "transaction_history_agent",
"property_profile_agent": "property_profile_agent",
"end": END,
},
)
SUPERVISOR_PROMPT = """You are a real estate supervisor based in Singapore that routes queries to specialists or handles them directly.
Available specialists:
- transaction_history_agent - For sales history, market trends, past transactions
- property_profile_agent - For property details, features, location info
Your tasks:
1. Determine which agent to route to (or "none" if you should handle directly)
2. Extract any property name mentioned (e.g., "38 Oxley Road", "One Oxley Rise")
3. If no routing needed, provide a conversational response
{
"document_id": "doc_ec646730",
"extractions": [
{
"extraction_class": "exclusion",
"extraction_text": "The insurance cover provided to You under this Policy is based on the information You have provided to Us.",
"plain_english": "This policy is only valid if you honestly and completely tell us all the facts about your vehicle and driving history. If you don't, you may not be able to make a claim."
},
{
"extraction_class": "exclusion",
output_filename = "data/output/extraction_results.jsonl"
lx.io.save_annotated_documents([result],
output_name=output_filename,
output_dir=".")
import langextract as lx
# Run PDF processor on insurance policy PDF and get concatenated parsed text
processor = PDFProcessor("data/input/driveshield_specimen_policy_value_plan.pdf")
input_text = processor.get_all_text()
# Run extraction
result = lx.extract(
text_or_documents=input_text,
prompt_description=prompt,
import langextract as lx
examples = [
lx.data.ExampleData(
text="This policy does not cover damage caused by floods, earthquakes, or nuclear accidents.",
extractions=[
lx.data.Extraction(
extraction_class="exclusion",
extraction_text="floods",
attributes={