haiku.rag 0.11.3__py3-none-any.whl → 0.11.4__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.

Potentially problematic release.


This version of haiku.rag might be problematic. Click here for more details.

haiku/rag/client.py CHANGED
@@ -46,6 +46,9 @@ class HaikuRAG:
46
46
 
47
47
  async def __aexit__(self, exc_type, exc_val, exc_tb): # noqa: ARG002
48
48
  """Async context manager exit."""
49
+ # Wait for any pending vacuum to complete before closing
50
+ async with self.store._vacuum_lock:
51
+ pass
49
52
  self.close()
50
53
  return False
51
54
 
@@ -522,19 +525,22 @@ class HaikuRAG:
522
525
  merged.append(current)
523
526
  return merged
524
527
 
525
- async def ask(self, question: str, cite: bool = False) -> str:
528
+ async def ask(
529
+ self, question: str, cite: bool = False, system_prompt: str | None = None
530
+ ) -> str:
526
531
  """Ask a question using the configured QA agent.
527
532
 
528
533
  Args:
529
534
  question: The question to ask.
530
535
  cite: Whether to include citations in the response.
536
+ system_prompt: Optional custom system prompt for the QA agent.
531
537
 
532
538
  Returns:
533
539
  The generated answer as a string.
534
540
  """
535
541
  from haiku.rag.qa import get_qa_agent
536
542
 
537
- qa_agent = get_qa_agent(self, use_citations=cite)
543
+ qa_agent = get_qa_agent(self, use_citations=cite, system_prompt=system_prompt)
538
544
  return await qa_agent.answer(question)
539
545
 
540
546
  async def rebuild_database(self) -> AsyncGenerator[str, None]:
@@ -617,13 +623,13 @@ class HaikuRAG:
617
623
 
618
624
  # Final maintenance: centralized vacuum to curb disk usage
619
625
  try:
620
- self.store.vacuum()
626
+ await self.store.vacuum()
621
627
  except Exception:
622
628
  pass
623
629
 
624
630
  async def vacuum(self) -> None:
625
631
  """Optimize and clean up old versions across all tables."""
626
- self.store.vacuum()
632
+ await self.store.vacuum()
627
633
 
628
634
  def close(self):
629
635
  """Close the underlying store connection."""
haiku/rag/config.py CHANGED
@@ -57,6 +57,11 @@ class AppConfig(BaseModel):
57
57
  # and error out when the database does not already exist.
58
58
  DISABLE_DB_AUTOCREATE: bool = False
59
59
 
60
+ # Vacuum retention threshold in seconds. Only versions older than this
61
+ # threshold will be removed during vacuum operations. Default is 60 seconds
62
+ # to allow concurrent connections to safely use recent versions.
63
+ VACUUM_RETENTION_SECONDS: int = 60
64
+
60
65
  @field_validator("MONITOR_DIRECTORIES", mode="before")
61
66
  @classmethod
62
67
  def parse_monitor_directories(cls, v):
haiku/rag/migration.py CHANGED
@@ -27,7 +27,7 @@ class SQLiteToLanceDBMigrator:
27
27
  self.lancedb_path = lancedb_path
28
28
  self.console = Console()
29
29
 
30
- def migrate(self) -> bool:
30
+ async def migrate(self) -> bool:
31
31
  """Perform the migration."""
32
32
  try:
