refactor: implement review suggestions

- Extract model conversion logic to _get_llm_instance helper method
- Improve error message clarity
- Simplify LLM instance creation in CrewEvaluator

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-02-09 22:52:13 +00:00
parent 0ab66041da
commit f4efdc55e2
2 changed files with 18 additions and 9 deletions

View File

@@ -1161,14 +1161,26 @@ class Crew(BaseModel):
inputs: Optional dictionary of inputs to pass to the crew
"""
if not llm and not openai_model_name:
raise ValueError("Either llm or openai_model_name must be provided")
model_to_use: Union[str, LLM] = llm if llm is not None else openai_model_name
if isinstance(model_to_use, str):
model_to_use = LLM(model=model_to_use)
raise ValueError("Must provide either 'llm' or 'openai_model_name' parameter")
model_to_use = self._get_llm_instance(llm, openai_model_name)
test_crew = self.copy()
def _get_llm_instance(self, llm: Optional[Union[str, LLM]], openai_model_name: Optional[str]) -> LLM:
"""Get an LLM instance from either llm or openai_model_name parameter.
Args:
llm: LLM instance or model name
openai_model_name: OpenAI model name (deprecated)
Returns:
LLM instance
"""
model = llm if llm is not None else openai_model_name
if isinstance(model, str):
return LLM(model=model)
return model
self._test_execution_span = test_crew._telemetry.test_execution_span(
test_crew,
n_iterations,

View File

@@ -36,10 +36,7 @@ class CrewEvaluator:
def __init__(self, crew, llm: Union[str, LLM]):
self.crew = crew
if isinstance(llm, str):
self.llm = LLM(model=llm)
else:
self.llm = llm
self.llm = LLM(model=llm) if isinstance(llm, str) else llm
self._telemetry = Telemetry()
self._setup_for_evaluating()