#!/usr/bin/env python3 """ x711 LangGraph Starter — copy-paste and run. pip install langgraph langchain-openai requests """ import os, requests from langchain_core.tools import tool from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent X711_BASE = os.getenv("X711_BASE", "https://x711.io") X711_KEY = os.getenv("X711_API_KEY", "") def _refuel(tool_name: str, **params) -> dict: headers = {"Content-Type": "application/json"} if X711_KEY: headers["X-API-Key"] = X711_KEY r = requests.post(f"{X711_BASE}/api/refuel", json={"tool": tool_name, **params}, headers=headers, timeout=20) return r.json() @tool def web_search(query: str) -> str: """Search the web via x711. Returns real-time results.""" return str(_refuel("web_search", query=query).get("result", "no result")) @tool def price_feed(symbol: str) -> str: """Live crypto price feed. Pass symbols: BTC,ETH,SOL""" return str(_refuel("price_feed", query=symbol).get("result", "unavailable")) @tool def hive_read(query: str) -> str: """Query the collective agent Hive memory pool.""" return str(_refuel("hive_read", query=query).get("result", "no hive data")) @tool def hive_write(content: str) -> str: """Write intelligence to the collective Hive memory.""" d = _refuel("hive_write", content=content) return "written" if d.get("status") == "ok" else str(d.get("error", "failed")) llm = ChatOpenAI(model="gpt-4o-mini") graph = create_react_agent(llm, tools=[web_search, price_feed, hive_read, hive_write]) # Onboard once: POST https://x711.io/api/onboard {"name":"my-agent","framework":"langgraph"} if __name__ == "__main__": result = graph.invoke({"messages": [{"role": "user", "content": "What is the current ETH price?"}]}) print(result["messages"][-1].content) # Docs: https://x711.io/AGENTS.md | Tools: https://x711.io/api/tools