anysql-sdk 0.1.0__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.
anysql/__init__.py ADDED
@@ -0,0 +1,82 @@
1
+ """
2
+ anySQL — SQL analytics for AI systems.
3
+
4
+ Quick start:
5
+ import anysql
6
+
7
+ db = anysql.init("myproject.db")
8
+
9
+ # Wrap your LLM client (one line change)
10
+ client = anysql.openai(db).wrap(OpenAI())
11
+ client = anysql.claude(db).wrap(anthropic.Anthropic())
12
+
13
+ # Tag calls for cost attribution
14
+ with anysql.context_scope(feature="summarizer", segment="enterprise"):
15
+ response = client.chat.completions.create(...)
16
+
17
+ # Query with SQL
18
+ db.query("SELECT model, AVG(cost_usd) FROM llm_responses GROUP BY model")
19
+
20
+ # Or use built-in queries
21
+ db.model_comparison() # UC1: quality vs cost vs latency
22
+ db.prompt_regressions() # UC2: version-to-version score drops
23
+ db.cost_by_feature() # UC3: spend per feature flag
24
+ db.tool_failure_rates() # UC4: which tools break most often
25
+ db.rag_failure_modes() # UC5: retrieval vs generation failures
26
+ """
27
+
28
+ from .engine import AnySQL
29
+ from .adapters.openai import OpenAIAdapter
30
+ from .adapters.claude import ClaudeAdapter
31
+ from .tracers.agent import AgentTracer
32
+ from .tracers.rag import RAGTracer
33
+ from . import context as _ctx_module
34
+ from .context import context, context_scope, get_context
35
+
36
+ __version__ = "0.1.0"
37
+
38
+
39
+ def init(
40
+ db_path: str = ":memory:",
41
+ echo: bool = False,
42
+ enable_context_tracking: bool = True,
43
+ ) -> AnySQL:
44
+ """
45
+ Create anySQL engine.
46
+
47
+ Args:
48
+ db_path: ":memory:" (ephemeral) or path to SQLite file (persistent).
49
+ echo: Print SQL before executing (debug mode).
50
+ enable_context_tracking: Wire @context decorator to auto-write pipeline.runs.
51
+ """
52
+ db = AnySQL(db_path=db_path, echo=echo)
53
+ if enable_context_tracking:
54
+ _ctx_module._set_engine(db)
55
+ return db
56
+
57
+
58
+ def openai(db: AnySQL, task_type: str = None) -> OpenAIAdapter:
59
+ """Return OpenAI adapter for auto-logging."""
60
+ return OpenAIAdapter(db, task_type=task_type)
61
+
62
+
63
+ def claude(db: AnySQL, task_type: str = None) -> ClaudeAdapter:
64
+ """Return Claude adapter for auto-logging."""
65
+ return ClaudeAdapter(db, task_type=task_type)
66
+
67
+
68
+ def agent_tracer(db: AnySQL, session_id: str = None) -> AgentTracer:
69
+ """Return agent tracer for UC4 session debugging."""
70
+ return AgentTracer(db, session_id=session_id)
71
+
72
+
73
+ def rag_tracer(db: AnySQL) -> RAGTracer:
74
+ """Return RAG tracer for UC5 forensics."""
75
+ return RAGTracer(db)
76
+
77
+
78
+ __all__ = [
79
+ "init", "openai", "claude", "agent_tracer", "rag_tracer",
80
+ "context", "context_scope", "get_context",
81
+ "AnySQL", "OpenAIAdapter", "ClaudeAdapter", "AgentTracer", "RAGTracer",
82
+ ]
File without changes
@@ -0,0 +1,103 @@
1
+ """
2
+ anysql/adapters/claude.py
3
+ Anthropic Claude transparent proxy wrapper.
4
+
5
+ Usage:
6
+ import anthropic
7
+ import anysql
8
+
9
+ db = anysql.init("myproject.db")
10
+ client = anysql.claude(db).wrap(anthropic.Anthropic())
11
+
12
+ response = client.messages.create(
13
+ model="claude-sonnet-4-6",
14
+ max_tokens=1024,
15
+ messages=[{"role": "user", "content": "Summarize this..."}]
16
+ )
17
+ """
18
+
19
+ import uuid
20
+ import time
21
+ from datetime import datetime, timezone
22
+ from typing import Optional
23
+
24
+ ANTHROPIC_PRICING = {
25
+ "claude-opus-4-6": {"input": 15.00, "output": 75.00},
26
+ "claude-sonnet-4-6": {"input": 3.00, "output": 15.00},
27
+ "claude-haiku-4-5": {"input": 0.80, "output": 4.00},
28
+ "claude-3-5-sonnet": {"input": 3.00, "output": 15.00},
29
+ "claude-3-5-haiku": {"input": 0.80, "output": 4.00},
30
+ "claude-3-opus": {"input": 15.00, "output": 75.00},
31
+ }
32
+
33
+
34
+ def _calc_cost(model: str, input_tokens: int, output_tokens: int) -> Optional[float]:
35
+ for key, pricing in ANTHROPIC_PRICING.items():
36
+ if key in model or model.startswith(key):
37
+ return (input_tokens * pricing["input"] + output_tokens * pricing["output"]) / 1_000_000
38
+ return None
39
+
40
+
41
+ class _WrappedMessages:
42
+ def __init__(self, messages, db, task_type=None):
43
+ self._messages = messages
44
+ self._db = db
45
+ self._task_type = task_type
46
+
47
+ def create(self, **kwargs):
48
+ from ..context import get_context
49
+ start = time.monotonic()
50
+ response = self._messages.create(**kwargs)
51
+ latency_ms = int((time.monotonic() - start) * 1000)
52
+
53
+ ctx = get_context()
54
+ model = kwargs.get("model", "unknown")
55
+ messages = kwargs.get("messages", [])
56
+ prompt = messages[-1]["content"] if messages else ""
57
+ if isinstance(prompt, list):
58
+ prompt = " ".join(b.get("text","") if isinstance(b, dict) else str(b) for b in prompt)
59
+
60
+ usage = getattr(response, "usage", None)
61
+ it = getattr(usage, "input_tokens", 0) or 0
62
+ ot = getattr(usage, "output_tokens", 0) or 0
63
+
64
+ content = "".join(
65
+ block.text for block in getattr(response, "content", [])
66
+ if hasattr(block, "text")
67
+ )
68
+
69
+ self._db.insert("llm.responses", [{
70
+ "response_id": response.id or str(uuid.uuid4()),
71
+ "model": model,
72
+ "model_version": model,
73
+ "prompt": prompt,
74
+ "content": content,
75
+ "prompt_tokens": it,
76
+ "completion_tokens": ot,
77
+ "total_tokens": it + ot,
78
+ "cost_usd": _calc_cost(model, it, ot),
79
+ "latency_ms": latency_ms,
80
+ "stop_reason": getattr(response, "stop_reason", None),
81
+ "task_type": self._task_type or ctx.get("tags", {}).get("task_type"),
82
+ "session_id": ctx.get("session_id"),
83
+ "created_at": datetime.now(timezone.utc).isoformat(),
84
+ }])
85
+ return response
86
+
87
+
88
+ class ClaudeAdapter:
89
+ def __init__(self, db, task_type: Optional[str] = None):
90
+ self._db = db
91
+ self._task_type = task_type
92
+
93
+ def wrap(self, client):
94
+ return _WrappedAnthropicClient(client, self._db, self._task_type)
95
+
96
+
97
+ class _WrappedAnthropicClient:
98
+ def __init__(self, client, db, task_type=None):
99
+ self._client = client
100
+ self.messages = _WrappedMessages(client.messages, db, task_type)
101
+
102
+ def __getattr__(self, name):
103
+ return getattr(self._client, name)
@@ -0,0 +1,45 @@
1
+ """
2
+ anysql/adapters/generic.py
3
+ Generic JSON/dict adapter for any LLM provider.
4
+ """
5
+ import uuid
6
+ from datetime import datetime, timezone
7
+ from typing import Optional
8
+
9
+
10
+ class GenericAdapter:
11
+ def __init__(self, db):
12
+ self._db = db
13
+
14
+ def log(
15
+ self,
16
+ model: str,
17
+ prompt: str,
18
+ content: Optional[str] = None,
19
+ prompt_tokens: Optional[int] = None,
20
+ completion_tokens: Optional[int] = None,
21
+ cost_usd: Optional[float] = None,
22
+ latency_ms: Optional[int] = None,
23
+ task_type: Optional[str] = None,
24
+ session_id: Optional[str] = None,
25
+ **kwargs,
26
+ ) -> str:
27
+ from ..context import get_context
28
+ ctx = get_context()
29
+ response_id = str(uuid.uuid4())
30
+ total = (prompt_tokens or 0) + (completion_tokens or 0)
31
+ self._db.insert("llm.responses", [{
32
+ "response_id": response_id,
33
+ "model": model,
34
+ "prompt": prompt,
35
+ "content": content,
36
+ "prompt_tokens": prompt_tokens,
37
+ "completion_tokens": completion_tokens,
38
+ "total_tokens": total or None,
39
+ "cost_usd": cost_usd,
40
+ "latency_ms": latency_ms,
41
+ "task_type": task_type or ctx.get("tags", {}).get("task_type"),
42
+ "session_id": session_id or ctx.get("session_id"),
43
+ "created_at": datetime.now(timezone.utc).isoformat(),
44
+ }])
45
+ return response_id
@@ -0,0 +1,108 @@
1
+ """
2
+ anysql/adapters/openai.py
3
+ OpenAI transparent proxy wrapper.
4
+
5
+ Usage:
6
+ from openai import OpenAI
7
+ import anysql
8
+
9
+ db = anysql.init("myproject.db")
10
+ client = anysql.openai(db).wrap(OpenAI())
11
+
12
+ # Use exactly as before — all calls auto-logged to llm.responses
13
+ response = client.chat.completions.create(
14
+ model="gpt-4o-mini",
15
+ messages=[{"role": "user", "content": "Summarize this article..."}]
16
+ )
17
+ """
18
+
19
+ import uuid
20
+ import time
21
+ from datetime import datetime, timezone
22
+ from typing import Optional
23
+
24
+ # Pricing: USD per 1M tokens
25
+ OPENAI_PRICING = {
26
+ "gpt-4o": {"input": 2.50, "output": 10.00},
27
+ "gpt-4o-mini": {"input": 0.15, "output": 0.60},
28
+ "gpt-4-turbo": {"input": 10.00, "output": 30.00},
29
+ "gpt-4": {"input": 30.00, "output": 60.00},
30
+ "gpt-3.5-turbo":{"input": 0.50, "output": 1.50},
31
+ "o1": {"input": 15.00, "output": 60.00},
32
+ "o1-mini": {"input": 3.00, "output": 12.00},
33
+ "o3-mini": {"input": 1.10, "output": 4.40},
34
+ }
35
+
36
+
37
+ def _calc_cost(model: str, prompt_tokens: int, completion_tokens: int) -> Optional[float]:
38
+ # Match by prefix to handle dated version suffixes like "gpt-4o-2024-11-20"
39
+ # Sort by key length descending so more-specific keys (e.g. "gpt-4o-mini") match before shorter ones (e.g. "gpt-4o")
40
+ for key, pricing in sorted(OPENAI_PRICING.items(), key=lambda x: -len(x[0])):
41
+ if model.startswith(key):
42
+ return (prompt_tokens * pricing["input"] + completion_tokens * pricing["output"]) / 1_000_000
43
+ return None
44
+
45
+
46
+ class _WrappedCompletions:
47
+ def __init__(self, completions, db, task_type=None):
48
+ self._completions = completions
49
+ self._db = db
50
+ self._task_type = task_type
51
+
52
+ def create(self, **kwargs):
53
+ from ..context import get_context
54
+ start = time.monotonic()
55
+ response = self._completions.create(**kwargs)
56
+ latency_ms = int((time.monotonic() - start) * 1000)
57
+
58
+ ctx = get_context()
59
+ model = kwargs.get("model", "unknown")
60
+ messages = kwargs.get("messages", [])
61
+ prompt = messages[-1]["content"] if messages else ""
62
+
63
+ usage = getattr(response, "usage", None)
64
+ pt = getattr(usage, "prompt_tokens", 0) or 0
65
+ ct = getattr(usage, "completion_tokens", 0) or 0
66
+
67
+ choice = response.choices[0] if response.choices else None
68
+
69
+ self._db.insert("llm.responses", [{
70
+ "response_id": response.id or str(uuid.uuid4()),
71
+ "model": model,
72
+ "model_version": getattr(response, "model", model),
73
+ "prompt": prompt if isinstance(prompt, str) else str(prompt),
74
+ "content": choice.message.content if choice else None,
75
+ "prompt_tokens": pt,
76
+ "completion_tokens": ct,
77
+ "total_tokens": pt + ct,
78
+ "cost_usd": _calc_cost(model, pt, ct),
79
+ "latency_ms": latency_ms,
80
+ "stop_reason": choice.finish_reason if choice else None,
81
+ "task_type": self._task_type or ctx.get("tags", {}).get("task_type"),
82
+ "session_id": ctx.get("session_id"),
83
+ "created_at": datetime.now(timezone.utc).isoformat(),
84
+ }])
85
+ return response
86
+
87
+
88
+ class _WrappedChat:
89
+ def __init__(self, chat, db, task_type=None):
90
+ self.completions = _WrappedCompletions(chat.completions, db, task_type)
91
+
92
+
93
+ class OpenAIAdapter:
94
+ def __init__(self, db, task_type: Optional[str] = None):
95
+ self._db = db
96
+ self._task_type = task_type
97
+
98
+ def wrap(self, client):
99
+ return _WrappedOpenAIClient(client, self._db, self._task_type)
100
+
101
+
102
+ class _WrappedOpenAIClient:
103
+ def __init__(self, client, db, task_type=None):
104
+ self._client = client
105
+ self.chat = _WrappedChat(client.chat, db, task_type)
106
+
107
+ def __getattr__(self, name):
108
+ return getattr(self._client, name)
anysql/cli.py ADDED
@@ -0,0 +1,48 @@
1
+ """
2
+ anysql/cli.py
3
+ CLI entry point: anysql query "SELECT ..." / anysql stats
4
+ """
5
+ import sys
6
+ import argparse
7
+
8
+
9
+ def main():
10
+ parser = argparse.ArgumentParser(
11
+ prog="anysql",
12
+ description="anySQL — SQL analytics for AI systems",
13
+ )
14
+ subparsers = parser.add_subparsers(dest="command")
15
+
16
+ # anysql query "SELECT ..."
17
+ query_parser = subparsers.add_parser("query", help="Run SQL against anysql.db")
18
+ query_parser.add_argument("sql", help="SQL query string")
19
+ query_parser.add_argument("--db", default="anysql.db", help="Path to SQLite database")
20
+
21
+ # anysql stats
22
+ stats_parser = subparsers.add_parser("stats", help="Show row counts for all tables")
23
+ stats_parser.add_argument("--db", default="anysql.db", help="Path to SQLite database")
24
+
25
+ args = parser.parse_args()
26
+
27
+ if args.command is None:
28
+ parser.print_help()
29
+ sys.exit(0)
30
+
31
+ import anysql
32
+ db = anysql.init(args.db)
33
+
34
+ if args.command == "query":
35
+ result = db.query(args.sql)
36
+ print(result.to_string(index=False))
37
+
38
+ elif args.command == "stats":
39
+ from anysql.schema import TABLE_NAMES
40
+ print("\nanySQL table row counts:")
41
+ for table in TABLE_NAMES:
42
+ count = db.count(table)
43
+ print(f" {table:<25} {count:>6} rows")
44
+ print()
45
+
46
+
47
+ if __name__ == "__main__":
48
+ main()
anysql/context.py ADDED
@@ -0,0 +1,131 @@
1
+ """
2
+ anysql/context.py
3
+ Cost attribution via Python contextvars.
4
+ Works across sync, async, and threaded code without any setup.
5
+ """
6
+
7
+ import uuid
8
+ import time
9
+ from contextlib import contextmanager
10
+ from contextvars import ContextVar
11
+ from functools import wraps
12
+ from typing import Optional, Callable
13
+
14
+ _current_run_id = ContextVar("run_id", default=None)
15
+ _current_feature = ContextVar("feature", default=None)
16
+ _current_segment = ContextVar("segment", default=None)
17
+ _current_session_id = ContextVar("session_id", default=None)
18
+ _current_pipeline = ContextVar("pipeline", default=None)
19
+ _current_tags = ContextVar("tags", default={})
20
+
21
+ _engine = None # set by anysql.init()
22
+
23
+
24
+ def get_context() -> dict:
25
+ return {
26
+ "run_id": _current_run_id.get(),
27
+ "feature_flag": _current_feature.get(),
28
+ "user_segment": _current_segment.get(),
29
+ "session_id": _current_session_id.get(),
30
+ "pipeline_name": _current_pipeline.get(),
31
+ "tags": _current_tags.get(),
32
+ }
33
+
34
+
35
+ def _set_context(feature=None, segment=None, session_id=None, pipeline=None, tags=None):
36
+ tokens = {}
37
+ if feature is not None: tokens["feature"] = _current_feature.set(feature)
38
+ if segment is not None: tokens["segment"] = _current_segment.set(segment)
39
+ if session_id is not None: tokens["session_id"] = _current_session_id.set(session_id)
40
+ if pipeline is not None: tokens["pipeline"] = _current_pipeline.set(pipeline)
41
+ if tags is not None: tokens["tags"] = _current_tags.set({**_current_tags.get(), **tags})
42
+ tokens["run_id"] = _current_run_id.set(str(uuid.uuid4()))
43
+ return tokens
44
+
45
+
46
+ def _reset_context(tokens):
47
+ for key, token in tokens.items():
48
+ var = dict(feature=_current_feature, segment=_current_segment,
49
+ session_id=_current_session_id, pipeline=_current_pipeline,
50
+ tags=_current_tags, run_id=_current_run_id).get(key)
51
+ if var:
52
+ var.reset(token)
53
+
54
+
55
+ def context(feature=None, segment=None, session_id=None, pipeline=None, tags=None):
56
+ """
57
+ Decorator — tags all LLM calls within the function for cost attribution.
58
+
59
+ Usage:
60
+ @anysql.context(feature="premium_summarizer", segment="enterprise")
61
+ def summarize(text: str) -> str:
62
+ return openai_client.chat.completions.create(...)
63
+ """
64
+ def decorator(fn: Callable) -> Callable:
65
+ @wraps(fn)
66
+ def sync_wrapper(*args, **kwargs):
67
+ tokens = _set_context(feature, segment, session_id, pipeline, tags)
68
+ start = time.monotonic()
69
+ try:
70
+ return fn(*args, **kwargs)
71
+ finally:
72
+ _flush_run(int((time.monotonic() - start) * 1000))
73
+ _reset_context(tokens)
74
+
75
+ @wraps(fn)
76
+ async def async_wrapper(*args, **kwargs):
77
+ tokens = _set_context(feature, segment, session_id, pipeline, tags)
78
+ start = time.monotonic()
79
+ try:
80
+ return await fn(*args, **kwargs)
81
+ finally:
82
+ _flush_run(int((time.monotonic() - start) * 1000))
83
+ _reset_context(tokens)
84
+
85
+ import asyncio
86
+ return async_wrapper if asyncio.iscoroutinefunction(fn) else sync_wrapper
87
+
88
+ return decorator
89
+
90
+
91
+ @contextmanager
92
+ def context_scope(feature=None, segment=None, session_id=None, pipeline=None, tags=None):
93
+ """
94
+ Context manager version — for notebooks and inline code.
95
+
96
+ Usage:
97
+ with anysql.context_scope(feature="rag_search", segment="free"):
98
+ result = my_rag_pipeline(query)
99
+ """
100
+ tokens = _set_context(feature, segment, session_id, pipeline, tags)
101
+ start = time.monotonic()
102
+ try:
103
+ yield get_context()
104
+ finally:
105
+ _flush_run(int((time.monotonic() - start) * 1000))
106
+ _reset_context(tokens)
107
+
108
+
109
+ def _set_engine(engine):
110
+ global _engine
111
+ _engine = engine
112
+
113
+
114
+ def _flush_run(elapsed_ms: int):
115
+ if _engine is None:
116
+ return
117
+ ctx = get_context()
118
+ if not any([ctx["feature_flag"], ctx["pipeline_name"]]):
119
+ return
120
+ from datetime import datetime, timezone
121
+ _engine.insert("pipeline.runs", [{
122
+ "run_id": ctx["run_id"] or str(uuid.uuid4()),
123
+ "session_id": ctx["session_id"],
124
+ "feature_flag": ctx["feature_flag"],
125
+ "user_segment": ctx["user_segment"],
126
+ "pipeline_name": ctx["pipeline_name"],
127
+ "total_latency_ms": elapsed_ms,
128
+ "status": "success",
129
+ "tags": ctx["tags"],
130
+ "started_at": datetime.now(timezone.utc).isoformat(),
131
+ }])