mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-03 00:02:36 +00:00
Address GitHub review feedback: add URL validation, helper functions, and edge case tests
- Add _validate_url() helper function with proper URL validation using urllib.parse - Add _construct_embeddings_url() helper function to refactor URL construction logic - Add comprehensive error handling with ValueError for invalid URLs - Fix test mocking to use correct chromadb import path - Add edge case tests for invalid URLs with pytest markers - Organize tests with @pytest.mark.url_configuration and @pytest.mark.error_handling - Remove unused imports (pytest, MagicMock) to fix lint issues This addresses all suggestions from joaomdmoura's AI review while maintaining backward compatibility. Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -91,6 +91,29 @@ class EmbeddingConfigurator:
|
||||
organization_id=config.get("organization_id"),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _validate_url(url):
|
||||
"""Validate that a URL is properly formatted."""
|
||||
if not url:
|
||||
return False
|
||||
|
||||
from urllib.parse import urlparse
|
||||
|
||||
try:
|
||||
result = urlparse(url)
|
||||
return all([result.scheme, result.netloc])
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _construct_embeddings_url(base_url):
|
||||
"""Construct the full embeddings URL from a base URL."""
|
||||
if not base_url:
|
||||
return "http://localhost:11434/api/embeddings"
|
||||
|
||||
base_url = base_url.rstrip('/')
|
||||
return f"{base_url}/api/embeddings" if not base_url.endswith('/api/embeddings') else base_url
|
||||
|
||||
@staticmethod
|
||||
def _configure_ollama(config, model_name):
|
||||
from chromadb.utils.embedding_functions.ollama_embedding_function import (
|
||||
@@ -103,13 +126,10 @@ class EmbeddingConfigurator:
|
||||
or os.getenv("API_BASE")
|
||||
)
|
||||
|
||||
if url and not url.endswith("/api/embeddings"):
|
||||
if not url.endswith("/"):
|
||||
url += "/"
|
||||
url += "api/embeddings"
|
||||
if url and not EmbeddingConfigurator._validate_url(url):
|
||||
raise ValueError(f"Invalid Ollama API URL: {url}")
|
||||
|
||||
if not url:
|
||||
url = "http://localhost:11434/api/embeddings"
|
||||
url = EmbeddingConfigurator._construct_embeddings_url(url)
|
||||
|
||||
return OllamaEmbeddingFunction(
|
||||
url=url,
|
||||
|
||||
Reference in New Issue
Block a user