synth-ai 0.2.4.dev3__py3-none-any.whl → 0.2.4.dev4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,188 @@
1
+ """Basic usage example for tracing v3."""
2
+
3
+ import asyncio
4
+ import time
5
+ from datetime import datetime
6
+
7
+ from synth_ai.tracing_v3 import SessionTracer
8
+ from synth_ai.tracing_v3.abstractions import LMCAISEvent, EnvironmentEvent, RuntimeEvent, TimeRecord
9
+ from synth_ai.tracing_v3.turso.daemon import SqldDaemon
10
+
11
+
12
+ async def simulate_llm_call(model: str, prompt: str) -> dict:
13
+ """Simulate an LLM API call."""
14
+ await asyncio.sleep(0.1) # Simulate network latency
15
+
16
+ # Simulate response
17
+ tokens = len(prompt.split()) * 3
18
+ return {
19
+ "model": model,
20
+ "response": f"Response to: {prompt[:50]}...",
21
+ "usage": {
22
+ "prompt_tokens": len(prompt.split()) * 2,
23
+ "completion_tokens": tokens,
24
+ "total_tokens": len(prompt.split()) * 2 + tokens,
25
+ },
26
+ }
27
+
28
+
29
+ async def main():
30
+ """Demonstrate basic tracing v3 usage."""
31
+ print("Starting tracing v3 example...")
32
+
33
+ # Option 1: Start sqld daemon programmatically
34
+ with SqldDaemon() as daemon:
35
+ print("✓ Started sqld daemon")
36
+
37
+ # Wait for daemon to be ready
38
+ await asyncio.sleep(1)
39
+
40
+ # Create tracer
41
+ tracer = SessionTracer()
42
+ await tracer.initialize()
43
+ print("✓ Initialized tracer")
44
+
45
+ # Example 1: Basic session with events
46
+ print("\n--- Example 1: Basic Session ---")
47
+ async with tracer.session(metadata={"example": "basic"}) as session_id:
48
+ print(f"Started session: {session_id}")
49
+
50
+ # Timestep 1: LLM interaction
51
+ async with tracer.timestep("llm_step", turn_number=1):
52
+ # Simulate LLM call
53
+ result = await simulate_llm_call("gpt-4", "What is the capital of France?")
54
+
55
+ # Record LLM event
56
+ event = LMCAISEvent(
57
+ system_instance_id="llm_system",
58
+ time_record=TimeRecord(event_time=time.time()),
59
+ model_name=result["model"],
60
+ input_tokens=result["usage"]["prompt_tokens"],
61
+ output_tokens=result["usage"]["completion_tokens"],
62
+ total_tokens=result["usage"]["total_tokens"],
63
+ cost_usd=0.003, # $0.003
64
+ latency_ms=100,
65
+ metadata={"prompt": "What is the capital of France?"},
66
+ )
67
+ await tracer.record_event(event)
68
+
69
+ # Record messages
70
+ await tracer.record_message(
71
+ content="What is the capital of France?", message_type="user"
72
+ )
73
+ await tracer.record_message(content=result["response"], message_type="assistant")
74
+ print("✓ Recorded LLM interaction")
75
+
76
+ # Timestep 2: Environment interaction
77
+ async with tracer.timestep("env_step", turn_number=2):
78
+ # Record environment event
79
+ env_event = EnvironmentEvent(
80
+ system_instance_id="environment",
81
+ time_record=TimeRecord(event_time=time.time()),
82
+ reward=0.8,
83
+ terminated=False,
84
+ system_state_before={"position": [0, 0]},
85
+ system_state_after={"position": [1, 0]},
86
+ )
87
+ await tracer.record_event(env_event)
88
+ print("✓ Recorded environment event")
89
+
90
+ # Timestep 3: Runtime action
91
+ async with tracer.timestep("runtime_step", turn_number=3):
92
+ # Record runtime event
93
+ runtime_event = RuntimeEvent(
94
+ system_instance_id="agent",
95
+ time_record=TimeRecord(event_time=time.time()),
96
+ actions=[1, 0, 0, 1], # Example action vector
97
+ metadata={"action_type": "move_right"},
98
+ )
99
+ await tracer.record_event(runtime_event)
100
+ print("✓ Recorded runtime event")
101
+
102
+ print(f"✓ Session {session_id} saved\n")
103
+
104
+ # Example 2: Concurrent sessions
105
+ print("--- Example 2: Concurrent Sessions ---")
106
+
107
+ async def run_concurrent_session(session_num: int):
108
+ """Run a session concurrently."""
109
+ async with tracer.session(
110
+ metadata={"example": "concurrent", "session_num": session_num}
111
+ ) as sid:
112
+ for i in range(3):
113
+ async with tracer.timestep(f"step_{i}", turn_number=i):
114
+ # Simulate some work
115
+ await asyncio.sleep(0.05)
116
+
117
+ # Record event
118
+ event = RuntimeEvent(
119
+ system_instance_id=f"worker_{session_num}",
120
+ time_record=TimeRecord(event_time=time.time()),
121
+ actions=[i],
122
+ metadata={"iteration": i},
123
+ )
124
+ await tracer.record_event(event)
125
+
126
+ return sid
127
+
128
+ # Run 5 concurrent sessions
129
+ tasks = [run_concurrent_session(i) for i in range(5)]
130
+ session_ids = await asyncio.gather(*tasks)
131
+ print(f"✓ Completed {len(session_ids)} concurrent sessions")
132
+
133
+ # Example 3: Query stored data
134
+ print("\n--- Example 3: Querying Data ---")
135
+
136
+ # Get model usage statistics
137
+ model_usage = await tracer.db.get_model_usage()
138
+ print("\nModel Usage:")
139
+ print(model_usage)
140
+
141
+ # Query recent sessions
142
+ recent_sessions = await tracer.get_session_history(limit=5)
143
+ print(f"\nRecent Sessions: {len(recent_sessions)} found")
144
+ for session in recent_sessions:
145
+ print(
146
+ f" - {session['session_id']}: "
147
+ f"{session['num_events']} events, "
148
+ f"{session['num_messages']} messages"
149
+ )
150
+
151
+ # Get specific session details
152
+ if recent_sessions:
153
+ session_detail = await tracer.db.get_session_trace(recent_sessions[0]["session_id"])
154
+ print(f"\nSession Detail for {session_detail['session_id']}:")
155
+ print(f" Created: {session_detail['created_at']}")
156
+ print(f" Timesteps: {len(session_detail['timesteps'])}")
157
+
158
+ # Example 4: Using hooks
159
+ print("\n--- Example 4: Hooks ---")
160
+
161
+ # Add a custom hook
162
+ call_count = {"count": 0}
163
+
164
+ async def count_events(event, **kwargs):
165
+ call_count["count"] += 1
166
+ print(f" Hook: Event #{call_count['count']} recorded")
167
+
168
+ tracer.hooks.register("event_recorded", count_events, name="event_counter")
169
+
170
+ async with tracer.session(metadata={"example": "hooks"}) as session_id:
171
+ async with tracer.timestep("hook_test"):
172
+ for i in range(3):
173
+ event = RuntimeEvent(
174
+ system_instance_id="hook_test",
175
+ time_record=TimeRecord(event_time=time.time()),
176
+ actions=[i],
177
+ )
178
+ await tracer.record_event(event)
179
+
180
+ print(f"✓ Hook called {call_count['count']} times")
181
+
182
+ # Cleanup
183
+ await tracer.close()
184
+ print("\n✓ Example completed successfully!")
185
+
186
+
187
+ if __name__ == "__main__":
188
+ asyncio.run(main())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: synth-ai
3
- Version: 0.2.4.dev3
3
+ Version: 0.2.4.dev4
4
4
  Summary: Software for aiding the best and multiplying the will - Core AI functionality and tracing
