Fix docstrings
This commit is contained in:
parent
feff962f2e
commit
20beb23a38
3 changed files with 75 additions and 49 deletions
|
|
@ -6,15 +6,11 @@ from haiku.rag.config import Config
|
||||||
|
|
||||||
|
|
||||||
class Chunker:
|
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
|
Args:
|
||||||
----------
|
chunk_size: The maximum size of a chunk in tokens.
|
||||||
chunk_size : int
|
chunk_overlap: The number of tokens of overlap between chunks.
|
||||||
The maximum size of a chunk in characters.
|
|
||||||
chunk_overlap : int
|
|
||||||
The number of characters of overlap between chunks.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
encoder: ClassVar[tiktoken.Encoding] = tiktoken.encoding_for_model("gpt-4o")
|
encoder: ClassVar[tiktoken.Encoding] = tiktoken.encoding_for_model("gpt-4o")
|
||||||
|
|
@ -28,18 +24,13 @@ class Chunker:
|
||||||
self.chunk_overlap = chunk_overlap
|
self.chunk_overlap = chunk_overlap
|
||||||
|
|
||||||
async def chunk(self, text: str) -> list[str]:
|
async def chunk(self, text: str) -> list[str]:
|
||||||
"""
|
"""Split the text into chunks based on token boundaries.
|
||||||
Split the text into chunks.
|
|
||||||
|
|
||||||
Parameters
|
Args:
|
||||||
----------
|
text: The text to be split into chunks.
|
||||||
text : str
|
|
||||||
The text to be split into chunks.
|
|
||||||
|
|
||||||
Returns
|
Returns:
|
||||||
-------
|
A list of text chunks with token-based boundaries and overlap.
|
||||||
list
|
|
||||||
A list of text chunks.
|
|
||||||
"""
|
"""
|
||||||
if not text:
|
if not text:
|
||||||
return []
|
return []
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,12 @@ class HaikuRAG:
|
||||||
/ "haiku.rag.sqlite",
|
/ "haiku.rag.sqlite",
|
||||||
skip_validation: bool = False,
|
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 isinstance(db_path, Path):
|
||||||
if not db_path.parent.exists():
|
if not db_path.parent.exists():
|
||||||
Path.mkdir(db_path.parent, parents=True)
|
Path.mkdir(db_path.parent, parents=True)
|
||||||
|
|
@ -46,7 +51,16 @@ class HaikuRAG:
|
||||||
async def create_document(
|
async def create_document(
|
||||||
self, content: str, uri: str | None = None, metadata: dict | None = None
|
self, content: str, uri: str | None = None, metadata: dict | None = None
|
||||||
) -> Document:
|
) -> 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(
|
document = Document(
|
||||||
content=content,
|
content=content,
|
||||||
uri=uri,
|
uri=uri,
|
||||||
|
|
@ -219,11 +233,25 @@ class HaikuRAG:
|
||||||
return ".html"
|
return ".html"
|
||||||
|
|
||||||
async def get_document_by_id(self, document_id: int) -> Document | None:
|
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)
|
return await self.document_repository.get_by_id(document_id)
|
||||||
|
|
||||||
async def get_document_by_uri(self, uri: str) -> Document | None:
|
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)
|
return await self.document_repository.get_by_uri(uri)
|
||||||
|
|
||||||
async def update_document(self, document: Document) -> Document:
|
async def update_document(self, document: Document) -> Document:
|
||||||
|
|
@ -237,7 +265,15 @@ class HaikuRAG:
|
||||||
async def list_documents(
|
async def list_documents(
|
||||||
self, limit: int | None = None, offset: int | None = None
|
self, limit: int | None = None, offset: int | None = None
|
||||||
) -> list[Document]:
|
) -> 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)
|
return await self.document_repository.list_all(limit=limit, offset=offset)
|
||||||
|
|
||||||
async def search(
|
async def search(
|
||||||
|
|
@ -246,12 +282,12 @@ class HaikuRAG:
|
||||||
"""Search for relevant chunks using hybrid search (vector similarity + full-text search).
|
"""Search for relevant chunks using hybrid search (vector similarity + full-text search).
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
query: The search query string
|
query: The search query string.
|
||||||
limit: Maximum number of results to return
|
limit: Maximum number of results to return.
|
||||||
k: Parameter for Reciprocal Rank Fusion (default: 60)
|
k: Parameter for Reciprocal Rank Fusion (default: 60).
|
||||||
|
|
||||||
Returns:
|
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)
|
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.
|
"""Ask a question using the configured QA agent.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
question: The question to ask
|
question: The question to ask.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The generated answer as a string
|
The generated answer as a string.
|
||||||
"""
|
"""
|
||||||
from haiku.rag.qa import get_qa_agent
|
from haiku.rag.qa import get_qa_agent
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,14 @@ from packaging.version import Version, parse
|
||||||
|
|
||||||
|
|
||||||
def get_default_data_dir() -> Path:
|
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
|
Linux: ~/.local/share/haiku.rag
|
||||||
macOS: ~/Library/Application Support/haiku.rag
|
macOS: ~/Library/Application Support/haiku.rag
|
||||||
Windows: C:/Users/<USER>/AppData/Roaming/haiku.rag
|
Windows: C:/Users/<USER>/AppData/Roaming/haiku.rag
|
||||||
|
|
||||||
:return: User Data Path
|
Returns:
|
||||||
:rtype: Path
|
User Data Path.
|
||||||
"""
|
"""
|
||||||
home = Path.home()
|
home = Path.home()
|
||||||
|
|
||||||
|
|
@ -30,13 +29,13 @@ def get_default_data_dir() -> Path:
|
||||||
|
|
||||||
|
|
||||||
def semantic_version_to_int(version: str) -> int:
|
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
|
Args:
|
||||||
:type version: str
|
version: Semantic version string.
|
||||||
:return: Integer representation of semantic version
|
|
||||||
:rtype: int
|
Returns:
|
||||||
|
Integer representation of semantic version.
|
||||||
"""
|
"""
|
||||||
major, minor, patch = version.split(".")
|
major, minor, patch = version.split(".")
|
||||||
major = int(major) << 16
|
major = int(major) << 16
|
||||||
|
|
@ -46,13 +45,13 @@ def semantic_version_to_int(version: str) -> int:
|
||||||
|
|
||||||
|
|
||||||
def int_to_semantic_version(version: int) -> str:
|
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
|
Args:
|
||||||
:type version: int
|
version: Integer representation of semantic version.
|
||||||
:return: Semantic version string
|
|
||||||
:rtype: str
|
Returns:
|
||||||
|
Semantic version string.
|
||||||
"""
|
"""
|
||||||
major = version >> 16
|
major = version >> 16
|
||||||
minor = (version >> 8) & 255
|
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]:
|
async def is_up_to_date() -> tuple[bool, Version, Version]:
|
||||||
"""
|
"""Check whether haiku.rag is current.
|
||||||
Checks whether haiku.rag is current.
|
|
||||||
|
|
||||||
:return: A tuple containing a boolean indicating whether haiku.rag is current, the running version and the latest version
|
Returns:
|
||||||
:rtype: tuple[bool, Version, Version]
|
A tuple containing a boolean indicating whether haiku.rag is current,
|
||||||
|
the running version and the latest version.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue