fava-nl2bql 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.
- fava_nl2bql/FavaNl2Bql.js +11 -0
- fava_nl2bql/__init__.py +10 -0
- fava_nl2bql/extension.py +41 -0
- fava_nl2bql/templates/FavaNl2Bql.html +26 -0
- fava_nl2bql/translator.py +75 -0
- fava_nl2bql-0.1.0.dist-info/METADATA +37 -0
- fava_nl2bql-0.1.0.dist-info/RECORD +9 -0
- fava_nl2bql-0.1.0.dist-info/WHEEL +4 -0
- fava_nl2bql-0.1.0.dist-info/licenses/LICENSE.txt +21 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/** @type import("../../../../frontend/src/extension-api").ExtensionModule */
|
|
4
|
+
export default {
|
|
5
|
+
init() {
|
|
6
|
+
console.log("Initialising extension FavaNl2Bql");
|
|
7
|
+
},
|
|
8
|
+
onExtensionPageLoad() {
|
|
9
|
+
document.getElementById("nl2bql-question")?.focus();
|
|
10
|
+
},
|
|
11
|
+
};
|
fava_nl2bql/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
2
|
+
|
|
3
|
+
try:
|
|
4
|
+
# Change here if project is renamed and does not equal the package name
|
|
5
|
+
dist_name = __name__
|
|
6
|
+
__version__ = version(dist_name)
|
|
7
|
+
except PackageNotFoundError: # pragma: no cover
|
|
8
|
+
__version__ = "unknown"
|
|
9
|
+
finally:
|
|
10
|
+
del version, PackageNotFoundError
|
fava_nl2bql/extension.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""The FavaNl2Bql extension: ask a question, see the BQL, see the result."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from fava.context import g
|
|
6
|
+
from fava.core.query import QueryResultTable, QueryResultText
|
|
7
|
+
from fava.ext import FavaExtensionBase
|
|
8
|
+
from fava.helpers import FavaAPIError
|
|
9
|
+
|
|
10
|
+
from fava_nl2bql.translator import (
|
|
11
|
+
DEFAULT_HOST,
|
|
12
|
+
DEFAULT_MODEL,
|
|
13
|
+
Translation,
|
|
14
|
+
translate_to_bql,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class FavaNl2Bql(FavaExtensionBase):
|
|
19
|
+
"""Translate a natural-language question into BQL and run it."""
|
|
20
|
+
|
|
21
|
+
report_title = "Ask"
|
|
22
|
+
|
|
23
|
+
has_js_module = True
|
|
24
|
+
|
|
25
|
+
def translate(self, question: str) -> Translation:
|
|
26
|
+
"""Translate a natural-language question into a BQL query."""
|
|
27
|
+
config = self.config if isinstance(self.config, dict) else {}
|
|
28
|
+
return translate_to_bql(
|
|
29
|
+
question,
|
|
30
|
+
host=config.get("ollama_host", DEFAULT_HOST),
|
|
31
|
+
model=config.get("model", DEFAULT_MODEL),
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def run_query(self, bql: str) -> QueryResultTable | QueryResultText:
|
|
35
|
+
"""Run a BQL query against the currently filtered ledger."""
|
|
36
|
+
try:
|
|
37
|
+
return self.ledger.query_shell.execute_query_serialised(
|
|
38
|
+
g.filtered.entries_with_all_prices, bql
|
|
39
|
+
)
|
|
40
|
+
except FavaAPIError as error:
|
|
41
|
+
return QueryResultText(contents=str(error))
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{% set question = request.args.get('question', '') %}
|
|
2
|
+
|
|
3
|
+
<form method="get">
|
|
4
|
+
<label for="nl2bql-question">Ask a question about your ledger</label>
|
|
5
|
+
<input id="nl2bql-question" name="question" value="{{ question }}"
|
|
6
|
+
placeholder="e.g. how much did I spend on groceries last month?" size="60">
|
|
7
|
+
<button type="submit">Ask</button>
|
|
8
|
+
</form>
|
|
9
|
+
|
|
10
|
+
{% if question %}
|
|
11
|
+
{% set translation = extension.translate(question) %}
|
|
12
|
+
{% if translation.error %}
|
|
13
|
+
<p class="error">{{ translation.error }}</p>
|
|
14
|
+
{% elif translation.bql %}
|
|
15
|
+
<h4>Generated BQL</h4>
|
|
16
|
+
<pre>{{ translation.bql }}</pre>
|
|
17
|
+
|
|
18
|
+
<h4>Result</h4>
|
|
19
|
+
{% set result = extension.run_query(translation.bql) %}
|
|
20
|
+
{% if result.t == 'table' %}
|
|
21
|
+
<svelte-component type="query-table"><script type="application/json">{{ result|tojson }}</script></svelte-component>
|
|
22
|
+
{% else %}
|
|
23
|
+
<pre>{{ result.contents }}</pre>
|
|
24
|
+
{% endif %}
|
|
25
|
+
{% endif %}
|
|
26
|
+
{% endif %}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Natural language to BQL translation via a local Ollama model."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from ollama import Client, RequestError, ResponseError
|
|
8
|
+
|
|
9
|
+
DEFAULT_HOST = "http://localhost:11434"
|
|
10
|
+
DEFAULT_MODEL = "tarioch/qwen2.5-coder-bql"
|
|
11
|
+
_TIMEOUT = 30.0 # seconds; a stalled server must not block the request forever
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class Translation:
|
|
16
|
+
"""Result of translating a question into BQL, or why it failed."""
|
|
17
|
+
|
|
18
|
+
bql: str | None
|
|
19
|
+
error: str | None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def translate_to_bql(
|
|
23
|
+
question: str,
|
|
24
|
+
*,
|
|
25
|
+
host: str = DEFAULT_HOST,
|
|
26
|
+
model: str = DEFAULT_MODEL,
|
|
27
|
+
) -> Translation:
|
|
28
|
+
"""Translate a natural-language question into a BQL query via Ollama.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
question: The user's question, as typed into the extension's report page.
|
|
32
|
+
host: Base URL of the Ollama server.
|
|
33
|
+
model: Name of the Ollama model to use.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
The translation, or the reason it failed. Never executed without being
|
|
37
|
+
shown to the user first.
|
|
38
|
+
"""
|
|
39
|
+
question = question.strip()
|
|
40
|
+
if not question:
|
|
41
|
+
return Translation(bql=None, error=None)
|
|
42
|
+
|
|
43
|
+
try:
|
|
44
|
+
with Client(host=host, timeout=_TIMEOUT) as client:
|
|
45
|
+
response = client.generate(model=model, prompt=question, stream=False)
|
|
46
|
+
except (ConnectionError, RequestError, ResponseError) as error:
|
|
47
|
+
return Translation(bql=None, error=f"Could not translate with Ollama: {error}")
|
|
48
|
+
|
|
49
|
+
bql = _extract_query(response.response or "").strip()
|
|
50
|
+
if not bql:
|
|
51
|
+
return Translation(bql=None, error="Ollama returned an empty response.")
|
|
52
|
+
return Translation(bql=bql, error=None)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _extract_query(text: str) -> str:
|
|
56
|
+
"""Pull the query out of the model's raw response.
|
|
57
|
+
|
|
58
|
+
The model wraps the query in a ```` ``` ```` fence when asked for one, but more often
|
|
59
|
+
emits the query bare and follows it with a lone closing fence and a one-line
|
|
60
|
+
explanation - e.g. ``"SELECT ...\\n```\\n\\nSums the postings for ..."``. Both shapes
|
|
61
|
+
are handled by stopping at the first bare fence line, and unwrapping one if the
|
|
62
|
+
response opens with one too.
|
|
63
|
+
"""
|
|
64
|
+
stripped = text.strip()
|
|
65
|
+
lines = stripped.splitlines()
|
|
66
|
+
|
|
67
|
+
if stripped.startswith("```"):
|
|
68
|
+
end = next((i for i in range(1, len(lines)) if lines[i].strip() == "```"), None)
|
|
69
|
+
return "\n".join(lines[1:end]).strip()
|
|
70
|
+
|
|
71
|
+
fence = next((i for i, line in enumerate(lines) if line.strip() == "```"), None)
|
|
72
|
+
if fence is not None:
|
|
73
|
+
return "\n".join(lines[:fence]).strip()
|
|
74
|
+
|
|
75
|
+
return stripped
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: fava-nl2bql
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fava extension: ask questions about your ledger in plain English, get BQL results
|
|
5
|
+
Project-URL: Homepage, https://github.com/tarioch/fava-nl2bql
|
|
6
|
+
Project-URL: Documentation, https://fava-nl2bql.rtfd.io
|
|
7
|
+
Project-URL: Repository, https://github.com/tarioch/fava-nl2bql/
|
|
8
|
+
Project-URL: Issues, https://github.com/tarioch/fava-nl2bql/issues
|
|
9
|
+
Author-email: Patrick Ruckstuhl <patrick@ch.tario.org>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE.txt
|
|
12
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
13
|
+
Classifier: Framework :: Flask
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
17
|
+
Classifier: Topic :: Office/Business :: Financial :: Accounting
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Requires-Dist: fava>=1.30
|
|
20
|
+
Requires-Dist: ollama>=0.6
|
|
21
|
+
Description-Content-Type: text/x-rst
|
|
22
|
+
|
|
23
|
+
.. image:: https://img.shields.io/pypi/l/fava-nl2bql.svg
|
|
24
|
+
:target: https://pypi.python.org/pypi/fava-nl2bql
|
|
25
|
+
.. image:: https://img.shields.io/pypi/v/fava-nl2bql.svg
|
|
26
|
+
:target: https://pypi.python.org/pypi/fava-nl2bql
|
|
27
|
+
.. image:: https://readthedocs.org/projects/fava-nl2bql/badge
|
|
28
|
+
:target: https://fava-nl2bql.rtfd.io
|
|
29
|
+
|
|
30
|
+
fava-nl2bql
|
|
31
|
+
===========
|
|
32
|
+
|
|
33
|
+
A `Fava <https://beancount.github.io/fava/>`__ extension that translates plain-English questions about
|
|
34
|
+
your ledger into `BQL <https://beancount.github.io/docs/beancount_query_language.html>`__ queries and runs
|
|
35
|
+
them, using a small tuned model instead of hand-written heuristics.
|
|
36
|
+
|
|
37
|
+
This project is a work in progress. See the detailed `Documentation <https://fava-nl2bql.rtfd.io/>`__.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
fava_nl2bql/FavaNl2Bql.js,sha256=fnuNILPn6bOLqRZxO2Co84TT_PQlyNCnURieqKdFxOc,270
|
|
2
|
+
fava_nl2bql/__init__.py,sha256=MJweWK6gHNNZsVrWrX-BcJZZQ3EysY3bQ1sf4p5qn7o,329
|
|
3
|
+
fava_nl2bql/extension.py,sha256=T254hzbLSGtqcXqNUPABO_9Ry164xPngNJ4V6S2Lkks,1318
|
|
4
|
+
fava_nl2bql/translator.py,sha256=Io9Rs5WpysCpcsnbEfn-Xtuv_DbZqn-GU6PZ0U4BzsQ,2551
|
|
5
|
+
fava_nl2bql/templates/FavaNl2Bql.html,sha256=zdcKI32mKvWuT3MeAuGPURJvtbtU-5MZi1QHIeuJsSA,925
|
|
6
|
+
fava_nl2bql-0.1.0.dist-info/METADATA,sha256=LTk7sWfxu6xo9Iv_-w0Z9D92onfWBPe-tmGgGjkVbGk,1626
|
|
7
|
+
fava_nl2bql-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
8
|
+
fava_nl2bql-0.1.0.dist-info/licenses/LICENSE.txt,sha256=xUbQIFm78yzQqTBUzlMlrJnnLpzdym-A6z2R_UAq7Os,1074
|
|
9
|
+
fava_nl2bql-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Patrick Ruckstuhl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|