Checking supports_function_calling isntead of gpt models

This commit is contained in:
João Moura
2024-09-23 16:23:38 -03:00
parent 3b6d1838b4
commit 493f046c03
8 changed files with 34 additions and 61 deletions

View File

@@ -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,

View File

@@ -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()