aipython-shell 0.2.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.
- aipython/__init__.py +28 -0
- aipython/cli.py +17 -0
- aipython/config.py +82 -0
- aipython/context.py +198 -0
- aipython/detect.py +81 -0
- aipython/llm.py +238 -0
- aipython/log.py +72 -0
- aipython/shell.py +208 -0
- aipython_shell-0.2.0.dist-info/METADATA +256 -0
- aipython_shell-0.2.0.dist-info/RECORD +13 -0
- aipython_shell-0.2.0.dist-info/WHEEL +4 -0
- aipython_shell-0.2.0.dist-info/entry_points.txt +2 -0
- aipython_shell-0.2.0.dist-info/licenses/LICENSE +373 -0
aipython/__init__.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
"""Seamless AI for IPython: type Python or describe what you want."""
|
|
6
|
+
|
|
7
|
+
__version__ = "0.2.0"
|
|
8
|
+
|
|
9
|
+
_installed = None
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def load_ipython_extension(ipython):
|
|
13
|
+
global _installed
|
|
14
|
+
from .llm import preload
|
|
15
|
+
from .shell import AIShell
|
|
16
|
+
if _installed is None:
|
|
17
|
+
preload()
|
|
18
|
+
_installed = AIShell(ipython)
|
|
19
|
+
_installed.install()
|
|
20
|
+
for warning in _installed.translator.settings.warnings:
|
|
21
|
+
print(f"aipython: {warning}")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def unload_ipython_extension(ipython):
|
|
25
|
+
global _installed
|
|
26
|
+
if _installed is not None:
|
|
27
|
+
_installed.uninstall()
|
|
28
|
+
_installed = None
|
aipython/cli.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
"""`aipython` command: IPython with the extension preloaded."""
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
|
|
9
|
+
from IPython import start_ipython
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def main():
|
|
13
|
+
start_ipython(argv=["--ext", "aipython", *sys.argv[1:]])
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if __name__ == "__main__":
|
|
17
|
+
main()
|
aipython/config.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
"""Settings from ~/.config/aipython/config.toml, overridden by environment variables.
|
|
6
|
+
|
|
7
|
+
model = "anthropic/claude-haiku-5-5"
|
|
8
|
+
api_base = "http://localhost:1234/v1" # optional, for OpenAI-compatible servers
|
|
9
|
+
log = true # write each session as a .py file (default off)
|
|
10
|
+
debug = true # also log the full model exchange (default off)
|
|
11
|
+
log_dir = "~/somewhere" # default ~/.local/share/aipython/sessions
|
|
12
|
+
|
|
13
|
+
[keys] # per provider; else the provider's env var
|
|
14
|
+
anthropic = "sk-ant-..."
|
|
15
|
+
openai = "sk-..."
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import os
|
|
19
|
+
import stat
|
|
20
|
+
import sys
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
if sys.version_info >= (3, 11):
|
|
25
|
+
import tomllib
|
|
26
|
+
else:
|
|
27
|
+
import tomli as tomllib
|
|
28
|
+
|
|
29
|
+
DEFAULT_MODEL = "anthropic/claude-haiku-5-5"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def default_log_dir():
|
|
33
|
+
base = Path(os.getenv("XDG_DATA_HOME") or Path.home() / ".local" / "share")
|
|
34
|
+
return base / "aipython" / "sessions"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def config_path():
|
|
38
|
+
if os.getenv("AIPYTHON_CONFIG"):
|
|
39
|
+
return Path(os.environ["AIPYTHON_CONFIG"]).expanduser()
|
|
40
|
+
base = Path(os.getenv("XDG_CONFIG_HOME") or Path.home() / ".config")
|
|
41
|
+
return base / "aipython" / "config.toml"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass
|
|
45
|
+
class Settings:
|
|
46
|
+
model: str = DEFAULT_MODEL
|
|
47
|
+
api_base: str | None = None
|
|
48
|
+
keys: dict = field(default_factory=dict)
|
|
49
|
+
log: bool = False
|
|
50
|
+
debug: bool = False
|
|
51
|
+
log_dir: Path = field(default_factory=default_log_dir)
|
|
52
|
+
path: Path | None = None
|
|
53
|
+
warnings: list = field(default_factory=list)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def load(path=None):
|
|
57
|
+
path = path or config_path()
|
|
58
|
+
settings = Settings(path=path)
|
|
59
|
+
if path.exists():
|
|
60
|
+
try:
|
|
61
|
+
with open(path, "rb") as f:
|
|
62
|
+
data = tomllib.load(f)
|
|
63
|
+
except (OSError, tomllib.TOMLDecodeError) as e:
|
|
64
|
+
settings.warnings.append(f"could not read {path}: {e}")
|
|
65
|
+
data = {}
|
|
66
|
+
settings.model = data.get("model", settings.model)
|
|
67
|
+
settings.api_base = data.get("api_base")
|
|
68
|
+
settings.keys = dict(data.get("keys", {}))
|
|
69
|
+
settings.log = bool(data.get("log", False))
|
|
70
|
+
settings.debug = bool(data.get("debug", False))
|
|
71
|
+
if data.get("log_dir"):
|
|
72
|
+
settings.log_dir = Path(data["log_dir"]).expanduser()
|
|
73
|
+
if settings.keys and os.name == "posix" and path.stat().st_mode & (stat.S_IRWXG | stat.S_IRWXO):
|
|
74
|
+
settings.warnings.append(
|
|
75
|
+
f"{path} holds API keys but is readable by others; run: chmod 600 {path}")
|
|
76
|
+
settings.model = os.getenv("AIPYTHON_MODEL", settings.model)
|
|
77
|
+
settings.api_base = os.getenv("AIPYTHON_API_BASE", settings.api_base)
|
|
78
|
+
return settings
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def mask(key):
|
|
82
|
+
return key[:7] + "…" + key[-4:] if len(key) > 16 else "…"
|
aipython/context.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
"""Describe the user's namespace to the model, compactly by default and in depth on request."""
|
|
6
|
+
|
|
7
|
+
import inspect
|
|
8
|
+
import re
|
|
9
|
+
import types
|
|
10
|
+
|
|
11
|
+
MAX_SUMMARY_CHARS = 8000
|
|
12
|
+
MAX_COLUMNS = 40
|
|
13
|
+
SKIP_NAMES = {"In", "Out", "exit", "quit", "get_ipython"}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def user_variables(shell):
|
|
17
|
+
"""The user's own names: no IPython internals, no _-prefixed names."""
|
|
18
|
+
hidden = shell.user_ns_hidden
|
|
19
|
+
return {
|
|
20
|
+
k: v for k, v in shell.user_ns.items()
|
|
21
|
+
if not k.startswith("_") and k not in SKIP_NAMES
|
|
22
|
+
and not (k in hidden and hidden[k] is v)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _short_repr(value, limit=80):
|
|
27
|
+
try:
|
|
28
|
+
r = repr(value)
|
|
29
|
+
except Exception:
|
|
30
|
+
return "<unrepresentable>"
|
|
31
|
+
return r if len(r) <= limit else r[: limit - 3] + "..."
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _columns(df, limit=MAX_COLUMNS):
|
|
35
|
+
cols = [f"{c!r}: {t}" for c, t in list(df.dtypes.items())[:limit]]
|
|
36
|
+
more = f", ... +{len(df.columns) - limit} more" if len(df.columns) > limit else ""
|
|
37
|
+
return "{" + ", ".join(cols) + more + "}"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def describe_short(value):
|
|
41
|
+
"""One line describing a value."""
|
|
42
|
+
kind = type(value).__name__
|
|
43
|
+
module = type(value).__module__ or ""
|
|
44
|
+
if module.startswith("pandas"):
|
|
45
|
+
if kind == "DataFrame":
|
|
46
|
+
return f"DataFrame {value.shape} columns {_columns(value)}"
|
|
47
|
+
if kind == "Series":
|
|
48
|
+
return f"Series len={len(value)} dtype={value.dtype} name={value.name!r}"
|
|
49
|
+
if module.startswith("numpy") and hasattr(value, "shape"):
|
|
50
|
+
return f"ndarray shape={value.shape} dtype={value.dtype}"
|
|
51
|
+
if isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
|
|
52
|
+
try:
|
|
53
|
+
return f"function{inspect.signature(value)}"
|
|
54
|
+
except (TypeError, ValueError):
|
|
55
|
+
return "function"
|
|
56
|
+
if isinstance(value, type):
|
|
57
|
+
return f"class {value.__name__}"
|
|
58
|
+
if isinstance(value, (list, tuple, set, dict)) and len(value) > 5:
|
|
59
|
+
return f"{kind} len={len(value)} e.g. {_short_repr(value, 60)}"
|
|
60
|
+
return f"{kind} = {_short_repr(value)}"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def namespace_summary(shell):
|
|
64
|
+
"""Compact text summary of everything in scope, for the system context."""
|
|
65
|
+
variables = user_variables(shell)
|
|
66
|
+
modules = {k: v for k, v in variables.items() if isinstance(v, types.ModuleType)}
|
|
67
|
+
lines = []
|
|
68
|
+
if modules:
|
|
69
|
+
lines.append("imports: " + ", ".join(
|
|
70
|
+
k if k == v.__name__ else f"{k}={v.__name__}" for k, v in modules.items()))
|
|
71
|
+
for name, value in variables.items():
|
|
72
|
+
if name not in modules:
|
|
73
|
+
lines.append(f"{name}: {describe_short(value)}")
|
|
74
|
+
text = "\n".join(lines) or "(namespace is empty)"
|
|
75
|
+
if len(text) > MAX_SUMMARY_CHARS:
|
|
76
|
+
text = text[:MAX_SUMMARY_CHARS] + "\n... (truncated; use the tools to look further)"
|
|
77
|
+
return text
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# --- tools the model can call -------------------------------------------------
|
|
81
|
+
|
|
82
|
+
def _lookup(shell, name):
|
|
83
|
+
"""Resolve a dotted name like `df` or `obj.attr` in the user namespace."""
|
|
84
|
+
head, *rest = name.split(".")
|
|
85
|
+
if head not in shell.user_ns:
|
|
86
|
+
raise KeyError(f"no variable named {head!r}")
|
|
87
|
+
value = shell.user_ns[head]
|
|
88
|
+
for attr in rest:
|
|
89
|
+
value = getattr(value, attr)
|
|
90
|
+
return value
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def inspect_variable(shell, name):
|
|
94
|
+
value = _lookup(shell, name)
|
|
95
|
+
kind = type(value).__name__
|
|
96
|
+
module = type(value).__module__ or ""
|
|
97
|
+
if module.startswith("pandas") and kind == "DataFrame":
|
|
98
|
+
return "\n".join([
|
|
99
|
+
f"DataFrame shape={value.shape}",
|
|
100
|
+
f"index: {type(value.index).__name__} {_short_repr(list(value.index[:5]))}",
|
|
101
|
+
"dtypes / non-null counts:",
|
|
102
|
+
*(f" {c!r}: {t} ({value[c].notna().sum()} non-null)"
|
|
103
|
+
for c, t in value.dtypes.items()),
|
|
104
|
+
"head:",
|
|
105
|
+
value.head(5).to_string(max_cols=30, max_colwidth=40),
|
|
106
|
+
])
|
|
107
|
+
if module.startswith("pandas") and kind == "Series":
|
|
108
|
+
return "\n".join([
|
|
109
|
+
f"Series len={len(value)} dtype={value.dtype} name={value.name!r}",
|
|
110
|
+
value.head(10).to_string(),
|
|
111
|
+
])
|
|
112
|
+
if callable(value):
|
|
113
|
+
doc = (inspect.getdoc(value) or "").split("\n\n")[0]
|
|
114
|
+
try:
|
|
115
|
+
sig = str(inspect.signature(value))
|
|
116
|
+
except (TypeError, ValueError):
|
|
117
|
+
sig = "(...)"
|
|
118
|
+
return f"{kind} {name}{sig}\n{doc}"
|
|
119
|
+
out = [f"type: {module}.{kind}", f"repr: {_short_repr(value, 1500)}"]
|
|
120
|
+
if isinstance(value, dict):
|
|
121
|
+
out.append(f"keys: {_short_repr(list(value)[:50], 1500)}")
|
|
122
|
+
attrs = [a for a in dir(value) if not a.startswith("_")]
|
|
123
|
+
if attrs and not isinstance(value, (str, int, float, list, dict, tuple)):
|
|
124
|
+
out.append(f"attributes: {', '.join(attrs[:60])}")
|
|
125
|
+
return "\n".join(out)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def column_values(shell, variable, column):
|
|
129
|
+
df = _lookup(shell, variable)
|
|
130
|
+
s = df[column]
|
|
131
|
+
out = [f"{column!r}: dtype={s.dtype} nulls={s.isna().sum()} unique={s.nunique()}"]
|
|
132
|
+
if s.dtype.kind in "iufM":
|
|
133
|
+
out.append(f"min={s.min()} max={s.max()}")
|
|
134
|
+
out.append("most common:\n" + s.value_counts(dropna=False).head(20).to_string())
|
|
135
|
+
return "\n".join(out)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def search_namespace(shell, pattern):
|
|
139
|
+
rx = re.compile(pattern, re.IGNORECASE)
|
|
140
|
+
hits = []
|
|
141
|
+
for name, value in user_variables(shell).items():
|
|
142
|
+
if rx.search(name):
|
|
143
|
+
hits.append(f"variable {name}: {describe_short(value)}")
|
|
144
|
+
if type(value).__name__ == "DataFrame":
|
|
145
|
+
hits += [f"column {name}[{c!r}] ({t})" for c, t in value.dtypes.items()
|
|
146
|
+
if rx.search(str(c))]
|
|
147
|
+
elif isinstance(value, dict):
|
|
148
|
+
hits += [f"key {name}[{k!r}]" for k in value if rx.search(str(k))]
|
|
149
|
+
return "\n".join(hits[:100]) or "no matches"
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
TOOLS = {
|
|
153
|
+
"inspect_variable": (inspect_variable, {
|
|
154
|
+
"description": "Detailed view of a variable (dotted names allowed): for a DataFrame all "
|
|
155
|
+
"column dtypes, non-null counts and the first rows; for objects their "
|
|
156
|
+
"attributes; for callables their signature and docstring.",
|
|
157
|
+
"properties": {"name": {"type": "string"}},
|
|
158
|
+
}),
|
|
159
|
+
"column_values": (column_values, {
|
|
160
|
+
"description": "Value distribution of one DataFrame column: dtype, null count, number "
|
|
161
|
+
"of unique values, range, and the most common values. Use it to get "
|
|
162
|
+
"exact category spellings before filtering.",
|
|
163
|
+
"properties": {"variable": {"type": "string"}, "column": {"type": "string"}},
|
|
164
|
+
}),
|
|
165
|
+
"search_namespace": (search_namespace, {
|
|
166
|
+
"description": "Case-insensitive regex search over variable names, DataFrame column "
|
|
167
|
+
"names and dict keys in the user's session.",
|
|
168
|
+
"properties": {"pattern": {"type": "string"}},
|
|
169
|
+
}),
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def tool_schemas():
|
|
174
|
+
"""Tool definitions in the OpenAI function format that litellm translates per provider."""
|
|
175
|
+
return [
|
|
176
|
+
{
|
|
177
|
+
"type": "function",
|
|
178
|
+
"function": {
|
|
179
|
+
"name": name,
|
|
180
|
+
"description": spec["description"],
|
|
181
|
+
"parameters": {
|
|
182
|
+
"type": "object",
|
|
183
|
+
"properties": spec["properties"],
|
|
184
|
+
"required": list(spec["properties"]),
|
|
185
|
+
"additionalProperties": False,
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
for name, (_, spec) in TOOLS.items()
|
|
190
|
+
]
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def run_tool(shell, name, args):
|
|
194
|
+
"""Run a tool; returns (text, is_error)."""
|
|
195
|
+
try:
|
|
196
|
+
return TOOLS[name][0](shell, **args), False
|
|
197
|
+
except Exception as e:
|
|
198
|
+
return f"{type(e).__name__}: {e}", True
|
aipython/detect.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
"""Decide whether a cell is real Python or something the AI should interpret."""
|
|
6
|
+
|
|
7
|
+
import ast
|
|
8
|
+
import builtins
|
|
9
|
+
import re
|
|
10
|
+
|
|
11
|
+
import pyflakes.checker
|
|
12
|
+
import pyflakes.messages
|
|
13
|
+
|
|
14
|
+
_MAGIC_CALL = re.compile(r"^get_ipython\(\)\.run_line_magic\(")
|
|
15
|
+
_BARE_WORD = re.compile(r"^[A-Za-z][A-Za-z']*$")
|
|
16
|
+
HELP_MAGICS = {"pinfo", "pinfo2", "psearch"}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def undefined_names(code, known):
|
|
20
|
+
"""Names that `code` reads but that are defined neither in it nor in `known`."""
|
|
21
|
+
tree = ast.parse(code)
|
|
22
|
+
checker = pyflakes.checker.Checker(tree, builtins=set(known) | set(dir(builtins)))
|
|
23
|
+
return sorted({
|
|
24
|
+
m.message_args[0] for m in checker.messages
|
|
25
|
+
if isinstance(m, pyflakes.messages.UndefinedName)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _magic_call(code):
|
|
30
|
+
"""(name, arg) if `code` is a single get_ipython().run_line_magic(...) call."""
|
|
31
|
+
if not _MAGIC_CALL.match(code.strip()):
|
|
32
|
+
return None
|
|
33
|
+
try:
|
|
34
|
+
call = ast.parse(code.strip(), mode="eval").body
|
|
35
|
+
name, arg = (a.value for a in call.args[:2])
|
|
36
|
+
except (SyntaxError, ValueError, AttributeError):
|
|
37
|
+
return None
|
|
38
|
+
return name, arg
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def looks_like_prose_magic(raw, code):
|
|
42
|
+
"""True when IPython turned prose into a magic call.
|
|
43
|
+
|
|
44
|
+
Two cases: help syntax ("what is x?" becomes `x?`, i.e. %pinfo x) and automagic
|
|
45
|
+
("reset the index of df" becomes %reset). Real help requests (`x?`, `?x`, `x??`),
|
|
46
|
+
explicit `%magic` lines and argument-like lines ("ls -la", "cd data") are left alone.
|
|
47
|
+
"""
|
|
48
|
+
if raw.lstrip().startswith("%"):
|
|
49
|
+
return False
|
|
50
|
+
call = _magic_call(code)
|
|
51
|
+
if not call:
|
|
52
|
+
return False
|
|
53
|
+
if call[0] in HELP_MAGICS:
|
|
54
|
+
return raw.strip().strip("?").strip() != call[1].strip()
|
|
55
|
+
words = call[1].split()
|
|
56
|
+
return len(words) >= 2 and all(_BARE_WORD.match(w) for w in words)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def classify(raw, code, namespace):
|
|
60
|
+
"""Return (is_python, reason) for a cell.
|
|
61
|
+
|
|
62
|
+
`raw` is what the user typed, `code` is the cell after IPython's own transforms
|
|
63
|
+
(magics, shell escapes, help syntax) have been applied.
|
|
64
|
+
"""
|
|
65
|
+
if not code.strip():
|
|
66
|
+
return True, "empty"
|
|
67
|
+
try:
|
|
68
|
+
missing = undefined_names(code, namespace)
|
|
69
|
+
except SyntaxError as e:
|
|
70
|
+
return False, f"syntax error: {e.msg}"
|
|
71
|
+
if missing:
|
|
72
|
+
return False, "undefined: " + ", ".join(missing)
|
|
73
|
+
if looks_like_prose_magic(raw, code):
|
|
74
|
+
return False, "prose turned into a magic"
|
|
75
|
+
return True, "ok"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def ai_magic_text(code):
|
|
79
|
+
"""The text of a `%ai <text>` cell, or None if the cell is something else."""
|
|
80
|
+
call = _magic_call(code)
|
|
81
|
+
return call[1] if call and call[0] == "ai" else None
|
aipython/llm.py
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
|
|
5
|
+
"""Turn natural language / pseudo-Python into runnable Python with any LLM.
|
|
6
|
+
|
|
7
|
+
Models are reached through litellm, so any model string it understands works:
|
|
8
|
+
"anthropic/claude-haiku-5-5", "openai/gpt-5.4", "ollama_chat/qwen2.5-coder", ...
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import re
|
|
13
|
+
import sys
|
|
14
|
+
import time
|
|
15
|
+
from dataclasses import dataclass, field
|
|
16
|
+
|
|
17
|
+
from . import config, context
|
|
18
|
+
|
|
19
|
+
MODELS = {
|
|
20
|
+
"haiku": "anthropic/claude-haiku-5-5",
|
|
21
|
+
"sonnet": "anthropic/claude-sonnet-5-5",
|
|
22
|
+
"opus": "anthropic/claude-opus-5-5",
|
|
23
|
+
}
|
|
24
|
+
MAX_TOOL_ROUNDS = 4
|
|
25
|
+
|
|
26
|
+
SYSTEM = """\
|
|
27
|
+
You are embedded in an IPython session. The user typed a cell that is not valid Python as-is: \
|
|
28
|
+
it is natural language, pseudo-Python, or Python with mistakes. Reply with the Python code that \
|
|
29
|
+
does what they meant. Your reply is executed directly in their session.
|
|
30
|
+
|
|
31
|
+
- Output only code. No markdown fences, no prose before or after.
|
|
32
|
+
- Use the variables, imports and column names that exist in the session; never invent data.
|
|
33
|
+
- If it is ordinary Python with a typo or a slip, fix it minimally.
|
|
34
|
+
- End with an expression when the user wants to see a result (IPython displays it), as in a notebook.
|
|
35
|
+
- Import what you need. Prefer the libraries already imported.
|
|
36
|
+
- Keep it short: what a skilled user would have typed.
|
|
37
|
+
- If the user asks a question that needs no code, answer in Python comments (`# ...`).
|
|
38
|
+
- If something you need is not in the summary below (exact column values, a nested structure), \
|
|
39
|
+
look it up with the tools first rather than guessing. Most requests need no tools."""
|
|
40
|
+
|
|
41
|
+
_FENCE = re.compile(r"^```[a-zA-Z]*\n(.*?)\n?```\s*$", re.DOTALL)
|
|
42
|
+
DIM, RESET = "\033[2m", "\033[0m"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def strip_fences(text):
|
|
46
|
+
text = text.strip()
|
|
47
|
+
m = _FENCE.match(text)
|
|
48
|
+
return m.group(1).strip() if m else text
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def build_prompt(shell, request, history, last_error):
|
|
52
|
+
parts = ["<namespace>", context.namespace_summary(shell), "</namespace>"]
|
|
53
|
+
if history:
|
|
54
|
+
parts.append("<recent_cells>")
|
|
55
|
+
for entry in history:
|
|
56
|
+
parts.append(f"In: {entry['raw']}")
|
|
57
|
+
if entry.get("code") and entry["code"] != entry["raw"]:
|
|
58
|
+
parts.append(f"Ran: {entry['code']}")
|
|
59
|
+
if entry.get("error"):
|
|
60
|
+
parts.append(f"Error: {entry['error']}")
|
|
61
|
+
parts.append("</recent_cells>")
|
|
62
|
+
if last_error:
|
|
63
|
+
parts.append(f"<last_error>\n{last_error}\n</last_error>")
|
|
64
|
+
parts.append(f"<cell>\n{request}\n</cell>")
|
|
65
|
+
return "\n".join(parts)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class _Echo:
|
|
69
|
+
"""Writes streamed code to the terminal, dimmed and indented."""
|
|
70
|
+
|
|
71
|
+
def __init__(self, out):
|
|
72
|
+
self.out = out
|
|
73
|
+
self.started = False
|
|
74
|
+
|
|
75
|
+
def write(self, text):
|
|
76
|
+
if not self.started:
|
|
77
|
+
self.out.write(f"{DIM}✨ ")
|
|
78
|
+
self.started = True
|
|
79
|
+
self.out.write(text.replace("\n", "\n "))
|
|
80
|
+
self.out.flush()
|
|
81
|
+
|
|
82
|
+
def note(self, text):
|
|
83
|
+
self.close()
|
|
84
|
+
self.started = False
|
|
85
|
+
self.out.write(f"{DIM} ⋯ {text}{RESET}\n")
|
|
86
|
+
self.out.flush()
|
|
87
|
+
|
|
88
|
+
def close(self):
|
|
89
|
+
if self.started:
|
|
90
|
+
self.out.write(f"{RESET}\n")
|
|
91
|
+
self.out.flush()
|
|
92
|
+
|
|
93
|
+
def usage(self, translation):
|
|
94
|
+
self.close()
|
|
95
|
+
self.started = False
|
|
96
|
+
self.out.write(f"{DIM} ↳ {translation.usage_text()}{RESET}\n")
|
|
97
|
+
self.out.flush()
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def format_cost(cost):
|
|
101
|
+
if cost >= 1:
|
|
102
|
+
return f"${cost:.2f}"
|
|
103
|
+
return f"${cost:.4f}" if cost >= 0.0001 else "<$0.0001"
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass
|
|
107
|
+
class Translation:
|
|
108
|
+
"""Generated code plus what it cost. `messages` is the full exchange, for debug logs."""
|
|
109
|
+
code: str
|
|
110
|
+
model: str
|
|
111
|
+
input_tokens: int = 0
|
|
112
|
+
output_tokens: int = 0
|
|
113
|
+
cost: float | None = None
|
|
114
|
+
seconds: float = 0.0
|
|
115
|
+
messages: list = field(default_factory=list)
|
|
116
|
+
|
|
117
|
+
def usage_text(self):
|
|
118
|
+
parts = [f"{self.input_tokens:,} in · {self.output_tokens:,} out"]
|
|
119
|
+
if self.cost:
|
|
120
|
+
parts.append(format_cost(self.cost))
|
|
121
|
+
parts.append(f"{self.seconds:.1f}s")
|
|
122
|
+
return " · ".join(parts)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _cost(response, model):
|
|
126
|
+
try:
|
|
127
|
+
return _litellm().completion_cost(completion_response=response, model=model)
|
|
128
|
+
except Exception:
|
|
129
|
+
return None
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _litellm():
|
|
133
|
+
"""litellm takes over a second to import, so it is loaded on first use (or in the
|
|
134
|
+
background at startup, see preload)."""
|
|
135
|
+
import litellm
|
|
136
|
+
litellm.suppress_debug_info = True
|
|
137
|
+
return litellm
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def preload():
|
|
141
|
+
import threading
|
|
142
|
+
threading.Thread(target=_litellm, daemon=True).start()
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def supports_tools(model):
|
|
146
|
+
try:
|
|
147
|
+
return "tools" in (_litellm().get_supported_openai_params(model) or [])
|
|
148
|
+
except Exception:
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def provider(model):
|
|
153
|
+
try:
|
|
154
|
+
return _litellm().get_llm_provider(model)[1]
|
|
155
|
+
except Exception:
|
|
156
|
+
return None
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class Translator:
|
|
160
|
+
def __init__(self, settings=None, completion=None):
|
|
161
|
+
self.settings = settings or config.Settings()
|
|
162
|
+
self.model = self.settings.model
|
|
163
|
+
self._completion = completion
|
|
164
|
+
|
|
165
|
+
def completion(self, **kwargs):
|
|
166
|
+
if self.settings.api_base:
|
|
167
|
+
kwargs["api_base"] = self.settings.api_base
|
|
168
|
+
key = self.settings.keys.get(provider(self.model)) if self.settings.keys else None
|
|
169
|
+
if key:
|
|
170
|
+
kwargs["api_key"] = key
|
|
171
|
+
return (self._completion or _litellm().completion)(**kwargs)
|
|
172
|
+
|
|
173
|
+
def translate(self, shell, request, history=(), last_error=None, out=sys.stdout):
|
|
174
|
+
"""Return a Translation for `request`, streaming the code to `out` as it arrives."""
|
|
175
|
+
result = Translation(code="", model=self.model)
|
|
176
|
+
started = time.monotonic()
|
|
177
|
+
messages = [
|
|
178
|
+
{"role": "system", "content": SYSTEM},
|
|
179
|
+
{"role": "user", "content": build_prompt(shell, request, history, last_error)},
|
|
180
|
+
]
|
|
181
|
+
tools = context.tool_schemas() if supports_tools(self.model) else None
|
|
182
|
+
echo = _Echo(out)
|
|
183
|
+
try:
|
|
184
|
+
for _ in range(MAX_TOOL_ROUNDS + 1):
|
|
185
|
+
chunks = []
|
|
186
|
+
for chunk in self.completion(
|
|
187
|
+
model=self.model,
|
|
188
|
+
messages=messages,
|
|
189
|
+
tools=tools,
|
|
190
|
+
max_tokens=4096,
|
|
191
|
+
reasoning_effort="low",
|
|
192
|
+
drop_params=True,
|
|
193
|
+
stream=True,
|
|
194
|
+
stream_options={"include_usage": True},
|
|
195
|
+
):
|
|
196
|
+
chunks.append(chunk)
|
|
197
|
+
text = chunk.choices[0].delta.content if chunk.choices else None
|
|
198
|
+
if text:
|
|
199
|
+
echo.write(text)
|
|
200
|
+
response = _litellm().stream_chunk_builder(chunks, messages=messages)
|
|
201
|
+
choice = response.choices[0]
|
|
202
|
+
message = choice.message
|
|
203
|
+
if response.usage:
|
|
204
|
+
result.input_tokens += response.usage.prompt_tokens or 0
|
|
205
|
+
result.output_tokens += response.usage.completion_tokens or 0
|
|
206
|
+
cost = _cost(response, self.model)
|
|
207
|
+
if cost is not None:
|
|
208
|
+
result.cost = (result.cost or 0) + cost
|
|
209
|
+
|
|
210
|
+
if choice.finish_reason == "content_filter":
|
|
211
|
+
raise RuntimeError("the model declined this request")
|
|
212
|
+
if not message.tool_calls:
|
|
213
|
+
messages.append({"role": "assistant", "content": message.content})
|
|
214
|
+
result.code = strip_fences(message.content or "")
|
|
215
|
+
result.seconds = time.monotonic() - started
|
|
216
|
+
result.messages = messages
|
|
217
|
+
echo.usage(result)
|
|
218
|
+
return result
|
|
219
|
+
|
|
220
|
+
messages.append({
|
|
221
|
+
"role": "assistant",
|
|
222
|
+
"content": message.content,
|
|
223
|
+
"tool_calls": [call.model_dump() for call in message.tool_calls],
|
|
224
|
+
})
|
|
225
|
+
for call in message.tool_calls:
|
|
226
|
+
name = call.function.name
|
|
227
|
+
try:
|
|
228
|
+
args = json.loads(call.function.arguments or "{}")
|
|
229
|
+
except json.JSONDecodeError as e:
|
|
230
|
+
text, is_error = f"invalid JSON arguments: {e}", True
|
|
231
|
+
else:
|
|
232
|
+
echo.note(f"{name}({', '.join(map(str, args.values()))})")
|
|
233
|
+
text, is_error = context.run_tool(shell, name, args)
|
|
234
|
+
messages.append({"role": "tool", "tool_call_id": call.id,
|
|
235
|
+
"content": f"Error: {text}" if is_error else text})
|
|
236
|
+
raise RuntimeError("gave up after too many lookups")
|
|
237
|
+
finally:
|
|
238
|
+
echo.close()
|