Skip to content

Commit a13bd14

Browse files
committed
cloud trading example
1 parent 74850d4 commit a13bd14

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

examples/cloud_tracing_agent.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""
2+
Example: Agent with Cloud Tracing
3+
4+
Demonstrates how to use cloud tracing with SentienceAgent to upload traces
5+
and screenshots to cloud storage for remote viewing and analysis.
6+
7+
Requirements:
8+
- Pro or Enterprise tier API key (SENTIENCE_API_KEY)
9+
- OpenAI API key (OPENAI_API_KEY) for LLM
10+
11+
Usage:
12+
python examples/cloud_tracing_agent.py
13+
"""
14+
15+
import os
16+
17+
from sentience import SentienceAgent, SentienceBrowser
18+
from sentience.agent_config import AgentConfig
19+
from sentience.llm_provider import OpenAIProvider
20+
from sentience.tracer_factory import create_tracer
21+
22+
23+
def main():
24+
# Get API keys from environment
25+
sentience_key = os.environ.get("SENTIENCE_API_KEY")
26+
openai_key = os.environ.get("OPENAI_API_KEY")
27+
28+
if not sentience_key:
29+
print("❌ Error: SENTIENCE_API_KEY not set")
30+
print(" Cloud tracing requires Pro or Enterprise tier")
31+
print(" Get your API key at: https://sentience.studio")
32+
return
33+
34+
if not openai_key:
35+
print("❌ Error: OPENAI_API_KEY not set")
36+
return
37+
38+
print("🚀 Starting Agent with Cloud Tracing Demo\n")
39+
40+
# 1. Create tracer with automatic tier detection
41+
# If api_key is Pro/Enterprise, uses CloudTraceSink
42+
# If api_key is missing/invalid, falls back to local JsonlTraceSink
43+
run_id = "cloud-tracing-demo"
44+
tracer = create_tracer(api_key=sentience_key, run_id=run_id)
45+
46+
print(f"🆔 Run ID: {run_id}\n")
47+
48+
# 2. Configure agent with screenshot capture
49+
config = AgentConfig(
50+
snapshot_limit=50,
51+
capture_screenshots=True, # Enable screenshot capture
52+
screenshot_format="jpeg", # JPEG for smaller file size
53+
screenshot_quality=80, # 80% quality (good balance)
54+
)
55+
56+
# 3. Create browser and LLM
57+
browser = SentienceBrowser(api_key=sentience_key, headless=False)
58+
llm = OpenAIProvider(api_key=openai_key, model="gpt-4o-mini")
59+
60+
# 4. Create agent with tracer
61+
agent = SentienceAgent(browser, llm, tracer=tracer, config=config)
62+
63+
try:
64+
# 5. Navigate and execute agent actions
65+
print("🌐 Navigating to Google...\n")
66+
browser.start()
67+
browser.page.goto("https://www.google.com")
68+
browser.page.wait_for_load_state("networkidle")
69+
70+
# All actions are automatically traced!
71+
print("📝 Executing agent actions (all automatically traced)...\n")
72+
agent.act("Click the search box")
73+
agent.act("Type 'Sentience AI agent SDK' into the search field")
74+
agent.act("Press Enter key")
75+
76+
# Wait for results
77+
import time
78+
79+
time.sleep(2)
80+
81+
agent.act("Click the first non-ad search result")
82+
83+
print("\n✅ Agent execution complete!")
84+
85+
# 6. Get token usage stats
86+
stats = agent.get_token_stats()
87+
print("\n📊 Token Usage:")
88+
print(f" Total tokens: {stats['total_tokens']}")
89+
print(f" Prompt tokens: {stats['total_prompt_tokens']}")
90+
print(f" Completion tokens: {stats['total_completion_tokens']}")
91+
92+
except Exception as e:
93+
print(f"\n❌ Error during execution: {e}")
94+
raise
95+
96+
finally:
97+
# 7. Close tracer (uploads to cloud)
98+
print("\n📤 Uploading trace to cloud...")
99+
try:
100+
tracer.close(blocking=True) # Wait for upload to complete
101+
print("✅ Trace uploaded successfully!")
102+
print(f" View at: https://studio.sentienceapi.com (run_id: {run_id})")
103+
except Exception as e:
104+
print(f"⚠️ Upload failed: {e}")
105+
print(f" Trace preserved locally at: ~/.sentience/traces/pending/{run_id}.jsonl")
106+
107+
browser.close()
108+
109+
110+
if __name__ == "__main__":
111+
main()

0 commit comments

Comments
 (0)