Compare commits

...

1 Commits

Author SHA1 Message Date
Devin AI
819bd0b3b2 Fix #2645: Remove 'models/' prefix from LLM model names
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-04-19 20:20:46 +00:00
2 changed files with 53 additions and 3 deletions

View File

@@ -196,9 +196,11 @@ class Agent(BaseAgent):
else:
# For any other type, attempt to extract relevant attributes
llm_params = {
"model": getattr(self.llm, "model_name", None)
or getattr(self.llm, "deployment_name", None)
or str(self.llm),
"model": self._normalize_model_name(
getattr(self.llm, "model_name", None)
or getattr(self.llm, "deployment_name", None)
or str(self.llm)
),
"temperature": getattr(self.llm, "temperature", None),
"max_tokens": getattr(self.llm, "max_tokens", None),
"logprobs": getattr(self.llm, "logprobs", None),
@@ -534,5 +536,14 @@ class Agent(BaseAgent):
def __tools_names(tools) -> str:
return ", ".join([t.name for t in tools])
def _normalize_model_name(self, model_name):
"""
Normalize the model name by removing any 'models/' prefix.
This fixes the issue with ChatGoogleGenerativeAI and potentially other LLM providers.
"""
if model_name and isinstance(model_name, str) and model_name.startswith("models/"):
return model_name[7:] # Remove "models/" prefix
return model_name
def __repr__(self):
return f"Agent(role={self.role}, goal={self.goal}, backstory={self.backstory})"

View File

@@ -0,0 +1,39 @@
import pytest
from unittest.mock import MagicMock, patch
from crewai import Agent
from crewai.llm import LLM
def test_normalize_model_name_method():
"""Test that the _normalize_model_name method correctly handles model names with 'models/' prefix"""
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
llm="gpt-4"
)
model_with_prefix = "models/gemini/gemini-1.5-flash"
normalized_name = agent._normalize_model_name(model_with_prefix)
assert normalized_name == "gemini/gemini-1.5-flash"
regular_model = "gpt-4"
assert agent._normalize_model_name(regular_model) == "gpt-4"
assert agent._normalize_model_name(None) is None
assert agent._normalize_model_name(123) == 123
def test_agent_with_regular_model_name():
"""Test that the Agent class doesn't modify normal model names"""
with patch('crewai.agent.LLM') as mock_llm:
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
llm="gpt-4"
)
args, kwargs = mock_llm.call_args
assert kwargs["model"] == "gpt-4"