Skip to content

Build and run agents you can see, understand and trust.

feat(ollama): update the ollama embedding API (#874)

qbc5f62604c8c56
diff --git a/pyproject.toml b/pyproject.toml
index abfe2c924..24635a5ce 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -42,7 +42,7 @@ dependencies = [
[project.optional-dependencies]
full = [
# Model
- "ollama",
+ "ollama>=0.5.4",
"google-genai",
"Pillow",
"transformers",
diff --git a/src/agentscope/embedding/_ollama_embedding.py b/src/agentscope/embedding/_ollama_embedding.py
index f5445072b..fc3a94892 100644
--- a/src/agentscope/embedding/_ollama_embedding.py
+++ b/src/agentscope/embedding/_ollama_embedding.py
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""The ollama text embedding model class."""
-import asyncio
from datetime import datetime
from typing import List, Any
@@ -68,6 +67,13 @@ class OllamaTextEmbedding(EmbeddingModelBase):
"Input text must be a list of strings or TextBlock dicts.",
)
+ kwargs = {
+ "input": gather_text,
+ "model": self.model_name,
+ "dimensions": self.dimensions,
+ **kwargs,
+ }
+
if self.embedding_cache:
cached_embeddings = await self.embedding_cache.retrieve(
identifier=kwargs,
@@ -83,22 +89,17 @@ class OllamaTextEmbedding(EmbeddingModelBase):
)
start_time = datetime.now()
- response = await asyncio.gather(
- *[
- self.client.embeddings(self.model_name, _, **kwargs)
- for _ in gather_text
- ],
- )
+ response = await self.client.embed(**kwargs)
time = (datetime.now() - start_time).total_seconds()
if self.embedding_cache:
await self.embedding_cache.store(
identifier=kwargs,
- embeddings=[_.embedding for _ in response],
+ embeddings=response.embeddings,
)
return EmbeddingResponse(
- embeddings=[_.embedding for _ in response],
+ embeddings=response.embeddings,
usage=EmbeddingUsage(
time=time,
),