33
33
  self.console.print(
@@ -94,7 +94,7 @@ class SQLiteToLanceDBMigrator:
94
94
  # Optimize and cleanup using centralized vacuum
95
95
  self.console.print("[cyan]Optimizing LanceDB...[/cyan]")
96
96
  try:
97
- lance_store.vacuum()
97
+ await lance_store.vacuum()
98
98
  self.console.print("[green]✅ Optimization completed[/green]")
99
99
  except Exception as e:
100
100
  self.console.print(
@@ -313,4 +313,4 @@ async def migrate_sqlite_to_lancedb(
313
313
  lancedb_path = sqlite_path.parent / (sqlite_path.stem + ".lancedb")
314
314
 
315
315
  migrator = SQLiteToLanceDBMigrator(sqlite_path, lancedb_path)
316
- return migrator.migrate()
316
+ return await migrator.migrate()
haiku/rag/qa/__init__.py CHANGED
@@ -3,7 +3,11 @@ from haiku.rag.config import Config
3
3
  from haiku.rag.qa.agent import QuestionAnswerAgent
4
4
 
5
5
 
6
- def get_qa_agent(client: HaikuRAG, use_citations: bool = False) -> QuestionAnswerAgent:
6
+ def get_qa_agent(
7
+ client: HaikuRAG,
8
+ use_citations: bool = False,
9
+ system_prompt: str | None = None,
10
+ ) -> QuestionAnswerAgent:
7
11
  provider = Config.QA_PROVIDER
8
12
  model_name = Config.QA_MODEL
9
13
 
@@ -12,4 +16,5 @@ def get_qa_agent(client: HaikuRAG, use_citations: bool = False) -> QuestionAnswe
12
16
  provider=provider,
13
17
  model=model_name,
14
18
  use_citations=use_citations,
19
+ system_prompt=system_prompt,
15
20
  )
haiku/rag/qa/agent.py CHANGED
@@ -30,18 +30,21 @@ class QuestionAnswerAgent:
30
30
  model: str,
31
31
  use_citations: bool = False,
32
32
  q: float = 0.0,
33
+ system_prompt: str | None = None,
33
34
  ):
34
35
  self._client = client
35
36
 
36
- system_prompt = (
37
- QA_SYSTEM_PROMPT_WITH_CITATIONS if use_citations else QA_SYSTEM_PROMPT
38
- )
37
+ if system_prompt is None:
38
+ system_prompt = (
39
+ QA_SYSTEM_PROMPT_WITH_CITATIONS if use_citations else QA_SYSTEM_PROMPT
40
+ )
39
41
  model_obj = self._get_model(provider, model)
40
42
 
41
43
  self._agent = Agent(
42
44
  model=model_obj,
43
45
  deps_type=Dependencies,
44
46
  system_prompt=system_prompt,
47
+ retries=3,
45
48
  )
46
49
 
47
50
  @self._agent.tool
haiku/rag/store/engine.py CHANGED
@@ -1,3 +1,4 @@
1
+ import asyncio
1
2
  import json
2
3
  import logging
3
4
  from datetime import timedelta
@@ -51,6 +52,7 @@ class Store:
51
52
  def __init__(self, db_path: Path, skip_validation: bool = False):
52
53
  self.db_path: Path = db_path
53
54
  self.embedder = get_embedder()
55
+ self._vacuum_lock = asyncio.Lock()
54
56
 
55
57
  # Create the ChunkRecord model with the correct vector dimension
56
58
  self.ChunkRecord = create_chunk_model(self.embedder._vector_dim)
@@ -78,14 +80,40 @@ class Store:
78
80
  if not skip_validation:
79
81
  self._validate_configuration()
80
82
 
81
- def vacuum(self) -> None:
82
- """Optimize and clean up old versions across all tables to reduce disk usage."""
83
+ async def vacuum(self, retention_seconds: int | None = None) -> None:
84
+ """Optimize and clean up old versions across all tables to reduce disk usage.
85
+
86
+ Args:
87
+ retention_seconds: Retention threshold in seconds. Only versions older
88
+ than this will be removed. If None, uses Config.VACUUM_RETENTION_SECONDS.
89
+
90
+ Note:
91
+ If vacuum is already running, this method returns immediately without blocking.
92
+ Use asyncio.create_task(store.vacuum()) for non-blocking background execution.
93
+ """
83
94
  if self._has_cloud_config() and str(Config.LANCEDB_URI).startswith("db://"):
84
95
  return
85
96
 
86
- # Perform maintenance per table using optimize() with cleanup_older_than 0
87
- for table in [self.documents_table, self.chunks_table, self.settings_table]:
88
- table.optimize(cleanup_older_than=timedelta(0))
97
+ # Skip if already running (non-blocking)
98
+ if self._vacuum_lock.locked():
99
+ return
100
+
101
+ async with self._vacuum_lock:
102
+ try:
103
+ # Evaluate config at runtime to allow dynamic changes
104
+ if retention_seconds is None:
105
+ retention_seconds = Config.VACUUM_RETENTION_SECONDS
106
+ # Perform maintenance per table using optimize() with configurable retention
107
+ retention = timedelta(seconds=retention_seconds)
108
+ for table in [
109
+ self.documents_table,
110
+ self.chunks_table,
111
+ self.settings_table,
112
+ ]:
113
+ table.optimize(cleanup_older_than=retention)
114
+ except (RuntimeError, OSError) as e:
115
+ # Handle resource errors gracefully
116
+ logger.debug(f"Vacuum skipped due to resource constraints: {e}")
89
117
 
90
118
  def _connect_to_lancedb(self, db_path: Path):
91
119
  """Establish connection to LanceDB (local, cloud, or object storage)."""
@@ -1,4 +1,3 @@
1
- import asyncio
2
1
  import inspect
3
2
  import json
4
3
  import logging
@@ -23,7 +22,6 @@ class ChunkRepository:
23
22
  def __init__(self, store: Store) -> None:
24
23
  self.store = store
25
24
  self.embedder = get_embedder()
26
- self._optimize_lock = asyncio.Lock()
27
25
 
28
26
  def _ensure_fts_index(self) -> None:
29
27
  """Ensure FTS index exists on the content column."""
@@ -35,21 +33,6 @@ class ChunkRepository:
35
33
  # Log the error but don't fail - FTS might already exist
36
34
  logger.debug(f"FTS index creation skipped: {e}")
37
35
 
38
- async def _optimize(self) -> None:
39
- """Optimize the chunks table to refresh indexes."""
40
- # Skip optimization for LanceDB Cloud as it handles this automatically
41
- if Config.LANCEDB_URI and Config.LANCEDB_URI.startswith("db://"):
42
- return
43
-
44
- async with self._optimize_lock:
45
- try:
46
- self.store.chunks_table.optimize()
47
- except (RuntimeError, OSError) as e:
48
- # Handle "too many open files" and other resource errors gracefully
49
- logger.debug(
50
- f"Table optimization skipped due to resource constraints: {e}"
51
- )
52
-
53
36
  async def create(self, entity: Chunk) -> Chunk:
54
37
  """Create a chunk in the database."""
55
38
  assert entity.document_id, "Chunk must have a document_id to be created"
@@ -77,11 +60,6 @@ class ChunkRepository:
77
60
  self.store.chunks_table.add([chunk_record])
78
61
 
79
62
  entity.id = chunk_id
80
-
81
- # Try to optimize if not currently locked (non-blocking)
82
- if not self._optimize_lock.locked():
83
- asyncio.create_task(self._optimize())
84
-
85
63
  return entity
86
64
 
87
65
  async def get_by_id(self, entity_id: str) -> Chunk | None:
@@ -125,10 +103,6 @@ class ChunkRepository:
125
103
  "vector": embedding,
126
104
  },
