Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Support Text to BaseModel (#1060)

Xukun Liu729728c7eac0
diff --git a/camel/schemas/__init__.py b/camel/schemas/__init__.py
new file mode 100644
index 000000000..dacbaaeb5
--- /dev/null
+++ b/camel/schemas/__init__.py
@@ -0,0 +1,29 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+from .openai_converter import OpenAISchemaConverter
+
+__all__ = ["OpenAISchemaConverter"]
diff --git a/camel/schemas/base.py b/camel/schemas/base.py
new file mode 100644
index 000000000..d3ca113aa
--- /dev/null
+++ b/camel/schemas/base.py
@@ -0,0 +1,57 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+from abc import ABC, abstractmethod
+from typing import Any, Dict
+
+from pydantic import BaseModel
+
+
+class BaseConverter(ABC):
+ r"""A base class for schema outputs that includes functionality
+ for managing the response format.
+
+ Args:
+ output_schema (Optional[Type[BaseModel]], optional): The expected
+ format of the response. (default: :obj:`None`)
+ """
+
+ @abstractmethod
+ def convert(
+ self, content: str, *args: Any, **kwargs: Dict[str, Any]
+ ) -> BaseModel:
+ r"""Structures the input text into the expected response format.
+
+ Args:
+ text (str): The input text to be structured.
+ output_schema (Optional[Type[BaseModel]], optional):
+ The expected format of the response. Defaults to None.
+ prompt (Optional[str], optional): The prompt to be used.
+
+ Returns:
+ Optional[BaseModel]: The structured response.
+ """
+ pass
diff --git a/camel/schemas/openai_converter.py b/camel/schemas/openai_converter.py
new file mode 100644
index 000000000..8c80a675d
--- /dev/null
+++ b/camel/schemas/openai_converter.py
@@ -0,0 +1,129 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+import os
+from typing import Any, Callable, Dict, Optional, Type, Union
+
+from pydantic import BaseModel
+
+from camel.models import ModelFactory
+from camel.types import ModelType
+from camel.types.enums import ModelPlatformType
+from camel.utils import (
+ api_keys_required,
+ get_pydantic_model,
+)
+
+from .base import BaseConverter
+
+DEFAULT_CONVERTER_PROMPTS = """
+ Extract key entities and attributes from the provided text,
+ and convert them into a structured JSON format.
+"""
+
+
+class OpenAISchemaConverter(BaseConverter):
+ r"""OpenAISchemaConverter is a class that
+ converts a string or a function into a BaseModel schema.
+
+ Args:
+ model_type (ModelType, optional): The model type to be used.
+ (default: ModelType.GPT_4O_MINI)
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`. If
+ :obj:`None`, :obj:`ChatGPTConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating
+ with the OpenAI service. (default: :obj:`None`)
+ output_schema (Optional[Type[BaseModel]], optional): The expected
+ format of the response. (default: :obj:`None`)
+ prompt (Optional[str], optional): The prompt to be used.
+ (default: :obj:`None`)
+
+ """
+
+ def __init__(
+ self,
+ model_type: ModelType = ModelType.GPT_4O_MINI,
+ model_config_dict: Optional[Dict[str, Any]] = None,
+ api_key: Optional[str] = None,
+ ):
+ self.model_type = model_type
+ self.model_config_dict = model_config_dict or {}
+ api_key = api_key or os.environ.get("OPENAI_API_KEY")
+ self._client = ModelFactory.create( # type: ignore[attr-defined]
+ ModelPlatformType.OPENAI,
+ model_type,
+ api_key=api_key,
+ )._client
+ super().__init__()
+
+ @api_keys_required("OPENAI_API_KEY")
+ def convert( # type: ignore[override]
+ self,
+ content: str,
+ output_schema: Union[Type[BaseModel], str, Callable],
+ prompt: Optional[str] = DEFAULT_CONVERTER_PROMPTS,
+ ) -> BaseModel:
+ r"""Formats the input content into the expected BaseModel
+
+ Args:
+ content (str): The content to be formatted.
+ output_schema (Union[Type[BaseModel], str, Callable]): The expected
+ format of the response.
+
+ Returns:
+ BaseModel: The formatted response.
+ """
+ prompt = prompt or DEFAULT_CONVERTER_PROMPTS
+ if output_schema is None:
+ raise ValueError("Expected an output schema, got None.")
+ if not isinstance(output_schema, type):
+ output_schema = get_pydantic_model(output_schema)
+ elif not issubclass(output_schema, BaseModel):
+ raise ValueError(
+ f"Expected a BaseModel, got {type(output_schema)}"
+ )
+
+ self.model_config_dict["response_format"] = output_schema
+ response = self._client.beta.chat.completions.parse(
+ messages=[
+ {'role': 'system', 'content': prompt},
+ {'role': 'user', 'content': content},
+ ],
+ model=self.model_type,
+ **self.model_config_dict,
+ )
+
+ message = response.choices[0].message
+
+ if not isinstance(message.parsed, output_schema):
+ raise ValueError(
+ f"Expected a {output_schema}, got {type(message.parsed)}."
+ )
+
+ return message.parsed
diff --git a/camel/utils/__init__.py b/camel/utils/__init__.py
index e90d98cec..0e3342d49 100644
--- a/camel/utils/__init__.py
+++ b/camel/utils/__init__.py
@@ -36,6 +36,7 @@ from .commons import (
track_agent,
)
from .constants import Constants
+from .response_format import get_pydantic_model
from .token_counting import (
AnthropicTokenCounter,
BaseTokenCounter,
@@ -47,33 +48,34 @@ from .token_counting import (
)
__all__ = [
- 'print_text_animated',
- 'get_prompt_template_key_words',
- 'get_first_int',
- 'download_tasks',
- 'get_task_list',
- 'check_server_running',
- 'AnthropicTokenCounter',
- 'get_system_information',
- 'to_pascal',
- 'get_model_encoding',
- 'BaseTokenCounter',
- 'OpenAITokenCounter',
- 'LiteLLMTokenCounter',
- 'Constants',
- 'text_extract_from_web',
- 'create_chunks',
- 'dependencies_required',
- 'api_keys_required',
- 'is_docker_running',
- 'GeminiTokenCounter',
- 'MistralTokenCounter',
- 'get_pydantic_major_version',
- 'get_pydantic_object_schema',
- 'func_string_to_callable',
- 'json_to_function_code',
- 'agentops_decorator',
- 'AgentOpsMeta',
- 'track_agent',
- 'handle_http_error',
+ "print_text_animated",
+ "get_prompt_template_key_words",
+ "get_first_int",
+ "download_tasks",
+ "get_task_list",
+ "check_server_running",
+ "AnthropicTokenCounter",
+ "get_system_information",
+ "to_pascal",
+ "get_model_encoding",
+ "BaseTokenCounter",
+ "OpenAITokenCounter",
+ "LiteLLMTokenCounter",
+ "Constants",
+ "text_extract_from_web",
+ "create_chunks",
+ "dependencies_required",
+ "api_keys_required",
+ "is_docker_running",
+ "GeminiTokenCounter",
+ "MistralTokenCounter",
+ "get_pydantic_major_version",
+ "get_pydantic_object_schema",
+ "func_string_to_callable",
+ "json_to_function_code",
+ "agentops_decorator",
+ "AgentOpsMeta",
+ "track_agent",
+ "handle_http_error",
+ "get_pydantic_model",
]
diff --git a/camel/utils/response_format.py b/camel/utils/response_format.py
new file mode 100644
index 000000000..c9014c9dd
--- /dev/null
+++ b/camel/utils/response_format.py
@@ -0,0 +1,76 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from __future__ import annotations
+
+import inspect
+import json
+from typing import Callable, Type, Union
+
+from pydantic import BaseModel, create_model
+
+
+def get_pydantic_model(
+ input_data: Union[str, Type[BaseModel], Callable],
+) -> Type[BaseModel]:
+ r"""A multi-purpose function that can be used as a normal function,
+ a class decorator, or a function decorator.
+
+ Args:
+ input_data (Union[str, type, Callable]):
+ - If a string is provided, it should be a JSON-encoded string
+ that will be converted into a BaseModel.
+ - If a function is provided, it will be decorated such that
+ its arguments are converted into a BaseModel.
+ - If a BaseModel class is provided, it will be returned directly.
+
+ Returns:
+ Type[BaseModel]: The BaseModel class that will be used to
+ structure the input data.
+ """
+ if isinstance(input_data, str):
+ data_dict = json.loads(input_data)
+ TemporaryModel = create_model( # type: ignore[call-overload]
+ "TemporaryModel",
+ **{key: (type(value), None) for key, value in data_dict.items()},
+ )
+ return TemporaryModel(**data_dict).__class__
+
+ elif callable(input_data):
+ WrapperClass = create_model( # type: ignore[call-overload]
+ f"{input_data.__name__.capitalize()}Model",
+ **{
+ name: (param.annotation, ...)
+ for name, param in inspect.signature(
+ input_data
+ ).parameters.items()
+ },
+ )
+ return WrapperClass
+ if issubclass(input_data, BaseModel):
+ return input_data
+ raise ValueError("Invalid input data provided.")
diff --git a/examples/schema_outputs/openai_converter_example.py b/examples/schema_outputs/openai_converter_example.py
new file mode 100644
index 000000000..6e3da9630
--- /dev/null
+++ b/examples/schema_outputs/openai_converter_example.py
@@ -0,0 +1,74 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from pydantic import BaseModel
+
+from camel.schemas import OpenAISchemaConverter
+
+
+def get_temperature(location: str, date: str, temperature: float):
+ print(f"Temperature in {location} on {date} is {temperature} degrees.")
+
+
+class Temperature(BaseModel):
+ location: str
+ date: str
+ temperature: float
+
+
+temperature_template = (
+ '{"location": "Beijing", "date": "2023-09-01", "temperature": 30.0}'
+)
+
+
+model = OpenAISchemaConverter()
+
+print(
+ model.convert(
+ "Today is 2023-09-01, the temperature in Beijing is 30 degrees.",
+ output_schema=temperature_template,
+ )
+)
+
+print(
+ model.convert(
+ "Today is 2023-09-01, the temperature in Beijing is 30 degrees.",
+ output_schema=get_temperature,
+ )
+)
+
+print(
+ model.convert(
+ "Today is 2023-09-01, the temperature in Beijing is 30 degrees.",
+ output_schema=Temperature,
+ )
+)
+"""
+location='Beijing' date='2023-09-01' temperature=30.0
+location='Beijing' date='2023-09-01' temperature=30.0
+location='Beijing' date='2023-09-01' temperature=30.0
+"""
diff --git a/test/schema_outputs/test_openai_converter.py b/test/schema_outputs/test_openai_converter.py
new file mode 100644
index 000000000..1b3b21ea3
--- /dev/null
+++ b/test/schema_outputs/test_openai_converter.py
@@ -0,0 +1,89 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+# Licensed under the Apache License, Version 2.0 (the ā€œLicenseā€);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an ā€œAS ISā€ BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from pydantic import BaseModel
+
+from camel.schemas import OpenAISchemaConverter
+
+
+class Temperature(BaseModel):
+ location: str
+ date: str
+ temperature: float
+
+
+def get_temperature(location: str, date: str, temperature: float):
+ print(f"Temperature in {location} on {date} is {temperature} degrees.")
+
+
+def test_openai_converter_with_str_template():
+ temperature_template = (
+ '{"location": "Beijing", "date": "2023-09-01", "temperature": 30.0}'
+ )
+
+ model = OpenAISchemaConverter()
+
+ structured_output = model.convert(
+ "Today is 2023-09-01, the temperature in Beijing is 30 degrees.",
+ temperature_template,
+ )
+
+ assert structured_output.model_dump() == {
+ "location": "Beijing",
+ "date": "2023-09-01",
+ "temperature": 30.0,
+ }
+
+
+def test_openai_converter_with_function():
+ model = OpenAISchemaConverter()
+
+ structured_output = model.convert(
+ "Today is 2023-09-01, the temperature in Beijing is 30 degrees.",
+ get_temperature,
+ )
+
+ assert structured_output.model_dump() == {
+ "location": "Beijing",
+ "date": "2023-09-01",
+ "temperature": 30.0,
+ }
+
+
+def test_openai_converter_with_model():
+ model = OpenAISchemaConverter()
+
+ structured_output = model.convert(
+ "Today is 2023-09-01, the temperature in Beijing is 30 degrees.",
+ Temperature,
+ )
+
+ assert structured_output.model_dump() == {
+ "location": "Beijing",
+ "date": "2023-09-01",
+ "temperature": 30.0,
+ }