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: pydantic (#746)

maoquan35e04cebf038
diff --git a/apps/agents/agents.py b/apps/agents/agents.py
index 07ced6d18..253376f3f 100644
--- a/apps/agents/agents.py
+++ b/apps/agents/agents.py
@@ -19,11 +19,11 @@ a chat between collaborative agents.
import argparse
import os
import re
-from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple, Union
import gradio as gr
import openai
+from pydantic import BaseModel, ConfigDict
from apps.agents.text_utils import split_markdown_code
from camel.agents import TaskSpecifyAgent
@@ -38,8 +38,12 @@ REPO_ROOT = os.path.realpath(
ChatBotHistory = List[Tuple[Optional[str], Optional[str]]]
-@dataclass
-class State:
+class State(BaseModel):
+ model_config = ConfigDict(
+ arbitrary_types_allowed=True,
+ extra="forbid",
+ )
+
session: Optional[RolePlaying]
max_messages: int
chat: ChatBotHistory
@@ -47,7 +51,12 @@ class State:
@classmethod
def empty(cls) -> 'State':
- return cls(None, 0, [], None)
+ return cls(
+ session=None,
+ max_messages=0,
+ chat=[],
+ saved_assistant_msg=None,
+ )
@staticmethod
def construct_inplace(
@@ -57,10 +66,17 @@ class State:
chat: ChatBotHistory,
saved_assistant_msg: Optional[BaseMessage],
) -> None:
- state.session = session
- state.max_messages = max_messages
- state.chat = chat
- state.saved_assistant_msg = saved_assistant_msg
+ validated_data = State(
+ session=session,
+ max_messages=max_messages,
+ chat=chat,
+ saved_assistant_msg=saved_assistant_msg,
+ )
+
+ state.session = validated_data.session
+ state.max_messages = validated_data.max_messages
+ state.chat = validated_data.chat
+ state.saved_assistant_msg = validated_data.saved_assistant_msg
def parse_arguments():
@@ -122,7 +138,7 @@ def load_roles(path: str) -> List[str]:
return roles
-def cleanup_on_launch(state) -> Tuple[State, ChatBotHistory, Dict]:
+def cleanup_on_launch(state: State) -> Tuple[State, ChatBotHistory, Dict]:
"""Prepare the UI for a new session.
Args:
@@ -143,7 +159,7 @@ def cleanup_on_launch(state) -> Tuple[State, ChatBotHistory, Dict]:
def role_playing_start(
- state,
+ state: State,
society_name: str,
assistant: str,
user: str,
@@ -254,7 +270,7 @@ def role_playing_start(
def role_playing_chat_init(
- state,
+ state: State,
) -> Union[Dict, Tuple[State, ChatBotHistory, Dict]]:
"""Initialize role playing.
@@ -292,7 +308,9 @@ def role_playing_chat_init(
# WORKAROUND: do not add type hints for session and chatbot_history
-def role_playing_chat_cont(state) -> Tuple[State, ChatBotHistory, Dict, Dict]:
+def role_playing_chat_cont(
+ state: State,
+) -> Tuple[State, ChatBotHistory, Dict, Dict]:
"""Produce a pair of messages by an assistant and a user.
To be run multiple times.
@@ -355,7 +373,7 @@ def role_playing_chat_cont(state) -> Tuple[State, ChatBotHistory, Dict, Dict]:
return state, state.chat, progress_update, start_bn_update
-def stop_session(state) -> Tuple[State, Dict, Dict]:
+def stop_session(state: State) -> Tuple[State, Dict, Dict]:
"""Finish the session and leave chat contents as an artefact.
Args:
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 6a512f138..401387806 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -15,8 +15,16 @@ from __future__ import annotations
import json
from collections import defaultdict
-from dataclasses import dataclass
-from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple
+from typing import (
+ TYPE_CHECKING,
+ Any,
+ Callable,
+ Dict,
+ List,
+ Optional,
+ Tuple,
+ Union,
+)
from pydantic import BaseModel
@@ -31,7 +39,6 @@ from camel.memories import (
from camel.messages import BaseMessage, FunctionCallingMessage, OpenAIMessage
from camel.models import BaseModelBackend, ModelFactory
from camel.responses import ChatAgentResponse
-from camel.toolkits import OpenAIFunction
from camel.types import (
ChatCompletion,
ChatCompletionChunk,
@@ -52,10 +59,10 @@ if TYPE_CHECKING:
from openai import Stream
from camel.terminators import ResponseTerminator
+ from camel.toolkits import OpenAIFunction
-@dataclass(frozen=True)
-class FunctionCallingRecord:
+class FunctionCallingRecord(BaseModel):
r"""Historical records of functions called in the conversation.
Attributes:
@@ -75,13 +82,15 @@ class FunctionCallingRecord:
Returns:
str: Modified string to represent the function calling.
"""
-
return (
f"Function Execution: {self.func_name}\n"
f"\tArgs: {self.args}\n"
f"\tResult: {self.result}"
)
+ def as_dict(self) -> dict[str, Any]:
+ return self.model_dump()
+
class ChatAgent(BaseAgent):
r"""Class for managing conversations of CAMEL Chat Agents.
@@ -136,7 +145,7 @@ class ChatAgent(BaseAgent):
else ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().__dict__,
+ model_config_dict=ChatGPTConfig().model_dump(),
api_key=self._api_key,
)
)
@@ -217,7 +226,9 @@ class ChatAgent(BaseAgent):
messages.
role (OpenAIBackendRole): The backend role type.
"""
- self.memory.write_record(MemoryRecord(message, role))
+ self.memory.write_record(
+ MemoryRecord(message=message, role_at_backend=role)
+ )
def set_output_language(self, output_language: str) -> BaseMessage:
r"""Sets the output language for the system message. This method
@@ -276,7 +287,8 @@ class ChatAgent(BaseAgent):
message.
"""
system_record = MemoryRecord(
- self.system_message, OpenAIBackendRole.SYSTEM
+ message=self.system_message,
+ role_at_backend=OpenAIBackendRole.SYSTEM,
)
self.memory.clear()
self.memory.write_record(system_record)
@@ -323,7 +335,7 @@ class ChatAgent(BaseAgent):
tool_calls: List[FunctionCallingRecord] = []
while True:
# Format messages and get the token number
- openai_messages: list[OpenAIMessage] | None
+ openai_messages: Optional[List[OpenAIMessage]]
try:
openai_messages, num_tokens = self.memory.get_context()
@@ -419,7 +431,9 @@ class ChatAgent(BaseAgent):
for base_message_item in output_messages:
base_message_item.content = str(info['tool_calls'][-1].result)
- return ChatAgentResponse(output_messages, self.terminated, info)
+ return ChatAgentResponse(
+ msgs=output_messages, terminated=self.terminated, info=info
+ )
async def step_async(
self,
@@ -452,7 +466,7 @@ class ChatAgent(BaseAgent):
tool_calls: List[FunctionCallingRecord] = []
while True:
# Format messages and get the token number
- openai_messages: list[OpenAIMessage] | None
+ openai_messages: Optional[List[OpenAIMessage]]
try:
openai_messages, num_tokens = self.memory.get_context()
@@ -543,7 +557,9 @@ class ChatAgent(BaseAgent):
for base_message_item in output_messages:
base_message_item.content = str(info['tool_calls'][0].result)
- return ChatAgentResponse(output_messages, self.terminated, info)
+ return ChatAgentResponse(
+ msgs=output_messages, terminated=self.terminated, info=info
+ )
def _add_tools_for_func_call(
self,
@@ -594,6 +610,7 @@ class ChatAgent(BaseAgent):
output_schema (BaseModel): The schema representing the expected
output structure.
"""
+ from camel.toolkits import OpenAIFunction
# step 1 extract the output_schema info as json.
schema_json = get_pydantic_object_schema(output_schema)
@@ -611,23 +628,23 @@ class ChatAgent(BaseAgent):
if self.model_type.is_openai:
self.model_backend.model_config_dict = ChatGPTConfig(
tools=tools
- ).__dict__
+ ).as_dict()
elif self.model_type.is_gemini:
from camel.configs.gemini_config import GeminiConfig
self.model_backend.model_config_dict = GeminiConfig(
tools=tools
- ).__dict__
+ ).as_dict()
def _step_model_response(
self,
- openai_messages: list[OpenAIMessage],
+ openai_messages: List[OpenAIMessage],
num_tokens: int,
) -> tuple[
- ChatCompletion | Stream[ChatCompletionChunk],
- list[BaseMessage],
- list[str],
- dict[str, int],
+ Union[ChatCompletion, Stream],
+ List[BaseMessage],
+ List[str],
+ Dict[str, int],
str,
]:
r"""Internal function for agent step model response."""
@@ -797,9 +814,9 @@ class ChatAgent(BaseAgent):
)
return ChatAgentResponse(
- output_messages,
- self.terminated,
- info,
+ msgs=output_messages,
+ terminated=self.terminated,
+ info=info,
)
def step_tool_call(
@@ -856,7 +873,9 @@ class ChatAgent(BaseAgent):
)
# Record information about this function call
- func_record = FunctionCallingRecord(func_name, args, result)
+ func_record = FunctionCallingRecord(
+ func_name=func_name, args=args, result=result
+ )
return assist_msg, func_msg, func_record
async def step_tool_call_async(
@@ -915,7 +934,9 @@ class ChatAgent(BaseAgent):
)
# Record information about this function call
- func_record = FunctionCallingRecord(func_name, args, result)
+ func_record = FunctionCallingRecord(
+ func_name=func_name, args=args, result=result
+ )
return assist_msg, func_msg, func_record
def get_usage_dict(
diff --git a/camel/agents/critic_agent.py b/camel/agents/critic_agent.py
index 0a8de5f63..0fcfb4b2c 100644
--- a/camel/agents/critic_agent.py
+++ b/camel/agents/critic_agent.py
@@ -184,4 +184,8 @@ class CriticAgent(ChatAgent):
output_msg = meta_chat_message.create_new_instance(option)
# TODO: The return `info` can be improved.
- return ChatAgentResponse([output_msg], terminated=False, info={})
+ return ChatAgentResponse(
+ msgs=[output_msg],
+ terminated=False,
+ info={},
+ )
diff --git a/camel/agents/embodied_agent.py b/camel/agents/embodied_agent.py
index 88285c288..e5fa3579e 100644
--- a/camel/agents/embodied_agent.py
+++ b/camel/agents/embodied_agent.py
@@ -182,4 +182,8 @@ class EmbodiedAgent(ChatAgent):
input_message.meta_dict,
content,
)
- return ChatAgentResponse([message], response.terminated, response.info)
+ return ChatAgentResponse(
+ msgs=[message],
+ terminated=response.terminated,
+ info=response.info,
+ )
diff --git a/camel/agents/knowledge_graph_agent.py b/camel/agents/knowledge_graph_agent.py
index 53dfed377..514ba99f5 100644
--- a/camel/agents/knowledge_graph_agent.py
+++ b/camel/agents/knowledge_graph_agent.py
@@ -224,7 +224,7 @@ class KnowledgeGraphAgent(ChatAgent):
id, type = match.groups()
properties = {'source': 'agent_created'}
if id not in nodes:
- node = Node(id, type, properties)
+ node = Node(id=id, type=type, properties=properties)
if self._validate_node(node):
nodes[id] = node
@@ -235,8 +235,14 @@ class KnowledgeGraphAgent(ChatAgent):
if subj_id in nodes and obj_id in nodes:
subj = nodes[subj_id]
obj = nodes[obj_id]
- relationship = Relationship(subj, obj, rel_type, properties)
+ relationship = Relationship(
+ subj=subj, obj=obj, type=rel_type, properties=properties
+ )
if self._validate_relationship(relationship):
relationships.append(relationship)
- return GraphElement(list(nodes.values()), relationships, self.element)
+ return GraphElement(
+ nodes=list(nodes.values()),
+ relationships=relationships,
+ source=self.element,
+ )
diff --git a/camel/configs/__init__.py b/camel/configs/__init__.py
index 6fd5fc792..a4eb67185 100644
--- a/camel/configs/__init__.py
+++ b/camel/configs/__init__.py
@@ -13,19 +13,12 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from .anthropic_config import ANTHROPIC_API_PARAMS, AnthropicConfig
from .base_config import BaseConfig
-from .gemini_config import (
- Gemini_API_PARAMS,
- GeminiConfig,
-)
+from .gemini_config import Gemini_API_PARAMS, GeminiConfig
from .groq_config import GROQ_API_PARAMS, GroqConfig
from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfig
from .mistral_config import MISTRAL_API_PARAMS, MistralConfig
from .ollama_config import OLLAMA_API_PARAMS, OllamaConfig
-from .openai_config import (
- OPENAI_API_PARAMS,
- ChatGPTConfig,
- OpenSourceConfig,
-)
+from .openai_config import OPENAI_API_PARAMS, ChatGPTConfig, OpenSourceConfig
from .vllm_config import VLLM_API_PARAMS, VLLMConfig
from .zhipuai_config import ZHIPUAI_API_PARAMS, ZhipuAIConfig
diff --git a/camel/configs/anthropic_config.py b/camel/configs/anthropic_config.py
index 373ac538c..4bc4063ff 100644
--- a/camel/configs/anthropic_config.py
+++ b/camel/configs/anthropic_config.py
@@ -13,14 +13,13 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass
+from typing import List, Union
from anthropic import NOT_GIVEN, NotGiven
from camel.configs.base_config import BaseConfig
-@dataclass(frozen=True)
class AnthropicConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using the
Anthropic API.
@@ -62,12 +61,12 @@ class AnthropicConfig(BaseConfig):
"""
max_tokens: int = 256
- stop_sequences: list[str] | NotGiven = NOT_GIVEN
+ stop_sequences: Union[List[str], NotGiven] = NOT_GIVEN
temperature: float = 1
- top_p: float | NotGiven = NOT_GIVEN
- top_k: int | NotGiven = NOT_GIVEN
+ top_p: Union[float, NotGiven] = NOT_GIVEN
+ top_k: Union[int, NotGiven] = NOT_GIVEN
metadata: NotGiven = NOT_GIVEN
stream: bool = False
-ANTHROPIC_API_PARAMS = {param for param in asdict(AnthropicConfig()).keys()}
+ANTHROPIC_API_PARAMS = {param for param in AnthropicConfig.model_fields.keys()}
diff --git a/camel/configs/base_config.py b/camel/configs/base_config.py
index 9a28a3cb6..3d6c95bdc 100644
--- a/camel/configs/base_config.py
+++ b/camel/configs/base_config.py
@@ -14,9 +14,55 @@
from __future__ import annotations
from abc import ABC
-from dataclasses import dataclass
+from typing import Any, List, Optional
+from pydantic import BaseModel, ConfigDict, field_validator
-@dataclass(frozen=True)
-class BaseConfig(ABC): # noqa: B024
- pass
+
+class BaseConfig(ABC, BaseModel):
+ model_config = ConfigDict(
+ arbitrary_types_allowed=True,
+ extra="forbid",
+ frozen=True,
+ # UserWarning: conflict with protected namespace "model_"
+ protected_namespaces=(),
+ )
+
+ tools: Optional[List[Any]] = None
+ """A list of tools the model may
+ call. Currently, only functions are supported as a tool. Use this
+ to provide a list of functions the model may generate JSON inputs
+ for. A max of 128 functions are supported.
+ """
+
+ @field_validator("tools", mode="before")
+ @classmethod
+ def fields_type_checking(cls, tools):
+ if tools is not None:
+ from camel.toolkits import OpenAIFunction
+
+ for tool in tools:
+ if not isinstance(tool, OpenAIFunction):
+ raise ValueError(
+ f"The tool {tool} should "
+ "be an instance of `OpenAIFunction`."
+ )
+ return tools
+
+ def as_dict(self) -> dict[str, Any]:
+ config_dict = self.model_dump()
+
+ tools_schema = None
+ if self.tools:
+ from camel.toolkits import OpenAIFunction
+
+ tools_schema = []
+ for tool in self.tools:
+ if not isinstance(tool, OpenAIFunction):
+ raise ValueError(
+ f"The tool {tool} should "
+ "be an instance of `OpenAIFunction`."
+ )
+ tools_schema.append(tool.get_openai_tool_schema())
+ config_dict["tools"] = tools_schema
+ return config_dict
diff --git a/camel/configs/gemini_config.py b/camel/configs/gemini_config.py
index 757905a6f..67702080f 100644
--- a/camel/configs/gemini_config.py
+++ b/camel/configs/gemini_config.py
@@ -14,22 +14,13 @@
from collections.abc import Iterable
-from dataclasses import asdict, dataclass
-from typing import TYPE_CHECKING, Optional
+from typing import Any, Optional
-from camel.configs.base_config import BaseConfig
+from pydantic import model_validator
-if TYPE_CHECKING:
- from google.generativeai.protos import Schema
- from google.generativeai.types.content_types import (
- FunctionLibraryType,
- ToolConfigType,
- )
- from google.generativeai.types.helper_types import RequestOptionsType
- from google.generativeai.types.safety_types import SafetySettingOptions
+from camel.configs.base_config import BaseConfig
-@dataclass(frozen=True)
class GeminiConfig(BaseConfig):
r"""A simple dataclass used to configure the generation parameters of
`GenerativeModel.generate_content`.
@@ -88,11 +79,71 @@ class GeminiConfig(BaseConfig):
top_p: Optional[float] = None
top_k: Optional[int] = None
response_mime_type: Optional[str] = None
- response_schema: Optional['Schema'] = None
- safety_settings: Optional['SafetySettingOptions'] = None
- tools: Optional['FunctionLibraryType'] = None
- tool_config: Optional['ToolConfigType'] = None
- request_options: Optional['RequestOptionsType'] = None
+ response_schema: Optional[Any] = None
+ safety_settings: Optional[Any] = None
+ tools: Optional[Any] = None
+ tool_config: Optional[Any] = None
+ request_options: Optional[Any] = None
+
+ @model_validator(mode="before")
+ @classmethod
+ def fields_type_checking(cls, data: Any):
+ if isinstance(data, dict):
+ response_schema = data.get("response_schema")
+ safety_settings = data.get("safety_settings")
+ tools = data.get("tools")
+ tool_config = data.get("tool_config")
+ request_options = data.get("request_options")
+
+ if response_schema:
+ from google.generativeai.protos import Schema
+ from google.generativeai.types.content_types import (
+ FunctionLibraryType,
+ ToolConfigType,
+ )
+ from google.generativeai.types.helper_types import (
+ RequestOptionsType,
+ )
+ from google.generativeai.types.safety_types import (
+ SafetySettingOptions,
+ )
+ else:
+ return data
+
+ if response_schema and not isinstance(response_schema, Schema):
+ raise ValueError(
+ "The response_schema should be "
+ "an instance of `google.generativeai.protos.Schema`."
+ )
+
+ if safety_settings and not isinstance(
+ safety_settings, SafetySettingOptions
+ ):
+ raise ValueError(
+ "The response_schema should be an instance of "
+ "`google.generativeai.types.safety_types.SafetySettingOptions`."
+ )
+
+ if tools is not None:
+ for tool in tools:
+ if not isinstance(tool, FunctionLibraryType):
+ raise ValueError(
+ "The tool should be an instance of "
+ "`google.generativeai.types.content_types.FunctionLibraryType`."
+ )
+ if tool_config and not isinstance(tool_config, ToolConfigType):
+ raise ValueError(
+ "The response_schema should be an instance of "
+ "`google.generativeai.types.content_types.ToolConfigType`."
+ )
+ if request_options and not isinstance(
+ request_options, RequestOptionsType
+ ):
+ raise ValueError(
+ "The response_schema should be an instance of "
+ "`google.generativeai.types.helper_types.RequestOptionsType`."
+ )
+ return data
-Gemini_API_PARAMS = {param for param in asdict(GeminiConfig()).keys()}
+Gemini_API_PARAMS = {param for param in GeminiConfig().model_fields.keys()}
diff --git a/camel/configs/groq_config.py b/camel/configs/groq_config.py
index b5cab2b2a..5c945e8c8 100644
--- a/camel/configs/groq_config.py
+++ b/camel/configs/groq_config.py
@@ -13,18 +13,13 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass
-from typing import TYPE_CHECKING, Optional, Sequence
+from typing import Optional, Sequence, Union
from openai._types import NOT_GIVEN, NotGiven
from camel.configs.base_config import BaseConfig
-if TYPE_CHECKING:
- from camel.toolkits import OpenAIFunction
-
-@dataclass(frozen=True)
class GroqConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using OpenAI
compatibility.
@@ -98,22 +93,13 @@ class GroqConfig(BaseConfig):
top_p: float = 1.0
n: int = 1
stream: bool = False
- stop: str | Sequence[str] | NotGiven = NOT_GIVEN
- max_tokens: int | NotGiven = NOT_GIVEN
+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
+ max_tokens: Union[int, NotGiven] = NOT_GIVEN
presence_penalty: float = 0.0
- response_format: dict | NotGiven = NOT_GIVEN
+ response_format: Union[dict, NotGiven] = NOT_GIVEN
frequency_penalty: float = 0.0
user: str = ""
- tools: Optional[list[OpenAIFunction]] = None
- tool_choice: Optional[dict[str, str] | str] = "none"
-
- def __post_init__(self):
- if self.tools is not None:
- object.__setattr__(
- self,
- 'tools',
- [tool.get_openai_tool_schema() for tool in self.tools],
- )
+ tool_choice: Optional[Union[dict[str, str], str]] = "none"
-GROQ_API_PARAMS = {param for param in asdict(GroqConfig()).keys()}
+GROQ_API_PARAMS = {param for param in GroqConfig.model_fields.keys()}
diff --git a/camel/configs/litellm_config.py b/camel/configs/litellm_config.py
index 1203d33f0..dcce355b7 100644
--- a/camel/configs/litellm_config.py
+++ b/camel/configs/litellm_config.py
@@ -13,16 +13,11 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass
-from typing import TYPE_CHECKING, List, Optional, Union
+from typing import List, Optional, Union
from camel.configs.base_config import BaseConfig
-if TYPE_CHECKING:
- from camel.toolkits import OpenAIFunction
-
-@dataclass(frozen=True)
class LiteLLMConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using the
LiteLLM API.
@@ -88,7 +83,6 @@ class LiteLLMConfig(BaseConfig):
user: Optional[str] = None
response_format: Optional[dict] = None
seed: Optional[int] = None
- tools: Optional[list[OpenAIFunction]] = None
tool_choice: Optional[Union[str, dict]] = None
logprobs: Optional[bool] = None
top_logprobs: Optional[int] = None
@@ -100,4 +94,4 @@ class LiteLLMConfig(BaseConfig):
max_retries: Optional[int] = None
-LITELLM_API_PARAMS = {param for param in asdict(LiteLLMConfig()).keys()}
+LITELLM_API_PARAMS = {param for param in LiteLLMConfig.model_fields.keys()}
diff --git a/camel/configs/mistral_config.py b/camel/configs/mistral_config.py
index 9d9819167..58b559e43 100644
--- a/camel/configs/mistral_config.py
+++ b/camel/configs/mistral_config.py
@@ -13,20 +13,13 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass
-from typing import TYPE_CHECKING, Dict, Optional, Union
+from typing import Any, Dict, Optional, Union
-from camel.configs.base_config import BaseConfig
-
-if TYPE_CHECKING:
- from mistralai.models.chat_completion import (
- ResponseFormat,
- )
+from pydantic import field_validator
- from camel.toolkits import OpenAIFunction
+from camel.configs.base_config import BaseConfig
-@dataclass(frozen=True)
class MistralConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using the
Mistral API.
@@ -65,17 +58,21 @@ class MistralConfig(BaseConfig):
random_seed: Optional[int] = None
safe_mode: bool = False
safe_prompt: bool = False
- response_format: Optional[Union[Dict[str, str], ResponseFormat]] = None
- tools: Optional[list[OpenAIFunction]] = None
+ response_format: Optional[Union[Dict[str, str], Any]] = None
tool_choice: Optional[str] = "auto"
- def __post_init__(self):
- if self.tools is not None:
- object.__setattr__(
- self,
- 'tools',
- [tool.get_openai_tool_schema() for tool in self.tools],
- )
+ @field_validator("response_format", mode="before")
+ @classmethod
+ def fields_type_checking(cls, response_format):
+ if response_format and not isinstance(response_format, dict):
+ from mistralai.models.chat_completion import ResponseFormat
+
+ if not isinstance(response_format, ResponseFormat):
+ raise ValueError(
+ f"The tool {response_format} should be an instance "
+ "of `mistralai.models.chat_completion.ResponseFormat`."
+ )
+ return response_format
-MISTRAL_API_PARAMS = {param for param in asdict(MistralConfig()).keys()}
+MISTRAL_API_PARAMS = {param for param in MistralConfig().model_fields.keys()}
diff --git a/camel/configs/ollama_config.py b/camel/configs/ollama_config.py
index 3e983e41d..a6a3aa973 100644
--- a/camel/configs/ollama_config.py
+++ b/camel/configs/ollama_config.py
@@ -13,18 +13,16 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass
-from typing import Sequence
+from typing import Sequence, Union
from openai._types import NOT_GIVEN, NotGiven
from camel.configs.base_config import BaseConfig
-@dataclass(frozen=True)
class OllamaConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using OpenAI
- compatibility.
+ compatibility
Reference: https://github.com/ollama/ollama/blob/main/docs/openai.md
@@ -75,11 +73,11 @@ class OllamaConfig(BaseConfig):
temperature: float = 0.2
top_p: float = 1.0
stream: bool = False
- stop: str | Sequence[str] | NotGiven = NOT_GIVEN
- max_tokens: int | NotGiven = NOT_GIVEN
+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
+ max_tokens: Union[int, NotGiven] = NOT_GIVEN
presence_penalty: float = 0.0
- response_format: dict | NotGiven = NOT_GIVEN
+ response_format: Union[dict, NotGiven] = NOT_GIVEN
frequency_penalty: float = 0.0
-OLLAMA_API_PARAMS = {param for param in asdict(OllamaConfig()).keys()}
+OLLAMA_API_PARAMS = {param for param in OllamaConfig.model_fields.keys()}
diff --git a/camel/configs/openai_config.py b/camel/configs/openai_config.py
index 88b6590e9..1adb8cc3f 100644
--- a/camel/configs/openai_config.py
+++ b/camel/configs/openai_config.py
@@ -13,18 +13,14 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass, field
-from typing import TYPE_CHECKING, Optional, Sequence
+from typing import Optional, Sequence, Union
from openai._types import NOT_GIVEN, NotGiven
+from pydantic import Field
from camel.configs.base_config import BaseConfig
-if TYPE_CHECKING:
- from camel.toolkits import OpenAIFunction
-
-@dataclass(frozen=True)
class ChatGPTConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using the
OpenAI API.
@@ -105,29 +101,19 @@ class ChatGPTConfig(BaseConfig):
top_p: float = 1.0
n: int = 1
stream: bool = False
- stop: str | Sequence[str] | NotGiven = NOT_GIVEN
- max_tokens: int | NotGiven = NOT_GIVEN
+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
+ max_tokens: Union[int, NotGiven] = NOT_GIVEN
presence_penalty: float = 0.0
- response_format: dict | NotGiven = NOT_GIVEN
+ response_format: Union[dict, NotGiven] = NOT_GIVEN
frequency_penalty: float = 0.0
- logit_bias: dict = field(default_factory=dict)
+ logit_bias: dict = Field(default_factory=dict)
user: str = ""
- tools: Optional[list[OpenAIFunction]] = None
- tool_choice: Optional[dict[str, str] | str] = None
-
- def __post_init__(self):
- if self.tools is not None:
- object.__setattr__(
- self,
- 'tools',
- [tool.get_openai_tool_schema() for tool in self.tools],
- )
+ tool_choice: Optional[Union[dict[str, str], str]] = None
-OPENAI_API_PARAMS = {param for param in asdict(ChatGPTConfig()).keys()}
+OPENAI_API_PARAMS = {param for param in ChatGPTConfig.model_fields.keys()}
-@dataclass(frozen=True)
class OpenSourceConfig(BaseConfig):
r"""Defines parameters for setting up open-source models and includes
parameters to be passed to chat completion function of OpenAI API.
@@ -141,6 +127,9 @@ class OpenSourceConfig(BaseConfig):
contain the arguments to be passed to OpenAI API.
"""
+ # Maybe the param needs to be renamed.
+ # Warning: Field "model_path" has conflict with protected namespace
+ # "model_".
model_path: str
server_url: str
- api_params: ChatGPTConfig = field(default_factory=ChatGPTConfig)
+ api_params: ChatGPTConfig = Field(default_factory=ChatGPTConfig)
diff --git a/camel/configs/vllm_config.py b/camel/configs/vllm_config.py
index 2e0df9598..8efbeca63 100644
--- a/camel/configs/vllm_config.py
+++ b/camel/configs/vllm_config.py
@@ -13,16 +13,15 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass, field
-from typing import Sequence
+from typing import Sequence, Union
from openai._types import NOT_GIVEN, NotGiven
+from pydantic import Field
from camel.configs.base_config import BaseConfig
# flake8: noqa: E501
-@dataclass(frozen=True)
class VLLMConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using the
OpenAI API.
@@ -91,13 +90,13 @@ class VLLMConfig(BaseConfig):
top_p: float = 1.0
n: int = 1
stream: bool = False
- stop: str | Sequence[str] | NotGiven = NOT_GIVEN
- max_tokens: int | NotGiven = NOT_GIVEN
+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
+ max_tokens: Union[int, NotGiven] = NOT_GIVEN
presence_penalty: float = 0.0
- response_format: dict | NotGiven = NOT_GIVEN
+ response_format: Union[dict, NotGiven] = NOT_GIVEN
frequency_penalty: float = 0.0
- logit_bias: dict = field(default_factory=dict)
+ logit_bias: dict = Field(default_factory=dict)
user: str = ""
-VLLM_API_PARAMS = {param for param in asdict(VLLMConfig()).keys()}
+VLLM_API_PARAMS = {param for param in VLLMConfig.model_fields.keys()}
diff --git a/camel/configs/zhipuai_config.py b/camel/configs/zhipuai_config.py
index eab9a4ac0..89f2061df 100644
--- a/camel/configs/zhipuai_config.py
+++ b/camel/configs/zhipuai_config.py
@@ -13,18 +13,13 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import asdict, dataclass
-from typing import TYPE_CHECKING, Optional, Sequence
+from typing import Optional, Sequence, Union
from openai._types import NOT_GIVEN, NotGiven
from camel.configs.base_config import BaseConfig
-if TYPE_CHECKING:
- from camel.toolkits import OpenAIFunction
-
-@dataclass(frozen=True)
class ZhipuAIConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using OpenAI
compatibility
@@ -69,10 +64,9 @@ class ZhipuAIConfig(BaseConfig):
temperature: float = 0.2
top_p: float = 0.6
stream: bool = False
- stop: str | Sequence[str] | NotGiven = NOT_GIVEN
- max_tokens: int | NotGiven = NOT_GIVEN
- tools: Optional[list[OpenAIFunction]] = None
- tool_choice: Optional[dict[str, str] | str] = None
+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
+ max_tokens: Union[int, NotGiven] = NOT_GIVEN
+ tool_choice: Optional[Union[dict[str, str], str]] = None
-ZHIPUAI_API_PARAMS = {param for param in asdict(ZhipuAIConfig()).keys()}
+ZHIPUAI_API_PARAMS = {param for param in ZhipuAIConfig.model_fields.keys()}
diff --git a/camel/human.py b/camel/human.py
index 67e5d5140..c5bba04c8 100644
--- a/camel/human.py
+++ b/camel/human.py
@@ -135,4 +135,4 @@ class Human:
human_input = self.get_input()
content = self.parse_input(human_input)
message = meta_chat_message.create_new_instance(content)
- return ChatAgentResponse([message], terminated=False, info={})
+ return ChatAgentResponse(msgs=[message], terminated=False, info={})
diff --git a/camel/memories/agent_memories.py b/camel/memories/agent_memories.py
index c9308d839..7cd4c3729 100644
--- a/camel/memories/agent_memories.py
+++ b/camel/memories/agent_memories.py
@@ -16,10 +16,7 @@ from typing import List, Optional
from camel.memories.base import AgentMemory, BaseContextCreator
from camel.memories.blocks import ChatHistoryBlock, VectorDBBlock
-from camel.memories.records import (
- ContextRecord,
- MemoryRecord,
-)
+from camel.memories.records import ContextRecord, MemoryRecord
from camel.storages import BaseKeyValueStorage, BaseVectorStorage
from camel.types import OpenAIBackendRole
diff --git a/camel/memories/blocks/chat_history_block.py b/camel/memories/blocks/chat_history_block.py
index af877f94c..1d011c058 100644
--- a/camel/memories/blocks/chat_history_block.py
+++ b/camel/memories/blocks/chat_history_block.py
@@ -84,11 +84,15 @@ class ChatHistoryBlock(MemoryBlock):
for record in reversed(chat_records):
if record.role_at_backend == OpenAIBackendRole.SYSTEM:
# System messages are always kept.
- output_records.append(ContextRecord(record, 1.0))
+ output_records.append(
+ ContextRecord(memory_record=record, score=1.0)
+ )
else:
# Other messages' score drops down gradually
score *= self.keep_rate
- output_records.append(ContextRecord(record, score))
+ output_records.append(
+ ContextRecord(memory_record=record, score=score)
+ )
output_records.reverse()
return output_records
diff --git a/camel/memories/blocks/vectordb_block.py b/camel/memories/blocks/vectordb_block.py
index 2798ded65..f1aced095 100644
--- a/camel/memories/blocks/vectordb_block.py
+++ b/camel/memories/blocks/vectordb_block.py
@@ -67,7 +67,9 @@ class VectorDBBlock(MemoryBlock):
vector database based on similarity to :obj:`current_state`.
"""
query_vector = self.embedding.embed(keyword)
- results = self.storage.query(VectorDBQuery(query_vector, top_k=limit))
+ results = self.storage.query(
+ VectorDBQuery(query_vector=query_vector, top_k=limit)
+ )
return [
ContextRecord(
memory_record=MemoryRecord.from_dict(result.record.payload),
diff --git a/camel/memories/context_creators/score_based.py b/camel/memories/context_creators/score_based.py
index 6ae9d248d..2d6f45140 100644
--- a/camel/memories/context_creators/score_based.py
+++ b/camel/memories/context_creators/score_based.py
@@ -11,17 +11,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from dataclasses import dataclass
from typing import List, Tuple
+from pydantic import BaseModel
+
from camel.memories.base import BaseContextCreator
from camel.memories.records import ContextRecord
from camel.messages import OpenAIMessage
from camel.utils import BaseTokenCounter
-@dataclass(frozen=True)
-class _ContextUnit:
+class _ContextUnit(BaseModel):
idx: int
record: ContextRecord
num_tokens: int
@@ -88,9 +88,9 @@ class ScoreBasedContextCreator(BaseContextCreator):
uuid_set.add(record.memory_record.uuid)
context_units.append(
_ContextUnit(
- idx,
- record,
- self.token_counter.count_tokens_from_messages(
+ idx=idx,
+ record=record,
+ num_tokens=self.token_counter.count_tokens_from_messages(
[record.memory_record.to_openai_message()]
),
)
diff --git a/camel/memories/records.py b/camel/memories/records.py
index e9f5a0464..aa5297784 100644
--- a/camel/memories/records.py
+++ b/camel/memories/records.py
@@ -12,16 +12,17 @@
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from dataclasses import asdict, dataclass, field
+from dataclasses import asdict
from typing import Any, ClassVar, Dict
from uuid import UUID, uuid4
+from pydantic import BaseModel, ConfigDict, Field
+
from camel.messages import BaseMessage, FunctionCallingMessage, OpenAIMessage
from camel.types import OpenAIBackendRole
-@dataclass(frozen=True)
-class MemoryRecord:
+class MemoryRecord(BaseModel):
r"""The basic message storing unit in the CAMEL memory system.
Attributes:
@@ -38,10 +39,12 @@ class MemoryRecord:
will be an empty `Dict`.
"""
+ model_config = ConfigDict(arbitrary_types_allowed=True)
+
message: BaseMessage
role_at_backend: OpenAIBackendRole
- uuid: UUID = field(default_factory=uuid4)
- extra_info: Dict[str, str] = field(default_factory=dict)
+ uuid: UUID = Field(default_factory=uuid4)
+ extra_info: Dict[str, str] = Field(default_factory=dict)
_MESSAGE_TYPES: ClassVar[dict] = {
"BaseMessage": BaseMessage,
@@ -85,8 +88,7 @@ class MemoryRecord:
return self.message.to_openai_message(self.role_at_backend)
-@dataclass(frozen=True)
-class ContextRecord:
+class ContextRecord(BaseModel):
r"""The result of memory retrieving."""
memory_record: MemoryRecord
diff --git a/camel/models/open_source_model.py b/camel/models/open_source_model.py
index cfadc7094..efa658d77 100644
--- a/camel/models/open_source_model.py
+++ b/camel/models/open_source_model.py
@@ -95,7 +95,7 @@ class OpenSourceModel(BaseModelBackend):
# Replace `model_config_dict` with only the params to be
# passed to OpenAI API
- self.model_config_dict = self.model_config_dict["api_params"].__dict__
+ self.model_config_dict = self.model_config_dict["api_params"]
@property
def token_counter(self) -> BaseTokenCounter:
@@ -152,7 +152,7 @@ class OpenSourceModel(BaseModelBackend):
":obj:`model_path` or :obj:`server_url` missing."
)
- for param in self.model_config_dict["api_params"].__dict__:
+ for param in self.model_config_dict["api_params"]:
if param not in OPENAI_API_PARAMS:
raise ValueError(
f"Unexpected argument `{param}` is "
diff --git a/camel/responses/agent_responses.py b/camel/responses/agent_responses.py
index 7f5cbc249..abe0652cf 100644
--- a/camel/responses/agent_responses.py
+++ b/camel/responses/agent_responses.py
@@ -11,14 +11,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from dataclasses import dataclass
from typing import Any, Dict, List
+from pydantic import BaseModel, ConfigDict
+
from camel.messages import BaseMessage
-@dataclass(frozen=True)
-class ChatAgentResponse:
+class ChatAgentResponse(BaseModel):
r"""Response of a ChatAgent.
Attributes:
@@ -31,6 +31,7 @@ class ChatAgentResponse:
info (Dict[str, Any]): Extra information about the chat message.
"""
+ model_config = ConfigDict(arbitrary_types_allowed=True)
msgs: List[BaseMessage]
terminated: bool
info: Dict[str, Any]
diff --git a/camel/societies/babyagi_playing.py b/camel/societies/babyagi_playing.py
index 5ba30b507..2455754cb 100644
--- a/camel/societies/babyagi_playing.py
+++ b/camel/societies/babyagi_playing.py
@@ -270,10 +270,12 @@ class BabyAGI:
"All tasks are solved"
)
return ChatAgentResponse(
- [assistant_msg], terminated, assistant_response.info
+ msgs=[assistant_msg],
+ terminated=terminated,
+ info=assistant_response.info,
)
return ChatAgentResponse(
- [assistant_msg],
- assistant_response.terminated,
- assistant_response.info,
+ msgs=[assistant_msg],
+ terminated=assistant_response.terminated,
+ info=assistant_response.info,
)
diff --git a/camel/societies/role_playing.py b/camel/societies/role_playing.py
index b2fd410e4..f546cb4f4 100644
--- a/camel/societies/role_playing.py
+++ b/camel/societies/role_playing.py
@@ -478,9 +478,11 @@ class RolePlaying:
user_response = self.user_agent.step(assistant_msg)
if user_response.terminated or user_response.msgs is None:
return (
- ChatAgentResponse([], False, {}),
+ ChatAgentResponse(msgs=[], terminated=False, info={}),
ChatAgentResponse(
- [], user_response.terminated, user_response.info
+ msgs=[],
+ terminated=user_response.terminated,
+ info=user_response.info,
),
)
user_msg = self._reduce_message_options(user_response.msgs)
@@ -490,20 +492,26 @@ class RolePlaying:
if assistant_response.terminated or assistant_response.msgs is None:
return (
ChatAgentResponse(
- [], assistant_response.terminated, assistant_response.info
+ msgs=[],
+ terminated=assistant_response.terminated,
+ info=assistant_response.info,
+ ),
+ ChatAgentResponse(
+ msgs=[user_msg], terminated=False, info=user_response.info
),
- ChatAgentResponse([user_msg], False, user_response.info),
)
assistant_msg = self._reduce_message_options(assistant_response.msgs)
self.assistant_agent.record_message(assistant_msg)
return (
ChatAgentResponse(
- [assistant_msg],
- assistant_response.terminated,
- assistant_response.info,
+ msgs=[assistant_msg],
+ terminated=assistant_response.terminated,
+ info=assistant_response.info,
),
ChatAgentResponse(
- [user_msg], user_response.terminated, user_response.info
+ msgs=[user_msg],
+ terminated=user_response.terminated,
+ info=user_response.info,
),
)
diff --git a/camel/storages/graph_storages/graph_element.py b/camel/storages/graph_storages/graph_element.py
index afa387480..6486bb0b3 100644
--- a/camel/storages/graph_storages/graph_element.py
+++ b/camel/storages/graph_storages/graph_element.py
@@ -13,17 +13,17 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations
-from dataclasses import dataclass, field
from typing import List, Union
+from pydantic import BaseModel, ConfigDict, Field
+
try:
from unstructured.documents.elements import Element
except ImportError:
Element = None
-@dataclass
-class Node:
+class Node(BaseModel):
r"""Represents a node in a graph with associated properties.
Attributes:
@@ -35,11 +35,10 @@ class Node:
id: Union[str, int]
type: str = "Node"
- properties: dict = field(default_factory=dict)
+ properties: dict = Field(default_factory=dict)
-@dataclass
-class Relationship:
+class Relationship(BaseModel):
r"""Represents a directed relationship between two nodes in a graph.
Attributes:
@@ -53,24 +52,21 @@ class Relationship:
subj: Node
obj: Node
type: str = "Relationship"
- properties: dict = field(default_factory=dict)
+ properties: dict = Field(default_factory=dict)
-@dataclass
-class GraphElement:
+class GraphElement(BaseModel):
r"""A graph element with lists of nodes and relationships.
Attributes:
nodes (List[Node]): A list of nodes in the graph.
relationships (List[Relationship]): A list of relationships in the
- graph.
+ graph.
source (Element): The element from which the graph information is
- derived.
+ derived.
"""
- # Allow arbitrary types for Element
- class Config:
- arbitrary_types_allowed = True
+ model_config = ConfigDict(arbitrary_types_allowed=True)
nodes: List[Node]
relationships: List[Relationship]
diff --git a/camel/storages/vectordb_storages/base.py b/camel/storages/vectordb_storages/base.py
index 64fdcd4fa..e2d2b476f 100644
--- a/camel/storages/vectordb_storages/base.py
+++ b/camel/storages/vectordb_storages/base.py
@@ -13,13 +13,13 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from abc import ABC, abstractmethod
-from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
from uuid import uuid4
+from pydantic import BaseModel, Field
-@dataclass
-class VectorRecord:
+
+class VectorRecord(BaseModel):
r"""Encapsulates information about a vector's unique identifier and its
payload, which is primarily used as a data transfer object when saving
to vector storage.
@@ -33,12 +33,11 @@ class VectorRecord:
"""
vector: List[float]
- id: str = field(default_factory=lambda: str(uuid4()))
+ id: str = Field(default_factory=lambda: str(uuid4()))
payload: Optional[Dict[str, Any]] = None
-@dataclass
-class VectorDBQuery:
+class VectorDBQuery(BaseModel):
r"""Represents a query to a vector database.
Attributes:
@@ -49,11 +48,24 @@ class VectorDBQuery:
"""
query_vector: List[float]
+ """The numerical representation of the query vector."""
top_k: int = 1
+ """The number of top similar vectors to retrieve from the database."""
+
+ def __init__(
+ self, query_vector: List[float], top_k: int, **kwargs: Any
+ ) -> None:
+ """Pass in query_vector and tok_k as positional arg.
+ Args:
+ query_vector (List[float]): The numerical representation of the
+ query vector.
+ top_k (int, optional): The number of top similar vectors to
+ retrieve from the database. (default: :obj:`1`)
+ """
+ super().__init__(query_vector=query_vector, top_k=top_k, **kwargs)
-@dataclass
-class VectorDBQueryResult:
+class VectorDBQueryResult(BaseModel):
r"""Encapsulates the result of a query against a vector database.
Attributes:
@@ -66,7 +78,7 @@ class VectorDBQueryResult:
similarity: float
@classmethod
- def construct(
+ def create(
cls,
similarity: float,
vector: List[float],
@@ -75,13 +87,12 @@ class VectorDBQueryResult:
) -> "VectorDBQueryResult":
r"""A class method to construct a `VectorDBQueryResult` instance."""
return cls(
- record=VectorRecord(vector, id, payload),
+ record=VectorRecord(vector=vector, id=id, payload=payload),
similarity=similarity,
)
-@dataclass
-class VectorDBStatus:
+class VectorDBStatus(BaseModel):
r"""Vector database status.
Attributes:
@@ -195,7 +206,7 @@ class BaseVectorStorage(ABC):
List[List[Dict[str, Any]]]: A list of vector payloads retrieved
from the storage based on similarity to the query vector.
"""
- results = self.query(VectorDBQuery(vector, top_k))
+ results = self.query(VectorDBQuery(query_vector=vector, top_k=top_k))
return [
result.record.payload
for result in results
diff --git a/camel/storages/vectordb_storages/milvus.py b/camel/storages/vectordb_storages/milvus.py
index 38ae3dfd9..e01036d26 100644
--- a/camel/storages/vectordb_storages/milvus.py
+++ b/camel/storages/vectordb_storages/milvus.py
@@ -361,7 +361,7 @@ class MilvusStorage(BaseVectorStorage):
query_results = []
for point in search_result:
query_results.append(
- VectorDBQueryResult.construct(
+ VectorDBQueryResult.create(
similarity=(point[0]['distance']),
id=str(point[0]['id']),
payload=(point[0]['entity'].get('payload')),
diff --git a/camel/storages/vectordb_storages/qdrant.py b/camel/storages/vectordb_storages/qdrant.py
index fcb5bc0e9..828f2be95 100644
--- a/camel/storages/vectordb_storages/qdrant.py
+++ b/camel/storages/vectordb_storages/qdrant.py
@@ -11,7 +11,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from dataclasses import asdict
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple, Union, cast
@@ -261,7 +260,7 @@ class QdrantStorage(BaseVectorStorage):
"""
from qdrant_client.http.models import PointStruct, UpdateStatus
- qdrant_points = [PointStruct(**asdict(p)) for p in records]
+ qdrant_points = [PointStruct(**p.model_dump()) for p in records]
op_info = self._client.upsert(
collection_name=self.collection_name,
points=qdrant_points,
@@ -340,7 +339,7 @@ class QdrantStorage(BaseVectorStorage):
query_results = []
for point in search_result:
query_results.append(
- VectorDBQueryResult.construct(
+ VectorDBQueryResult.create(
similarity=point.score,
id=str(point.id),
payload=point.payload,
diff --git a/camel/toolkits/github_toolkit.py b/camel/toolkits/github_toolkit.py
index c0106cf6a..141d7706b 100644
--- a/camel/toolkits/github_toolkit.py
+++ b/camel/toolkits/github_toolkit.py
@@ -13,18 +13,18 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
import os
-from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import List, Optional
+from pydantic import BaseModel
+
from camel.utils import dependencies_required
from .base import BaseToolkit
from .openai_function import OpenAIFunction
-@dataclass
-class GithubIssue:
+class GithubIssue(BaseModel):
r"""Represents a GitHub issue.
Attributes:
@@ -35,29 +35,11 @@ class GithubIssue:
file_content (str): The content of the file associated with the issue.
"""
- def __init__(
- self,
- title: str,
- body: str,
- number: int,
- file_path: str,
- file_content: str,
- ) -> None:
- r"""Initialize a GithubIssue object.
-
- Args:
- title (str): The title of the GitHub issue.
- body (str): The body/content of the GitHub issue.
- number (int): The issue number.
- file_path (str): The path of the file associated with the issue.
- file_content (str): The content of the file associated with the
- issue.
- """
- self.title = title
- self.body = body
- self.number = number
- self.file_path = file_path
- self.file_content = file_content
+ title: str
+ body: str
+ number: int
+ file_path: str
+ file_content: str
def __str__(self) -> str:
r"""Returns a string representation of the issue.
@@ -75,8 +57,7 @@ class GithubIssue:
)
-@dataclass
-class GithubPullRequestDiff:
+class GithubPullRequestDiff(BaseModel):
r"""Represents a single diff of a pull request on Github.
Attributes:
@@ -92,8 +73,7 @@ class GithubPullRequestDiff:
return f"Filename: {self.filename}\nPatch: {self.patch}"
-@dataclass
-class GithubPullRequest:
+class GithubPullRequest(BaseModel):
r"""Represents a pull request on Github.
Attributes:
@@ -251,13 +231,17 @@ class GithubToolkit(BaseToolkit):
and pr.merged_at is not None
and pr.merged_at.timestamp() > earliest_date.timestamp()
):
- pr_details = GithubPullRequest(pr.title, pr.body, [])
+ pr_details = GithubPullRequest(
+ title=pr.title, body=pr.body, diffs=[]
+ )
# Get files changed in the PR
files = pr.get_files()
for file in files:
- diff = GithubPullRequestDiff(file.filename, file.patch)
+ diff = GithubPullRequestDiff(
+ filename=file.filename, patch=file.patch
+ )
pr_details.diffs.append(diff)
merged_prs.append(str(pr_details))
diff --git a/examples/ai_society/role_playing_multiprocess.py b/examples/ai_society/role_playing_multiprocess.py
index 7ef793620..fd21a0fd3 100644
--- a/examples/ai_society/role_playing_multiprocess.py
+++ b/examples/ai_society/role_playing_multiprocess.py
@@ -42,7 +42,7 @@ def generate_data(
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=ChatGPTConfig(temperature=1.4).__dict__,
+ model_config_dict=ChatGPTConfig(temperature=1.4).as_dict(),
)
role_play_session = RolePlaying(
diff --git a/examples/ai_society/role_playing_with_critic.py b/examples/ai_society/role_playing_with_critic.py
index 21e7c1d63..c8479fdd6 100644
--- a/examples/ai_society/role_playing_with_critic.py
+++ b/examples/ai_society/role_playing_with_critic.py
@@ -25,7 +25,7 @@ def main() -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=ChatGPTConfig(temperature=0.8, n=3).__dict__,
+ model_config_dict=ChatGPTConfig(temperature=0.8, n=3).as_dict(),
)
assistant_agent_kwargs = dict(model=model)
user_agent_kwargs = dict(model=model)
diff --git a/examples/ai_society/role_playing_with_human.py b/examples/ai_society/role_playing_with_human.py
index fa28c7956..8f8ecf8dd 100644
--- a/examples/ai_society/role_playing_with_human.py
+++ b/examples/ai_society/role_playing_with_human.py
@@ -25,7 +25,7 @@ def main() -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=ChatGPTConfig(temperature=1.4, n=3).__dict__,
+ model_config_dict=ChatGPTConfig(temperature=1.4, n=3).as_dict(),
)
assistant_agent_kwargs = dict(model=model)
user_agent_kwargs = dict(model=model)
diff --git a/examples/function_call/code_execution.py b/examples/function_call/code_execution.py
index c0dfe39d0..c5b22cad3 100644
--- a/examples/function_call/code_execution.py
+++ b/examples/function_call/code_execution.py
@@ -35,7 +35,7 @@ assistant_model_config = ChatGPTConfig(
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=assistant_model_config.__dict__,
+ model_config_dict=assistant_model_config.as_dict(),
)
diff --git a/examples/function_call/github_examples.py b/examples/function_call/github_examples.py
index c63b407c3..4dc6fb2fb 100644
--- a/examples/function_call/github_examples.py
+++ b/examples/function_call/github_examples.py
@@ -61,7 +61,7 @@ def write_weekly_pr_summary(repo_name, model=None):
)
assistant_model_config_dict = ChatGPTConfig(
tools=[OpenAIFunction(toolkit.retrieve_pull_requests)], temperature=0.0
- ).__dict__
+ ).as_dict()
assistant_model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
@@ -116,7 +116,7 @@ def solve_issue(
assistant_model_config_dict = ChatGPTConfig(
tools=toolkit.get_tools(),
temperature=0.0,
- ).__dict__
+ ).as_dict()
model = ModelFactory.create(
model_platform=ModelPlatformType.OpenAI,
diff --git a/examples/function_call/openapi_function.py b/examples/function_call/openapi_function.py
index 643ee38f5..2b3d8a9f4 100644
--- a/examples/function_call/openapi_function.py
+++ b/examples/function_call/openapi_function.py
@@ -28,7 +28,7 @@ tools = [*OPENAPI_FUNCS]
model_config_dict = ChatGPTConfig(
tools=tools,
temperature=0.0,
-).__dict__
+).as_dict()
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
diff --git a/examples/function_call/role_playing_with_functions.py b/examples/function_call/role_playing_with_functions.py
index 201244294..d51ed2ede 100644
--- a/examples/function_call/role_playing_with_functions.py
+++ b/examples/function_call/role_playing_with_functions.py
@@ -65,7 +65,7 @@ def main(
model=ModelFactory.create(
model_platform=model_platform,
model_type=model_type,
- model_config_dict=assistant_model_config.__dict__,
+ model_config_dict=assistant_model_config.as_dict(),
),
tools=function_list,
),
@@ -73,7 +73,7 @@ def main(
model=ModelFactory.create(
model_platform=model_platform,
model_type=model_type,
- model_config_dict=user_model_config.__dict__,
+ model_config_dict=user_model_config.as_dict(),
),
),
task_prompt=task_prompt,
@@ -129,8 +129,9 @@ def main(
# Print output from the assistant, including any function
# execution information
print_text_animated(Fore.GREEN + "AI Assistant:")
- tool_calls: List[FunctionCallingRecord] = assistant_response.info[
- 'tool_calls'
+ tool_calls: List[FunctionCallingRecord] = [
+ FunctionCallingRecord(**call.as_dict())
+ for call in assistant_response.info['tool_calls']
]
for func_record in tool_calls:
print_text_animated(f"{func_record}")
diff --git a/examples/generate_text_embedding_data/single_agent.py b/examples/generate_text_embedding_data/single_agent.py
index 9129d0332..c42f3a71c 100644
--- a/examples/generate_text_embedding_data/single_agent.py
+++ b/examples/generate_text_embedding_data/single_agent.py
@@ -65,7 +65,7 @@ def main() -> None:
model_type=ModelType.GPT_3_5_TURBO,
model_config_dict=ChatGPTConfig(
temperature=0.0, response_format={"type": "json_object"}
- ).__dict__,
+ ).as_dict(),
)
assistant_agent = ChatAgent(
diff --git a/examples/generate_text_embedding_data/task_generation.py b/examples/generate_text_embedding_data/task_generation.py
index a525e4e58..ad9b26b37 100644
--- a/examples/generate_text_embedding_data/task_generation.py
+++ b/examples/generate_text_embedding_data/task_generation.py
@@ -38,7 +38,7 @@ def main() -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=ChatGPTConfig(temperature=0.0).__dict__,
+ model_config_dict=ChatGPTConfig(temperature=0.0).as_dict(),
)
agent = ChatAgent(
assistant_sys_msg,
diff --git a/examples/misalignment/role_playing_multiprocess.py b/examples/misalignment/role_playing_multiprocess.py
index b88e163bd..0a2567451 100644
--- a/examples/misalignment/role_playing_multiprocess.py
+++ b/examples/misalignment/role_playing_multiprocess.py
@@ -48,7 +48,7 @@ def generate_data(
model=ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=ChatGPTConfig(temperature=1.4).__dict__,
+ model_config_dict=ChatGPTConfig(temperature=1.4).as_dict(),
)
),
)
diff --git a/examples/misalignment/role_playing_with_human.py b/examples/misalignment/role_playing_with_human.py
index 40056eab6..3aeb9d05e 100644
--- a/examples/misalignment/role_playing_with_human.py
+++ b/examples/misalignment/role_playing_with_human.py
@@ -25,7 +25,7 @@ def main() -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=ChatGPTConfig(temperature=1.4, n=3).__dict__,
+ model_config_dict=ChatGPTConfig(temperature=1.4, n=3).as_dict(),
)
assistant_agent_kwargs = dict(model=model)
user_agent_kwargs = dict(model=model)
diff --git a/examples/models/azure_openai_model_example.py b/examples/models/azure_openai_model_example.py
index 3369fbc79..2d535405f 100644
--- a/examples/models/azure_openai_model_example.py
+++ b/examples/models/azure_openai_model_example.py
@@ -28,7 +28,7 @@ export AZURE_DEPLOYMENT_NAME=""
model = ModelFactory.create(
model_platform=ModelPlatformType.AZURE,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=ChatGPTConfig(temperature=0.2).__dict__,
+ model_config_dict=ChatGPTConfig(temperature=0.2).as_dict(),
)
# Define system message
diff --git a/examples/models/gemini_model_example.py b/examples/models/gemini_model_example.py
index 8e470a440..45b7aa746 100644
--- a/examples/models/gemini_model_example.py
+++ b/examples/models/gemini_model_example.py
@@ -20,7 +20,7 @@ from camel.types import ModelPlatformType, ModelType
model = ModelFactory.create(
model_platform=ModelPlatformType.GEMINI,
model_type=ModelType.GEMINI_1_5_PRO,
- model_config_dict=GeminiConfig(temperature=0.2).__dict__,
+ model_config_dict=GeminiConfig(temperature=0.2).as_dict(),
)
# Define system message
diff --git a/examples/models/litellm_model_example.py b/examples/models/litellm_model_example.py
index 309ca847e..35009981f 100644
--- a/examples/models/litellm_model_example.py
+++ b/examples/models/litellm_model_example.py
@@ -20,7 +20,7 @@ from camel.types import ModelPlatformType
model = ModelFactory.create(
model_platform=ModelPlatformType.LITELLM,
model_type="gpt-4o",
- model_config_dict=LiteLLMConfig(temperature=0.2).__dict__,
+ model_config_dict=LiteLLMConfig(temperature=0.2).as_dict(),
)
# Define system message
diff --git a/examples/models/role_playing_with_claude.py b/examples/models/role_playing_with_claude.py
index c0c0ac372..ede25cc24 100644
--- a/examples/models/role_playing_with_claude.py
+++ b/examples/models/role_playing_with_claude.py
@@ -26,7 +26,7 @@ def main(model_type=None) -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.ANTHROPIC,
model_type=model_type,
- model_config_dict=AnthropicConfig().__dict__,
+ model_config_dict=AnthropicConfig().as_dict(),
)
# Update agent_kwargs to use the created models
diff --git a/examples/models/role_playing_with_gemini.py b/examples/models/role_playing_with_gemini.py
index 182f897f1..b4cd9596f 100644
--- a/examples/models/role_playing_with_gemini.py
+++ b/examples/models/role_playing_with_gemini.py
@@ -27,7 +27,7 @@ def main(model_type=None) -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.GEMINI,
model_type=model_type,
- model_config_dict=GeminiConfig().__dict__,
+ model_config_dict=GeminiConfig().as_dict(),
)
# Update agent_kwargs to use the created models
diff --git a/examples/models/role_playing_with_groq.py b/examples/models/role_playing_with_groq.py
index bfde64aec..eb3295621 100644
--- a/examples/models/role_playing_with_groq.py
+++ b/examples/models/role_playing_with_groq.py
@@ -27,7 +27,7 @@ def main(model_type=None) -> None:
role: ModelFactory.create(
model_platform=ModelPlatformType.GROQ,
model_type=model_type,
- model_config_dict=GroqConfig().__dict__,
+ model_config_dict=GroqConfig().as_dict(),
)
for role in ["assistant", "user", "task-specify"]
}
diff --git a/examples/models/role_playing_with_open_source_model.py b/examples/models/role_playing_with_open_source_model.py
index a17d91170..a6ddec554 100644
--- a/examples/models/role_playing_with_open_source_model.py
+++ b/examples/models/role_playing_with_open_source_model.py
@@ -40,7 +40,7 @@ def main(
model_path=model_path,
server_url=server_url,
api_params=ChatGPTConfig(temperature=0),
- ).__dict__,
+ ).as_dict(),
)
# Update agent_kwargs to use the created models
diff --git a/examples/models/zhipuai_model_example.py b/examples/models/zhipuai_model_example.py
index 504df4589..eaa62c7d9 100644
--- a/examples/models/zhipuai_model_example.py
+++ b/examples/models/zhipuai_model_example.py
@@ -21,7 +21,7 @@ from camel.types import ModelPlatformType, ModelType
model = ModelFactory.create(
model_platform=ModelPlatformType.ZHIPU,
model_type=ModelType.GLM_4,
- model_config_dict=ZhipuAIConfig(temperature=0.2).__dict__,
+ model_config_dict=ZhipuAIConfig(temperature=0.2).as_dict(),
)
# Define system message
diff --git a/examples/vision/object_recognition.py b/examples/vision/object_recognition.py
index 73676a038..9d74b8cc9 100644
--- a/examples/vision/object_recognition.py
+++ b/examples/vision/object_recognition.py
@@ -54,7 +54,7 @@ def detect_image_obj(image_paths: str) -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().__dict__,
+ model_config_dict=ChatGPTConfig().as_dict(),
)
agent = ChatAgent(
assistant_sys_msg,
diff --git a/examples/vision/video_description.py b/examples/vision/video_description.py
index 7b30071b5..8c250278f 100644
--- a/examples/vision/video_description.py
+++ b/examples/vision/video_description.py
@@ -32,7 +32,7 @@ sys_msg = BaseMessage.make_assistant_message(
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().__dict__,
+ model_config_dict=ChatGPTConfig().as_dict(),
)
# Set agent
diff --git a/poetry.lock b/poetry.lock
index 1eb601738..d47c31b24 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -1,4 +1,4 @@
-# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand.
+# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
[[package]]
name = "accelerate"
@@ -1335,6 +1335,20 @@ files = [
{file = "et_xmlfile-1.1.0.tar.gz", hash = "sha256:8eb9e2bc2f8c97e37a2dc85a09ecdcdec9d8a396530a6d5a33b30b9a92da0c5c"},
]
+[[package]]
+name = "eval-type-backport"
+version = "0.2.0"
+description = "Like `typing._eval_type`, but lets older Python versions use newer typing features."
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "eval_type_backport-0.2.0-py3-none-any.whl", hash = "sha256:ac2f73d30d40c5a30a80b8739a789d6bb5e49fdffa66d7912667e2015d9c9933"},
+ {file = "eval_type_backport-0.2.0.tar.gz", hash = "sha256:68796cfbc7371ebf923f03bdf7bef415f3ec098aeced24e054b253a0e78f7b37"},
+]
+
+[package.extras]
+tests = ["pytest"]
+
[[package]]
name = "exceptiongroup"
version = "1.2.2"
@@ -8318,4 +8332,4 @@ vector-databases = ["pymilvus", "qdrant-client"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.9.0,<3.12"
-content-hash = "5c3cf8edf982ad4da84d6c74bcc241cce5cf977eb59c8b8adee513e0dc8f319f"
+content-hash = "5dd7f3811feeec8b3d9b294d65e083cb8a2847e304b0661127bf22cf228e035e"
diff --git a/pyproject.toml b/pyproject.toml
index 8f41feda4..3ee33c18a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -39,6 +39,7 @@ protobuf = "^4"
pathlib = "^1.0.1"
docstring-parser = "^0.15"
pydantic = ">=1.9,<3"
+eval-type-backport = "0.2.0"
curl_cffi = "0.6.2"
jupyter_client = "^8.6.2"
ipykernel = "^6.0.0"
diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.py
index 32e26a461..416f930bd 100644
--- a/test/agents/test_chat_agent.py
+++ b/test/agents/test_chat_agent.py
@@ -49,7 +49,7 @@ parametrize = pytest.mark.parametrize(
ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().__dict__,
+ model_config_dict=ChatGPTConfig().as_dict(),
),
pytest.param(None, marks=pytest.mark.model_backend),
],
@@ -178,8 +178,8 @@ def test_chat_agent_messages_window():
assistant.memory.write_records(
[
MemoryRecord(
- user_msg,
- OpenAIBackendRole.USER,
+ message=user_msg,
+ role_at_backend=OpenAIBackendRole.USER,
)
for _ in range(5)
]
@@ -220,7 +220,7 @@ def test_chat_agent_multiple_return_messages(n):
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=model_config.__dict__,
+ model_config_dict=model_config.as_dict(),
)
system_msg = BaseMessage(
"Assistant",
@@ -248,7 +248,7 @@ def test_chat_agent_multiple_return_message_error(n):
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=model_config.__dict__,
+ model_config_dict=model_config.as_dict(),
)
system_msg = BaseMessage(
"Assistant",
@@ -296,7 +296,7 @@ def test_chat_agent_stream_output():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=stream_model_config.__dict__,
+ model_config_dict=stream_model_config.as_dict(),
)
stream_assistant = ChatAgent(system_msg, model=model)
stream_assistant.reset()
@@ -404,7 +404,7 @@ def test_function_enabled():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=model_config.__dict__,
+ model_config_dict=model_config.as_dict(),
)
agent_no_func = ChatAgent(system_message=system_message)
agent_with_funcs = ChatAgent(
@@ -429,7 +429,7 @@ def test_tool_calling_sync():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=model_config.__dict__,
+ model_config_dict=model_config.as_dict(),
)
agent = ChatAgent(
system_message=system_message,
@@ -449,7 +449,9 @@ def test_tool_calling_sync():
)
agent_response = agent.step(user_msg)
- tool_calls: List[FunctionCallingRecord] = agent_response.info['tool_calls']
+ tool_calls: List[FunctionCallingRecord] = [
+ call for call in agent_response.info['tool_calls']
+ ]
for called_func in tool_calls:
print(str(called_func))
@@ -475,7 +477,7 @@ async def test_tool_calling_math_async():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=model_config.__dict__,
+ model_config_dict=model_config.as_dict(),
)
agent = ChatAgent(
system_message=system_message,
@@ -495,7 +497,9 @@ async def test_tool_calling_math_async():
)
agent_response = await agent.step_async(user_msg)
- tool_calls: List[FunctionCallingRecord] = agent_response.info['tool_calls']
+ tool_calls: List[FunctionCallingRecord] = [
+ call for call in agent_response.info['tool_calls']
+ ]
for called_func in tool_calls:
print(str(called_func))
@@ -534,7 +538,7 @@ async def test_tool_calling_async():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=model_config.__dict__,
+ model_config_dict=model_config.as_dict(),
)
agent = ChatAgent(
@@ -554,7 +558,9 @@ async def test_tool_calling_async():
)
agent_response = await agent.step_async(user_msg)
- tool_calls: List[FunctionCallingRecord] = agent_response.info['tool_calls']
+ tool_calls: List[FunctionCallingRecord] = [
+ call for call in agent_response.info['tool_calls']
+ ]
for called_func in tool_calls:
print(str(called_func))
@@ -602,7 +608,7 @@ def test_chat_agent_vision():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=model_config.__dict__,
+ model_config_dict=model_config.as_dict(),
)
agent = ChatAgent(
system_message=system_message,
diff --git a/test/agents/test_knowledge_agent.py b/test/agents/test_knowledge_agent.py
index 305e15175..80043c09e 100644
--- a/test/agents/test_knowledge_agent.py
+++ b/test/agents/test_knowledge_agent.py
@@ -11,8 +11,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from unittest.mock import Mock
-
from camel.agents import KnowledgeGraphAgent
from camel.storages.graph_storages.graph_element import Node, Relationship
@@ -31,10 +29,10 @@ def test_validate_node_invalid():
def test_validate_relationship_valid():
valid_relationship = Relationship(
- Node(id='subj_id', type='subj_type', properties={}),
- Node(id='obj_id', type='obj_type', properties={}),
- 'test_type',
- {},
+ subj=Node(id='subj_id', type='subj_type', properties={}),
+ obj=Node(id='obj_id', type='obj_type', properties={}),
+ type='test_type',
+ properties={},
)
assert agent._validate_relationship(valid_relationship)
@@ -45,7 +43,9 @@ def test_validate_relationship_invalid():
def test_parse_graph_elements():
- agent.element = Mock()
+ from unstructured.documents.elements import Element
+
+ agent.element = Element()
input_string = """
Node(id='node_id', type='node_type')
Relationship(subj=Node(id='subj_id', type='subj_type'),
diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.py
index e126d26d2..77110e380 100644
--- a/test/agents/test_role_playing.py
+++ b/test/agents/test_role_playing.py
@@ -25,7 +25,7 @@ from camel.types import ModelPlatformType, ModelType, RoleType, TaskType
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().__dict__,
+ model_config_dict=ChatGPTConfig().as_dict(),
)
@@ -142,7 +142,7 @@ def test_role_playing_with_function():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
- model_config_dict=assistant_model_config.__dict__,
+ model_config_dict=assistant_model_config.as_dict(),
)
role_playing = RolePlaying(
diff --git a/test/memories/test_agent_memories.py b/test/memories/test_agent_memories.py
index 2a2a67233..494affa27 100644
--- a/test/memories/test_agent_memories.py
+++ b/test/memories/test_agent_memories.py
@@ -88,13 +88,13 @@ class TestLongtermAgentMemory:
)
records = [
MemoryRecord(
- BaseMessage(
+ message=BaseMessage(
"user",
RoleType.USER,
None,
"test message {}".format(i),
),
- OpenAIBackendRole.USER,
+ role_at_backend=OpenAIBackendRole.USER,
)
for i in range(5)
]
diff --git a/test/memories/test_blocks.py b/test/memories/test_blocks.py
index 832d08db8..c858c9c38 100644
--- a/test/memories/test_blocks.py
+++ b/test/memories/test_blocks.py
@@ -92,13 +92,13 @@ class TestChatHistoryBlock:
chat_history = ChatHistoryBlock(storage=mock_storage)
records_to_write = [
MemoryRecord(
- BaseMessage(
+ message=BaseMessage(
"user",
RoleType.USER,
None,
"test message {}".format(i),
),
- OpenAIBackendRole.USER,
+ role_at_backend=OpenAIBackendRole.USER,
)
for i in range(5)
]
@@ -142,7 +142,7 @@ class TestVectorDBBlock:
# Create VectorDBQueryResult list using mock_records
mock_query_results = [
- VectorDBQueryResult.construct(
+ VectorDBQueryResult.create(
similarity=0.9,
vector=[0.1] * 128, # Example vector
id=record["uuid"],
@@ -167,13 +167,13 @@ class TestVectorDBBlock:
)
records_to_write = [
MemoryRecord(
- BaseMessage(
+ message=BaseMessage(
"user",
RoleType.USER,
None,
"test message {}".format(i),
),
- OpenAIBackendRole.USER,
+ role_at_backend=OpenAIBackendRole.USER,
)
for i in range(5)
]
diff --git a/test/memories/test_chat_history_memory.py b/test/memories/test_chat_history_memory.py
index 98ff545f2..0a16f4bc1 100644
--- a/test/memories/test_chat_history_memory.py
+++ b/test/memories/test_chat_history_memory.py
@@ -66,9 +66,15 @@ def test_chat_history_memory(memory: ChatHistoryMemory):
meta_dict=None,
content="OK",
)
- system_record = MemoryRecord(system_msg, OpenAIBackendRole.SYSTEM)
- user_record = MemoryRecord(user_msg, OpenAIBackendRole.USER)
- assistant_record = MemoryRecord(assistant_msg, OpenAIBackendRole.ASSISTANT)
+ system_record = MemoryRecord(
+ message=system_msg, role_at_backend=OpenAIBackendRole.SYSTEM
+ )
+ user_record = MemoryRecord(
+ message=user_msg, role_at_backend=OpenAIBackendRole.USER
+ )
+ assistant_record = MemoryRecord(
+ message=assistant_msg, role_at_backend=OpenAIBackendRole.ASSISTANT
+ )
memory.write_records([system_record, user_record, assistant_record])
output_messages, _ = memory.get_context()
assert output_messages[0] == system_msg.to_openai_system_message()
diff --git a/test/models/test_anthropic_model.py b/test/models/test_anthropic_model.py
index 6ea732011..861127a2d 100644
--- a/test/models/test_anthropic_model.py
+++ b/test/models/test_anthropic_model.py
@@ -34,8 +34,8 @@ from camel.utils import AnthropicTokenCounter
ModelType.CLAUDE_3_5_SONNET,
],
)
-def test_anthropic_model(model_type):
- model_config_dict = AnthropicConfig().__dict__
+def test_anthropic_model(model_type: ModelType):
+ model_config_dict = AnthropicConfig().as_dict()
model = AnthropicModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -51,7 +51,7 @@ def test_anthropic_model_unexpected_argument():
model_path="vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/models/test_azure_openai_model.py b/test/models/test_azure_openai_model.py
index e9b5043d6..36b36db97 100644
--- a/test/models/test_azure_openai_model.py
+++ b/test/models/test_azure_openai_model.py
@@ -43,7 +43,7 @@ from camel.utils import OpenAITokenCounter
],
)
def test_openai_model(model_type):
- model_config_dict = ChatGPTConfig().__dict__
+ model_config_dict = ChatGPTConfig().as_dict()
model = AzureOpenAIModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -67,7 +67,7 @@ def test_openai_model_create(model_type):
model = ModelFactory.create(
model_platform=ModelPlatformType.AZURE,
model_type=model_type,
- model_config_dict=ChatGPTConfig(temperature=0.8, n=3).__dict__,
+ model_config_dict=ChatGPTConfig(temperature=0.8, n=3).as_dict(),
)
assert model.model_type == model_type
@@ -79,7 +79,7 @@ def test_openai_model_unexpected_argument():
model_path="vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/models/test_gemini_model.py b/test/models/test_gemini_model.py
index 08d246549..d56122dd9 100644
--- a/test/models/test_gemini_model.py
+++ b/test/models/test_gemini_model.py
@@ -30,7 +30,7 @@ from camel.utils import GeminiTokenCounter
],
)
def test_gemini_model(model_type):
- model_config_dict = GeminiConfig().__dict__
+ model_config_dict = GeminiConfig().as_dict()
model = GeminiModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -46,7 +46,7 @@ def test_gemini_model_unexpected_argument():
model_path="vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/models/test_groq_llama3_model.py b/test/models/test_groq_llama3_model.py
index 226b1f2ae..32a606520 100644
--- a/test/models/test_groq_llama3_model.py
+++ b/test/models/test_groq_llama3_model.py
@@ -30,7 +30,7 @@ from camel.utils import OpenAITokenCounter
],
)
def test_groq_llama3_model(model_type):
- model_config_dict = GroqConfig().__dict__
+ model_config_dict = GroqConfig().as_dict()
model = GroqModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -48,7 +48,7 @@ def test_groq_llama3_model_unexpected_argument():
model_path="vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/models/test_litellm_model.py b/test/models/test_litellm_model.py
index 6fd33c649..9b78c9ced 100644
--- a/test/models/test_litellm_model.py
+++ b/test/models/test_litellm_model.py
@@ -32,24 +32,24 @@ from camel.utils import LiteLLMTokenCounter
ModelType.GPT_4O,
],
)
-def test_litellm_model(model_type):
- model_config_dict = LiteLLMConfig().__dict__
- model = LiteLLMModel(model_type, model_config_dict)
- assert model.model_type == model_type
+def test_litellm_model(model_type: ModelType):
+ model_config_dict = LiteLLMConfig().as_dict()
+ model = LiteLLMModel(model_type.value, model_config_dict)
+ assert model.model_type == model_type.value
assert model.model_config_dict == model_config_dict
assert isinstance(model.token_counter, LiteLLMTokenCounter)
- assert isinstance(model.model_type.value_for_tiktoken, str)
- assert isinstance(model.model_type.token_limit, int)
+ assert isinstance(model_type.value_for_tiktoken, str)
+ assert isinstance(model_type.token_limit, int)
@pytest.mark.model_backend
def test_litellm_model_unexpected_argument():
- model_type = ModelType.GPT_4
+ model_type = ModelType.GPT_4.value
model_config = OpenSourceConfig(
model_path="vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/models/test_mistral_model.py b/test/models/test_mistral_model.py
index 3dc4ae2cd..3aab51058 100644
--- a/test/models/test_mistral_model.py
+++ b/test/models/test_mistral_model.py
@@ -35,7 +35,7 @@ from camel.utils import MistralTokenCounter
],
)
def test_mistral_model(model_type):
- model_config_dict = MistralConfig().__dict__
+ model_config_dict = MistralConfig().as_dict()
model = MistralModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -51,7 +51,7 @@ def test_mistral_model_unexpected_argument():
model_path="vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/models/test_model_factory.py b/test/models/test_model_factory.py
index bb666dc6c..88605dfa7 100644
--- a/test/models/test_model_factory.py
+++ b/test/models/test_model_factory.py
@@ -46,7 +46,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.OPENAI,
ModelType.GPT_3_5_TURBO,
- ChatGPTConfig().__dict__,
+ ChatGPTConfig().as_dict(),
None,
OpenAITokenCounter,
ModelType.GPT_3_5_TURBO,
@@ -54,7 +54,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.OPENAI,
ModelType.GPT_4,
- ChatGPTConfig().__dict__,
+ ChatGPTConfig().as_dict(),
None,
OpenAITokenCounter,
ModelType.GPT_4,
@@ -64,7 +64,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.OPENSOURCE,
ModelType.STUB,
- ChatGPTConfig().__dict__,
+ ChatGPTConfig().as_dict(),
None,
StubTokenCounter,
None,
@@ -72,7 +72,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.OPENSOURCE,
ModelType.STUB,
- ChatGPTConfig().__dict__,
+ ChatGPTConfig().as_dict(),
OpenAITokenCounter(ModelType.GPT_4),
OpenAITokenCounter,
ModelType.GPT_4,
@@ -82,7 +82,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.ANTHROPIC,
ModelType.CLAUDE_2_0,
- AnthropicConfig().__dict__,
+ AnthropicConfig().as_dict(),
None,
AnthropicTokenCounter,
ModelType.CLAUDE_2_0,
@@ -90,7 +90,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.ANTHROPIC,
ModelType.CLAUDE_2_0,
- AnthropicConfig().__dict__,
+ AnthropicConfig().as_dict(),
OpenAITokenCounter(ModelType.GPT_3_5_TURBO),
OpenAITokenCounter,
ModelType.GPT_3_5_TURBO,
@@ -102,7 +102,7 @@ parameterize_token_counter = pytest.mark.parametrize(
OpenSourceConfig(
model_path="lmsys/vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
- ).__dict__,
+ ).as_dict(),
None,
OpenSourceTokenCounter,
ModelType.VICUNA,
@@ -113,7 +113,7 @@ parameterize_token_counter = pytest.mark.parametrize(
OpenSourceConfig(
model_path="lmsys/vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
- ).__dict__,
+ ).as_dict(),
OpenAITokenCounter(ModelType.GPT_4),
OpenAITokenCounter,
ModelType.GPT_4,
@@ -122,7 +122,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.GEMINI,
ModelType.GEMINI_1_5_FLASH,
- GeminiConfig().__dict__,
+ GeminiConfig().as_dict(),
OpenAITokenCounter(ModelType.GPT_4),
OpenAITokenCounter,
ModelType.GPT_4,
@@ -131,7 +131,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.OLLAMA,
"gpt-3.5-turbo",
- OllamaConfig().__dict__,
+ OllamaConfig().as_dict(),
None,
OpenAITokenCounter,
ModelType.GPT_3_5_TURBO,
@@ -139,7 +139,7 @@ parameterize_token_counter = pytest.mark.parametrize(
(
ModelPlatformType.OLLAMA,
"gpt-3.5-turbo",
- OllamaConfig().__dict__,
+ OllamaConfig().as_dict(),
OpenAITokenCounter(ModelType.GPT_4),
OpenAITokenCounter,
ModelType.GPT_4,
@@ -150,7 +150,7 @@ parameterize_token_counter = pytest.mark.parametrize(
@parametrize
def test_model_factory(model_platform, model_type):
- model_config_dict = ChatGPTConfig().__dict__
+ model_config_dict = ChatGPTConfig().as_dict()
model_inst = ModelFactory.create(
model_platform, model_type, model_config_dict
)
diff --git a/test/models/test_ollama_model.py b/test/models/test_ollama_model.py
index c0ed5f630..806c365ff 100644
--- a/test/models/test_ollama_model.py
+++ b/test/models/test_ollama_model.py
@@ -33,7 +33,7 @@ from camel.utils import OpenAITokenCounter
],
)
def test_ollama_model(model_type: ModelType):
- model_config_dict = OllamaConfig().__dict__
+ model_config_dict = OllamaConfig().as_dict()
model = OllamaModel(model_type.value, model_config_dict)
assert model.model_type == model_type.value
assert model.model_config_dict == model_config_dict
diff --git a/test/models/test_open_source_model.py b/test/models/test_open_source_model.py
index 2c775541d..e48c03e99 100644
--- a/test/models/test_open_source_model.py
+++ b/test/models/test_open_source_model.py
@@ -45,13 +45,13 @@ def test_open_source_model(model_type):
model_path=model_path,
server_url=DEFAULT_SERVER_URL,
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
model = OpenSourceModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_name == model_name
assert model.server_url == model_config.server_url
- assert model.model_config_dict == model_config.api_params.__dict__
+ assert model.model_config_dict == model_config.api_params.as_dict()
assert isinstance(model.token_counter, OpenSourceTokenCounter)
assert isinstance(model.model_type.value_for_tiktoken, str)
@@ -70,7 +70,7 @@ def test_open_source_model_run(model_type):
model_path=model_path,
server_url=DEFAULT_SERVER_URL,
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
model = OpenSourceModel(model_type, model_config_dict)
messages = [{"role": "user", "content": "Tell me a joke."}]
@@ -87,7 +87,7 @@ def test_open_source_model_close_source_model_type():
model_path=model_path,
server_url=DEFAULT_SERVER_URL,
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
@@ -105,7 +105,7 @@ def test_open_source_model_close_source_model_type():
def test_open_source_model_mismatched_model_config():
model_type = ModelType.VICUNA
model_config = ChatGPTConfig()
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
@@ -128,7 +128,7 @@ def test_open_source_model_unexpected_argument():
server_url=DEFAULT_SERVER_URL,
api_params=ChatGPTConfig(),
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
TypeError,
@@ -149,7 +149,7 @@ def test_open_source_model_invalid_model_path():
model_path=model_path,
server_url=DEFAULT_SERVER_URL,
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
@@ -172,7 +172,7 @@ def test_open_source_model_unmatched_model_path():
model_path=model_path,
server_url=DEFAULT_SERVER_URL,
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
@@ -188,10 +188,10 @@ def test_open_source_model_unmatched_model_path():
def test_open_source_model_missing_model_path():
model_type = ModelType.VICUNA
model_config = OpenSourceConfig(
- model_path=None,
+ model_path="",
server_url=DEFAULT_SERVER_URL,
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError, match=("Path to open-source model is not provided.")
@@ -203,8 +203,8 @@ def test_open_source_model_missing_model_path():
def test_open_source_model_missing_server_url():
model_type = ModelType.VICUNA
model_path = MODEL_PATH_MAP[ModelType.VICUNA]
- model_config = OpenSourceConfig(model_path=model_path, server_url=None)
- model_config_dict = model_config.__dict__
+ model_config = OpenSourceConfig(model_path=model_path, server_url="")
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/models/test_openai_model.py b/test/models/test_openai_model.py
index d84c70145..6d72f48f2 100644
--- a/test/models/test_openai_model.py
+++ b/test/models/test_openai_model.py
@@ -34,7 +34,7 @@ from camel.utils import OpenAITokenCounter
],
)
def test_openai_model(model_type):
- model_config_dict = ChatGPTConfig().__dict__
+ model_config_dict = ChatGPTConfig().as_dict()
model = OpenAIModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -50,7 +50,7 @@ def test_openai_model_unexpected_argument():
model_path="vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/models/test_vllm_model.py b/test/models/test_vllm_model.py
index a6fab1c55..b65481c3e 100644
--- a/test/models/test_vllm_model.py
+++ b/test/models/test_vllm_model.py
@@ -33,7 +33,7 @@ from camel.utils import OpenAITokenCounter
],
)
def test_vllm_model(model_type: ModelType):
- model_config_dict = VLLMConfig().__dict__
+ model_config_dict = VLLMConfig().as_dict()
model = VLLMModel(model_type.value, model_config_dict, api_key="vllm")
assert model.model_type == model_type.value
assert model.model_config_dict == model_config_dict
diff --git a/test/models/test_zhipuai_model.py b/test/models/test_zhipuai_model.py
index 8568d022b..a69f11eb6 100644
--- a/test/models/test_zhipuai_model.py
+++ b/test/models/test_zhipuai_model.py
@@ -31,7 +31,7 @@ from camel.utils import OpenAITokenCounter
],
)
def test_zhipuai_model(model_type):
- model_config_dict = ZhipuAIConfig().__dict__
+ model_config_dict = ZhipuAIConfig().as_dict()
model = ZhipuAIModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
@@ -47,7 +47,7 @@ def test_zhipuai_model_unexpected_argument():
model_path="vicuna-7b-v1.5",
server_url="http://localhost:8000/v1",
)
- model_config_dict = model_config.__dict__
+ model_config_dict = model_config.as_dict()
with pytest.raises(
ValueError,
diff --git a/test/retrievers/test_vector_retriever.py b/test/retrievers/test_vector_retriever.py
index 49a31b9ce..718e5c9e2 100644
--- a/test/retrievers/test_vector_retriever.py
+++ b/test/retrievers/test_vector_retriever.py
@@ -26,7 +26,9 @@ MockUnstructuredIO = Mock()
@pytest.fixture
def mock_embedding_model():
- return MockBaseEmbedding()
+ mock_instance = MockBaseEmbedding()
+ mock_instance.embed.return_value = [0.0, 0.0]
+ return mock_instance
@pytest.fixture