decisions-reranker 0.2.0__tar.gz
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.
- decisions_reranker-0.2.0/LICENSE +21 -0
- decisions_reranker-0.2.0/PKG-INFO +89 -0
- decisions_reranker-0.2.0/README.md +77 -0
- decisions_reranker-0.2.0/pyproject.toml +17 -0
- decisions_reranker-0.2.0/pyproject.toml.orig +18 -0
- decisions_reranker-0.2.0/src/decisions_reranker/__init__.py +3 -0
- decisions_reranker-0.2.0/src/decisions_reranker/py.typed +0 -0
- decisions_reranker-0.2.0/src/decisions_reranker/reranker.py +92 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Malte Weber
|
|
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.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: decisions-reranker
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A simple reranker for RAG systems, built on the OpenAI Decisions API. Requires an OpenAI API key.
|
|
5
|
+
Author: Malte Weber
|
|
6
|
+
Author-email: Malte Weber <dev-weber@posteo.de>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: openai>=3.28.0
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
## decisions-reranker
|
|
14
|
+
|
|
15
|
+
A simple reranker for RAG systems, built on top of the OpenAI Decisions API.
|
|
16
|
+
|
|
17
|
+
OpenAI doesn't offer a dedicated reranking model, so this package uses the Decisions API to fill that gap. If your stack already runs on OpenAI, you can add reranking without bringing in another provider.
|
|
18
|
+
|
|
19
|
+
### requirements
|
|
20
|
+
|
|
21
|
+
This package calls the OpenAI API and requires an OpenAI API key. By default it creates an `OpenAI()` client, which reads the key from the `OPENAI_API_KEY` environment variable:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
export OPENAI_API_KEY="sk-..."
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Alternatively, pass your own configured client:
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from openai import OpenAI
|
|
31
|
+
from decisions_reranker import DecisionsReranker
|
|
32
|
+
|
|
33
|
+
reranker = DecisionsReranker(client=OpenAI(api_key="sk-..."))
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Each `filter()` or `rerank()` call makes one request to the Decisions API and is billed to your OpenAI account.
|
|
37
|
+
|
|
38
|
+
### methods
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
DecisionsReranker().filter()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Filters a list of documents for relevance regarding a given query. Returns only documents above a given threshold.
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
DecisionsReranker().rerank()
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Reorders a list of documents by relevance regarding a given query. Returns all documents in a new order.
|
|
51
|
+
|
|
52
|
+
### parameters
|
|
53
|
+
|
|
54
|
+
The DecisionsReranker constructor takes the following parameters. All of them are optional:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
client: OpenAI | None = None,
|
|
58
|
+
model: str = "gpt-6-luna",
|
|
59
|
+
instruction: str = DEFAULT_INSTRUCTION,
|
|
60
|
+
threshold: float = 0.5
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Example usage
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from decisions_reranker import DecisionsReranker, Document
|
|
67
|
+
|
|
68
|
+
reranker = DecisionsReranker(
|
|
69
|
+
instruction="Is this snippet relevant to answer the given query: {document}. Only answer with true if the snippet really contains directly relevant information for the question",
|
|
70
|
+
threshold=0.8,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
query = "How can I reduce memory usage in a Python application?"
|
|
74
|
+
|
|
75
|
+
documents = [
|
|
76
|
+
{ "id": "d01", "text": "Python generators yield items lazily, so you avoid loading an entire list into memory at once."},
|
|
77
|
+
{ "id": "d02", "text": "Defining __slots__ on a Python class removes the per-instance __dict__ and cuts memory overhead."},
|
|
78
|
+
{ "id": "d03", "text": "The tracemalloc module shows which lines of Python code allocate the most memory."},
|
|
79
|
+
{ "id": "d04", "text": "Python's GIL prevents multiple threads from executing bytecode in parallel."},
|
|
80
|
+
{ "id": "d05", "text": "NumPy arrays store numbers far more compactly than Python lists of int objects."}
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
documents = [Document(id=d["id"], text=d["text"]) for d in documents]
|
|
84
|
+
|
|
85
|
+
filtered = reranker.filter(query, documents)
|
|
86
|
+
|
|
87
|
+
reranked = reranker.rerank(query, documents)
|
|
88
|
+
|
|
89
|
+
```
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
## decisions-reranker
|
|
2
|
+
|
|
3
|
+
A simple reranker for RAG systems, built on top of the OpenAI Decisions API.
|
|
4
|
+
|
|
5
|
+
OpenAI doesn't offer a dedicated reranking model, so this package uses the Decisions API to fill that gap. If your stack already runs on OpenAI, you can add reranking without bringing in another provider.
|
|
6
|
+
|
|
7
|
+
### requirements
|
|
8
|
+
|
|
9
|
+
This package calls the OpenAI API and requires an OpenAI API key. By default it creates an `OpenAI()` client, which reads the key from the `OPENAI_API_KEY` environment variable:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
export OPENAI_API_KEY="sk-..."
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Alternatively, pass your own configured client:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from openai import OpenAI
|
|
19
|
+
from decisions_reranker import DecisionsReranker
|
|
20
|
+
|
|
21
|
+
reranker = DecisionsReranker(client=OpenAI(api_key="sk-..."))
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Each `filter()` or `rerank()` call makes one request to the Decisions API and is billed to your OpenAI account.
|
|
25
|
+
|
|
26
|
+
### methods
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
DecisionsReranker().filter()
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Filters a list of documents for relevance regarding a given query. Returns only documents above a given threshold.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
DecisionsReranker().rerank()
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Reorders a list of documents by relevance regarding a given query. Returns all documents in a new order.
|
|
39
|
+
|
|
40
|
+
### parameters
|
|
41
|
+
|
|
42
|
+
The DecisionsReranker constructor takes the following parameters. All of them are optional:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
client: OpenAI | None = None,
|
|
46
|
+
model: str = "gpt-6-luna",
|
|
47
|
+
instruction: str = DEFAULT_INSTRUCTION,
|
|
48
|
+
threshold: float = 0.5
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Example usage
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from decisions_reranker import DecisionsReranker, Document
|
|
55
|
+
|
|
56
|
+
reranker = DecisionsReranker(
|
|
57
|
+
instruction="Is this snippet relevant to answer the given query: {document}. Only answer with true if the snippet really contains directly relevant information for the question",
|
|
58
|
+
threshold=0.8,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
query = "How can I reduce memory usage in a Python application?"
|
|
62
|
+
|
|
63
|
+
documents = [
|
|
64
|
+
{ "id": "d01", "text": "Python generators yield items lazily, so you avoid loading an entire list into memory at once."},
|
|
65
|
+
{ "id": "d02", "text": "Defining __slots__ on a Python class removes the per-instance __dict__ and cuts memory overhead."},
|
|
66
|
+
{ "id": "d03", "text": "The tracemalloc module shows which lines of Python code allocate the most memory."},
|
|
67
|
+
{ "id": "d04", "text": "Python's GIL prevents multiple threads from executing bytecode in parallel."},
|
|
68
|
+
{ "id": "d05", "text": "NumPy arrays store numbers far more compactly than Python lists of int objects."}
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
documents = [Document(id=d["id"], text=d["text"]) for d in documents]
|
|
72
|
+
|
|
73
|
+
filtered = reranker.filter(query, documents)
|
|
74
|
+
|
|
75
|
+
reranked = reranker.rerank(query, documents)
|
|
76
|
+
|
|
77
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "decisions-reranker"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "A simple reranker for RAG systems, built on the OpenAI Decisions API. Requires an OpenAI API key."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
dependencies = ["openai>=3.28.0"]
|
|
10
|
+
|
|
11
|
+
[[project.authors]]
|
|
12
|
+
name = "Malte Weber"
|
|
13
|
+
email = "dev-weber@posteo.de"
|
|
14
|
+
|
|
15
|
+
[build-system]
|
|
16
|
+
requires = ["uv_build>=0.12.15,<0.13.0"]
|
|
17
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "decisions-reranker"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "A simple reranker for RAG systems, built on the OpenAI Decisions API. Requires an OpenAI API key."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Malte Weber", email = "dev-weber@posteo.de" }
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"openai>=3.28.0",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["uv_build>=0.12.15,<0.13.0"]
|
|
18
|
+
build-backend = "uv_build"
|
|
File without changes
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from openai import OpenAI
|
|
4
|
+
from openai.types.decision import AnswerAnswerResourcePredicate
|
|
5
|
+
from openai.types.decision_create_params import QuestionQuestionParamPredicate
|
|
6
|
+
|
|
7
|
+
DEFAULT_INSTRUCTION = "Is this snippet relevant to answer the given query: {document}"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class Document:
|
|
12
|
+
id: str
|
|
13
|
+
text: str
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class ScoredDocument:
|
|
17
|
+
id: str
|
|
18
|
+
text: str
|
|
19
|
+
score: float
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _validate_instruction(instruction: str) -> str:
|
|
23
|
+
if "{document}" not in instruction:
|
|
24
|
+
raise ValueError("instruction must contain the placeholder '{document}'")
|
|
25
|
+
try:
|
|
26
|
+
instruction.format(document="")
|
|
27
|
+
except (KeyError, IndexError) as error:
|
|
28
|
+
raise ValueError(f"instruction contains an unknown placeholder: {error}") from error
|
|
29
|
+
return instruction
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class DecisionsReranker:
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
client: OpenAI | None = None,
|
|
36
|
+
model: str = "gpt-6-luna",
|
|
37
|
+
instruction: str = DEFAULT_INSTRUCTION,
|
|
38
|
+
threshold: float = 0.5
|
|
39
|
+
) -> None:
|
|
40
|
+
self._client = client or OpenAI()
|
|
41
|
+
self._model = model
|
|
42
|
+
self._instruction = _validate_instruction(instruction)
|
|
43
|
+
self._threshold = threshold
|
|
44
|
+
|
|
45
|
+
def filter(
|
|
46
|
+
self,
|
|
47
|
+
query: str,
|
|
48
|
+
documents: list[Document],
|
|
49
|
+
threshold: float | None = None,
|
|
50
|
+
instruction: str | None = None,
|
|
51
|
+
) -> list[ScoredDocument]:
|
|
52
|
+
cutoff = self._threshold if threshold is None else threshold
|
|
53
|
+
filtered = [
|
|
54
|
+
ScoredDocument(document.id, document.text, score)
|
|
55
|
+
for document, score in self._score(query, documents, instruction)
|
|
56
|
+
if score > cutoff
|
|
57
|
+
]
|
|
58
|
+
filtered_sorted = sorted(filtered, key=lambda d: d.score, reverse=True)
|
|
59
|
+
return filtered_sorted
|
|
60
|
+
|
|
61
|
+
def rerank(
|
|
62
|
+
self,
|
|
63
|
+
query: str,
|
|
64
|
+
documents: list[Document],
|
|
65
|
+
instruction: str | None = None,
|
|
66
|
+
) -> list[ScoredDocument]:
|
|
67
|
+
scored = sorted(self._score(query, documents, instruction), key=lambda item: item[1], reverse=True)
|
|
68
|
+
return [ScoredDocument(document.id, document.text, score) for document, score in scored]
|
|
69
|
+
|
|
70
|
+
def _score(
|
|
71
|
+
self,
|
|
72
|
+
query: str,
|
|
73
|
+
documents: list[Document],
|
|
74
|
+
instruction: str | None,
|
|
75
|
+
) -> list[tuple[Document, float]]:
|
|
76
|
+
template = self._instruction if instruction is None else _validate_instruction(instruction)
|
|
77
|
+
decision = self._client.decisions.create(
|
|
78
|
+
model=self._model,
|
|
79
|
+
input=query,
|
|
80
|
+
questions=[
|
|
81
|
+
QuestionQuestionParamPredicate(
|
|
82
|
+
type="predicate",
|
|
83
|
+
name=document.id,
|
|
84
|
+
instructions=template.format(document=document.text),
|
|
85
|
+
)
|
|
86
|
+
for document in documents
|
|
87
|
+
],
|
|
88
|
+
)
|
|
89
|
+
return [
|
|
90
|
+
(document, answer.probability if isinstance(answer, AnswerAnswerResourcePredicate) else 0.0)
|
|
91
|
+
for document, answer in zip(documents, decision.answers, strict=True)
|
|
92
|
+
]
|