c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
137 lines
5.0 KiB
Python
137 lines
5.0 KiB
Python
from unittest.mock import Mock, patch
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from cognee.infrastructure.databases.exceptions import EmbeddingException
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ollama_embedding_splits_batch_on_context_window_error():
|
|
with patch(
|
|
"cognee.infrastructure.databases.vector.embeddings.OllamaEmbeddingEngine."
|
|
"OllamaEmbeddingEngine.get_tokenizer",
|
|
return_value=Mock(),
|
|
):
|
|
from cognee.infrastructure.databases.vector.embeddings.OllamaEmbeddingEngine import (
|
|
OllamaEmbeddingEngine,
|
|
)
|
|
|
|
engine = OllamaEmbeddingEngine(model="test-model", dimensions=2)
|
|
|
|
async def fake_get_embedding(prompt):
|
|
if prompt == "too long":
|
|
raise ValueError("input length exceeds context length")
|
|
# "too long" (8 chars) splits into third=8//3=2 → left="too " (s[:4]), right="o long" (s[2:])
|
|
mapping = {
|
|
"too ": [1.0, 1.0],
|
|
"o long": [2.0, 2.0],
|
|
"ok 1": [1.0, 1.0],
|
|
"ok 2": [2.0, 2.0],
|
|
}
|
|
return mapping[prompt]
|
|
|
|
with patch.object(engine, "_get_embedding", side_effect=fake_get_embedding):
|
|
result = await engine.embed_text(["too long", "ok 1", "ok 2"])
|
|
|
|
# "too long" → pooled([1.0,1.0], [2.0,2.0]) = [1.5,1.5]; others pass through
|
|
assert result == [[1.5, 1.5], [1.0, 1.0], [2.0, 2.0]]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ollama_embedding_raises_when_short_text_still_exceeds_context_window():
|
|
with patch(
|
|
"cognee.infrastructure.databases.vector.embeddings.OllamaEmbeddingEngine."
|
|
"OllamaEmbeddingEngine.get_tokenizer",
|
|
return_value=Mock(),
|
|
):
|
|
from cognee.infrastructure.databases.vector.embeddings.OllamaEmbeddingEngine import (
|
|
OllamaEmbeddingEngine,
|
|
)
|
|
|
|
engine = OllamaEmbeddingEngine(model="test-model", dimensions=2)
|
|
|
|
async def always_fail(_prompt):
|
|
raise ValueError("maximum context length exceeded")
|
|
|
|
with patch.object(engine, "_get_embedding", side_effect=always_fail):
|
|
with pytest.raises(EmbeddingException, match="too short to split further"):
|
|
await engine.embed_text(["ab"])
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fastembed_splits_batch_on_context_window_error():
|
|
pytest.importorskip("fastembed")
|
|
|
|
with (
|
|
patch(
|
|
"cognee.infrastructure.databases.vector.embeddings.FastembedEmbeddingEngine."
|
|
"FastembedEmbeddingEngine.get_tokenizer",
|
|
return_value=Mock(),
|
|
),
|
|
patch(
|
|
"cognee.infrastructure.databases.vector.embeddings.FastembedEmbeddingEngine."
|
|
"TextEmbedding",
|
|
) as mock_text_embedding,
|
|
):
|
|
embedding_model = Mock()
|
|
mock_text_embedding.return_value = embedding_model
|
|
|
|
def fake_embed(inputs, batch_size, parallel):
|
|
if inputs == ["too long", "ok 1", "ok 2"]:
|
|
raise RuntimeError("input length exceeds context window")
|
|
if inputs == ["too long", "ok 1"]:
|
|
raise RuntimeError("maximum tokens exceeded")
|
|
if inputs == ["too long"]:
|
|
raise RuntimeError("context length exceeded")
|
|
# "too long" (8 chars) splits into third=8//3=2 → left="too " (s[:4]), right="o long" (s[2:])
|
|
if inputs == ["too "]:
|
|
return iter([np.array([1.0, 1.0])])
|
|
if inputs == ["o long"]:
|
|
return iter([np.array([2.0, 2.0])])
|
|
if inputs == ["ok 1"]:
|
|
return iter([np.array([3.0, 3.0])])
|
|
if inputs == ["ok 2"]:
|
|
return iter([np.array([4.0, 4.0])])
|
|
raise AssertionError(f"Unexpected inputs: {inputs}")
|
|
|
|
embedding_model.embed.side_effect = fake_embed
|
|
|
|
from cognee.infrastructure.databases.vector.embeddings.FastembedEmbeddingEngine import (
|
|
FastembedEmbeddingEngine,
|
|
)
|
|
|
|
engine = FastembedEmbeddingEngine(model="test-model", dimensions=2)
|
|
result = await engine.embed_text(["too long", "ok 1", "ok 2"])
|
|
|
|
assert result == [[1.5, 1.5], [3.0, 3.0], [4.0, 4.0]]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fastembed_raises_when_short_text_still_exceeds_context_window():
|
|
pytest.importorskip("fastembed")
|
|
|
|
with (
|
|
patch(
|
|
"cognee.infrastructure.databases.vector.embeddings.FastembedEmbeddingEngine."
|
|
"FastembedEmbeddingEngine.get_tokenizer",
|
|
return_value=Mock(),
|
|
),
|
|
patch(
|
|
"cognee.infrastructure.databases.vector.embeddings.FastembedEmbeddingEngine."
|
|
"TextEmbedding",
|
|
) as mock_text_embedding,
|
|
):
|
|
embedding_model = Mock()
|
|
embedding_model.embed.side_effect = RuntimeError("context window exceeded")
|
|
mock_text_embedding.return_value = embedding_model
|
|
|
|
from cognee.infrastructure.databases.vector.embeddings.FastembedEmbeddingEngine import (
|
|
FastembedEmbeddingEngine,
|
|
)
|
|
|
|
engine = FastembedEmbeddingEngine(model="test-model", dimensions=2)
|
|
|
|
with pytest.raises(EmbeddingException, match="too short to split further"):
|
|
await engine.embed_text(["ab"])
|