-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlangchain_react_agent.py
More file actions
47 lines (33 loc) · 1.26 KB
/
langchain_react_agent.py
File metadata and controls
47 lines (33 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
Basic LangChain agent using OpenGradient.
Creates a simple ReAct agent with a tool, powered by an OpenGradient LLM.
Usage:
export OG_PRIVATE_KEY="your_private_key"
python examples/langchain_react_agent.py
"""
import os
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
import opengradient as og
private_key = os.environ.get("OG_PRIVATE_KEY")
# One-time Permit2 approval for OPG spending (idempotent)
llm_client = og.LLM(private_key=private_key)
llm_client.ensure_opg_approval(opg_amount=5)
# Create the OpenGradient LangChain adapter
llm = og.agents.langchain_adapter(
private_key=private_key,
model_cid=og.TEE_LLM.GPT_4_1_2025_04_14,
max_tokens=300,
x402_settlement_mode=og.x402SettlementMode.INDIVIDUAL_FULL,
)
# Define a simple tool
@tool
def get_balance(account: str) -> str:
"""Returns the balance for a given account name."""
balances = {"main": "1.25 ETH", "savings": "10.0 ETH", "treasury": "100.0 ETH"}
return balances.get(account, "Account not found")
# Create a ReAct agent with the tool
agent = create_react_agent(llm, [get_balance])
# Run the agent
result = agent.invoke({"messages": [("user", "What is the balance of my 'treasury' account?")]})
print(result["messages"][-1].content)