Skip to content
Open
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
10 changes: 4 additions & 6 deletions packages/genkit-ollama/src/genkit_ollama/_embedders.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def _get_client(self) -> ollama_api.AsyncClient:
async def embed(self, request: EmbedRequest, client: ollama_api.AsyncClient | None = None) -> EmbedResponse:
"""Generates embeddings for the provided input text.

Converts the input documents from the Genkit EmbedRequest into a raw
list of strings, sends them to the Ollama server for embedding, and then
formats the response into a Genkit EmbedResponse.
Joins the text parts of each Genkit input document into one string,
sends those strings to the Ollama server for embedding, and formats
the response into a Genkit EmbedResponse.

Args:
request: The embedding request containing the input documents.
Expand All @@ -87,9 +87,7 @@ async def embed(self, request: EmbedRequest, client: ollama_api.AsyncClient | No
"""
if client is None:
client = self._get_client()
input_raw: list[str] = []
for doc in request.input:
input_raw.extend([str(content.text) for content in doc.content if content.text is not None])
input_raw = [doc.text for doc in request.input]
try:
response = await client.embed(
model=self.model,
Expand Down
18 changes: 6 additions & 12 deletions packages/genkit-ollama/tests/models/embedders_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def test_embed_single_document_single_content(self) -> None:
self.assertEqual(response, EmbedResponse(embeddings=expected_genkit_embeddings))

async def test_embed_multiple_documents_multiple_content(self) -> None:
"""Test embed with multiple documents, each with multiple text contents."""
"""Test that multipart documents each produce one embedding in input order."""
request = EmbedRequest(
input=[
Document(
Expand All @@ -73,24 +73,18 @@ async def test_embed_multiple_documents_multiple_content(self) -> None:
Document(content=[Part.from_text('doc2_part1')]),
]
)
expected_ollama_embeddings = [[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]]
self.mock_ollama_client_instance.embed.return_value = ollama_api.EmbedResponse(
embeddings=expected_ollama_embeddings
# Ollama returns one embedding for each string it receives.
self.mock_ollama_client_instance.embed.side_effect = lambda **kwargs: ollama_api.EmbedResponse(
embeddings=[[float(index)] for index, _ in enumerate(kwargs['input'])]
)

response = await self.ollama_embedder.embed(request)

# Assertions
self.assertEqual(response, EmbedResponse(embeddings=[Embedding(embedding=[0.0]), Embedding(embedding=[1.0])]))
self.mock_ollama_client_instance.embed.assert_awaited_once_with(
model='test-embed-model',
input=['doc1_part1', 'doc1_part2', 'doc2_part1'],
input=['doc1_part1doc1_part2', 'doc2_part1'],
)
expected_genkit_embeddings = [
Embedding(embedding=[0.1, 0.2]),
Embedding(embedding=[0.3, 0.4]),
Embedding(embedding=[0.5, 0.6]),
]
self.assertEqual(response, EmbedResponse(embeddings=expected_genkit_embeddings))

async def test_embed_empty_input(self) -> None:
"""Test embed with an empty input request."""
Expand Down
Loading