Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/utils/document_vector_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,18 @@ def kmeans_cluster_documents(doc_embeddings: Dict[str, np.ndarray], k: Optional[
doc_ids = list(doc_embeddings.keys())
embeddings_array = np.array([doc_embeddings[doc_id] for doc_id in doc_ids])

# Handle single document case
if len(doc_ids) == 1:
logger.info("Only one document found, skipping clustering")
return {0: doc_ids}

# Determine K value
if k is None:
k = auto_determine_k(embeddings_array)

# Ensure k is not greater than number of documents
k = min(k, len(doc_ids))

logger.info(f"Clustering {len(doc_ids)} documents into {k} clusters")

# Perform K-means clustering
Expand Down
9 changes: 9 additions & 0 deletions frontend/components/ui/markdownRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ export const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({
const code = mmd[1];
return <Diagram key={`mmd-${index}`} code={code} className="my-4" />;
}
// Handle line breaks in text content
if (part.includes('\n')) {
return part.split('\n').map((line, lineIndex) => (
<React.Fragment key={`${index}-${lineIndex}`}>
{line}
{lineIndex < part.split('\n').length - 1 && <br />}
</React.Fragment>
));
}
return part;
})}
</>
Expand Down
13 changes: 13 additions & 0 deletions test/backend/test_document_vector_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ def test_kmeans_cluster_documents_empty(self):
clusters = kmeans_cluster_documents(doc_embeddings)

assert clusters == {}

def test_kmeans_cluster_documents_single(self):
"""Test handling of single document"""
doc_embeddings = {
'doc1': np.array([1.0, 1.0, 1.0])
}
clusters = kmeans_cluster_documents(doc_embeddings)

# Should return single cluster with one document
assert len(clusters) == 1
assert 0 in clusters
assert len(clusters[0]) == 1
assert clusters[0][0] == 'doc1'


class TestExtractRepresentativeChunksSmart:
Expand Down