127
105
  )
128
- # Try to optimize if not currently locked (non-blocking)
129
- if not self._optimize_lock.locked():
130
- asyncio.create_task(self._optimize())
131
-
132
106
  return entity
133
107
 
134
108
  async def delete(self, entity_id: str) -> bool:
@@ -227,8 +201,6 @@ class ChunkRepository:
227
201
  if chunk_records:
228
202
  self.store.chunks_table.add(chunk_records)
229
203
 
230
- # Force optimization once at the end for bulk operations
231
- await self._optimize()
232
204
  return created_chunks
233
205
 
234
206
  async def delete_all(self) -> None:
@@ -1,3 +1,4 @@
1
+ import asyncio
1
2
  import json
2
3
  from datetime import datetime
3
4
  from typing import TYPE_CHECKING
@@ -200,6 +201,9 @@ class DocumentRepository:
200
201
  chunk.order = order
201
202
  await self.chunk_repository.create(chunk)
202
203
 
204
+ # Vacuum old versions in background (non-blocking)
205
+ asyncio.create_task(self.store.vacuum())
206
+
203
207
  return created_doc
204
208
  except Exception:
205
209
  # Roll back to the captured versions and re-raise
@@ -230,6 +234,9 @@ class DocumentRepository:
230
234
  updated_doc.id, docling_document
231
235
  )
232
236
 
237
+ # Vacuum old versions in background (non-blocking)
238
+ asyncio.create_task(self.store.vacuum())
239
+
233
240
  return updated_doc
234
241
  except Exception:
235
242
  # Roll back to the captured versions and re-raise
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: haiku.rag
3
- Version: 0.11.3
3
+ Version: 0.11.4
4
4
  Summary: Agentic Retrieval Augmented Generation (RAG) with LanceDB
5
5
  Author-email: Yiorgis Gozadinos <ggozadinos@gmail.com>
6
6
  License: MIT
