-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
from IPython.core.magic import register_line_magic
import requests
@register_line_magic
def nl(line):
"""
Natural language input for Sage using Ollama
Do not use with untrusted input!
Example usage:
%nl "Invert the 4x4 matrix with 0s on the diagonal and 1s off diagonal"
"""
r = requests.post("http://localhost:11434/api/generate", json={
"model": "gemma3:27b",
"prompt": f"Translate this request directly into SageMath code. Be very concise and don't output Markdown code blocks or anything extraneous, only output the raw SageMath code. Make sure it's valid SageMath syntax! Request: {line}",
"stream": false
})
code = r.json()["response"]
print(code)
if input("Run? (y/N) ") == "y":
shell = get_ipython()
shell.run_cell(code)