Skip to content

Commit d4002c6

Browse files
authored
Fix ollama (#2351)
## Description - **Summary of changes**: Describe the key changes in this PR and their purpose. - **Related issues**: Mention if this PR fixes or is connected to any issues. - **Motivation and context**: Explain the reason for the changes and the problem they solve. - **Environment or dependencies**: Specify any changes in dependencies or environment configurations required for this update. - **Impact on metrics**: (If applicable) Describe changes in any metrics or performance benchmarks. Fixes #2349 --- ## Type of change Please check the options that are relevant: - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Model update (Addition or modification of models) - [ ] Other (please describe): --- ## Checklist - [ ] Adherence to standards: Code complies with Agno’s style guidelines and best practices. - [ ] Formatting and validation: You have run `./scripts/format.sh` and `./scripts/validate.sh` to ensure code is formatted and linted. - [ ] Self-review completed: A thorough review has been performed by the contributor(s). - [ ] Documentation: Docstrings and comments have been added or updated for any complex logic. - [ ] Examples and guides: Relevant cookbook examples have been included or updated (if applicable). - [x] Tested in a clean environment: Changes have been tested in a clean environment to confirm expected behavior. - [ ] Tests (optional): Tests have been added or updated to cover any new or changed functionality. --- ## Additional Notes Include any deployment notes, performance implications, security considerations, or other relevant information (e.g., screenshots or logs if applicable).
1 parent 067fb4e commit d4002c6

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

libs/agno/agno/embedder/ollama.py

+35-14
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@
55
from agno.utils.log import logger
66

77
try:
8-
import pkg_resources
8+
from ollama import Client as OllamaClient
9+
import importlib.metadata as metadata
910
from packaging import version
1011

11-
ollama_version = pkg_resources.get_distribution("ollama").version
12-
if version.parse(ollama_version).major == 0 and version.parse(ollama_version).minor < 3:
12+
# Get installed Ollama version
13+
ollama_version = metadata.version("ollama")
14+
15+
# Check version compatibility (requires v0.3.x or higher)
16+
parsed_version = version.parse(ollama_version)
17+
if parsed_version.major == 0 and parsed_version.minor < 3:
1318
import warnings
19+
warnings.warn("Only Ollama v0.3.x and above are supported", UserWarning)
20+
raise RuntimeError("Incompatible Ollama version detected")
1421

15-
warnings.warn(
16-
"We only support Ollama v0.3.x and above.",
17-
UserWarning,
18-
)
19-
raise RuntimeError("Incompatible Ollama version detected. Execution halted.")
22+
except ImportError as e:
23+
# Handle different import error scenarios
24+
if "ollama" in str(e):
25+
raise ImportError(
26+
"Ollama not installed. Install with `pip install ollama`"
27+
) from e
28+
else:
29+
raise ImportError(
30+
"Missing dependencies. Install with `pip install packaging importlib-metadata`"
31+
) from e
2032

21-
from ollama import Client as OllamaClient
22-
except (ModuleNotFoundError, ImportError):
23-
raise ImportError("`ollama` not installed. Please install using `pip install ollama`")
33+
except Exception as e:
34+
# Catch-all for unexpected errors
35+
print(f"An unexpected error occurred: {e}")
2436

2537

2638
@dataclass
@@ -53,14 +65,23 @@ def _response(self, text: str) -> Dict[str, Any]:
5365
if self.options is not None:
5466
kwargs["options"] = self.options
5567

56-
return self.client.embed(input=text, model=self.id, **kwargs) # type: ignore
68+
response = self.client.embed(input=text, model=self.id, **kwargs)
69+
if response and "embeddings" in response:
70+
embeddings = response["embeddings"]
71+
if isinstance(embeddings, list) and len(embeddings) > 0 and isinstance(embeddings[0], list):
72+
return {"embeddings": embeddings[0]} # Use the first element
73+
elif isinstance(embeddings, list) and all(isinstance(x, (int, float)) for x in embeddings):
74+
return {"embeddings": embeddings} # Return as-is if already flat
75+
return {"embeddings": []} # Return an empty list if no valid embedding is found
5776

5877
def get_embedding(self, text: str) -> List[float]:
5978
try:
6079
response = self._response(text=text)
61-
if response is None:
80+
embedding = response.get("embeddings", [])
81+
if len(embedding) != self.dimensions:
82+
logger.warning(f"Expected embedding dimension {self.dimensions}, but got {len(embedding)}")
6283
return []
63-
return response.get("embeddings", [])
84+
return embedding
6485
except Exception as e:
6586
logger.warning(e)
6687
return []

0 commit comments

Comments
 (0)