Spaces:
Running
Running
axeronis commited on
Commit ·
bdedbed
1
Parent(s): 56e17a1
feat: auto-chunk large texts in knowledge base
Browse files
backend/app/services/vector_store.py
CHANGED
|
@@ -8,6 +8,51 @@ from datetime import datetime
|
|
| 8 |
from app.models.knowledge import KnowledgeBase
|
| 9 |
from app.services.embeddings import get_embedding_service
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class VectorStoreService:
|
| 13 |
"""Service for managing vector embeddings and semantic search."""
|
|
@@ -27,6 +72,7 @@ class VectorStoreService:
|
|
| 27 |
) -> KnowledgeBase:
|
| 28 |
"""
|
| 29 |
Add knowledge to the vector store.
|
|
|
|
| 30 |
|
| 31 |
Args:
|
| 32 |
session: Database session
|
|
@@ -38,9 +84,24 @@ class VectorStoreService:
|
|
| 38 |
tags: Optional comma-separated tags
|
| 39 |
|
| 40 |
Returns:
|
| 41 |
-
Created KnowledgeBase entry
|
| 42 |
"""
|
| 43 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
embedding_vector = self.embedding_service.embed_text(content)
|
| 45 |
|
| 46 |
# Convert to pgvector format (string representation)
|
|
|
|
| 8 |
from app.models.knowledge import KnowledgeBase
|
| 9 |
from app.services.embeddings import get_embedding_service
|
| 10 |
|
| 11 |
+
# OpenAI text-embedding-3-small has 8192 token limit (~32k chars)
|
| 12 |
+
# Use smaller chunks for better semantic search results
|
| 13 |
+
CHUNK_SIZE = 6000 # ~1500 tokens per chunk
|
| 14 |
+
CHUNK_OVERLAP = 500 # Overlap between chunks for context continuity
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def chunk_text(text: str, chunk_size: int = CHUNK_SIZE, overlap: int = CHUNK_OVERLAP) -> List[str]:
|
| 18 |
+
"""
|
| 19 |
+
Split large text into overlapping chunks for embedding.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
text: Text to split
|
| 23 |
+
chunk_size: Max size of each chunk in characters
|
| 24 |
+
overlap: Overlap between consecutive chunks
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
List of text chunks
|
| 28 |
+
"""
|
| 29 |
+
if len(text) <= chunk_size:
|
| 30 |
+
return [text]
|
| 31 |
+
|
| 32 |
+
chunks = []
|
| 33 |
+
start = 0
|
| 34 |
+
|
| 35 |
+
while start < len(text):
|
| 36 |
+
end = start + chunk_size
|
| 37 |
+
|
| 38 |
+
# Try to break at sentence or paragraph boundary
|
| 39 |
+
if end < len(text):
|
| 40 |
+
# Look for sentence ending within last 200 chars
|
| 41 |
+
for sep in ['\n\n', '\n', '. ', '! ', '? ']:
|
| 42 |
+
last_sep = text[max(start, end - 200):end].rfind(sep)
|
| 43 |
+
if last_sep != -1:
|
| 44 |
+
end = max(start, end - 200) + last_sep + len(sep)
|
| 45 |
+
break
|
| 46 |
+
|
| 47 |
+
chunk = text[start:end].strip()
|
| 48 |
+
if chunk:
|
| 49 |
+
chunks.append(chunk)
|
| 50 |
+
|
| 51 |
+
# Move start with overlap
|
| 52 |
+
start = end - overlap if end < len(text) else len(text)
|
| 53 |
+
|
| 54 |
+
return chunks
|
| 55 |
+
|
| 56 |
|
| 57 |
class VectorStoreService:
|
| 58 |
"""Service for managing vector embeddings and semantic search."""
|
|
|
|
| 72 |
) -> KnowledgeBase:
|
| 73 |
"""
|
| 74 |
Add knowledge to the vector store.
|
| 75 |
+
Auto-chunks large texts into multiple entries.
|
| 76 |
|
| 77 |
Args:
|
| 78 |
session: Database session
|
|
|
|
| 84 |
tags: Optional comma-separated tags
|
| 85 |
|
| 86 |
Returns:
|
| 87 |
+
Created KnowledgeBase entry (first one if chunked)
|
| 88 |
"""
|
| 89 |
+
# Auto-chunk large texts
|
| 90 |
+
chunks = chunk_text(content)
|
| 91 |
+
|
| 92 |
+
if len(chunks) > 1:
|
| 93 |
+
# Multiple chunks - use batch add
|
| 94 |
+
entries = await self.add_knowledge_batch(
|
| 95 |
+
session=session,
|
| 96 |
+
tenant_id=tenant_id,
|
| 97 |
+
chunks=chunks,
|
| 98 |
+
source_type=source_type,
|
| 99 |
+
source_file=source_file,
|
| 100 |
+
category=category
|
| 101 |
+
)
|
| 102 |
+
return entries[0] if entries else None
|
| 103 |
+
|
| 104 |
+
# Single chunk - normal add
|
| 105 |
embedding_vector = self.embedding_service.embed_text(content)
|
| 106 |
|
| 107 |
# Convert to pgvector format (string representation)
|