Skip to content

Commit ad67dc2

Browse files
authored
Publish collection stats (#2)
1 parent 20ed386 commit ad67dc2

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Needle Python Library
22

3+
[![PyPI - Version](https://img.shields.io/pypi/v/needle-python.svg)](https://pypi.org/project/needle-python)
4+
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/needle-python.svg)](https://pypi.org/project/needle-python)
5+
36
This Python library provides convenient acccess to Needle API. There are various methods and data types which, we believe will help you explore Needle API quickly. There may be some functionality available in REST API earlier than this Python library. In any case, we recommend to take look the the complete [documentation](https://docs.needle-ai.com). Thank you for flying with us. 🚀
47

58
## Installation
@@ -95,3 +98,7 @@ If a request to Needle API fails, `needle.v1.models.Error` object will be thrown
9598
## Support 📞
9699

97100
If you have questions you can contact us in our [Discord channel](https://discord.gg/JzJcHgTyZx).
101+
102+
# License
103+
104+
`needle-python` is distributed under the terms of the MIT license.

needle/v1/collections/__init__.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
Collection,
1313
Error,
1414
SearchResult,
15+
CollectionStats,
16+
CollectionDataStats,
1517
)
1618
from needle.v1.collections.files import NeedleCollectionsFiles
1719

@@ -129,7 +131,13 @@ def list(self):
129131
for c in body.get("result")
130132
]
131133

132-
def search(self, collection_id: str, text: str):
134+
def search(
135+
self,
136+
collection_id: str,
137+
text: str,
138+
max_distance: Optional[float] = None,
139+
top_k: Optional[int] = None,
140+
):
133141
"""
134142
Searches within a collection based on the provided parameters.
135143
@@ -143,7 +151,11 @@ def search(self, collection_id: str, text: str):
143151
Error: If the API request fails.
144152
"""
145153
endpoint = f"{self.search_endpoint}/{collection_id}/search"
146-
req_body = {"text": text}
154+
req_body = {
155+
"text": text,
156+
"max_distance": max_distance,
157+
"top_k": top_k,
158+
}
147159
resp = self.session.post(endpoint, headers=self.headers, json=req_body)
148160
body = resp.json()
149161
if resp.status_code >= 400:
@@ -156,3 +168,39 @@ def search(self, collection_id: str, text: str):
156168
)
157169
for r in body.get("result")
158170
]
171+
172+
def get_stats(self, collection_id: str):
173+
"""
174+
Retrieves statistics of a collection.
175+
176+
Args:
177+
collection_id (str): The ID of the collection to retrieve statistics for.
178+
179+
Returns:
180+
dict: The collection statistics.
181+
182+
Raises:
183+
Error: If the API request fails.
184+
"""
185+
endpoint = f"{self.endpoint}/{collection_id}/stats"
186+
resp = self.session.get(endpoint, headers=self.headers)
187+
body = resp.json()
188+
if resp.status_code >= 400:
189+
error = body.get("error")
190+
raise Error(**error)
191+
192+
result = body.get("result")
193+
data_stats = [
194+
CollectionDataStats(
195+
status=ds.get("status"),
196+
files=ds.get("files"),
197+
bytes=ds.get("bytes"),
198+
)
199+
for ds in result.get("data_stats")
200+
]
201+
return CollectionStats(
202+
data_stats=data_stats,
203+
chunks_count=result.get("chunks_count"),
204+
characters=result.get("characters"),
205+
users=result.get("users"),
206+
)

needle/v1/models.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ class CollectionFile:
9393
status: CollectionFileStatus
9494

9595

96+
@dataclass(frozen=True)
97+
class CollectionDataStats:
98+
"""
99+
Represents data statistics of a collection in the Needle API.
100+
"""
101+
102+
status: Optional[str]
103+
files: int
104+
bytes: int
105+
106+
107+
@dataclass(frozen=True)
108+
class CollectionStats:
109+
"""
110+
Represents statistics of a collection in the Needle API.
111+
"""
112+
113+
data_stats: list[CollectionDataStats]
114+
chunks_count: int
115+
characters: int
116+
users: int
117+
118+
96119
@dataclass(frozen=True)
97120
class SearchResult:
98121
"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "needle-python"
7-
version = "0.2.0"
7+
version = "0.3.0"
88
description = "Needle client library for Python"
99
authors = [
1010
"Onur Eken <m.onureken@gmail.com>",

0 commit comments

Comments
 (0)