@@ -2,11 +2,11 @@ haiku/rag/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  haiku/rag/app.py,sha256=B5BZaUgC9u3hz17tRKu_xKefPu5xqdZQPCxfa5K8_EI,19180
3
3
  haiku/rag/chunker.py,sha256=PVe6ysv8UlacUd4Zb3_8RFWIaWDXnzBAy2VDJ4TaUsE,1555
4
4
  haiku/rag/cli.py,sha256=6GmWfs30uUytcnyBMv-OE4tCkkuKnfYeJFU5DIrZ_vU,13212
5
- haiku/rag/client.py,sha256=fz5bZP1KNWbc9cvpEC8puMBHEvt-vVtFjRMItt_WD0M,23920
6
- haiku/rag/config.py,sha256=c2WoaieI3-HAWb6lCmVnJHY22NXl2SGLsndRbiqCzeA,2305
5
+ haiku/rag/client.py,sha256=tk3BWa9u2mtUEMEOEcgF2ebUqdZRv4jSnu4S6Zho-Xg,24210
6
+ haiku/rag/config.py,sha256=rJ-xi66gc94F_QA6BvgRgNq8CNiFTN0CCIaOv1TMMFw,2569
7
7
  haiku/rag/logging.py,sha256=dm65AwADpcQsH5OAPtRA-4hsw0w5DK-sGOvzYkj6jzw,1720
8
8
  haiku/rag/mcp.py,sha256=H7XibtSNUviFeaJVsXzHiRqUm0nJCpA7A1QHuBv6SKQ,5057
9
- haiku/rag/migration.py,sha256=zm0-60PiS1hIQnZz65B7qfsgM7GwZVXFqMFowjpVBs8,11058
9
+ haiku/rag/migration.py,sha256=XldX0CTHPXNGrkdQ-gocr4kQGBsz-316WcE0ZDRfb48,11076
10
10
  haiku/rag/monitor.py,sha256=VP3bqY0mEodOP60eN4RMldgrL1ti5gMjuDuQ-_vBvFc,2759
11
11
  haiku/rag/reader.py,sha256=aW8LG0X31kVWS7kU2tKVpe8RqP3Ne_oIidd_X3UDLH0,3307
12
12
  haiku/rag/utils.py,sha256=dBzhKaOHI9KRiJqHErcXUnqtnXY2AgOK8PCLA3rhO0A,6115
@@ -26,8 +26,8 @@ haiku/rag/graph/nodes/analysis.py,sha256=g-Aw3nPuCHWo0CXM96Ixa4vQI4TpI6tg6ooHT_J
26
26
  haiku/rag/graph/nodes/plan.py,sha256=Bb6Fva9vwArCU-5xBr24N4pM3wfLP-Vwufgss8HfXMQ,2622
27
27
  haiku/rag/graph/nodes/search.py,sha256=DdHhEY7fmWUqis6Nk0bj-di56-ML262B51N9zytzKYk,3699
28
28
  haiku/rag/graph/nodes/synthesize.py,sha256=WF0D44SwLP1OK8C6ViOAhFOtGQ0mj3aO54z5bemJb4E,1828
29
- haiku/rag/qa/__init__.py,sha256=Sl7Kzrg9CuBOcMF01wc1NtQhUNWjJI0MhIHfCWrb8V4,434
30
- haiku/rag/qa/agent.py,sha256=rtUkEmnD8lMHIxpPPVY6TdmF4aSlZnLjad5eDefrlBw,3145
29
+ haiku/rag/qa/__init__.py,sha256=eFRV5GFwe1UsqniEqOLdzAMT2J6QhSiHq5_Li7c6Fs4,520
30
+ haiku/rag/qa/agent.py,sha256=A4FrzoYP4pRzJOOJQGlNFp48yRWMSICH4d8JfxFabqk,3256
31
31
  haiku/rag/qa/prompts.py,sha256=Lqwn3m4zCsu_CJiC4s9cLsuPNbb9nq6j2PqEF3lw1eA,3380
32
32
  haiku/rag/qa/deep/__init__.py,sha256=SnCpWxWip-TaFzVKlFyrOgYeXEqT_gpIlaSItEEJ6r0,50
33
33
  haiku/rag/qa/deep/dependencies.py,sha256=AKFqcC1D3N1VPudnFmLH29K5eJWEC5wtwUGkO4FM4jc,998
@@ -50,19 +50,19 @@ haiku/rag/research/prompts.py,sha256=opz4MXjoDHH1wjG6bPyiqT0LVzk3pBA6y_a9zpBW8yM
50
50
  haiku/rag/research/state.py,sha256=P8RXJMi3wA3l1j6yo8dsAyso6S27FgqS7fvZUUY447A,917
