|
| 1 | +""" |
| 2 | +LangGraph reference example: Sentience observe → act → verify → branch (self-correcting). |
| 3 | +
|
| 4 | +Install: |
| 5 | + pip install sentienceapi[langchain] |
| 6 | +
|
| 7 | +Run: |
| 8 | + python examples/langgraph/sentience_self_correcting_graph.py |
| 9 | +
|
| 10 | +Notes: |
| 11 | +- This is a template demonstrating control flow; you can replace the "decide" node |
| 12 | + with an LLM step (LangChain) that chooses actions based on snapshot_state/read_page. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import asyncio |
| 18 | +from dataclasses import dataclass |
| 19 | +from typing import Optional |
| 20 | + |
| 21 | +from sentience import AsyncSentienceBrowser |
| 22 | +from sentience.integrations.langchain import SentienceLangChainContext, SentienceLangChainCore |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class State: |
| 27 | + url: str | None = None |
| 28 | + last_action: str | None = None |
| 29 | + attempts: int = 0 |
| 30 | + done: bool = False |
| 31 | + |
| 32 | + |
| 33 | +async def main() -> None: |
| 34 | + # Lazy import so the file can exist without langgraph installed |
| 35 | + from langgraph.graph import END, StateGraph |
| 36 | + |
| 37 | + browser = AsyncSentienceBrowser(headless=False) |
| 38 | + await browser.start() |
| 39 | + |
| 40 | + core = SentienceLangChainCore(SentienceLangChainContext(browser=browser)) |
| 41 | + |
| 42 | + async def observe(state: State) -> State: |
| 43 | + s = await core.snapshot_state() |
| 44 | + state.url = s.url |
| 45 | + return state |
| 46 | + |
| 47 | + async def act(state: State) -> State: |
| 48 | + # Replace this with an LLM-driven decision. For demo purposes, we just navigate once. |
| 49 | + if state.attempts == 0: |
| 50 | + await core.navigate("https://example.com") |
| 51 | + state.last_action = "navigate" |
| 52 | + else: |
| 53 | + state.last_action = "noop" |
| 54 | + state.attempts += 1 |
| 55 | + return state |
| 56 | + |
| 57 | + async def verify(state: State) -> State: |
| 58 | + # Guard condition: URL should contain example.com |
| 59 | + out = await core.verify_url_matches(r"example\.com") |
| 60 | + state.done = bool(out.passed) |
| 61 | + return state |
| 62 | + |
| 63 | + def should_continue(state: State) -> str: |
| 64 | + # Self-correcting loop: retry observe→act→verify up to 3 attempts |
| 65 | + if state.done: |
| 66 | + return "done" |
| 67 | + if state.attempts >= 3: |
| 68 | + return "done" |
| 69 | + return "retry" |
| 70 | + |
| 71 | + g = StateGraph(State) |
| 72 | + g.add_node("observe", observe) |
| 73 | + g.add_node("act", act) |
| 74 | + g.add_node("verify", verify) |
| 75 | + g.set_entry_point("observe") |
| 76 | + g.add_edge("observe", "act") |
| 77 | + g.add_edge("act", "verify") |
| 78 | + g.add_conditional_edges("verify", should_continue, {"retry": "observe", "done": END}) |
| 79 | + app = g.compile() |
| 80 | + |
| 81 | + final = await app.ainvoke(State()) |
| 82 | + print(final) |
| 83 | + |
| 84 | + await browser.close() |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + asyncio.run(main()) |
0 commit comments