-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlanggraph_example.py
More file actions
99 lines (72 loc) · 3.13 KB
/
langgraph_example.py
File metadata and controls
99 lines (72 loc) · 3.13 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
LangGraph + Agent Veil Protocol — E2E example.
A tool-calling agent that uses AVP reputation tools inside a LangGraph
StateGraph to verify another agent before delegating work.
Prerequisites:
pip install agentveil langgraph langchain-openai langchain-core
export OPENAI_API_KEY="sk-..."
Usage:
python examples/langgraph_example.py
"""
import os
import sys
from langgraph.graph import StateGraph, MessagesState, START, END
from langgraph.prebuilt import ToolNode, tools_condition
from langchain_openai import ChatOpenAI
from agentveil import AVPAgent
from agentveil.tools.langgraph import (
avp_check_reputation,
avp_should_delegate,
avp_log_interaction,
configure,
)
AVP_URL = "https://agentveil.dev"
def main():
if not os.environ.get("OPENAI_API_KEY"):
print("ERROR: Set OPENAI_API_KEY environment variable.")
print(" export OPENAI_API_KEY='sk-...'")
sys.exit(1)
# === Step 1: Configure AVP and register agents ===
configure(base_url=AVP_URL, agent_name="langgraph_researcher")
print("=== Registering agents on AVP ===")
researcher = AVPAgent.create(AVP_URL, name="langgraph_researcher")
researcher.register(display_name="LangGraph Researcher")
researcher.publish_card(capabilities=["research", "analysis"], provider="openai")
writer = AVPAgent.create(AVP_URL, name="langgraph_writer")
writer.register(display_name="LangGraph Writer")
writer.publish_card(capabilities=["writing", "editing"], provider="openai")
print(f" Researcher DID: {researcher.did[:40]}...")
print(f" Writer DID: {writer.did[:40]}...")
# === Step 2: Build LangGraph with AVP tools ===
tools = [avp_check_reputation, avp_should_delegate, avp_log_interaction]
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
llm_with_tools = llm.bind_tools(tools)
def agent_node(state: MessagesState):
return {"messages": [llm_with_tools.invoke(state["messages"])]}
graph = StateGraph(MessagesState)
graph.add_node("agent", agent_node)
graph.add_node("tools", ToolNode(tools))
graph.add_edge(START, "agent")
graph.add_conditional_edges("agent", tools_condition)
graph.add_edge("tools", "agent")
app = graph.compile()
# === Step 3: Run the agent ===
print("\n=== Running LangGraph agent ===\n")
query = (
f"I need to delegate a research task to the agent with DID '{writer.did}'. "
"First check their reputation using check_avp_reputation, then decide "
"whether to delegate using should_delegate with a minimum score of 0.3. "
"If approved, log a positive interaction using log_avp_interaction. "
"Give me a summary of what you found."
)
result = app.invoke({"messages": [("user", query)]})
# Print the final response
print("=== Agent response ===")
print(result["messages"][-1].content)
# === Step 4: Verify reputation changed ===
print("\n=== Updated reputation ===")
rep = researcher.get_reputation(writer.did)
print(f" Writer: score={rep['score']:.3f}, confidence={rep['confidence']:.3f}")
print("\n=== Done ===")
if __name__ == "__main__":
main()