51
51
  haiku/rag/research/stream.py,sha256=amyGDimkNp_FHYUXCqtpbeDOx7sC1jQ-7DwoxuNOL1g,5576
52
52
  haiku/rag/store/__init__.py,sha256=R2IRcxtkFDxqa2sgMirqLq3l2-FPdWr6ydYStaqm5OQ,104
53
- haiku/rag/store/engine.py,sha256=i2t15j1hhEY0SMNXk2uUSeCio-mHnTpnN5S83afVx6o,10257
53
+ haiku/rag/store/engine.py,sha256=n2IxztyN2UpLLSUVXurjL-e_ANthKUpWyB1gdHfgBMM,11468
54
54
  haiku/rag/store/models/__init__.py,sha256=kc7Ctf53Jr483tk4QTIrcgqBbXDz4ZoeYSkFXfPnpks,89
55
55
  haiku/rag/store/models/chunk.py,sha256=3EuZav4QekJIeHBCub48EM8SjNX8HEJ6wVDXGot4PEQ,421
56
56
  haiku/rag/store/models/document.py,sha256=cZXy_jEti-hnhq7FKhuhCfd99ccY9fIHMLovB_Thbb8,425
57
57
  haiku/rag/store/repositories/__init__.py,sha256=Olv5dLfBQINRV3HrsfUpjzkZ7Qm7goEYyMNykgo_DaY,291
58
- haiku/rag/store/repositories/chunk.py,sha256=UfajEWf5VmMuSozGRDlWBjJNR0ngvOVFDrp6_augzBg,15217
59
- haiku/rag/store/repositories/document.py,sha256=C9GbIl8sa2-Djaml4hlaPTtjV2HwHaz_Wzs35sdbdhg,7876
58
+ haiku/rag/store/repositories/chunk.py,sha256=B0CowrBNy0fd8GLnVJVfqDaLoWxEPPJK3SODya0I0OI,14093
59
+ haiku/rag/store/repositories/document.py,sha256=EtgD5pDjghXf6dloBOOEVJp8DI9O_celc_FbYzOywAE,8125
60
60
  haiku/rag/store/repositories/settings.py,sha256=ObrDrzxHn-yA1WcbgIoJoVmAbVvQHAFvEdRyJFt5Opc,5685
61
61
  haiku/rag/store/upgrades/__init__.py,sha256=RQ8A6rEXBASLb5PD9vdDnEas_m_GgRzzdVu4B88Snqc,1975
62
62
  haiku/rag/store/upgrades/v0_10_1.py,sha256=qNGnxj6hoHaHJ1rKTiALfw0c9NQOi0KAK-VZCD_073A,1959
63
63
  haiku/rag/store/upgrades/v0_9_3.py,sha256=NrjNilQSgDtFWRbL3ZUtzQzJ8tf9u0dDRJtnDFwwbdw,3322
64
- haiku_rag-0.11.3.dist-info/METADATA,sha256=kohzziLYTajCvyfMxnJkaY7XeEfqPnmVuC_20NpkeLA,6748
65
- haiku_rag-0.11.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
66
- haiku_rag-0.11.3.dist-info/entry_points.txt,sha256=G1U3nAkNd5YDYd4v0tuYFbriz0i-JheCsFuT9kIoGCI,48
67
- haiku_rag-0.11.3.dist-info/licenses/LICENSE,sha256=eXZrWjSk9PwYFNK9yUczl3oPl95Z4V9UXH7bPN46iPo,1065
68
- haiku_rag-0.11.3.dist-info/RECORD,,
64
+ haiku_rag-0.11.4.dist-info/METADATA,sha256=YA7Fr6OnWYeOH139aZkLRP9Yj0S0KXEayzfKsgFaz08,6748
65
+ haiku_rag-0.11.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
66
+ haiku_rag-0.11.4.dist-info/entry_points.txt,sha256=G1U3nAkNd5YDYd4v0tuYFbriz0i-JheCsFuT9kIoGCI,48
67
+ haiku_rag-0.11.4.dist-info/licenses/LICENSE,sha256=eXZrWjSk9PwYFNK9yUczl3oPl95Z4V9UXH7bPN46iPo,1065
68
+ haiku_rag-0.11.4.dist-info/RECORD,,