-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangchain_tool_guard.py
More file actions
226 lines (174 loc) · 5.89 KB
/
langchain_tool_guard.py
File metadata and controls
226 lines (174 loc) · 5.89 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
LangChain tool interception with predicate-secure.
This example demonstrates:
- Wrapping a LangChain AgentExecutor with SecureAgent
- Policy-based authorization for tool calls
- Getting SentienceLangChainCore for tool interception
- Debug mode for troubleshooting
Requirements:
pip install predicate-secure[langchain]
pip install langchain langchain-openai
"""
import tempfile
from predicate_secure import SecureAgent
# Uncomment for actual LangChain usage
# from langchain.agents import AgentExecutor, create_react_agent
# from langchain_openai import ChatOpenAI
# from langchain.tools import Tool
def create_tool_policy() -> str:
"""Create a tool authorization policy."""
policy_content = """
# Tool authorization policy
rules:
# Allow search tool
- action: "tool.search"
resource: "*"
effect: allow
# Allow calculator tool
- action: "tool.calculator"
resource: "*"
effect: allow
# Allow file read (read-only)
- action: "tool.file_read"
resource: "/safe/path/*"
effect: allow
# Block file write
- action: "tool.file_write"
resource: "*"
effect: deny
# Block shell commands
- action: "tool.shell"
resource: "*"
effect: deny
# Block network requests to untrusted domains
- action: "tool.http_request"
resource: "https://untrusted.com/*"
effect: deny
# Allow API calls to trusted services
- action: "tool.http_request"
resource: "https://api.trusted.com/*"
effect: allow
# Default: deny
- action: "*"
resource: "*"
effect: deny
"""
with tempfile.NamedTemporaryFile(mode="w", suffix="_tool_policy.yaml", delete=False) as f:
f.write(policy_content)
return f.name
def run_secure_tool_agent():
"""Run a secure LangChain agent with tool interception."""
policy_path = create_tool_policy()
# Mock LangChain AgentExecutor for demonstration
# In real usage:
#
# llm = ChatOpenAI(model="gpt-4")
# tools = [search_tool, calculator_tool]
# agent = create_react_agent(llm, tools, prompt)
# agent_executor = AgentExecutor(agent=agent, tools=tools)
#
# secure_agent = SecureAgent(
# agent=agent_executor,
# policy=policy_path,
# mode="strict",
# )
class MockAgentExecutor:
"""Mock LangChain AgentExecutor for demonstration."""
__module__ = "langchain.agents.executor"
def __init__(self):
self.llm = "gpt-4"
self.tools = ["search", "calculator", "file_read"]
def invoke(self, inputs: dict):
print(f"Mock: invoke({inputs})")
return {"output": "Mock result"}
# Create mock executor
agent_executor = MockAgentExecutor()
# Wrap with SecureAgent
secure_agent = SecureAgent(
agent=agent_executor,
policy=policy_path,
mode="debug",
trace_format="console",
principal_id="langchain-agent:tools",
)
print(f"Created: {secure_agent}")
print(f"Framework: {secure_agent.framework}")
print(f"Executor: {secure_agent.wrapped.executor}")
print()
# Demonstrate tracing for tool calls
step = secure_agent.trace_step("tool.search", "query='AI news'")
secure_agent.trace_step_end(step, success=True)
step = secure_agent.trace_step("tool.calculator", "2 + 2")
secure_agent.trace_step_end(step, success=True)
step = secure_agent.trace_step("tool.file_write", "/etc/passwd")
secure_agent.trace_step_end(step, success=False, error="Action denied by policy")
secure_agent.trace_verification(
predicate="no_dangerous_tools",
passed=True,
message="All tool calls were safe",
)
print("\nTool interception flow traced successfully!")
def demonstrate_langchain_core():
"""Demonstrate getting SentienceLangChainCore for advanced usage."""
print("\n" + "=" * 60)
print("Advanced: Getting SentienceLangChainCore")
print("=" * 60 + "\n")
class MockAgentExecutor:
__module__ = "langchain.agents.executor"
def __init__(self):
self.llm = "gpt-4"
agent_executor = MockAgentExecutor()
secure_agent = SecureAgent(
agent=agent_executor,
mode="strict",
)
print(f"Framework: {secure_agent.framework}")
print()
# Without browser, get_langchain_core returns None
core = secure_agent.get_langchain_core()
print(f"Without browser: core = {core}")
print()
# With browser (for browser-enabled LangChain agents):
print("With browser (requires predicate SDK):")
print(" core = secure_agent.get_langchain_core(browser=browser)")
print(" # Use core for tool interception")
print()
# In real usage with browser:
# from playwright.async_api import async_playwright
# async with async_playwright() as p:
# browser = await p.chromium.launch()
# core = secure_agent.get_langchain_core(browser=browser)
def demonstrate_adapter():
"""Demonstrate using the adapter API."""
print("\n" + "=" * 60)
print("Advanced: Using Adapter API")
print("=" * 60 + "\n")
class MockAgentExecutor:
__module__ = "langchain.agents.executor"
def __init__(self):
self.llm = "gpt-4"
agent_executor = MockAgentExecutor()
secure_agent = SecureAgent(
agent=agent_executor,
mode="strict",
)
# Get adapter for the agent
# This provides access to all wired components
try:
adapter = secure_agent.get_adapter()
print(f"Adapter metadata: {adapter.metadata}")
print(f"Executor: {adapter.executor}")
print(f"Plugin: {adapter.plugin}")
except Exception as e:
print(f"Adapter requires predicate SDK: {e}")
def main():
"""Main entry point."""
print("=" * 60)
print("LangChain Tool Guard with predicate-secure")
print("=" * 60)
print()
run_secure_tool_agent()
demonstrate_langchain_core()
demonstrate_adapter()
if __name__ == "__main__":
main()