5
5
  Author-email: Synth AI <josh@usesynth.ai>
6
6
  License-Expression: MIT
@@ -126,6 +126,7 @@ synth_ai/tracing_v3/migration_helper.py,sha256=q6G4v3UKJAU4nMytz7znIK1M3xeLGObxl
126
126
  synth_ai/tracing_v3/replica_sync.py,sha256=CP7hPCR2oETrdsInwQl79mdInFA5xL1Q4Nzp3KGLu5A,8879
127
127
  synth_ai/tracing_v3/session_tracer.py,sha256=zPCtO73oXVsHN5N64_pleporKiMuAmue9dStAZSFESA,11379
128
128
  synth_ai/tracing_v3/utils.py,sha256=RAFZTf8JyRCQsR9DwJ1ykelKkoY3OSrCBUStCLvpn8w,3431
129
+ synth_ai/tracing_v3/examples/basic_usage.py,sha256=zbl9bJaZawToMEV0skzvP4sIwLnmBeo7Pa_f1gJlkf8,7265
129
130
  synth_ai/tracing_v3/storage/__init__.py,sha256=S_g7h34JZ6v1hoCcrlP7TaAAU-f0owKUx78QB8TUGkQ,332
130
131
  synth_ai/tracing_v3/storage/base.py,sha256=FlV3NMXsXK7MZaUqc7uXCv2YFD021C6LvFaPppyYr0A,3611
