-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_layers_demo.py
More file actions
222 lines (180 loc) · 7.45 KB
/
agent_layers_demo.py
File metadata and controls
222 lines (180 loc) · 7.45 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
"""
Demonstration of all three abstraction layers in Sentience SDK
Layer 1: Direct SDK (Full Control)
Layer 2: SentienceAgent (Technical Commands)
Layer 3: ConversationalAgent (Natural Language)
This script shows how the same task can be accomplished at different abstraction levels.
"""
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def demo_layer1_direct_sdk():
"""
Layer 1: Direct SDK Usage
- Full control over every action
- Requires knowing exact element selectors
- 50+ lines of code for typical automation
"""
print("\n" + "="*70)
print("LAYER 1: Direct SDK Usage (Full Control)")
print("="*70)
from sentience import SentienceBrowser, snapshot, find, click, type_text, press
with SentienceBrowser(headless=False) as browser:
# Navigate
browser.page.goto("https://google.com")
# Get snapshot
snap = snapshot(browser)
# Find search box manually
search_box = find(snap, "role=searchbox")
if not search_box:
search_box = find(snap, "role=textbox")
# Click search box
click(browser, search_box.id)
# Type query
type_text(browser, search_box.id, "magic mouse")
# Press Enter
press(browser, "Enter")
print("\n✅ Layer 1 Demo Complete")
print(" Code required: ~20 lines")
print(" Technical knowledge: High")
print(" Flexibility: Maximum")
def demo_layer2_sentience_agent():
"""
Layer 2: SentienceAgent (Technical Commands)
- High-level commands with LLM intelligence
- No need to know selectors
- 15 lines of code for typical automation
"""
print("\n" + "="*70)
print("LAYER 2: SentienceAgent (Technical Commands)")
print("="*70)
from sentience import SentienceBrowser, SentienceAgent
from sentience.llm_provider import OpenAIProvider
# Initialize
browser = SentienceBrowser(headless=False)
llm = OpenAIProvider(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o-mini")
agent = SentienceAgent(browser, llm, verbose=True)
with browser:
browser.page.goto("https://google.com")
# Execute technical commands
agent.act("Click the search box")
agent.act("Type 'magic mouse' into the search field")
agent.act("Press Enter key")
print("\n✅ Layer 2 Demo Complete")
print(" Code required: ~10 lines")
print(" Technical knowledge: Medium")
print(" Flexibility: High")
print(f" Tokens used: {agent.get_token_stats()['total_tokens']}")
def demo_layer3_conversational_agent():
"""
Layer 3: ConversationalAgent (Natural Language)
- Pure natural language interface
- Automatic planning and execution
- 3 lines of code for typical automation
"""
print("\n" + "="*70)
print("LAYER 3: ConversationalAgent (Natural Language)")
print("="*70)
from sentience import SentienceBrowser, ConversationalAgent
from sentience.llm_provider import OpenAIProvider
# Initialize
browser = SentienceBrowser(headless=False)
llm = OpenAIProvider(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o")
agent = ConversationalAgent(browser, llm, verbose=True)
with browser:
# Execute in natural language (agent plans and executes automatically)
response = agent.execute("Search for magic mouse on google.com")
print("\n✅ Layer 3 Demo Complete")
print(" Code required: ~5 lines")
print(" Technical knowledge: None")
print(" Flexibility: Medium")
print(f" Agent Response: {response}")
def demo_layer3_with_local_llm():
"""
Layer 3 with Local LLM (Zero Cost)
- Uses local Qwen 2.5 3B model
- No API costs
- Runs on your hardware
"""
print("\n" + "="*70)
print("LAYER 3: ConversationalAgent with Local LLM (Zero Cost)")
print("="*70)
from sentience import SentienceBrowser, ConversationalAgent
from sentience.llm_provider import LocalLLMProvider
# Initialize with local LLM
browser = SentienceBrowser(headless=False)
llm = LocalLLMProvider(
model_name="Qwen/Qwen2.5-3B-Instruct",
device="auto", # Use CUDA if available
load_in_4bit=True # Save memory with quantization
)
agent = ConversationalAgent(browser, llm, verbose=True)
with browser:
# Execute in natural language
response = agent.execute("Go to google.com and search for python tutorials")
print("\n✅ Layer 3 with Local LLM Demo Complete")
print(" API Cost: $0 (runs locally)")
print(" Privacy: 100% (no data sent to cloud)")
print(f" Agent Response: {response}")
def demo_comparison():
"""
Side-by-side comparison of all layers
"""
print("\n" + "="*70)
print("COMPARISON: All Three Layers")
print("="*70)
comparison_table = """
| Feature | Layer 1 (SDK) | Layer 2 (Agent) | Layer 3 (Conversational) |
|--------------------------|------------------|------------------|--------------------------|
| Lines of code | 50+ | 15 | 3-5 |
| Technical knowledge | High | Medium | None |
| Requires selectors? | Yes | No | No |
| LLM required? | No | Yes | Yes |
| Cost per action | $0 | ~$0.005 | ~$0.010 |
| Speed | Fastest | Fast | Medium |
| Error handling | Manual | Auto-retry | Auto-recovery |
| Multi-step planning | Manual | Manual | Automatic |
| Natural language I/O | No | Commands only | Full conversation |
| Best for | Production | AI developers | End users |
"""
print(comparison_table)
def main():
"""Run all demos"""
print("\n" + "="*70)
print("SENTIENCE SDK: Multi-Layer Abstraction Demo")
print("="*70)
print("\nThis demo shows how to use the SDK at different abstraction levels:")
print(" 1. Layer 1: Direct SDK (maximum control)")
print(" 2. Layer 2: SentienceAgent (technical commands)")
print(" 3. Layer 3: ConversationalAgent (natural language)")
print("\nChoose which demo to run:")
print(" 1 - Layer 1: Direct SDK")
print(" 2 - Layer 2: SentienceAgent")
print(" 3 - Layer 3: ConversationalAgent (OpenAI)")
print(" 4 - Layer 3: ConversationalAgent (Local LLM)")
print(" 5 - Show comparison table")
print(" 0 - Exit")
choice = input("\nEnter your choice (0-5): ").strip()
if choice == "1":
demo_layer1_direct_sdk()
elif choice == "2":
if not os.getenv("OPENAI_API_KEY"):
print("\n❌ Error: OPENAI_API_KEY not set")
return
demo_layer2_sentience_agent()
elif choice == "3":
if not os.getenv("OPENAI_API_KEY"):
print("\n❌ Error: OPENAI_API_KEY not set")
return
demo_layer3_conversational_agent()
elif choice == "4":
demo_layer3_with_local_llm()
elif choice == "5":
demo_comparison()
elif choice == "0":
print("Goodbye!")
else:
print("Invalid choice")
if __name__ == "__main__":
main()