mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-21 22:08:21 +00:00
Checking supports_function_calling isntead of gpt models
This commit is contained in:
@@ -39,9 +39,3 @@ class OutputConverter(BaseModel, ABC):
|
||||
def to_json(self, current_attempt=1):
|
||||
"""Convert text to json."""
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def is_gpt(self) -> bool:
|
||||
"""Return if llm provided is of gpt from openai."""
|
||||
pass
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
import logging
|
||||
import litellm
|
||||
from litellm import get_supported_openai_params
|
||||
|
||||
|
||||
class LLM:
|
||||
@@ -85,3 +86,11 @@ class LLM:
|
||||
except Exception as e:
|
||||
logging.error(f"LiteLLM call failed: {str(e)}")
|
||||
raise # Re-raise the exception after logging
|
||||
|
||||
def supports_function_calling(self) -> bool:
|
||||
try:
|
||||
params = get_supported_openai_params(model=self.model)
|
||||
return "response_format" in params
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to get supported params: {str(e)}")
|
||||
return False
|
||||
|
||||
@@ -73,7 +73,6 @@ class ToolUsage:
|
||||
# Set the maximum parsing attempts for bigger models
|
||||
if (
|
||||
self.function_calling_llm
|
||||
and self._is_gpt(self.function_calling_llm)
|
||||
and self.function_calling_llm in OPENAI_BIGGER_MODELS
|
||||
):
|
||||
self._max_parsing_attempts = 2
|
||||
@@ -299,13 +298,6 @@ class ToolUsage:
|
||||
)
|
||||
return "\n--\n".join(descriptions)
|
||||
|
||||
def _is_gpt(self, llm) -> bool:
|
||||
return (
|
||||
"gpt" in str(llm.model).lower()
|
||||
or "o1-preview" in str(llm.model).lower()
|
||||
or "o1-mini" in str(llm.model).lower()
|
||||
)
|
||||
|
||||
def _tool_calling(
|
||||
self, tool_string: str
|
||||
) -> Union[ToolCalling, InstructorToolCalling]:
|
||||
@@ -314,13 +306,9 @@ class ToolUsage:
|
||||
print("self.function_calling_llm")
|
||||
model = (
|
||||
InstructorToolCalling
|
||||
if self._is_gpt(self.function_calling_llm)
|
||||
if self.function_calling_llm.supports_function_calling()
|
||||
else ToolCalling
|
||||
)
|
||||
print("model", model)
|
||||
print(
|
||||
"self.function_calling_llm.model", self.function_calling_llm.model
|
||||
)
|
||||
converter = Converter(
|
||||
text=f"Only tools available:\n###\n{self._render()}\n\nReturn a valid schema for the tool, the tool name must be exactly equal one of the options, use this text to inform the valid output schema:\n\n### TEXT \n{tool_string}",
|
||||
llm=self.function_calling_llm,
|
||||
|
||||
@@ -2,7 +2,6 @@ import json
|
||||
import re
|
||||
from typing import Any, Optional, Type, Union
|
||||
|
||||
from crewai.llm import LLM
|
||||
from pydantic import BaseModel, ValidationError
|
||||
|
||||
from crewai.agents.agent_builder.utilities.base_output_converter import OutputConverter
|
||||
@@ -24,7 +23,7 @@ class Converter(OutputConverter):
|
||||
def to_pydantic(self, current_attempt=1):
|
||||
"""Convert text to pydantic."""
|
||||
try:
|
||||
if self.is_gpt:
|
||||
if self.llm.supports_function_calling():
|
||||
return self._create_instructor().to_pydantic()
|
||||
else:
|
||||
return self.llm.call(
|
||||
@@ -43,7 +42,7 @@ class Converter(OutputConverter):
|
||||
def to_json(self, current_attempt=1):
|
||||
"""Convert text to json."""
|
||||
try:
|
||||
if self.is_gpt:
|
||||
if self.llm.supports_function_calling():
|
||||
return self._create_instructor().to_json()
|
||||
else:
|
||||
return json.dumps(
|
||||
@@ -86,15 +85,6 @@ class Converter(OutputConverter):
|
||||
)
|
||||
return parser.parse_result(result)
|
||||
|
||||
@property
|
||||
def is_gpt(self) -> bool:
|
||||
"""Return if llm provided is of gpt from openai."""
|
||||
return (
|
||||
"gpt" in str(self.llm.model).lower()
|
||||
or "o1-preview" in str(self.llm.model).lower()
|
||||
or "o1-mini" in str(self.llm.model).lower()
|
||||
)
|
||||
|
||||
|
||||
def convert_to_model(
|
||||
result: str,
|
||||
@@ -202,21 +192,12 @@ def convert_with_instructions(
|
||||
|
||||
def get_conversion_instructions(model: Type[BaseModel], llm: Any) -> str:
|
||||
instructions = "I'm gonna convert this raw text into valid JSON."
|
||||
if not is_gpt(llm):
|
||||
if llm.supports_function_calling():
|
||||
model_schema = PydanticSchemaParser(model=model).get_schema()
|
||||
instructions = f"{instructions}\n\nThe json should have the following structure, with the following keys:\n{model_schema}"
|
||||
return instructions
|
||||
|
||||
|
||||
def is_gpt(llm: LLM) -> bool:
|
||||
"""Return if llm provided is of gpt from openai."""
|
||||
return (
|
||||
"gpt" in str(llm.model).lower()
|
||||
or "o1-preview" in str(llm.model).lower()
|
||||
or "o1-mini" in str(llm.model).lower()
|
||||
)
|
||||
|
||||
|
||||
def create_converter(
|
||||
agent: Optional[Any] = None,
|
||||
converter_cls: Optional[Type[Converter]] = None,
|
||||
|
||||
@@ -78,7 +78,7 @@ class TaskEvaluator:
|
||||
|
||||
instructions = "Convert all responses into valid JSON output."
|
||||
|
||||
if not self._is_gpt(self.llm):
|
||||
if not self.llm.supports_function_calling():
|
||||
model_schema = PydanticSchemaParser(model=TaskEvaluation).get_schema()
|
||||
instructions = f"{instructions}\n\nReturn only valid JSON with the following schema:\n```json\n{model_schema}\n```"
|
||||
|
||||
@@ -91,13 +91,6 @@ class TaskEvaluator:
|
||||
|
||||
return converter.to_pydantic()
|
||||
|
||||
def _is_gpt(self, llm) -> bool:
|
||||
return (
|
||||
"gpt" in str(self.llm.model).lower()
|
||||
or "o1-preview" in str(self.llm.model).lower()
|
||||
or "o1-mini" in str(self.llm.model).lower()
|
||||
)
|
||||
|
||||
def evaluate_training_data(
|
||||
self, training_data: dict, agent_id: str
|
||||
) -> TrainingTaskEvaluation:
|
||||
@@ -128,7 +121,7 @@ class TaskEvaluator:
|
||||
)
|
||||
instructions = "I'm gonna convert this raw text into valid JSON."
|
||||
|
||||
if not self._is_gpt(self.llm):
|
||||
if not self.llm.supports_function_calling():
|
||||
model_schema = PydanticSchemaParser(
|
||||
model=TrainingTaskEvaluation
|
||||
).get_schema()
|
||||
|
||||
Reference in New Issue
Block a user