131
132
  synth_ai/tracing_v3/storage/config.py,sha256=ZvBum3wkDtXjm_p5mcEMjBtPNSUHjDh-fhZqVxXC4JQ,2096
@@ -180,9 +181,9 @@ synth_ai/v0/tracing_v1/events/manage.py,sha256=dgIlH43aEz7yA1_rj4IisJP6ROiROmLwD
180
181
  synth_ai/v0/tracing_v1/events/scope.py,sha256=BuBkhSpVHUJt8iGT9HJZF82rbb88mQcd2vM2shg-w2I,2550
181
182
  synth_ai/v0/tracing_v1/events/store.py,sha256=gVM3L_bV_PAEh9GD9eg2AQqNfANu-zH9ied7bAKk_Eg,9040
182
183
  synth_ai/zyk/__init__.py,sha256=6HFa13sdHCouhzKOUPEWcJeVxKlzjWO7ERsrrwtTugs,752
183
- synth_ai-0.2.4.dev3.dist-info/licenses/LICENSE,sha256=ynhjRQUfqA_RdGRATApfFA_fBAy9cno04sLtLUqxVFM,1069
184
- synth_ai-0.2.4.dev3.dist-info/METADATA,sha256=ohKQPWyVxScWAB8xBzTvSW8hOrkEt_ttXEAa-LBIQ5I,7150
185
- synth_ai-0.2.4.dev3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
186
- synth_ai-0.2.4.dev3.dist-info/entry_points.txt,sha256=GSFXaJreq4PJXbixkUI0GHZwGh2dZDG5pYaoVmqr_KE,46
187
- synth_ai-0.2.4.dev3.dist-info/top_level.txt,sha256=fBmtZyVHuKaGa29oHBaaUkrUIWTqSpoVMPiVdCDP3k8,9
188
- synth_ai-0.2.4.dev3.dist-info/RECORD,,
184
+ synth_ai-0.2.4.dev4.dist-info/licenses/LICENSE,sha256=ynhjRQUfqA_RdGRATApfFA_fBAy9cno04sLtLUqxVFM,1069
185
+ synth_ai-0.2.4.dev4.dist-info/METADATA,sha256=rQqUijPG-9CeDt33ihFOSoDyKrQpF_3obxoNG-tCCJg,7150
186
+ synth_ai-0.2.4.dev4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
187
+ synth_ai-0.2.4.dev4.dist-info/entry_points.txt,sha256=GSFXaJreq4PJXbixkUI0GHZwGh2dZDG5pYaoVmqr_KE,46
188
+ synth_ai-0.2.4.dev4.dist-info/top_level.txt,sha256=fBmtZyVHuKaGa29oHBaaUkrUIWTqSpoVMPiVdCDP3k8,9
189
+ synth_ai-0.2.4.dev4.dist-info/RECORD,,