Fix docstrings

This commit is contained in:
Yiorgis Gozadinos 2025-07-16 19:06:54 +03:00
parent feff962f2e
commit 20beb23a38
No known key found for this signature in database
3 changed files with 75 additions and 49 deletions

View file

@ -6,15 +6,11 @@ from haiku.rag.config import Config
class Chunker:
"""
A class that chunks text into smaller pieces for embedding and retrieval.
"""A class that chunks text into smaller pieces for embedding and retrieval.
Parameters
----------
chunk_size : int
The maximum size of a chunk in characters.
chunk_overlap : int
The number of characters of overlap between chunks.
Args:
chunk_size: The maximum size of a chunk in tokens.
chunk_overlap: The number of tokens of overlap between chunks.
"""
encoder: ClassVar[tiktoken.Encoding] = tiktoken.encoding_for_model("gpt-4o")
@ -28,18 +24,13 @@ class Chunker:
self.chunk_overlap = chunk_overlap
async def chunk(self, text: str) -> list[str]:
"""
Split the text into chunks.
"""Split the text into chunks based on token boundaries.
Parameters
----------
text : str
The text to be split into chunks.
Args:
text: The text to be split into chunks.
Returns
-------
list
A list of text chunks.
Returns:
A list of text chunks with token-based boundaries and overlap.
"""
if not text:
return []

View file

@ -26,7 +26,12 @@ class HaikuRAG:
/ "haiku.rag.sqlite",
skip_validation: bool = False,
):
"""Initialize the RAG client with a database path."""
"""Initialize the RAG client with a database path.
Args:
db_path: Path to the SQLite database file or ":memory:" for in-memory database.
skip_validation: Whether to skip configuration validation on database load.
"""
if isinstance(db_path, Path):
if not db_path.parent.exists():
Path.mkdir(db_path.parent, parents=True)
@ -46,7 +51,16 @@ class HaikuRAG:
async def create_document(
self, content: str, uri: str | None = None, metadata: dict | None = None
) -> Document:
"""Create a new document with optional URI and metadata."""
"""Create a new document with optional URI and metadata.
Args:
content: The text content of the document.
uri: Optional URI identifier for the document.
metadata: Optional metadata dictionary.
Returns:
The created Document instance.
"""
document = Document(
content=content,
uri=uri,
@ -219,11 +233,25 @@ class HaikuRAG:
return ".html"
async def get_document_by_id(self, document_id: int) -> Document | None:
"""Get a document by its ID."""
"""Get a document by its ID.
Args:
document_id: The unique identifier of the document.
Returns:
The Document instance if found, None otherwise.
"""
return await self.document_repository.get_by_id(document_id)
async def get_document_by_uri(self, uri: str) -> Document | None:
"""Get a document by its URI."""
"""Get a document by its URI.
Args:
uri: The URI identifier of the document.
Returns:
The Document instance if found, None otherwise.
"""
return await self.document_repository.get_by_uri(uri)
async def update_document(self, document: Document) -> Document:
@ -237,7 +265,15 @@ class HaikuRAG:
async def list_documents(
self, limit: int | None = None, offset: int | None = None
) -> list[Document]:
"""List all documents with optional pagination."""
"""List all documents with optional pagination.
Args:
limit: Maximum number of documents to return.
offset: Number of documents to skip.
Returns:
List of Document instances.
"""
return await self.document_repository.list_all(limit=limit, offset=offset)
async def search(
@ -246,12 +282,12 @@ class HaikuRAG:
"""Search for relevant chunks using hybrid search (vector similarity + full-text search).
Args:
query: The search query string
limit: Maximum number of results to return
k: Parameter for Reciprocal Rank Fusion (default: 60)
query: The search query string.
limit: Maximum number of results to return.
k: Parameter for Reciprocal Rank Fusion (default: 60).
Returns:
List of (chunk, score) tuples ordered by relevance
List of (chunk, score) tuples ordered by relevance.
"""
return await self.chunk_repository.search_chunks_hybrid(query, limit, k)
@ -259,10 +295,10 @@ class HaikuRAG:
"""Ask a question using the configured QA agent.
Args:
question: The question to ask
question: The question to ask.
Returns:
The generated answer as a string
The generated answer as a string.
"""
from haiku.rag.qa import get_qa_agent

View file

@ -7,15 +7,14 @@ from packaging.version import Version, parse
def get_default_data_dir() -> Path:
"""
Get the user data directory for the current system platform.
"""Get the user data directory for the current system platform.
Linux: ~/.local/share/haiku.rag
macOS: ~/Library/Application Support/haiku.rag
Windows: C:/Users/<USER>/AppData/Roaming/haiku.rag
:return: User Data Path
:rtype: Path
Returns:
User Data Path.
"""
home = Path.home()
@ -30,13 +29,13 @@ def get_default_data_dir() -> Path:
def semantic_version_to_int(version: str) -> int:
"""
Convert a semantic version string to an integer.
"""Convert a semantic version string to an integer.
:param version: Semantic version string
:type version: str
:return: Integer representation of semantic version
:rtype: int
Args:
version: Semantic version string.
Returns:
Integer representation of semantic version.
"""
major, minor, patch = version.split(".")
major = int(major) << 16
@ -46,13 +45,13 @@ def semantic_version_to_int(version: str) -> int:
def int_to_semantic_version(version: int) -> str:
"""
Convert an integer to a semantic version string.
"""Convert an integer to a semantic version string.
:param version: Integer representation of semantic version
:type version: int
:return: Semantic version string
:rtype: str
Args:
version: Integer representation of semantic version.
Returns:
Semantic version string.
"""
major = version >> 16
minor = (version >> 8) & 255
@ -61,11 +60,11 @@ def int_to_semantic_version(version: int) -> str:
async def is_up_to_date() -> tuple[bool, Version, Version]:
"""
Checks whether haiku.rag is current.
"""Check whether haiku.rag is current.
:return: A tuple containing a boolean indicating whether haiku.rag is current, the running version and the latest version
:rtype: tuple[bool, Version, Version]
Returns:
A tuple containing a boolean indicating whether haiku.rag is current,
the running version and the latest version.
"""
async with httpx.AsyncClient() as client: