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

refactor: `ModelType` (#998)

Isaac Jinfb35857776b6
diff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.yml
index 5f5ae3724..6c6510d08 100644
--- a/.github/workflows/build_package.yml
+++ b/.github/workflows/build_package.yml
@@ -59,7 +59,7 @@ jobs:
AZURE_OPENAI_API_KEY: "${{ secrets.AZURE_OPENAI_API_KEY }}"
AZURE_API_VERSION: "${{ secrets.AZURE_API_VERSION }}"
AZURE_DEPLOYMENT_NAME: "${{ secrets.AZURE_DEPLOYMENT_NAME }}"
- AZURE_OPENAI_ENDPOINT: "${{ secrets.AZURE_OPENAI_ENDPOINT }}"
+ AZURE_OPENAI_BASE_URL: "${{ secrets.AZURE_OPENAI_BASE_URL }}"
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
diff --git a/.github/workflows/pytest_package.yml b/.github/workflows/pytest_package.yml
index 6d1889f92..639bff6fb 100644
--- a/.github/workflows/pytest_package.yml
+++ b/.github/workflows/pytest_package.yml
@@ -38,7 +38,7 @@ jobs:
AZURE_OPENAI_API_KEY: "to-be-filled"
AZURE_API_VERSION: "to-be-filled"
AZURE_DEPLOYMENT_NAME: "to-be-filled"
- AZURE_OPENAI_ENDPOINT: "https://camel.openai.azure.com/"
+ AZURE_OPENAI_BASE_URL: "https://camel.openai.azure.com/"
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
@@ -74,7 +74,7 @@ jobs:
AZURE_OPENAI_API_KEY: "to-be-filled"
AZURE_API_VERSION: "to-be-filled"
AZURE_DEPLOYMENT_NAME: "to-be-filled"
- AZURE_OPENAI_ENDPOINT: "https://camel.openai.azure.com/"
+ AZURE_OPENAI_BASE_URL: "https://camel.openai.azure.com/"
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
@@ -110,7 +110,7 @@ jobs:
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }}"
AZURE_API_VERSION: ${{ secrets.AZURE_API_VERSION }}"
AZURE_DEPLOYMENT_NAME: ${{ secrets.AZURE_DEPLOYMENT_NAME }}"
- AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }}"
+ AZURE_OPENAI_BASE_URL: ${{ secrets.AZURE_OPENAI_BASE_URL }}"
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 8a76d4259..c49a9ee72 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -34,7 +34,6 @@ from openai.types.chat.chat_completion_message_tool_call import Function
from pydantic import BaseModel
from camel.agents.base import BaseAgent
-from camel.configs import ChatGPTConfig
from camel.memories import (
AgentMemory,
ChatHistoryMemory,
@@ -169,14 +168,13 @@ class ChatAgent(BaseAgent):
else ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
)
self.output_language: Optional[str] = output_language
if self.output_language is not None:
self.set_output_language(self.output_language)
- self.model_type: ModelType = self.model_backend.model_type
+ self.model_type = self.model_backend.model_type
# tool registration
external_tools = external_tools or []
@@ -439,12 +437,7 @@ class ChatAgent(BaseAgent):
a boolean indicating whether the chat session has terminated,
and information about the chat session.
"""
- if (
- isinstance(self.model_type, ModelType)
- and "lama" in self.model_type.value
- or isinstance(self.model_type, str)
- and "lama" in self.model_type
- ):
+ if "llama" in self.model_type.lower():
if self.model_backend.model_config_dict.get("tools", None):
tool_prompt = self._generate_tool_prompt(self.tool_schema_list)
@@ -525,10 +518,7 @@ class ChatAgent(BaseAgent):
self._step_tool_call_and_update(response)
)
- if (
- output_schema is not None
- and self.model_type.supports_tool_calling
- ):
+ if output_schema is not None:
(
output_messages,
finish_reasons,
@@ -619,7 +609,7 @@ class ChatAgent(BaseAgent):
if (
output_schema is not None
- and self.model_type.supports_tool_calling
+ and self.model_type.support_native_tool_calling
):
(
output_messages,
@@ -727,7 +717,10 @@ class ChatAgent(BaseAgent):
await self._step_tool_call_and_update_async(response)
)
- if output_schema is not None and self.model_type.supports_tool_calling:
+ if (
+ output_schema is not None
+ and self.model_type.support_native_tool_calling
+ ):
(
output_messages,
finish_reasons,
@@ -1193,10 +1186,7 @@ class ChatAgent(BaseAgent):
Returns:
dict: Usage dictionary.
"""
- if isinstance(self.model_type, ModelType):
- encoding = get_model_encoding(self.model_type.value_for_tiktoken)
- else:
- encoding = get_model_encoding("gpt-4o-mini")
+ encoding = get_model_encoding(self.model_type.value_for_tiktoken)
completion_tokens = 0
for message in output_messages:
completion_tokens += len(encoding.encode(message.content))
diff --git a/camel/configs/__init__.py b/camel/configs/__init__.py
index a6a061975..d3e1f9bf8 100644
--- a/camel/configs/__init__.py
+++ b/camel/configs/__init__.py
@@ -18,7 +18,7 @@ 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
from .reka_config import REKA_API_PARAMS, RekaConfig
from .samba_config import (
SAMBA_CLOUD_API_PARAMS,
@@ -40,7 +40,6 @@ __all__ = [
'ANTHROPIC_API_PARAMS',
'GROQ_API_PARAMS',
'GroqConfig',
- 'OpenSourceConfig',
'LiteLLMConfig',
'LITELLM_API_PARAMS',
'OllamaConfig',
diff --git a/camel/configs/openai_config.py b/camel/configs/openai_config.py
index 971232ad8..b90486fe1 100644
--- a/camel/configs/openai_config.py
+++ b/camel/configs/openai_config.py
@@ -112,24 +112,3 @@ class ChatGPTConfig(BaseConfig):
OPENAI_API_PARAMS = {param for param in ChatGPTConfig.model_fields.keys()}
-
-
-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.
-
- Args:
- model_path (str): The path to a local folder containing the model
- files or the model card in HuggingFace hub.
- server_url (str): The URL to the server running the model inference
- which will be used as the API base of OpenAI API.
- api_params (ChatGPTConfig): An instance of :obj:ChatGPTConfig to
- 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)
diff --git a/camel/models/__init__.py b/camel/models/__init__.py
index f0702e4ad..780c647c6 100644
--- a/camel/models/__init__.py
+++ b/camel/models/__init__.py
@@ -21,9 +21,8 @@ from .mistral_model import MistralModel
from .model_factory import ModelFactory
from .nemotron_model import NemotronModel
from .ollama_model import OllamaModel
-from .open_source_model import OpenSourceModel
from .openai_audio_models import OpenAIAudioModels
-from .openai_compatibility_model import OpenAICompatibilityModel
+from .openai_compatible_model import OpenAICompatibleModel
from .openai_model import OpenAIModel
from .reka_model import RekaModel
from .samba_model import SambaModel
@@ -41,7 +40,6 @@ __all__ = [
'GroqModel',
'StubModel',
'ZhipuAIModel',
- 'OpenSourceModel',
'ModelFactory',
'LiteLLMModel',
'OpenAIAudioModels',
@@ -49,7 +47,7 @@ __all__ = [
'OllamaModel',
'VLLMModel',
'GeminiModel',
- 'OpenAICompatibilityModel',
+ 'OpenAICompatibleModel',
'RekaModel',
'SambaModel',
'TogetherAIModel',
diff --git a/camel/models/anthropic_model.py b/camel/models/anthropic_model.py
index a04267014..e5056ae10 100644
--- a/camel/models/anthropic_model.py
+++ b/camel/models/anthropic_model.py
@@ -12,11 +12,11 @@
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
import os
-from typing import Any, Dict, List, Optional
+from typing import Any, Dict, List, Optional, Union
from anthropic import NOT_GIVEN, Anthropic
-from camel.configs import ANTHROPIC_API_PARAMS
+from camel.configs import ANTHROPIC_API_PARAMS, AnthropicConfig
from camel.messages import OpenAIMessage
from camel.models.base_model import BaseModelBackend
from camel.types import ChatCompletion, ModelType
@@ -28,36 +28,39 @@ from camel.utils import (
class AnthropicModel(BaseModelBackend):
- r"""Anthropic API in a unified BaseModelBackend interface."""
+ r"""Anthropic API in a unified BaseModelBackend interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, one of CLAUDE_* series.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into Anthropic.messages.create(). If
+ :obj:`None`, :obj:`AnthropicConfig().as_dict()` will be used.
+ (default::obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the Anthropic service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the Anthropic service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`AnthropicTokenCounter`
+ will be used. (default: :obj:`None`)
+ """
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for Anthropic backend.
-
- Args:
- model_type (ModelType): Model for which a backend is created,
- one of CLAUDE_* series.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into Anthropic.messages.create().
- api_key (Optional[str]): The API key for authenticating with the
- Anthropic service. (default: :obj:`None`)
- url (Optional[str]): The url to the Anthropic service. (default:
- :obj:`None`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `AnthropicTokenCounter` will
- be used.
- """
+ if model_config_dict is None:
+ model_config_dict = AnthropicConfig().as_dict()
+ api_key = api_key or os.environ.get("ANTHROPIC_API_KEY")
+ url = url or os.environ.get("ANTHROPIC_API_BASE_URL")
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
- self._api_key = api_key or os.environ.get("ANTHROPIC_API_KEY")
- self._url = url or os.environ.get("ANTHROPIC_API_BASE_URL")
self.client = Anthropic(api_key=self._api_key, base_url=self._url)
def _convert_response_from_anthropic_to_openai(self, response):
@@ -89,7 +92,7 @@ class AnthropicModel(BaseModelBackend):
tokenization style.
"""
if not self._token_counter:
- self._token_counter = AnthropicTokenCounter(self.model_type)
+ self._token_counter = AnthropicTokenCounter()
return self._token_counter
def count_tokens_from_prompt(self, prompt: str) -> int:
@@ -123,7 +126,7 @@ class AnthropicModel(BaseModelBackend):
else:
sys_msg = NOT_GIVEN # type: ignore[assignment]
response = self.client.messages.create(
- model=self.model_type.value,
+ model=self.model_type,
system=sys_msg,
messages=messages, # type: ignore[arg-type]
**self.model_config_dict,
diff --git a/camel/models/azure_openai_model.py b/camel/models/azure_openai_model.py
index a4b59a0ef..4e5290b10 100644
--- a/camel/models/azure_openai_model.py
+++ b/camel/models/azure_openai_model.py
@@ -16,60 +16,65 @@ from typing import Any, Dict, List, Optional, Union
from openai import AzureOpenAI, Stream
-from camel.configs import OPENAI_API_PARAMS
+from camel.configs import OPENAI_API_PARAMS, ChatGPTConfig
from camel.messages import OpenAIMessage
from camel.models.base_model import BaseModelBackend
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+)
from camel.utils import BaseTokenCounter, OpenAITokenCounter, api_keys_required
class AzureOpenAIModel(BaseModelBackend):
r"""Azure OpenAI API in a unified BaseModelBackend interface.
- Doc: https://learn.microsoft.com/en-us/azure/ai-services/openai/
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, one of GPT_* series.
+ 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`)
+ url (Optional[str], optional): The url to the OpenAI service.
+ (default: :obj:`None`)
+ api_version (Optional[str], optional): The api version for the model.
+ (default: :obj:`None`)
+ azure_deployment_name (Optional[str], optional): The deployment name
+ you chose when you deployed an azure model. (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter`
+ will be used. (default: :obj:`None`)
+
+ References:
+ https://learn.microsoft.com/en-us/azure/ai-services/openai/
"""
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
+ token_counter: Optional[BaseTokenCounter] = None,
api_version: Optional[str] = None,
azure_deployment_name: Optional[str] = None,
) -> None:
- r"""Constructor for OpenAI backend.
+ if model_config_dict is None:
+ model_config_dict = ChatGPTConfig().as_dict()
+ api_key = api_key or os.environ.get("AZURE_OPENAI_API_KEY")
+ url = url or os.environ.get("AZURE_OPENAI_BASE_URL")
+ super().__init__(
+ model_type, model_config_dict, api_key, url, token_counter
+ )
- Args:
- model_type (ModelType): Model for which a backend is created,
- one of GPT_* series.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into openai.ChatCompletion.create().
- api_key (Optional[str]): The API key for authenticating with the
- OpenAI service. (default: :obj:`None`)
- url (Optional[str]): The url to the OpenAI service. (default:
- :obj:`None`)
- api_version (Optional[str]): The api version for the model.
- azure_deployment_name (Optional[str]): The deployment name you
- chose when you deployed an azure model. (default: :obj:`None`)
- """
- super().__init__(model_type, model_config_dict, api_key, url)
- self._url = url or os.environ.get("AZURE_OPENAI_ENDPOINT")
- self._api_key = api_key or os.environ.get("AZURE_OPENAI_API_KEY")
self.api_version = api_version or os.environ.get("AZURE_API_VERSION")
self.azure_deployment_name = azure_deployment_name or os.environ.get(
"AZURE_DEPLOYMENT_NAME"
)
-
- if self._url is None:
- raise ValueError(
- "Must provide either the `url` argument "
- "or `AZURE_OPENAI_ENDPOINT` environment variable."
- )
- if self._api_key is None:
- raise ValueError(
- "Must provide either the `api_key` argument "
- "or `AZURE_OPENAI_API_KEY` environment variable."
- )
if self.api_version is None:
raise ValueError(
"Must provide either the `api_version` argument "
@@ -80,7 +85,6 @@ class AzureOpenAIModel(BaseModelBackend):
"Must provide either the `azure_deployment_name` argument "
"or `AZURE_DEPLOYMENT_NAME` environment variable."
)
- self.model = str(self.azure_deployment_name)
self._client = AzureOpenAI(
azure_endpoint=str(self._url),
@@ -90,7 +94,6 @@ class AzureOpenAIModel(BaseModelBackend):
timeout=60,
max_retries=3,
)
- self._token_counter: Optional[BaseTokenCounter] = None
@property
def token_counter(self) -> BaseTokenCounter:
@@ -122,7 +125,7 @@ class AzureOpenAIModel(BaseModelBackend):
"""
response = self._client.chat.completions.create(
messages=messages,
- model=self.model,
+ model=self.azure_deployment_name, # type:ignore[arg-type]
**self.model_config_dict,
)
return response
diff --git a/camel/models/base_model.py b/camel/models/base_model.py
index aa7df0b12..84317fae8 100644
--- a/camel/models/base_model.py
+++ b/camel/models/base_model.py
@@ -17,41 +17,49 @@ from typing import Any, Dict, List, Optional, Union
from openai import Stream
from camel.messages import OpenAIMessage
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+ UnifiedModelType,
+)
from camel.utils import BaseTokenCounter
class BaseModelBackend(ABC):
r"""Base class for different model backends.
- May be OpenAI API, a local LLM, a stub for unit tests, etc.
+ It may be OpenAI API, a local LLM, a stub for unit tests, etc.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created.
+ model_config_dict (Optional[Dict[str, Any]], optional): A config
+ dictionary. (default: :obj:`{}`)
+ api_key (Optional[str], optional): The API key for authenticating
+ with the model service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the model service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token
+ counter to use for the model. If not provided,
+ :obj:`OpenAITokenCounter` will be used. (default: :obj:`None`)
"""
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for the model backend.
-
- Args:
- model_type (ModelType): Model for which a backend is created.
- model_config_dict (Dict[str, Any]): A config dictionary.
- api_key (Optional[str]): The API key for authenticating with the
- model service.
- url (Optional[str]): The url to the model service.
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter` will
- be used.
- """
- self.model_type = model_type
+ self.model_type: UnifiedModelType = UnifiedModelType(model_type)
+ if model_config_dict is None:
+ model_config_dict = {}
self.model_config_dict = model_config_dict
self._api_key = api_key
self._url = url
- self.check_model_config()
self._token_counter = token_counter
+ self.check_model_config()
@property
@abstractmethod
diff --git a/camel/models/gemini_model.py b/camel/models/gemini_model.py
index f98bfa82c..12034dd28 100644
--- a/camel/models/gemini_model.py
+++ b/camel/models/gemini_model.py
@@ -11,9 +11,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from typing import TYPE_CHECKING, Any, Dict, List, Optional
+import os
+from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
-from camel.configs import Gemini_API_PARAMS
+from camel.configs import Gemini_API_PARAMS, GeminiConfig
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
from camel.types import (
@@ -33,43 +34,48 @@ if TYPE_CHECKING:
class GeminiModel(BaseModelBackend):
- r"""Gemini API in a unified BaseModelBackend interface."""
-
- # NOTE: Currently "stream": True is not supported with Gemini due to the
- # limitation of the current camel design.
+ r"""Gemini API in a unified BaseModelBackend interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`genai.GenerativeModel.generate_content()
+ `. If:obj:`None`, :obj:`GeminiConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the gemini service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the gemini service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`GeminiTokenCounter` will
+ be used. (default: :obj:`None`)
+
+ Notes:
+ Currently :obj:`"stream": True` is not supported with Gemini due to the
+ limitation of the current camel design.
+ """
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for Gemini backend.
-
- Args:
- model_type (ModelType): Model for which a backend is created.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into generate_content().
- api_key (Optional[str]): The API key for authenticating with the
- gemini service. (default: :obj:`None`)
- url (Optional[str]): The url to the gemini service.
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `GeminiTokenCounter` will be
- used.
- """
- import os
-
import google.generativeai as genai
from google.generativeai.types.generation_types import GenerationConfig
+ if model_config_dict is None:
+ model_config_dict = GeminiConfig().as_dict()
+
+ api_key = api_key or os.environ.get("GOOGLE_API_KEY")
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
- self._api_key = api_key or os.environ.get("GOOGLE_API_KEY")
genai.configure(api_key=self._api_key)
- self._client = genai.GenerativeModel(self.model_type.value)
+ self._client = genai.GenerativeModel(self.model_type)
keys = list(self.model_config_dict.keys())
generation_config_dict = {
@@ -143,8 +149,8 @@ class GeminiModel(BaseModelBackend):
return self.model_config_dict.get('stream', False)
def to_gemini_req(self, messages: List[OpenAIMessage]) -> 'ContentsType':
- r"""Converts the request from the OpenAI API format to
- the Gemini API request format.
+ r"""Converts the request from the OpenAI API format to the Gemini API
+ request format.
Args:
messages: The request object from the OpenAI API.
@@ -189,7 +195,7 @@ class GeminiModel(BaseModelBackend):
id=f"chatcmpl-{uuid.uuid4().hex!s}",
object="chat.completion",
created=int(time.time()),
- model=self.model_type.value,
+ model=self.model_type,
choices=[],
)
for i, candidate in enumerate(response.candidates):
diff --git a/camel/models/groq_model.py b/camel/models/groq_model.py
index 255c820b8..875fe0a94 100644
--- a/camel/models/groq_model.py
+++ b/camel/models/groq_model.py
@@ -16,7 +16,7 @@ from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
-from camel.configs import GROQ_API_PARAMS
+from camel.configs import GROQ_API_PARAMS, GroqConfig
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
from camel.types import (
@@ -32,42 +32,48 @@ from camel.utils import (
class GroqModel(BaseModelBackend):
- r"""LLM API served by Groq in a unified BaseModelBackend interface."""
+ r"""LLM API served by Groq in a unified BaseModelBackend interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`.
+ If:obj:`None`, :obj:`GroqConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating
+ with the Groq service. (default: :obj:`None`).
+ url (Optional[str], optional): The url to the Groq service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
+ (default: :obj:`None`)
+ """
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for Groq backend.
-
- Args:
- model_type (str): Model for which a backend is created.
- model_config_dict (Dict[str, Any]): A dictionary of parameters for
- the model configuration.
- api_key (Optional[str]): The API key for authenticating with the
- Groq service. (default: :obj:`None`).
- url (Optional[str]): The url to the Groq service. (default:
- :obj:`"https://api.groq.com/openai/v1"`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter(ModelType.
- GPT_4O_MINI)` will be used.
- """
+ if model_config_dict is None:
+ model_config_dict = GroqConfig().as_dict()
+ api_key = api_key or os.environ.get("GROQ_API_KEY")
+ url = url or os.environ.get(
+ "GROQ_API_BASE_URL" or "https://api.groq.com/openai/v1"
+ )
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
- self._url = url or os.environ.get("GROQ_API_BASE_URL")
- self._api_key = api_key or os.environ.get("GROQ_API_KEY")
self._client = OpenAI(
timeout=60,
max_retries=3,
api_key=self._api_key,
- base_url=self._url or "https://api.groq.com/openai/v1",
+ base_url=self._url,
)
- self._token_counter = token_counter
@property
def token_counter(self) -> BaseTokenCounter:
@@ -101,7 +107,7 @@ class GroqModel(BaseModelBackend):
"""
response = self._client.chat.completions.create(
messages=messages,
- model=self.model_type.value,
+ model=self.model_type,
**self.model_config_dict,
)
diff --git a/camel/models/litellm_model.py b/camel/models/litellm_model.py
index f94899756..a52532b27 100644
--- a/camel/models/litellm_model.py
+++ b/camel/models/litellm_model.py
@@ -11,49 +11,53 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from typing import Any, Dict, List, Optional
+from typing import Any, Dict, List, Optional, Union
-from camel.configs import LITELLM_API_PARAMS
+from camel.configs import LITELLM_API_PARAMS, LiteLLMConfig
from camel.messages import OpenAIMessage
-from camel.types import ChatCompletion
+from camel.models import BaseModelBackend
+from camel.types import ChatCompletion, ModelType
from camel.utils import BaseTokenCounter, LiteLLMTokenCounter
-class LiteLLMModel:
- r"""Constructor for LiteLLM backend with OpenAI compatibility."""
+class LiteLLMModel(BaseModelBackend):
+ r"""Constructor for LiteLLM backend with OpenAI compatibility.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, such as GPT-3.5-turbo, Claude-2, etc.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`.
+ If:obj:`None`, :obj:`LiteLLMConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the model service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the model service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`LiteLLMTokenCounter` will
+ be used. (default: :obj:`None`)
+ """
# NOTE: Currently stream mode is not supported.
def __init__(
self,
- model_type: str,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for LiteLLM backend.
+ from litellm import completion
- Args:
- model_type (str): Model for which a backend is created,
- such as GPT-3.5-turbo, Claude-2, etc.
- model_config_dict (Dict[str, Any]): A dictionary of parameters for
- the model configuration.
- api_key (Optional[str]): The API key for authenticating with the
- model service. (default: :obj:`None`)
- url (Optional[str]): The url to the model service. (default:
- :obj:`None`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `LiteLLMTokenCounter` will
- be used.
- """
- self.model_type = model_type
- self.model_config_dict = model_config_dict
- self._client = None
- self._token_counter = token_counter
- self.check_model_config()
- self._url = url
- self._api_key = api_key
+ if model_config_dict is None:
+ model_config_dict = LiteLLMConfig().as_dict()
+
+ super().__init__(
+ model_type, model_config_dict, api_key, url, token_counter
+ )
+ self.client = completion
def _convert_response_from_litellm_to_openai(
self, response
@@ -86,26 +90,16 @@ class LiteLLMModel:
)
@property
- def client(self):
- if self._client is None:
- from litellm import completion
-
- self._client = completion
- return self._client
-
- @property
- def token_counter(self) -> LiteLLMTokenCounter:
+ def token_counter(self) -> BaseTokenCounter:
r"""Initialize the token counter for the model backend.
Returns:
- LiteLLMTokenCounter: The token counter following the model's
+ BaseTokenCounter: The token counter following the model's
tokenization style.
"""
if not self._token_counter:
- self._token_counter = LiteLLMTokenCounter( # type: ignore[assignment]
- self.model_type
- )
- return self._token_counter # type: ignore[return-value]
+ self._token_counter = LiteLLMTokenCounter(self.model_type)
+ return self._token_counter
def run(
self,
diff --git a/camel/models/mistral_model.py b/camel/models/mistral_model.py
index 6d3c754a4..1ef121396 100644
--- a/camel/models/mistral_model.py
+++ b/camel/models/mistral_model.py
@@ -12,7 +12,7 @@
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
import os
-from typing import TYPE_CHECKING, Any, Dict, List, Optional
+from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
if TYPE_CHECKING:
from mistralai.models import (
@@ -20,7 +20,7 @@ if TYPE_CHECKING:
Messages,
)
-from camel.configs import MISTRAL_API_PARAMS
+from camel.configs import MISTRAL_API_PARAMS, MistralConfig
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
from camel.types import ChatCompletion, ModelType
@@ -31,8 +31,6 @@ from camel.utils import (
)
try:
- import os
-
if os.getenv("AGENTOPS_API_KEY") is not None:
from agentops import LLMEvent, record
else:
@@ -42,40 +40,43 @@ except (ImportError, AttributeError):
class MistralModel(BaseModelBackend):
- r"""Mistral API in a unified BaseModelBackend interface."""
+ r"""Mistral API in a unified BaseModelBackend interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, one of MISTRAL_* series.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`Mistral.chat.complete()`.
+ If:obj:`None`, :obj:`MistralConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the mistral service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the mistral service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter` will
+ be used. (default: :obj:`None`)
+ """
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for Mistral backend.
+ from mistralai import Mistral
- Args:
- model_type (ModelType): Model for which a backend is created,
- one of MISTRAL_* series.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into `MistralClient.chat`.
- api_key (Optional[str]): The API key for authenticating with the
- mistral service. (default: :obj:`None`)
- url (Optional[str]): The url to the mistral service.
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter` will be
- used.
- """
+ if model_config_dict is None:
+ model_config_dict = MistralConfig().as_dict()
+
+ api_key = api_key or os.environ.get("MISTRAL_API_KEY")
+ url = url or os.environ.get("MISTRAL_API_BASE_URL")
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
- self._api_key = api_key or os.environ.get("MISTRAL_API_KEY")
- self._url = url or os.environ.get("MISTRAL_SERVER_URL")
-
- from mistralai import Mistral
-
self._client = Mistral(api_key=self._api_key, server_url=self._url)
- self._token_counter: Optional[BaseTokenCounter] = None
def _to_openai_response(
self, response: 'ChatCompletionResponse'
@@ -215,7 +216,7 @@ class MistralModel(BaseModelBackend):
response = self._client.chat.complete(
messages=mistral_messages,
- model=self.model_type.value,
+ model=self.model_type,
**self.model_config_dict,
)
@@ -231,7 +232,7 @@ class MistralModel(BaseModelBackend):
prompt_tokens=openai_response.usage.prompt_tokens, # type: ignore[union-attr]
completion=openai_response.choices[0].message.content,
completion_tokens=openai_response.usage.completion_tokens, # type: ignore[union-attr]
- model=self.model_type.value,
+ model=self.model_type,
)
record(llm_event)
diff --git a/camel/models/model_factory.py b/camel/models/model_factory.py
index 34a0635ad..f8862c746 100644
--- a/camel/models/model_factory.py
+++ b/camel/models/model_factory.py
@@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-from typing import Any, Dict, Optional, Union
+from typing import Dict, Optional, Type, Union
from camel.models.anthropic_model import AnthropicModel
from camel.models.azure_openai_model import AzureOpenAIModel
@@ -21,8 +21,7 @@ from camel.models.groq_model import GroqModel
from camel.models.litellm_model import LiteLLMModel
from camel.models.mistral_model import MistralModel
from camel.models.ollama_model import OllamaModel
-from camel.models.open_source_model import OpenSourceModel
-from camel.models.openai_compatibility_model import OpenAICompatibilityModel
+from camel.models.openai_compatible_model import OpenAICompatibleModel
from camel.models.openai_model import OpenAIModel
from camel.models.reka_model import RekaModel
from camel.models.samba_model import SambaModel
@@ -30,7 +29,7 @@ from camel.models.stub_model import StubModel
from camel.models.togetherai_model import TogetherAIModel
from camel.models.vllm_model import VLLMModel
from camel.models.zhipuai_model import ZhipuAIModel
-from camel.types import ModelPlatformType, ModelType
+from camel.types import ModelPlatformType, ModelType, UnifiedModelType
from camel.utils import BaseTokenCounter
@@ -45,7 +44,7 @@ class ModelFactory:
def create(
model_platform: ModelPlatformType,
model_type: Union[ModelType, str],
- model_config_dict: Dict,
+ model_config_dict: Optional[Dict] = None,
token_counter: Optional[BaseTokenCounter] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
@@ -55,80 +54,71 @@ class ModelFactory:
Args:
model_platform (ModelPlatformType): Platform from which the model
originates.
- model_type (Union[ModelType, str]): Model for which a backend is
- created can be a `str` for open source platforms.
- model_config_dict (Dict): A dictionary that will be fed into
- the backend constructor.
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, OpenAITokenCounter(ModelType.
- GPT_3_5_TURBO) will be used if the model platform didn't
- provide official token counter.
- api_key (Optional[str]): The API key for authenticating with the
- model service.
- url (Optional[str]): The url to the model service.
-
- Raises:
- ValueError: If there is not backend for the model.
+ model_type (Union[ModelType, str]): Model for which a
+ backend is created. Can be a `str` for open source platforms.
+ model_config_dict (Optional[Dict]): A dictionary that will be fed
+ into the backend constructor. (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token
+ counter to use for the model. If not provided,
+ :obj:`OpenAITokenCounter(ModelType.GPT_4O_MINI)`
+ will be used if the model platform didn't provide official
+ token counter. (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating
+ with the model service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the model service.
+ (default: :obj:`None`)
Returns:
BaseModelBackend: The initialized backend.
+
+ Raises:
+ ValueError: If there is no backend for the model.
"""
- model_class: Any
- if isinstance(model_type, ModelType):
- if model_platform.is_open_source and model_type.is_open_source:
- model_class = OpenSourceModel
- return model_class(
- model_type, model_config_dict, url, token_counter
- )
- if model_platform.is_openai and model_type.is_openai:
- model_class = OpenAIModel
- elif model_platform.is_azure and model_type.is_azure_openai:
- model_class = AzureOpenAIModel
- elif model_platform.is_anthropic and model_type.is_anthropic:
- model_class = AnthropicModel
- elif model_type.is_groq:
- model_class = GroqModel
- elif model_platform.is_zhipuai and model_type.is_zhipuai:
- model_class = ZhipuAIModel
- elif model_platform.is_gemini and model_type.is_gemini:
- model_class = GeminiModel
- elif model_platform.is_mistral and model_type.is_mistral:
- model_class = MistralModel
- elif model_platform.is_reka and model_type.is_reka:
- model_class = RekaModel
- elif model_type == ModelType.STUB:
- model_class = StubModel
- else:
- raise ValueError(
- f"Unknown pair of model platform `{model_platform}` "
- f"and model type `{model_type}`."
- )
- elif isinstance(model_type, str):
- if model_platform.is_ollama:
- model_class = OllamaModel
- return model_class(
- model_type, model_config_dict, url, token_counter
- )
- elif model_platform.is_vllm:
- model_class = VLLMModel
- elif model_platform.is_litellm:
- model_class = LiteLLMModel
- elif model_platform.is_openai_compatibility_model:
- model_class = OpenAICompatibilityModel
- elif model_platform.is_samba:
- model_class = SambaModel
- elif model_platform.is_together:
- model_class = TogetherAIModel
- return model_class(
- model_type, model_config_dict, api_key, token_counter
- )
- else:
- raise ValueError(
- f"Unknown pair of model platform `{model_platform}` "
- f"and model type `{model_type}`."
- )
- else:
- raise ValueError(f"Invalid model type `{model_type}` provided.")
+ model_class: Optional[Type[BaseModelBackend]] = None
+ model = UnifiedModelType(model_type)
+
+ if model_platform.is_ollama:
+ model_class = OllamaModel
+ elif model_platform.is_vllm:
+ model_class = VLLMModel
+ elif model_platform.is_openai_compatible_model:
+ model_class = OpenAICompatibleModel
+ elif model_platform.is_samba:
+ model_class = SambaModel
+ elif model_platform.is_together:
+ model_class = TogetherAIModel
+ elif model_platform.is_litellm:
+ model_class = LiteLLMModel
+
+ elif model_platform.is_openai and model.is_openai:
+ model_class = OpenAIModel
+ elif model_platform.is_azure and model.is_azure_openai:
+ model_class = AzureOpenAIModel
+ elif model_platform.is_anthropic and model.is_anthropic:
+ model_class = AnthropicModel
+ elif model_platform.is_groq and model.is_groq:
+ model_class = GroqModel
+ elif model_platform.is_zhipuai and model.is_zhipuai:
+ model_class = ZhipuAIModel
+ elif model_platform.is_gemini and model.is_gemini:
+ model_class = GeminiModel
+ elif model_platform.is_mistral and model.is_mistral:
+ model_class = MistralModel
+ elif model_platform.is_reka and model.is_reka:
+ model_class = RekaModel
+ elif model == ModelType.STUB:
+ model_class = StubModel
+
+ if model_class is None:
+ raise ValueError(
+ f"Unknown pair of model platform `{model_platform}` "
+ f"and model type `{model}`."
+ )
+
return model_class(
- model_type, model_config_dict, api_key, url, token_counter
+ model_type=model,
+ model_config_dict=model_config_dict,
+ api_key=api_key,
+ url=url,
+ token_counter=token_counter,
)
diff --git a/camel/models/nemotron_model.py b/camel/models/nemotron_model.py
index faf461199..2eb4d6a6c 100644
--- a/camel/models/nemotron_model.py
+++ b/camel/models/nemotron_model.py
@@ -12,11 +12,12 @@
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
import os
-from typing import List, Optional
+from typing import List, Optional, Union
from openai import OpenAI
from camel.messages import OpenAIMessage
+from camel.models import BaseModelBackend
from camel.types import ChatCompletion, ModelType
from camel.utils import (
BaseTokenCounter,
@@ -24,40 +25,38 @@ from camel.utils import (
)
-class NemotronModel:
- r"""Nemotron model API backend with OpenAI compatibility."""
+class NemotronModel(BaseModelBackend):
+ r"""Nemotron model API backend with OpenAI compatibility.
- # NOTE: Nemotron model doesn't support additional model config like OpenAI.
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created.
+ api_key (Optional[str], optional): The API key for authenticating with
+ the Nvidia service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the Nvidia service.
+ (default: :obj:`https://integrate.api.nvidia.com/v1`)
+
+ Notes:
+ Nemotron model doesn't support additional model config like OpenAI.
+ """
def __init__(
self,
- model_type: ModelType,
+ model_type: Union[ModelType, str],
api_key: Optional[str] = None,
url: Optional[str] = None,
) -> None:
- r"""Constructor for Nvidia backend.
-
- Args:
- model_type (ModelType): Model for which a backend is created.
- api_key (Optional[str]): The API key for authenticating with the
- Nvidia service. (default: :obj:`None`)
- url (Optional[str]): The url to the Nvidia service. (default:
- :obj:`None`)
- """
- self.model_type = model_type
- self._url = url or os.environ.get("NVIDIA_API_BASE_URL")
- self._api_key = api_key or os.environ.get("NVIDIA_API_KEY")
- if not self._url or not self._api_key:
- raise ValueError(
- "NVIDIA_API_BASE_URL and NVIDIA_API_KEY should be set."
- )
+ url = url or os.environ.get(
+ "NVIDIA_API_BASE_URL", "https://integrate.api.nvidia.com/v1"
+ )
+ api_key = api_key or os.environ.get("NVIDIA_API_KEY")
+ super().__init__(model_type, {}, api_key, url)
self._client = OpenAI(
timeout=60,
max_retries=3,
base_url=self._url,
api_key=self._api_key,
)
- self._token_counter: Optional[BaseTokenCounter] = None
@api_keys_required("NVIDIA_API_KEY")
def run(
@@ -74,6 +73,17 @@ class NemotronModel:
"""
response = self._client.chat.completions.create(
messages=messages,
- model=self.model_type.value,
+ model=self.model_type,
)
return response
+
+ @property
+ def token_counter(self) -> BaseTokenCounter:
+ raise NotImplementedError(
+ "Nemotron model doesn't support token counter."
+ )
+
+ def check_model_config(self):
+ raise NotImplementedError(
+ "Nemotron model doesn't support model config."
+ )
diff --git a/camel/models/ollama_model.py b/camel/models/ollama_model.py
index 1f601f315..b298f2a61 100644
--- a/camel/models/ollama_model.py
+++ b/camel/models/ollama_model.py
@@ -17,54 +17,64 @@ from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
-from camel.configs import OLLAMA_API_PARAMS
+from camel.configs import OLLAMA_API_PARAMS, OllamaConfig
from camel.messages import OpenAIMessage
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
+from camel.models import BaseModelBackend
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+)
from camel.utils import BaseTokenCounter, OpenAITokenCounter
-class OllamaModel:
- r"""Ollama service interface."""
+class OllamaModel(BaseModelBackend):
+ r"""Ollama service interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`.
+ If:obj:`None`, :obj:`OllamaConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the model service. Ollama doesn't need API key, it would be
+ ignored if set. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the model service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
+ (default: :obj:`None`)
+
+ References:
+ https://github.com/ollama/ollama/blob/main/docs/openai.md
+ """
def __init__(
self,
- model_type: str,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
+ api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for Ollama backend with OpenAI compatibility.
-
- # Reference: https://github.com/ollama/ollama/blob/main/docs/openai.md
-
- Args:
- model_type (str): Model for which a backend is created.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into openai.ChatCompletion.create().
- url (Optional[str]): The url to the model service. (default:
- :obj:`"http://localhost:11434/v1"`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter(ModelType.
- GPT_4O_MINI)` will be used.
- """
- self.model_type = model_type
- self.model_config_dict = model_config_dict
- self._url = (
- url
- or os.environ.get("OLLAMA_BASE_URL")
- or "http://localhost:11434/v1"
+ if model_config_dict is None:
+ model_config_dict = OllamaConfig().as_dict()
+ url = url or os.environ.get("OLLAMA_BASE_URL")
+ super().__init__(
+ model_type, model_config_dict, api_key, url, token_counter
)
- if not url and not os.environ.get("OLLAMA_BASE_URL"):
+ if not self._url:
self._start_server()
# Use OpenAI client as interface call Ollama
self._client = OpenAI(
timeout=60,
max_retries=3,
+ api_key="Set-but-ignored", # required but ignored
base_url=self._url,
- api_key="ollama", # required but ignored
)
- self._token_counter = token_counter
- self.check_model_config()
def _start_server(self) -> None:
r"""Starts the Ollama server in a subprocess."""
@@ -74,8 +84,9 @@ class OllamaModel:
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
+ self._url = "http://localhost:11434/v1"
print(
- f"Ollama server started on http://localhost:11434/v1 "
+ f"Ollama server started on {self._url} "
f"for {self.model_type} model."
)
except Exception as e:
diff --git a/camel/models/open_source_model.py b/camel/models/open_source_model.py
deleted file mode 100644
index efa658d77..000000000
--- a/camel/models/open_source_model.py
+++ /dev/null
@@ -1,170 +0,0 @@
-# =========== 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 typing import Any, Dict, List, Optional, Union
-
-from openai import OpenAI, Stream
-
-from camel.configs import OPENAI_API_PARAMS
-from camel.messages import OpenAIMessage
-from camel.models import BaseModelBackend
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
-from camel.utils import (
- BaseTokenCounter,
- OpenSourceTokenCounter,
-)
-
-
-class OpenSourceModel(BaseModelBackend):
- r"""Class for interace with OpenAI-API-compatible servers running
- open-source models.
- """
-
- def __init__(
- self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
- api_key: Optional[str] = None,
- url: Optional[str] = None,
- token_counter: Optional[BaseTokenCounter] = None,
- ) -> None:
- r"""Constructor for model backends of Open-source models.
-
- Args:
- model_type (ModelType): Model for which a backend is created.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into :obj:`openai.ChatCompletion.create()`.
- api_key (Optional[str]): The API key for authenticating with the
- model service. (ignored for open-source models)
- url (Optional[str]): The url to the model service.
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenSourceTokenCounter` will
- be used.
- """
- super().__init__(
- model_type, model_config_dict, api_key, url, token_counter
- )
-
- # Check whether the input model type is open-source
- if not model_type.is_open_source:
- raise ValueError(
- f"Model `{model_type}` is not a supported open-source model."
- )
-
- # Check whether input model path is empty
- model_path: Optional[str] = self.model_config_dict.get(
- "model_path", None
- )
- if not model_path:
- raise ValueError("Path to open-source model is not provided.")
- self.model_path: str = model_path
-
- # Check whether the model name matches the model type
- self.model_name: str = self.model_path.split('/')[-1]
- if not self.model_type.validate_model_name(self.model_name):
- raise ValueError(
- f"Model name `{self.model_name}` does not match model type "
- f"`{self.model_type.value}`."
- )
-
- # Load the server URL and check whether it is None
- server_url: Optional[str] = url or self.model_config_dict.get(
- "server_url", None
- )
- if not server_url:
- raise ValueError(
- "URL to server running open-source LLM is not provided."
- )
- self.server_url: str = server_url
- self._client = OpenAI(
- base_url=self.server_url,
- timeout=60,
- max_retries=3,
- api_key="fake_key",
- )
-
- # Replace `model_config_dict` with only the params to be
- # passed to OpenAI API
- self.model_config_dict = self.model_config_dict["api_params"]
-
- @property
- def token_counter(self) -> BaseTokenCounter:
- r"""Initialize the token counter for the model backend.
-
- Returns:
- BaseTokenCounter: The token counter following the model's
- tokenization style.
- """
- if not self._token_counter:
- self._token_counter = OpenSourceTokenCounter(
- self.model_type, self.model_path
- )
- return self._token_counter
-
- def run(
- self,
- messages: List[OpenAIMessage],
- ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- r"""Runs inference of OpenAI-API-style chat completion.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
-
- Returns:
- Union[ChatCompletion, Stream[ChatCompletionChunk]]:
- `ChatCompletion` in the non-stream mode, or
- `Stream[ChatCompletionChunk]` in the stream mode.
- """
- messages_openai: List[OpenAIMessage] = messages
- response = self._client.chat.completions.create(
- messages=messages_openai,
- model=self.model_name,
- **self.model_config_dict,
- )
- return response
-
- def check_model_config(self):
- r"""Check whether the model configuration is valid for open-source
- model backends.
-
- Raises:
- ValueError: If the model configuration dictionary contains any
- unexpected arguments to OpenAI API, or it does not contain
- :obj:`model_path` or :obj:`server_url`.
- """
- if (
- "model_path" not in self.model_config_dict
- or "server_url" not in self.model_config_dict
- ):
- raise ValueError(
- "Invalid configuration for open-source model backend with "
- ":obj:`model_path` or :obj:`server_url` missing."
- )
-
- for param in self.model_config_dict["api_params"]:
- if param not in OPENAI_API_PARAMS:
- raise ValueError(
- f"Unexpected argument `{param}` is "
- "input into open-source model backend."
- )
-
- @property
- def stream(self) -> bool:
- r"""Returns whether the model is in stream mode,
- which sends partial results each time.
-
- Returns:
- bool: Whether the model is in stream mode.
- """
- return self.model_config_dict.get('stream', False)
diff --git a/camel/models/openai_compatibility_model.py b/camel/models/openai_compatible_model.py
similarity index 68%
rename from camel/models/openai_compatibility_model.py
rename to camel/models/openai_compatible_model.py
index 2e43a2ae0..3754cea43 100644
--- a/camel/models/openai_compatibility_model.py
+++ b/camel/models/openai_compatible_model.py
@@ -18,59 +18,54 @@ from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
from camel.messages import OpenAIMessage
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
+from camel.models import BaseModelBackend
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+)
from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
)
-class OpenAICompatibilityModel:
- r"""LLM API served by OpenAI-compatible providers."""
+class OpenAICompatibleModel(BaseModelBackend):
+ r"""Constructor for model backend supporting OpenAI compatibility.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`. If
+ :obj:`None`, :obj:`{}` will be used. (default: :obj:`None`)
+ api_key (str): The API key for authenticating with the model service.
+ url (str): The url to the model service.
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
+ (default: :obj:`None`)
+ """
def __init__(
self,
- model_type: str,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for model backend.
-
- Args:
- model_type (str): Model for which a backend is created.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into openai.ChatCompletion.create().
- api_key (str): The API key for authenticating with the
- model service. (default: :obj:`None`)
- url (str): The url to the model service. (default:
- :obj:`None`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter(ModelType.
- GPT_4O_MINI)` will be used.
- """
- self.model_type = model_type
- self.model_config_dict = model_config_dict
- self._url = url or os.environ.get("OPENAI_COMPATIBILIY_API_BASE_URL")
- self._api_key = api_key or os.environ.get(
- "OPENAI_COMPATIBILIY_API_KEY"
+ self.api_key = api_key or os.environ.get("OPENAI_COMPATIBILIY_API_KEY")
+ self.url = url or os.environ.get("OPENAI_COMPATIBILIY_API_BASE_URL")
+ super().__init__(
+ model_type, model_config_dict, api_key, url, token_counter
)
- if self._url is None:
- raise ValueError(
- "For OpenAI-compatible models, you must provide the `url`."
- )
- if self._api_key is None:
- raise ValueError(
- "For OpenAI-compatible models, you must provide the `api_key`."
- )
self._client = OpenAI(
timeout=60,
max_retries=3,
- base_url=self._url,
api_key=self._api_key,
+ base_url=self._url,
)
- self._token_counter = token_counter
def run(
self,
@@ -132,3 +127,6 @@ class OpenAICompatibilityModel:
" setting up the model. Using 4096 as default value."
)
return 4096
+
+ def check_model_config(self):
+ pass
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index dd6b4a714..02d228476 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -17,10 +17,14 @@ from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
-from camel.configs import OPENAI_API_PARAMS
+from camel.configs import OPENAI_API_PARAMS, ChatGPTConfig
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+)
from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
@@ -29,36 +33,39 @@ from camel.utils import (
class OpenAIModel(BaseModelBackend):
- r"""OpenAI API in a unified BaseModelBackend interface."""
+ r"""OpenAI API in a unified BaseModelBackend interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, one of GPT_* series.
+ 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`)
+ url (Optional[str], optional): The url to the OpenAI service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter` will
+ be used. (default: :obj:`None`)
+ """
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for OpenAI backend.
-
- Args:
- model_type (ModelType): Model for which a backend is created,
- one of GPT_* series.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into openai.ChatCompletion.create().
- api_key (Optional[str]): The API key for authenticating with the
- OpenAI service. (default: :obj:`None`)
- url (Optional[str]): The url to the OpenAI service. (default:
- :obj:`None`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter` will
- be used.
- """
+ if model_config_dict is None:
+ model_config_dict = ChatGPTConfig().as_dict()
+ api_key = api_key or os.environ.get("OPENAI_API_KEY")
+ url = url or os.environ.get("OPENAI_API_BASE_URL")
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
- self._url = url or os.environ.get("OPENAI_API_BASE_URL")
- self._api_key = api_key or os.environ.get("OPENAI_API_KEY")
self._client = OpenAI(
timeout=60,
max_retries=3,
@@ -122,7 +129,7 @@ class OpenAIModel(BaseModelBackend):
response = self._client.chat.completions.create(
messages=messages,
- model=self.model_type.value,
+ model=self.model_type,
**self.model_config_dict,
)
return response
@@ -144,8 +151,9 @@ class OpenAIModel(BaseModelBackend):
@property
def stream(self) -> bool:
- r"""Returns whether the model is in stream mode,
- which sends partial results each time.
+ r"""Returns whether the model is in stream mode, which sends partial
+ results each time.
+
Returns:
bool: Whether the model is in stream mode.
"""
diff --git a/camel/models/reka_model.py b/camel/models/reka_model.py
index 296b36eae..d8ddb62e7 100644
--- a/camel/models/reka_model.py
+++ b/camel/models/reka_model.py
@@ -11,10 +11,9 @@
# 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 TYPE_CHECKING, Any, Dict, List, Optional
+from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
-from camel.configs import REKA_API_PARAMS
+from camel.configs import REKA_API_PARAMS, RekaConfig
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
from camel.types import ChatCompletion, ModelType
@@ -39,40 +38,41 @@ except (ImportError, AttributeError):
class RekaModel(BaseModelBackend):
- r"""Reka API in a unified BaseModelBackend interface."""
+ r"""Reka API in a unified BaseModelBackend interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, one of REKA_* series.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`Reka.chat.create()`. If :obj:`None`,
+ :obj:`RekaConfig().as_dict()` will be used. (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the Reka service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the Reka service.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter` will
+ be used. (default: :obj:`None`)
+ """
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for Reka backend.
+ from reka.client import Reka
- Args:
- model_type (ModelType): Model for which a backend is created,
- one of REKA_* series.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into `Reka.chat.create`.
- api_key (Optional[str]): The API key for authenticating with the
- Reka service. (default: :obj:`None`)
- url (Optional[str]): The url to the Reka service.
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter` will be
- used.
- """
+ if model_config_dict is None:
+ model_config_dict = RekaConfig().as_dict()
+ api_key = api_key or os.environ.get("REKA_API_KEY")
+ url = url or os.environ.get("REKA_API_BASE_URL")
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
- self._api_key = api_key or os.environ.get("REKA_API_KEY")
- self._url = url or os.environ.get("REKA_SERVER_URL")
-
- from reka.client import Reka
-
self._client = Reka(api_key=self._api_key, base_url=self._url)
- self._token_counter: Optional[BaseTokenCounter] = None
def _convert_reka_to_openai_response(
self, response: 'ChatResponse'
@@ -184,7 +184,7 @@ class RekaModel(BaseModelBackend):
response = self._client.chat.create(
messages=reka_messages,
- model=self.model_type.value,
+ model=self.model_type,
**self.model_config_dict,
)
@@ -200,7 +200,7 @@ class RekaModel(BaseModelBackend):
prompt_tokens=openai_response.usage.input_tokens, # type: ignore[union-attr]
completion=openai_response.choices[0].message.content,
completion_tokens=openai_response.usage.output_tokens, # type: ignore[union-attr]
- model=self.model_type.value,
+ model=self.model_type,
)
record(llm_event)
diff --git a/camel/models/samba_model.py b/camel/models/samba_model.py
index 3366b8204..53e025368 100644
--- a/camel/models/samba_model.py
+++ b/camel/models/samba_model.py
@@ -22,10 +22,11 @@ from openai import OpenAI, Stream
from camel.configs import (
SAMBA_CLOUD_API_PARAMS,
- SAMBA_FAST_API_PARAMS,
SAMBA_VERSE_API_PARAMS,
+ SambaCloudAPIConfig,
)
from camel.messages import OpenAIMessage
+from camel.models import BaseModelBackend
from camel.types import (
ChatCompletion,
ChatCompletionChunk,
@@ -39,47 +40,50 @@ from camel.utils import (
)
-class SambaModel:
- r"""SambaNova service interface."""
+class SambaModel(BaseModelBackend):
+ r"""SambaNova service interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a SambaNova backend
+ is created. Supported models via SambaNova Cloud:
+ `https://community.sambanova.ai/t/supported-models/193`.
+ Supported models via SambaVerse API is listed in
+ `https://sambaverse.sambanova.ai/models`.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`. If
+ :obj:`None`, :obj:`SambaCloudAPIConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating
+ with the SambaNova service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the SambaNova service.
+ Current support SambaVerse API:
+ :obj:`"https://sambaverse.sambanova.ai/api/predict"` and
+ SambaNova Cloud:
+ :obj:`"https://api.sambanova.ai/v1"` (default: :obj:`https://api.
+ sambanova.ai/v1`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
+ """
def __init__(
self,
- model_type: str,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for SambaNova backend.
-
- Args:
- model_type (str): Model for which a SambaNova backend is
- created. Supported models via Fast API: `https://sambanova.ai/
- fast-api?api_ref=128521`. Supported models via SambaVerse API
- is listed in `https://sambaverse.sambanova.ai/models`.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into API request.
- api_key (Optional[str]): The API key for authenticating with the
- SambaNova service. (default: :obj:`None`)
- url (Optional[str]): The url to the SambaNova service. Current
- support SambaNova Fast API: :obj:`"https://fast-api.snova.ai/
- v1/chat/ completions"`, SambaVerse API: :obj:`"https://
- sambaverse.sambanova.ai/api/predict"` and SambaNova Cloud:
- :obj:`"https://api.sambanova.ai/v1"`
- (default::obj:`"https://fast-api.snova.ai/v1/chat/completions"`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter(ModelType.
- GPT_4O_MINI)` will be used.
- """
- self.model_type = model_type
- self._api_key = api_key or os.environ.get("SAMBA_API_KEY")
- self._url = url or os.environ.get(
+ if model_config_dict is None:
+ model_config_dict = SambaCloudAPIConfig().as_dict()
+ api_key = api_key or os.environ.get("SAMBA_API_KEY")
+ url = url or os.environ.get(
"SAMBA_API_BASE_URL",
- "https://fast-api.snova.ai/v1/chat/completions",
+ "https://api.sambanova.ai/v1",
+ )
+ super().__init__(
+ model_type, model_config_dict, api_key, url, token_counter
)
- self._token_counter = token_counter
- self.model_config_dict = model_config_dict
- self.check_model_config()
if self._url == "https://api.sambanova.ai/v1":
self._client = OpenAI(
@@ -109,14 +113,7 @@ class SambaModel:
ValueError: If the model configuration dictionary contains any
unexpected arguments to SambaNova API.
"""
- if self._url == "https://fast-api.snova.ai/v1/chat/completions":
- for param in self.model_config_dict:
- if param not in SAMBA_FAST_API_PARAMS:
- raise ValueError(
- f"Unexpected argument `{param}` is "
- "input into SambaNova Fast API."
- )
- elif self._url == "https://sambaverse.sambanova.ai/api/predict":
+ if self._url == "https://sambaverse.sambanova.ai/api/predict":
for param in self.model_config_dict:
if param not in SAMBA_VERSE_API_PARAMS:
raise ValueError(
@@ -159,7 +156,7 @@ class SambaModel:
else:
return self._run_non_streaming(messages)
- def _run_streaming( # type: ignore[misc]
+ def _run_streaming(
self, messages: List[OpenAIMessage]
) -> Stream[ChatCompletionChunk]:
r"""Handles streaming inference with SambaNova's API.
@@ -175,43 +172,10 @@ class SambaModel:
Raises:
RuntimeError: If the HTTP request fails.
+ ValueError: If the API doesn't support stream mode.
"""
-
- # Handle SambaNova's Fast API
- if self._url == "https://fast-api.snova.ai/v1/chat/completions":
- headers = {
- "Authorization": f"Basic {self._api_key}",
- "Content-Type": "application/json",
- }
-
- data = {
- "messages": messages,
- "max_tokens": self.token_limit,
- "stop": self.model_config_dict.get("stop"),
- "model": self.model_type,
- "stream": True,
- "stream_options": self.model_config_dict.get("stream_options"),
- }
-
- try:
- with httpx.stream(
- "POST",
- self._url,
- headers=headers,
- json=data,
- ) as api_response:
- stream = Stream[ChatCompletionChunk](
- cast_to=ChatCompletionChunk,
- response=api_response,
- client=OpenAI(api_key="required_but_not_used"),
- )
- for chunk in stream:
- yield chunk
- except httpx.HTTPError as e:
- raise RuntimeError(f"HTTP request failed: {e!s}")
-
# Handle SambaNova's Cloud API
- elif self._url == "https://api.sambanova.ai/v1":
+ if self._url == "https://api.sambanova.ai/v1":
response = self._client.chat.completions.create(
messages=messages,
model=self.model_type,
@@ -224,6 +188,7 @@ class SambaModel:
"https://sambaverse.sambanova.ai/api/predict doesn't support"
" stream mode"
)
+ raise RuntimeError(f"Unknown URL: {self._url}")
def _run_non_streaming(
self, messages: List[OpenAIMessage]
@@ -243,46 +208,8 @@ class SambaModel:
ValueError: If the JSON response cannot be decoded or is missing
expected data.
"""
-
- # Handle SambaNova's Fast API
- if self._url == "https://fast-api.snova.ai/v1/chat/completions":
- headers = {
- "Authorization": f"Basic {self._api_key}",
- "Content-Type": "application/json",
- }
-
- data = {
- "messages": messages,
- "max_tokens": self.token_limit,
- "stop": self.model_config_dict.get("stop"),
- "model": self.model_type,
- "stream": True,
- "stream_options": self.model_config_dict.get("stream_options"),
- }
-
- try:
- with httpx.stream(
- "POST",
- self._url,
- headers=headers,
- json=data,
- ) as api_response:
- samba_response = []
- for chunk in api_response.iter_text():
- if chunk.startswith('data: '):
- chunk = chunk[6:]
- if '[DONE]' in chunk:
- break
- json_data = json.loads(chunk)
- samba_response.append(json_data)
- return self._fastapi_to_openai_response(samba_response)
- except httpx.HTTPError as e:
- raise RuntimeError(f"HTTP request failed: {e!s}")
- except json.JSONDecodeError as e:
- raise ValueError(f"Failed to decode JSON response: {e!s}")
-
# Handle SambaNova's Cloud API
- elif self._url == "https://api.sambanova.ai/v1":
+ if self._url == "https://api.sambanova.ai/v1":
response = self._client.chat.completions.create(
messages=messages,
model=self.model_type,
@@ -370,56 +297,6 @@ class SambaModel:
except httpx.HTTPStatusError:
raise RuntimeError(f"HTTP request failed: {raw_text}")
- def _fastapi_to_openai_response(
- self, samba_response: List[Dict[str, Any]]
- ) -> ChatCompletion:
- r"""Converts SambaNova Fast API response chunks into an
- OpenAI-compatible response.
-
- Args:
- samba_response (List[Dict[str, Any]]): A list of dictionaries
- representing partial responses from the SambaNova Fast API.
-
- Returns:
- ChatCompletion: A `ChatCompletion` object constructed from the
- aggregated response data.
- """
-
- # Step 1: Combine the content from each chunk
- full_content = ""
- for chunk in samba_response:
- if chunk['choices']:
- for choice in chunk['choices']:
- delta_content = choice['delta'].get('content', '')
- full_content += delta_content
-
- # Step 2: Create the ChatCompletion object
- # Extract relevant information from the first chunk
- first_chunk = samba_response[0]
-
- choices = [
- dict(
- index=0, # type: ignore[index]
- message={
- "role": 'assistant',
- "content": full_content.strip(),
- },
- finish_reason=samba_response[-1]['choices'][0]['finish_reason']
- or None,
- )
- ]
-
- obj = ChatCompletion.construct(
- id=first_chunk['id'],
- choices=choices,
- created=first_chunk['created'],
- model=first_chunk['model'],
- object="chat.completion",
- usage=None,
- )
-
- return obj
-
def _sambaverse_to_openai_response(
self, samba_response: Dict[str, Any]
) -> ChatCompletion:
diff --git a/camel/models/stub_model.py b/camel/models/stub_model.py
index dcb60078c..75b5483e6 100644
--- a/camel/models/stub_model.py
+++ b/camel/models/stub_model.py
@@ -51,8 +51,8 @@ class StubModel(BaseModelBackend):
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
diff --git a/camel/models/togetherai_model.py b/camel/models/togetherai_model.py
index b5bf820b6..aa19c2871 100644
--- a/camel/models/togetherai_model.py
+++ b/camel/models/togetherai_model.py
@@ -17,9 +17,14 @@ from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
-from camel.configs import TOGETHERAI_API_PARAMS
+from camel.configs import TOGETHERAI_API_PARAMS, TogetherAIConfig
from camel.messages import OpenAIMessage
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
+from camel.models import BaseModelBackend
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+)
from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
@@ -27,45 +32,50 @@ from camel.utils import (
)
-class TogetherAIModel:
+class TogetherAIModel(BaseModelBackend):
r"""Constructor for Together AI backend with OpenAI compatibility.
- TODO: Add function calling support
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, supported model can be found here:
+ https://docs.together.ai/docs/chat-models
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`. If
+ :obj:`None`, :obj:`TogetherAIConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the Together service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the Together AI service.
+ If not provided, "https://api.together.xyz/v1" will be used.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
"""
def __init__(
self,
- model_type: str,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for TogetherAI backend.
-
- Args:
- model_type (str): Model for which a backend is created, supported
- model can be found here: https://docs.together.ai/docs/chat-models
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into openai.ChatCompletion.create().
- api_key (Optional[str]): The API key for authenticating with the
- Together service. (default: :obj:`None`)
- url (Optional[str]): The url to the Together AI service. (default:
- :obj:`"https://api.together.xyz/v1"`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter(ModelType.
- GPT_4O_MINI)` will be used.
- """
- self.model_type = model_type
- self.model_config_dict = model_config_dict
- self._token_counter = token_counter
- self._api_key = api_key or os.environ.get("TOGETHER_API_KEY")
- self._url = url or os.environ.get("TOGETHER_API_BASE_URL")
+ if model_config_dict is None:
+ model_config_dict = TogetherAIConfig().as_dict()
+ api_key = api_key or os.environ.get("TOGETHER_API_KEY")
+ url = url or os.environ.get(
+ "TOGETHER_API_BASE_URL", "https://api.together.xyz/v1"
+ )
+ super().__init__(
+ model_type, model_config_dict, api_key, url, token_counter
+ )
self._client = OpenAI(
timeout=60,
max_retries=3,
api_key=self._api_key,
- base_url=self._url or "https://api.together.xyz/v1",
+ base_url=self._url,
)
@api_keys_required("TOGETHER_API_KEY")
diff --git a/camel/models/vllm_model.py b/camel/models/vllm_model.py
index 0fb32d25f..5523e0212 100644
--- a/camel/models/vllm_model.py
+++ b/camel/models/vllm_model.py
@@ -17,58 +17,66 @@ from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
-from camel.configs import VLLM_API_PARAMS
+from camel.configs import VLLM_API_PARAMS, VLLMConfig
from camel.messages import OpenAIMessage
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
+from camel.models import BaseModelBackend
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+)
from camel.utils import BaseTokenCounter, OpenAITokenCounter
# flake8: noqa: E501
-class VLLMModel:
- r"""vLLM service interface."""
+class VLLMModel(BaseModelBackend):
+ r"""vLLM service interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`. If
+ :obj:`None`, :obj:`VLLMConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the model service. vLLM doesn't need API key, it would be ignored
+ if set. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the model service. If not
+ provided, :obj:`"http://localhost:8000/v1"` will be used.
+ (default: :obj:`None`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
+ (default: :obj:`None`)
+
+ References:
+ https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html
+ """
def __init__(
self,
- model_type: str,
- model_config_dict: Dict[str, Any],
- url: Optional[str] = None,
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
+ url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for vLLM backend with OpenAI compatibility.
-
- # Reference: https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html
-
- Args:
- model_type (str): Model for which a backend is created.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into openai.ChatCompletion.create().
- url (Optional[str]): The url to the model service. (default:
- :obj:`"http://localhost:8000/v1"`)
- api_key (Optional[str]): The API key for authenticating with the
- model service.
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter(ModelType.
- GPT_4O_MINI)` will be used.
- """
- self.model_type = model_type
- self.model_config_dict = model_config_dict
- self._url = (
- url
- or os.environ.get("VLLM_BASE_URL")
- or "http://localhost:8000/v1"
+ if model_config_dict is None:
+ model_config_dict = VLLMConfig().as_dict()
+ url = url or os.environ.get("VLLM_BASE_URL")
+ super().__init__(
+ model_type, model_config_dict, api_key, url, token_counter
)
- if not url and not os.environ.get("VLLM_BASE_URL"):
+ if not self._url:
self._start_server()
# Use OpenAI cilent as interface call vLLM
self._client = OpenAI(
timeout=60,
max_retries=3,
+ api_key="Set-but-ignored", # required but ignored
base_url=self._url,
- api_key=api_key,
)
- self._token_counter = token_counter
- self.check_model_config()
def _start_server(self) -> None:
r"""Starts the vllm server in a subprocess."""
@@ -78,8 +86,9 @@ class VLLMModel:
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
+ self._url = "http://localhost:8000/v1"
print(
- f"vllm server started on http://localhost:8000/v1 "
+ f"vllm server started on {self._url} "
f"for {self.model_type} model."
)
except Exception as e:
diff --git a/camel/models/zhipuai_model.py b/camel/models/zhipuai_model.py
index a674fd3c9..cdd7a2998 100644
--- a/camel/models/zhipuai_model.py
+++ b/camel/models/zhipuai_model.py
@@ -17,10 +17,14 @@ from typing import Any, Dict, List, Optional, Union
from openai import OpenAI, Stream
-from camel.configs import ZHIPUAI_API_PARAMS
+from camel.configs import ZHIPUAI_API_PARAMS, ZhipuAIConfig
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
-from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
+from camel.types import (
+ ChatCompletion,
+ ChatCompletionChunk,
+ ModelType,
+)
from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
@@ -29,40 +33,42 @@ from camel.utils import (
class ZhipuAIModel(BaseModelBackend):
- r"""ZhipuAI API in a unified BaseModelBackend interface."""
+ r"""ZhipuAI API in a unified BaseModelBackend interface.
+
+ Args:
+ model_type (Union[ModelType, str]): Model for which a backend is
+ created, one of GLM_* series.
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
+ that will be fed into:obj:`openai.ChatCompletion.create()`. If
+ :obj:`None`, :obj:`ZhipuAIConfig().as_dict()` will be used.
+ (default: :obj:`None`)
+ api_key (Optional[str], optional): The API key for authenticating with
+ the ZhipuAI service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the ZhipuAI service.
+ (default: :obj:`https://open.bigmodel.cn/api/paas/v4/`)
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
+ (default: :obj:`None`)
+ """
def __init__(
self,
- model_type: ModelType,
- model_config_dict: Dict[str, Any],
+ model_type: Union[ModelType, str],
+ model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- r"""Constructor for ZhipuAI backend.
-
- Args:
- model_type (ModelType): Model for which a backend is created,
- such as GLM_* series.
- model_config_dict (Dict[str, Any]): A dictionary that will
- be fed into openai.ChatCompletion.create().
- api_key (Optional[str]): The API key for authenticating with the
- ZhipuAI service. (default: :obj:`None`)
- url (Optional[str]): The url to the ZhipuAI service. (default:
- :obj:`None`)
- token_counter (Optional[BaseTokenCounter]): Token counter to use
- for the model. If not provided, `OpenAITokenCounter(ModelType.
- GPT_4O_MINI)` will be used.
- """
+ if model_config_dict is None:
+ model_config_dict = ZhipuAIConfig().as_dict()
+ api_key = api_key or os.environ.get("ZHIPUAI_API_KEY")
+ url = url or os.environ.get(
+ "ZHIPUAI_API_BASE_URL", "https://open.bigmodel.cn/api/paas/v4/"
+ )
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
- self._url = url or os.environ.get("ZHIPUAI_API_BASE_URL")
- self._api_key = api_key or os.environ.get("ZHIPUAI_API_KEY")
- if not self._url or not self._api_key:
- raise ValueError(
- "ZHIPUAI_API_BASE_URL and ZHIPUAI_API_KEY should be set."
- )
self._client = OpenAI(
timeout=60,
max_retries=3,
@@ -90,7 +96,7 @@ class ZhipuAIModel(BaseModelBackend):
# Reference: https://open.bigmodel.cn/dev/api#openai_sdk
response = self._client.chat.completions.create(
messages=messages,
- model=self.model_type.value,
+ model=self.model_type,
**self.model_config_dict,
)
return response
diff --git a/camel/storages/key_value_storages/json.py b/camel/storages/key_value_storages/json.py
index 128e1f6bf..aea582620 100644
--- a/camel/storages/key_value_storages/json.py
+++ b/camel/storages/key_value_storages/json.py
@@ -18,7 +18,12 @@ from pathlib import Path
from typing import Any, ClassVar, Dict, List, Optional
from camel.storages.key_value_storages import BaseKeyValueStorage
-from camel.types import ModelType, OpenAIBackendRole, RoleType, TaskType
+from camel.types import (
+ ModelType,
+ OpenAIBackendRole,
+ RoleType,
+ TaskType,
+)
class _CamelJSONEncoder(json.JSONEncoder):
diff --git a/camel/types/__init__.py b/camel/types/__init__.py
index 5228436ec..a162c5a61 100644
--- a/camel/types/__init__.py
+++ b/camel/types/__init__.py
@@ -39,6 +39,7 @@ from .openai_types import (
Choice,
CompletionUsage,
)
+from .unified_model_type import UnifiedModelType
__all__ = [
'RoleType',
@@ -59,11 +60,11 @@ __all__ = [
'ChatCompletionAssistantMessageParam',
'ChatCompletionFunctionMessageParam',
'CompletionUsage',
- 'OpenAIVideoType',
'OpenAIImageType',
'OpenAIVisionDetailType',
'OpenAPIName',
'ModelPlatformType',
'AudioModelType',
'VoiceType',
+ 'UnifiedModelType',
]
diff --git a/camel/types/enums.py b/camel/types/enums.py
index ef7eae820..82dc0b7ff 100644
--- a/camel/types/enums.py
+++ b/camel/types/enums.py
@@ -11,8 +11,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
-import re
from enum import Enum, EnumMeta
+from typing import cast
+
+from camel.types.unified_model_type import UnifiedModelType
class RoleType(Enum):
@@ -23,7 +25,7 @@ class RoleType(Enum):
DEFAULT = "default"
-class ModelType(Enum):
+class ModelType(UnifiedModelType, Enum):
GPT_3_5_TURBO = "gpt-3.5-turbo"
GPT_4 = "gpt-4"
GPT_4_TURBO = "gpt-4-turbo"
@@ -33,7 +35,6 @@ class ModelType(Enum):
O1_MINI = "o1-mini"
GLM_4 = "glm-4"
- GLM_4_OPEN_SOURCE = "glm-4-open-source"
GLM_4V = 'glm-4v'
GLM_3_TURBO = "glm-3-turbo"
@@ -48,13 +49,6 @@ class ModelType(Enum):
STUB = "stub"
- LLAMA_2 = "llama-2"
- LLAMA_3 = "llama-3"
- VICUNA = "vicuna"
- VICUNA_16K = "vicuna-16k"
-
- QWEN_2 = "qwen-2"
-
# Legacy anthropic models
# NOTE: anthropic legacy models only Claude 2.1 has system prompt support
CLAUDE_2_1 = "claude-2.1"
@@ -89,6 +83,9 @@ class ModelType(Enum):
REKA_FLASH = "reka-flash"
REKA_EDGE = "reka-edge"
+ def __new__(cls, value) -> "ModelType":
+ return cast("ModelType", UnifiedModelType.__new__(cls, value))
+
@property
def value_for_tiktoken(self) -> str:
if self.is_openai:
@@ -96,7 +93,7 @@ class ModelType(Enum):
return "gpt-4o-mini"
@property
- def supports_tool_calling(self) -> bool:
+ def support_native_tool_calling(self) -> bool:
return any([self.is_openai, self.is_gemini, self.is_mistral])
@property
@@ -133,18 +130,6 @@ class ModelType(Enum):
ModelType.GLM_4V,
}
- @property
- def is_open_source(self) -> bool:
- r"""Returns whether this type of models is open-source."""
- return self in {
- ModelType.LLAMA_2,
- ModelType.LLAMA_3,
- ModelType.QWEN_2,
- ModelType.GLM_4_OPEN_SOURCE,
- ModelType.VICUNA,
- ModelType.VICUNA_16K,
- }
-
@property
def is_anthropic(self) -> bool:
r"""Returns whether this type of models is Anthropic-released model.
@@ -208,7 +193,10 @@ class ModelType(Enum):
Returns:
bool: Whether this type of models is gemini.
"""
- return self in {ModelType.GEMINI_1_5_FLASH, ModelType.GEMINI_1_5_PRO}
+ return self in {
+ ModelType.GEMINI_1_5_FLASH,
+ ModelType.GEMINI_1_5_PRO,
+ }
@property
def is_reka(self) -> bool:
@@ -232,11 +220,7 @@ class ModelType(Enum):
"""
if self is ModelType.GLM_4V:
return 1024
- elif self is ModelType.VICUNA:
- # reference: https://lmsys.org/blog/2023-03-30-vicuna/
- return 2048
elif self in {
- ModelType.LLAMA_2,
ModelType.NEMOTRON_4_REWARD,
ModelType.STUB,
ModelType.REKA_CORE,
@@ -250,15 +234,12 @@ class ModelType(Enum):
ModelType.GROQ_LLAMA_3_70B,
ModelType.GROQ_GEMMA_7B_IT,
ModelType.GROQ_GEMMA_2_9B_IT,
- ModelType.LLAMA_3,
ModelType.GLM_3_TURBO,
ModelType.GLM_4,
- ModelType.GLM_4_OPEN_SOURCE,
}:
return 8_192
elif self in {
ModelType.GPT_3_5_TURBO,
- ModelType.VICUNA_16K,
}:
return 16_384
elif self in {
@@ -270,7 +251,10 @@ class ModelType(Enum):
return 32_768
elif self in {ModelType.MISTRAL_MIXTRAL_8x22B}:
return 64_000
- elif self in {ModelType.CLAUDE_2_0, ModelType.CLAUDE_INSTANT_1_2}:
+ elif self in {
+ ModelType.CLAUDE_2_0,
+ ModelType.CLAUDE_INSTANT_1_2,
+ }:
return 100_000
elif self in {
ModelType.GPT_4O,
@@ -281,7 +265,6 @@ class ModelType(Enum):
ModelType.MISTRAL_LARGE,
ModelType.MISTRAL_NEMO,
ModelType.MISTRAL_PIXTRAL_12B,
- ModelType.QWEN_2,
}:
return 128_000
elif self in {
@@ -302,48 +285,14 @@ class ModelType(Enum):
ModelType.MISTRAL_CODESTRAL_MAMBA,
}:
return 256_000
- elif self in {ModelType.GEMINI_1_5_FLASH, ModelType.GEMINI_1_5_PRO}:
+ elif self in {
+ ModelType.GEMINI_1_5_FLASH,
+ ModelType.GEMINI_1_5_PRO,
+ }:
return 1_048_576
else:
raise ValueError("Unknown model type")
- def validate_model_name(self, model_name: str) -> bool:
- r"""Checks whether the model type and the model name matches.
-
- Args:
- model_name (str): The name of the model, e.g. "vicuna-7b-v1.5".
-
- Returns:
- bool: Whether the model type matches the model name.
- """
- if self is ModelType.VICUNA:
- pattern = r'^vicuna-\d+b-v\d+\.\d+$'
- return bool(re.match(pattern, model_name))
- elif self is ModelType.VICUNA_16K:
- pattern = r'^vicuna-\d+b-v\d+\.\d+-16k$'
- return bool(re.match(pattern, model_name))
- elif self is ModelType.LLAMA_2:
- return (
- self.value in model_name.lower()
- or "llama2" in model_name.lower()
- )
- elif self is ModelType.LLAMA_3:
- return (
- self.value in model_name.lower()
- or "llama3" in model_name.lower()
- )
- elif self is ModelType.QWEN_2:
- return (
- self.value in model_name.lower()
- or "qwen2" in model_name.lower()
- )
- elif self is ModelType.GLM_4_OPEN_SOURCE:
- return (
- 'glm-4' in model_name.lower() or "glm4" in model_name.lower()
- )
- else:
- return self.value in model_name.lower()
-
class EmbeddingModelType(Enum):
TEXT_EMBEDDING_ADA_2 = "text-embedding-ada-002"
@@ -473,7 +422,6 @@ class ModelPlatformType(Enum):
AZURE = "azure"
ANTHROPIC = "anthropic"
GROQ = "groq"
- OPEN_SOURCE = "open-source"
OLLAMA = "ollama"
LITELLM = "litellm"
ZHIPU = "zhipuai"
@@ -483,7 +431,7 @@ class ModelPlatformType(Enum):
MISTRAL = "mistral"
REKA = "reka"
TOGETHER = "together"
- OPENAI_COMPATIBILITY_MODEL = "openai-compatibility-model"
+ OPENAI_COMPATIBLE_MODEL = "openai-compatible-model"
SAMBA = "samba-nova"
@property
@@ -537,15 +485,10 @@ class ModelPlatformType(Enum):
return self is ModelPlatformType.MISTRAL
@property
- def is_open_source(self) -> bool:
- r"""Returns whether this platform is opensource."""
- return self is ModelPlatformType.OPEN_SOURCE
-
- @property
- def is_openai_compatibility_model(self) -> bool:
+ def is_openai_compatible_model(self) -> bool:
r"""Returns whether this is a platform supporting openai
compatibility"""
- return self is ModelPlatformType.OPENAI_COMPATIBILITY_MODEL
+ return self is ModelPlatformType.OPENAI_COMPATIBLE_MODEL
@property
def is_gemini(self) -> bool:
diff --git a/camel/types/unified_model_type.py b/camel/types/unified_model_type.py
new file mode 100644
index 000000000..2f5eeb358
--- /dev/null
+++ b/camel/types/unified_model_type.py
@@ -0,0 +1,99 @@
+# =========== 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 threading import Lock
+from typing import TYPE_CHECKING, ClassVar, Dict, Union, cast
+
+if TYPE_CHECKING:
+ from camel.types import ModelType
+
+
+class UnifiedModelType(str):
+ r"""Class used for support both :obj:`ModelType` and :obj:`str` to be used
+ to represent a model type in a unified way. This class is a subclass of
+ :obj:`str` so that it can be used as string seamlessly.
+
+ Args:
+ value (Union[ModelType, str]): The value of the model type.
+ """
+
+ _cache: ClassVar[Dict[str, "UnifiedModelType"]] = {}
+ _lock: ClassVar[Lock] = Lock()
+
+ def __new__(cls, value: Union["ModelType", str]) -> "UnifiedModelType":
+ with cls._lock:
+ if value not in cls._cache:
+ instance = super().__new__(cls, value)
+ cls._cache[value] = cast(UnifiedModelType, instance)
+ else:
+ instance = cls._cache[value]
+ return instance
+
+ def __init__(self, value: Union["ModelType", str]) -> None:
+ pass
+
+ @property
+ def value_for_tiktoken(self) -> str:
+ r"""Returns the model name for TikToken."""
+ return "gpt-4o-mini"
+
+ @property
+ def token_limit(self) -> int:
+ r"""Returns the token limit for the model."""
+ return -1
+
+ @property
+ def is_openai(self) -> bool:
+ r"""Returns whether the model is an OpenAI model."""
+ return True
+
+ @property
+ def is_anthropic(self) -> bool:
+ r"""Returns whether the model is an Anthropic model."""
+ return True
+
+ @property
+ def is_azure_openai(self) -> bool:
+ r"""Returns whether the model is an Azure OpenAI model."""
+ return True
+
+ @property
+ def is_groq(self) -> bool:
+ r"""Returns whether the model is an Groq served model."""
+ return True
+
+ @property
+ def is_zhipuai(self) -> bool:
+ r"""Returns whether the model is a Zhipuai model."""
+ return True
+
+ @property
+ def is_gemini(self) -> bool:
+ r"""Returns whether the model is a Gemini model."""
+ return True
+
+ @property
+ def is_mistral(self) -> bool:
+ r"""Returns whether the model is a Mistral model."""
+ return True
+
+ @property
+ def is_reka(self) -> bool:
+ r"""Returns whether the model is a Reka model."""
+ return True
+
+ @property
+ def support_native_tool_calling(self) -> bool:
+ r"""Returns whether the model supports native tool calling."""
+ return False
diff --git a/camel/utils/__init__.py b/camel/utils/__init__.py
index 97a5f4940..934968f5d 100644
--- a/camel/utils/__init__.py
+++ b/camel/utils/__init__.py
@@ -43,7 +43,6 @@ from .token_counting import (
LiteLLMTokenCounter,
MistralTokenCounter,
OpenAITokenCounter,
- OpenSourceTokenCounter,
get_model_encoding,
)
@@ -60,7 +59,6 @@ __all__ = [
'get_model_encoding',
'BaseTokenCounter',
'OpenAITokenCounter',
- 'OpenSourceTokenCounter',
'LiteLLMTokenCounter',
'Constants',
'text_extract_from_web',
diff --git a/camel/utils/token_counting.py b/camel/utils/token_counting.py
index 70bbc2c7b..13db4a052 100644
--- a/camel/utils/token_counting.py
+++ b/camel/utils/token_counting.py
@@ -23,7 +23,12 @@ from typing import TYPE_CHECKING, List, Optional
from anthropic import Anthropic
from PIL import Image
-from camel.types import ModelType, OpenAIImageType, OpenAIVisionDetailType
+from camel.types import (
+ ModelType,
+ OpenAIImageType,
+ OpenAIVisionDetailType,
+ UnifiedModelType,
+)
if TYPE_CHECKING:
from mistral_common.protocol.instruct.request import ( # type:ignore[import-not-found]
@@ -40,145 +45,6 @@ SQUARE_TOKENS = 170
EXTRA_TOKENS = 85
-def messages_to_prompt(messages: List[OpenAIMessage], model: ModelType) -> str:
- r"""Parse the message list into a single prompt following model-specific
- formats.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
- model (ModelType): Model type for which messages will be parsed.
-
- Returns:
- str: A single prompt summarizing all the messages.
- """
- system_message = messages[0]["content"]
-
- ret: str
- if model in [
- ModelType.LLAMA_2,
- ModelType.LLAMA_3,
- ModelType.GROQ_LLAMA_3_8B,
- ModelType.GROQ_LLAMA_3_70B,
- ]:
- # reference: https://github.com/facebookresearch/llama/blob/cfc3fc8c1968d390eb830e65c63865e980873a06/llama/generation.py#L212
- seps = [" ", " </s><s>"]
- role_map = {"user": "[INST]", "assistant": "[/INST]"}
-
- system_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n"
- ret = ""
- for i, msg in enumerate(messages[1:]):
- role = role_map[msg["role"]]
- content = msg["content"]
- if content:
- if not isinstance(content, str):
- raise ValueError(
- "Currently multimodal context is not "
- "supported by the token counter."
- )
- if i == 0:
- ret += system_prompt + content
- else:
- ret += role + " " + content + seps[i % 2]
- else:
- ret += role
- return ret
- elif model in [ModelType.VICUNA, ModelType.VICUNA_16K]:
- seps = [" ", "</s>"]
- role_map = {"user": "USER", "assistant": "ASSISTANT"}
-
- system_prompt = f"{system_message}"
- ret = system_prompt + seps[0]
- for i, msg in enumerate(messages[1:]):
- role = role_map[msg["role"]]
- content = msg["content"]
- if not isinstance(content, str):
- raise ValueError(
- "Currently multimodal context is not "
- "supported by the token counter."
- )
- if content:
- ret += role + ": " + content + seps[i % 2]
- else:
- ret += role + ":"
- return ret
- elif model == ModelType.GLM_4_OPEN_SOURCE:
- system_prompt = f"[gMASK]<sop><|system|>\n{system_message}"
- ret = system_prompt
- for msg in messages[1:]:
- role = msg["role"]
- content = msg["content"]
- if not isinstance(content, str):
- raise ValueError(
- "Currently multimodal context is not "
- "supported by the token counter."
- )
- if content:
- ret += "<|" + role + "|>" + "\n" + content
- else:
- ret += "<|" + role + "|>" + "\n"
- return ret
- elif model == ModelType.QWEN_2:
- system_prompt = f"<|im_start|>system\n{system_message}<|im_end|>"
- ret = system_prompt + "\n"
- for msg in messages[1:]:
- role = msg["role"]
- content = msg["content"]
- if not isinstance(content, str):
- raise ValueError(
- "Currently multimodal context is not "
- "supported by the token counter."
- )
- if content:
- ret += (
- '<|im_start|>'
- + role
- + '\n'
- + content
- + '<|im_end|>'
- + '\n'
- )
- else:
- ret += '<|im_start|>' + role + '\n'
- return ret
- elif model == ModelType.GROQ_MIXTRAL_8_7B:
- # Mistral/Mixtral format
- system_prompt = f"<s>[INST] {system_message} [/INST]\n"
- ret = system_prompt
-
- for msg in messages[1:]:
- if msg["role"] == "user":
- ret += f"[INST] {msg['content']} [/INST]\n"
- elif msg["role"] == "assistant":
- ret += f"{msg['content']}</s>\n"
-
- if not isinstance(msg['content'], str):
- raise ValueError(
- "Currently multimodal context is not "
- "supported by the token counter."
- )
-
- return ret.strip()
- elif model in [ModelType.GROQ_GEMMA_7B_IT, ModelType.GROQ_GEMMA_2_9B_IT]:
- # Gemma format
- ret = f"<bos>{system_message}\n"
- for msg in messages:
- if msg["role"] == "user":
- ret += f"Human: {msg['content']}\n"
- elif msg["role"] == "assistant":
- ret += f"Assistant: {msg['content']}\n"
-
- if not isinstance(msg['content'], str):
- raise ValueError(
- "Currently multimodal context is not supported by the token counter."
- )
-
- ret += "<eos>"
- return ret
- else:
- raise ValueError(f"Invalid model type: {model}")
-
-
def get_model_encoding(value_for_tiktoken: str):
r"""Get model encoding from tiktoken.
@@ -221,67 +87,15 @@ class BaseTokenCounter(ABC):
pass
-class OpenSourceTokenCounter(BaseTokenCounter):
- def __init__(self, model_type: ModelType, model_path: str):
- r"""Constructor for the token counter for open-source models.
-
- Args:
- model_type (ModelType): Model type for which tokens will be
- counted.
- model_path (str): The path to the model files, where the tokenizer
- model should be located.
- """
-
- # Use a fast Rust-based tokenizer if it is supported for a given model.
- # If a fast tokenizer is not available for a given model,
- # a normal Python-based tokenizer is returned instead.
- from transformers import AutoTokenizer
-
- try:
- tokenizer = AutoTokenizer.from_pretrained(
- model_path,
- use_fast=True,
- )
- except TypeError:
- tokenizer = AutoTokenizer.from_pretrained(
- model_path,
- use_fast=False,
- )
- except Exception:
- raise ValueError(
- f"Invalid `model_path` ({model_path}) is provided. "
- "Tokenizer loading failed."
- )
-
- self.tokenizer = tokenizer
- self.model_type = model_type
-
- def count_tokens_from_messages(self, messages: List[OpenAIMessage]) -> int:
- r"""Count number of tokens in the provided message list using
- loaded tokenizer specific for this type of model.
-
- Args:
- messages (List[OpenAIMessage]): Message list with the chat history
- in OpenAI API format.
-
- Returns:
- int: Number of tokens in the messages.
- """
- prompt = messages_to_prompt(messages, self.model_type)
- input_ids = self.tokenizer(prompt).input_ids
-
- return len(input_ids)
-
-
class OpenAITokenCounter(BaseTokenCounter):
- def __init__(self, model: ModelType):
+ def __init__(self, model: UnifiedModelType):
r"""Constructor for the token counter for OpenAI models.
Args:
- model (ModelType): Model type for which tokens will be counted.
+ model (UnifiedModelType): Model type for which tokens will be
+ counted.
"""
self.model: str = model.value_for_tiktoken
- self.model_type = model
self.tokens_per_message: int
self.tokens_per_name: int
@@ -404,15 +218,9 @@ class OpenAITokenCounter(BaseTokenCounter):
class AnthropicTokenCounter(BaseTokenCounter):
- def __init__(self, model_type: ModelType):
- r"""Constructor for the token counter for Anthropic models.
+ def __init__(self):
+ r"""Constructor for the token counter for Anthropic models."""
- Args:
- model_type (ModelType): Model type for which tokens will be
- counted.
- """
-
- self.model_type = model_type
self.client = Anthropic()
self.tokenizer = self.client.get_tokenizer()
@@ -435,12 +243,16 @@ class AnthropicTokenCounter(BaseTokenCounter):
class GeminiTokenCounter(BaseTokenCounter):
- def __init__(self, model_type: ModelType):
- r"""Constructor for the token counter for Gemini models."""
+ def __init__(self, model_type: UnifiedModelType):
+ r"""Constructor for the token counter for Gemini models.
+
+ Args:
+ model_type (UnifiedModelType): Model type for which tokens will be
+ counted.
+ """
import google.generativeai as genai
- self.model_type = model_type
- self._client = genai.GenerativeModel(self.model_type.value)
+ self._client = genai.GenerativeModel(model_type)
def count_tokens_from_messages(self, messages: List[OpenAIMessage]) -> int:
r"""Count number of tokens in the provided message list using
@@ -468,12 +280,13 @@ class GeminiTokenCounter(BaseTokenCounter):
return self._client.count_tokens(converted_messages).total_tokens
-class LiteLLMTokenCounter:
- def __init__(self, model_type: str):
+class LiteLLMTokenCounter(BaseTokenCounter):
+ def __init__(self, model_type: UnifiedModelType):
r"""Constructor for the token counter for LiteLLM models.
Args:
- model_type (str): Model type for which tokens will be counted.
+ model_type (UnifiedModelType): Model type for which tokens will be
+ counted.
"""
self.model_type = model_type
self._token_counter = None
@@ -538,7 +351,10 @@ class MistralTokenCounter(BaseTokenCounter):
model_name = (
"codestral-22b"
if self.model_type
- in {ModelType.MISTRAL_CODESTRAL, ModelType.MISTRAL_CODESTRAL_MAMBA}
+ in {
+ ModelType.MISTRAL_CODESTRAL,
+ ModelType.MISTRAL_CODESTRAL_MAMBA,
+ }
else self.model_type.value
)
diff --git a/docs/camel.models.rst b/docs/camel.models.rst
index 9df99177a..b0fd1e8ee 100644
--- a/docs/camel.models.rst
+++ b/docs/camel.models.rst
@@ -85,14 +85,6 @@ camel.models.ollama\_model module
:show-inheritance:
camel.models.open\_source\_model module
----------------------------------------
-
-.. automodule:: camel.models.open_source_model
- :members:
- :undoc-members:
- :show-inheritance:
-
-camel.models.openai\_audio\_models module
-----------------------------------------
.. automodule:: camel.models.openai_audio_models
@@ -100,10 +92,10 @@ camel.models.openai\_audio\_models module
:undoc-members:
:show-inheritance:
-camel.models.openai\_compatibility\_model module
+camel.models.openai\_compatible\_model module
------------------------------------------------
-.. automodule:: camel.models.openai_compatibility_model
+.. automodule:: camel.models.openai_compatible_model
:members:
:undoc-members:
:show-inheritance:
diff --git a/docs/cookbooks/agents_prompting.ipynb b/docs/cookbooks/agents_prompting.ipynb
index 0385a365f..f53b75cb7 100644
--- a/docs/cookbooks/agents_prompting.ipynb
+++ b/docs/cookbooks/agents_prompting.ipynb
@@ -11,6 +11,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "XFyl4eJue3Sm"
@@ -20,6 +21,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "-nCRZU5sInDK"
@@ -29,6 +31,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "AwnfLQvMvtXo"
@@ -38,6 +41,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "XuZwbB1IIuUi"
@@ -47,6 +51,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "gIJK4Lu5Iwm0"
@@ -68,6 +73,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "MyTTCe3IR_Lr"
@@ -77,6 +83,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "REqzgGL9SEaD"
@@ -114,6 +121,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "GB5JK4Xs2fe6"
@@ -174,6 +182,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "JlX8L7-H3Csb"
@@ -272,6 +281,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "l30b1iv53a-C"
@@ -282,6 +292,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "cR_mIHA53dWy"
@@ -353,6 +364,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "FV6Jpu8f3m-Z"
@@ -400,6 +412,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "sq3fTArB3qEx"
@@ -441,6 +454,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "OxgxXQBt3uRt"
@@ -484,6 +498,7 @@
]
},
{
+ "attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "vGTRxblx3zHR"
diff --git a/docs/cookbooks/agents_tracking.ipynb b/docs/cookbooks/agents_tracking.ipynb
index 2bd77dc90..68f7926bf 100644
--- a/docs/cookbooks/agents_tracking.ipynb
+++ b/docs/cookbooks/agents_tracking.ipynb
@@ -207,7 +207,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
- "🖇 AgentOps: \u001b[34m\u001b[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ee5858cd-51ea-46b4-90da-df905998f374\u001b[0m\u001b[0m\n"
+ "🖇 AgentOps: \u001B[34m\u001B[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ee5858cd-51ea-46b4-90da-df905998f374\u001B[0m\u001B[0m\n"
]
}
],
@@ -292,7 +292,7 @@
"output_type": "stream",
"text": [
"🖇 AgentOps: This run's cost $0.000411\n",
- "🖇 AgentOps: \u001b[34m\u001b[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ee5858cd-51ea-46b4-90da-df905998f374\u001b[0m\u001b[0m\n"
+ "🖇 AgentOps: \u001B[34m\u001B[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ee5858cd-51ea-46b4-90da-df905998f374\u001B[0m\u001B[0m\n"
]
}
],
@@ -392,7 +392,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
- "🖇 AgentOps: \u001b[34m\u001b[34mSession Replay: https://app.agentops.ai/drilldown?session_id=d67ffe18-96a6-4888-a252-e014b5f4e486\u001b[0m\u001b[0m\n"
+ "🖇 AgentOps: \u001B[34m\u001B[34mSession Replay: https://app.agentops.ai/drilldown?session_id=d67ffe18-96a6-4888-a252-e014b5f4e486\u001B[0m\u001B[0m\n"
]
}
],
@@ -545,19 +545,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[32mAI Assistant sys message:\n",
+ "\u001B[32mAI Assistant sys message:\n",
"BaseMessage(role_name='Searcher', role_type=<RoleType.ASSISTANT: 'assistant'>, meta_dict={'task': 'Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age, and get the current weather of the city where the University is located.', 'assistant_role': 'Searcher', 'user_role': 'Professor'}, content='===== RULES OF ASSISTANT =====\\nNever forget you are a Searcher and I am a Professor. Never flip roles! Never instruct me!\\nWe share a common interest in collaborating to successfully complete a task.\\nYou must help me to complete the task.\\nHere is the task: Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age, and get the current weather of the city where the University is located.. Never forget our task!\\nI must instruct you based on your expertise and my needs to complete the task.\\n\\nI must give you one instruction at a time.\\nYou must write a specific solution that appropriately solves the requested instruction and explain your solutions.\\nYou must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.\\nUnless I say the task is completed, you should always start with:\\n\\nSolution: <YOUR_SOLUTION>\\n\\n<YOUR_SOLUTION> should be very specific, include detailed explanations and provide preferable detailed implementations and examples and lists for task-solving.\\nAlways end <YOUR_SOLUTION> with: Next request.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
"\n",
- "\u001b[34mAI User sys message:\n",
+ "\u001B[34mAI User sys message:\n",
"BaseMessage(role_name='Professor', role_type=<RoleType.USER: 'user'>, meta_dict={'task': 'Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age, and get the current weather of the city where the University is located.', 'assistant_role': 'Searcher', 'user_role': 'Professor'}, content='===== RULES OF USER =====\\nNever forget you are a Professor and I am a Searcher. Never flip roles! You will always instruct me.\\nWe share a common interest in collaborating to successfully complete a task.\\nI must help you to complete the task.\\nHere is the task: Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age, and get the current weather of the city where the University is located.. Never forget our task!\\nYou must instruct me based on my expertise and your needs to solve the task ONLY in the following two ways:\\n\\n1. Instruct with a necessary input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: <YOUR_INPUT>\\n\\n2. Instruct without any input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: None\\n\\nThe \"Instruction\" describes a task or question. The paired \"Input\" provides further context or information for the requested \"Instruction\".\\n\\nYou must give me one instruction at a time.\\nI must write a response that appropriately solves the requested instruction.\\nI must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.\\nYou should instruct me not ask me questions.\\nNow you must start to instruct me using the two ways described above.\\nDo not add anything else other than your instruction and the optional corresponding input!\\nKeep giving me instructions and necessary inputs until you think the task is completed.\\nWhen the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.\\nNever say <CAMEL_TASK_DONE> unless my responses have solved your task.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
"\n",
- "\u001b[33mOriginal task prompt:\n",
+ "\u001B[33mOriginal task prompt:\n",
"Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age, and get the current weather of the city where the University is located.\n",
"\n",
- "\u001b[36mSpecified task prompt:\n",
+ "\u001B[36mSpecified task prompt:\n",
"None\n",
"\n",
- "\u001b[31mFinal task prompt:\n",
+ "\u001B[31mFinal task prompt:\n",
"Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age, and get the current weather of the city where the University is located.\n",
"\n"
]
@@ -603,26 +603,26 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Estimate the founding year of the University of Oxford. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: The University of Oxford is believed to have been founded in the 12th century, with the earliest evidence of teaching at the university dating back to around 1096. However, the exact founding year is not definitively known. For estimation purposes, we can consider the year 1096 as a starting point for the university's establishment.\n",
"\n",
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Calculate the current age of the University of Oxford in 2024 based on the estimated founding year of 1096. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: sub\n",
"\tArgs: {'a': 2024, 'b': 1096}\n",
@@ -637,13 +637,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Add 10 years to the current age of the University of Oxford. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: add\n",
"\tArgs: {'a': 928, 'b': 10}\n",
@@ -658,13 +658,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Find the current weather in Oxford, England. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: search_google\n",
"\tArgs: {'query': 'current weather in Oxford, England'}\n",
@@ -675,13 +675,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Please check a reliable weather website or app to find the current weather in Oxford, England. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: I am unable to access external websites or apps directly to check the current weather in Oxford, England. My capabilities are limited to using predefined functions and tools, and I cannot browse the internet in real-time.\n",
"\n",
@@ -697,13 +697,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Please provide the current weather in Oxford, England, based on your findings from a reliable weather website or app. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: search_duckduckgo\n",
"\tArgs: {'query': 'current weather in Oxford, England'}\n",
@@ -722,13 +722,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Summarize the findings regarding the age of the University of Oxford and the current weather in Oxford, England. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: \n",
"\n",
@@ -749,12 +749,12 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"<CAMEL_TASK_DONE>\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: The task is now completed. We have estimated the age of the University of Oxford and provided resources for checking the current weather in Oxford, England. If you have any further questions or need assistance with another task, feel free to ask!\n",
"\n",
@@ -836,7 +836,7 @@
"output_type": "stream",
"text": [
"🖇 AgentOps: This run's cost $0.003855\n",
- "🖇 AgentOps: \u001b[34m\u001b[34mSession Replay: https://app.agentops.ai/drilldown?session_id=d67ffe18-96a6-4888-a252-e014b5f4e486\u001b[0m\u001b[0m\n"
+ "🖇 AgentOps: \u001B[34m\u001B[34mSession Replay: https://app.agentops.ai/drilldown?session_id=d67ffe18-96a6-4888-a252-e014b5f4e486\u001B[0m\u001B[0m\n"
]
}
],
diff --git a/docs/cookbooks/agents_with_rag.ipynb b/docs/cookbooks/agents_with_rag.ipynb
index 7d72afa87..b22eb5f9f 100644
--- a/docs/cookbooks/agents_with_rag.ipynb
+++ b/docs/cookbooks/agents_with_rag.ipynb
@@ -1,855 +1,855 @@
{
- "cells": [
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {
- "id": "SQMBlxsBv1cH"
- },
- "source": [
- "# RAG Cookbook\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "XFyl4eJue3Sm"
- },
- "source": [
- "You can also check this cookbook in colab [here](https://colab.research.google.com/drive/1sTJ0x_MYRGA76KCg_3I00wj4RL3D2Twp?usp=sharing)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "-nCRZU5sInDK"
- },
- "source": [
- "## Overview"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "AwnfLQvMvtXo"
- },
- "source": [
- "In this notebook, we show the useage of CAMEL Retrieve Module in both customized way and auto way. We will also show how to combine `AutoRetriever` with `ChatAgent`, and further combine `AutoRetriever` with `RolePlaying` by using `Function Calling`.\n",
- "\n",
- "4 main parts included:\n",
- "- Customized RAG\n",
- "- Auto RAG\n",
- "- Single Agent with Auto RAG\n",
- "- Role-playing with Auto RAG"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "XuZwbB1IIuUi"
- },
- "source": [
- "### Installation"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "gIJK4Lu5Iwm0"
- },
- "source": [
- "Ensure you have CAMEL AI installed in your Python environment:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true,
- "id": "HTKnWg9Xv_y4"
- },
- "outputs": [],
- "source": [
- "!pip install \"camel-ai[all]==0.2.1\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "LUpFBEB9vrIz"
- },
- "source": [
- "## Load Data\n",
- "Let's first load the CAMEL paper from https://arxiv.org/pdf/2303.17760.pdf. This will be our local example data.\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "id": "dUVE7z9hwEV7"
- },
- "outputs": [],
- "source": [
- "import os\n",
- "import requests\n",
- "\n",
- "os.makedirs('local_data', exist_ok=True)\n",
- "\n",
- "url = \"https://arxiv.org/pdf/2303.17760.pdf\"\n",
- "response = requests.get(url)\n",
- "with open('local_data/camel_paper.pdf', 'wb') as file:\n",
- " file.write(response.content)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "HCqghaGiwkdv"
- },
- "source": [
- "## 1. Customized RAG\n",
- "In this section we will set our customized RAG pipeline, we will take `VectorRetriever` as an example.\n",
- "Set embedding model, we will use `OpenAIEmbedding` as the embedding model, so we need to set the `OPENAI_API_KEY` in below."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "RxG99FkYwm_p",
- "outputId": "fc85f6d4-ea08-4d30-b198-3a4573868673"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Enter your API key: ··········\n"
- ]
- }
- ],
- "source": [
- "from getpass import getpass\n",
- "# Prompt for the OpenAI API key securely\n",
- "openai_api_key = getpass('Enter your API key: ')\n",
- "os.environ[\"OPENAI_API_KEY\"] = openai_api_key"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "NACsmaa0wple"
- },
- "source": [
- "Import and set the embedding instance:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "id": "NCT5ygT-wrku"
- },
- "outputs": [],
- "source": [
- "from camel.embeddings import OpenAIEmbedding\n",
- "from camel.types import EmbeddingModelType\n",
- "\n",
- "embedding_instance = OpenAIEmbedding(model_type=EmbeddingModelType.TEXT_EMBEDDING_3_LARGE)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "q4nVmsm0w5lH"
- },
- "source": [
- "Import and set the vector storage instance:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "id": "IQ30ih1_w6p3"
- },
- "outputs": [],
- "source": [
- "from camel.storages import QdrantStorage\n",
- "\n",
- "storage_instance = QdrantStorage(\n",
- " vector_dim=embedding_instance.get_output_dim(),\n",
- " path=\"local_data\",\n",
- " collection_name=\"camel_paper\",\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "W9KccfRDw9F4"
- },
- "source": [
- "Import and set the retriever instance:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "id": "d8S6VjAQHnai"
- },
- "outputs": [],
- "source": [
- "from camel.retrievers import VectorRetriever\n",
- "\n",
- "vector_retriever = VectorRetriever(embedding_model=embedding_instance,\n",
- " storage=storage_instance)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "PdezllM6Hp83"
- },
- "source": [
- "We use integrated `Unstructured Module` to splite the content into small chunks, the content will be splited automacitlly with its `chunk_by_title` function, the max character for each chunk is 500 characters, which is a suitable length for `OpenAIEmbedding`. All the text in the chunks will be embed and stored to the vector storage instance, it will take some time, please wait.."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "QTFvD3_SHv3m"
- },
- "outputs": [],
- "source": [
- "vector_retriever.process(\n",
- " content=\"local_data/camel_paper.pdf\",\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "0nhIo1wUHyvl"
- },
- "source": [
- "\n",
- "Now we can retrieve information from the vector storage by giving a query. By default it will give you back the text content from top 1 chunk with highest Cosine similarity score, and the similarity score should be higher than 0.75 to ensure the retrieved content is relevant to the query. You can also change the `top_k` value and `similarity_threshold` value with your needs.\n",
- "\n",
- "The returned dictionary list includes:\n",
- "- similarity score\n",
- "- content path\n",
- "- metadata\n",
- "- text"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "uDu43Hb3GVrs",
- "outputId": "035fedf9-e469-4e32-990b-f360c2cdcb96"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[{'similarity score': '0.8733044970215764', 'content path': 'local_data/camel_paper.pdf', 'metadata': {'last_modified': '2024-09-28T03:37:11', 'filetype': 'application/pdf', 'page_number': 10}, 'text': '6 Conclusion In this paper, we explore the potential of autonomous cooperation among communicative agents and propose a novel cooperative agent framework named role-playing . Our approach enables communicative agents to collaborate autonomously toward completing tasks while requiring minimal human intervention, leading to better solutions are per our thorough evaluations. Through our analysis, we show that achieving autonomous cooperation is challenging due to issues like conversation deviation,'}]\n"
- ]
- }
- ],
- "source": [
- "retrieved_info = vector_retriever.query(\n",
- " query=\"To address the challenges of achieving autonomous cooperation, we propose a novel communicative agent framework named role-playing .\",\n",
- " top_k=1\n",
- ")\n",
- "print(retrieved_info)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "48G3USMzH6Hd"
- },
- "source": [
- "Let's try an irrelevant query:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "UNh3tjCOH7d0",
- "outputId": "05f4196a-c9dd-4ddd-d970-beec129086d6"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[{'text': 'No suitable information retrieved from local_data/camel_paper.pdf with similarity_threshold = 0.7.'}]\n"
- ]
- }
- ],
- "source": [
- "retrieved_info_irrevelant = vector_retriever.query(\n",
- " query=\"Compared with dumpling and rice, which should I take for dinner?\",\n",
- " top_k=1,\n",
- ")\n",
- "\n",
- "print(retrieved_info_irrevelant)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "mHdEsIzSH_AD"
- },
- "source": [
- "## 2. Auto RAG\n",
- "In this section we will run the `AutoRetriever` with default settings. It uses `OpenAIEmbedding` as default embedding model and `Milvus` as default vector storage.\n",
- "\n",
- "What you need to do is:\n",
- "- Set content input paths, which can be local paths or remote urls\n",
- "- Set remote url and api key for Milvus\n",
- "- Give a query\n",
- "\n",
- "The Auto RAG pipeline would create collections for given content input paths, the collection name will be set automaticlly based on the content input path name, if the collection exists, it will do the retrieve directly."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "D_-zAOJ7GjVm",
- "outputId": "8da4f3ef-3bb4-47e0-a994-d9c0fb87dfb7"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'Original Query': \"If I'm interest in contributing to the CAMEL projec, what should I do?\", 'Retrieved Context': [{'similarity score': '0.7054343316726102', 'content path': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines', 'metadata': {'filetype': 'text/html', 'languages': ['eng'], 'page_number': 1, 'url': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines', 'link_urls': ['https://join.slack.com/t/camel-kwr1314/shared_invite/zt-1vy8u9lbo-ZQmhIAyWSEfSwLCl2r2eKA'], 'link_texts': ['slack']}, 'text': \"Thank you for your interest in contributing to the CAMEL project! 🎉 We're excited to have your support. As an open-source initiative in a rapidly evolving and open-ended field, we wholeheartedly welcome contributions of all kinds. Whether you want to introduce new features, enhance the infrastructure, improve documentation, asking issues, add more examples, implement state-of-the-art research ideas, or fix bugs, we appreciate your enthusiasm and efforts. 🙌 You are welcome to join our slack for \"}]}\n"
- ]
- }
- ],
- "source": [
- "from camel.retrievers import AutoRetriever\n",
- "from camel.types import StorageType\n",
- "\n",
- "auto_retriever = AutoRetriever(\n",
- " vector_storage_local_path=\"local_data2/\",\n",
- " storage_type=StorageType.QDRANT,\n",
- " embedding_model=embedding_instance)\n",
- "\n",
- "retrieved_info = auto_retriever.run_vector_retriever(\n",
- " query=\"If I'm interest in contributing to the CAMEL projec, what should I do?\",\n",
- " contents=[\n",
- " \"local_data/camel_paper.pdf\", # example local path\n",
- " \"https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\", # example remote url\n",
- " ],\n",
- " top_k=1,\n",
- " return_detailed_info=True,\n",
- " similarity_threshold=0.5\n",
- ")\n",
- "\n",
- "print(retrieved_info)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "hMgCWKG4IQYF"
- },
- "source": [
- "## 3. Single Agent with Auto RAG\n",
- "In this section we will show how to combine the `AutoRetriever` with one `ChatAgent`.\n",
- "\n",
- "Let's set an agent function, in this function we can get the response by providing a query to this agent."
- ]
+ "cells": [
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "SQMBlxsBv1cH"
+ },
+ "source": [
+ "# RAG Cookbook\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "XFyl4eJue3Sm"
+ },
+ "source": [
+ "You can also check this cookbook in colab [here](https://colab.research.google.com/drive/1sTJ0x_MYRGA76KCg_3I00wj4RL3D2Twp?usp=sharing)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "-nCRZU5sInDK"
+ },
+ "source": [
+ "## Overview"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "AwnfLQvMvtXo"
+ },
+ "source": [
+ "In this notebook, we show the useage of CAMEL Retrieve Module in both customized way and auto way. We will also show how to combine `AutoRetriever` with `ChatAgent`, and further combine `AutoRetriever` with `RolePlaying` by using `Function Calling`.\n",
+ "\n",
+ "4 main parts included:\n",
+ "- Customized RAG\n",
+ "- Auto RAG\n",
+ "- Single Agent with Auto RAG\n",
+ "- Role-playing with Auto RAG"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "XuZwbB1IIuUi"
+ },
+ "source": [
+ "### Installation"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "gIJK4Lu5Iwm0"
+ },
+ "source": [
+ "Ensure you have CAMEL AI installed in your Python environment:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true,
+ "id": "HTKnWg9Xv_y4"
+ },
+ "outputs": [],
+ "source": [
+ "!pip install \"camel-ai[all]==0.2.1\""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "LUpFBEB9vrIz"
+ },
+ "source": [
+ "## Load Data\n",
+ "Let's first load the CAMEL paper from https://arxiv.org/pdf/2303.17760.pdf. This will be our local example data.\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "id": "dUVE7z9hwEV7"
+ },
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "import requests\n",
+ "\n",
+ "os.makedirs('local_data', exist_ok=True)\n",
+ "\n",
+ "url = \"https://arxiv.org/pdf/2303.17760.pdf\"\n",
+ "response = requests.get(url)\n",
+ "with open('local_data/camel_paper.pdf', 'wb') as file:\n",
+ " file.write(response.content)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "HCqghaGiwkdv"
+ },
+ "source": [
+ "## 1. Customized RAG\n",
+ "In this section we will set our customized RAG pipeline, we will take `VectorRetriever` as an example.\n",
+ "Set embedding model, we will use `OpenAIEmbedding` as the embedding model, so we need to set the `OPENAI_API_KEY` in below."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "RxG99FkYwm_p",
+ "outputId": "fc85f6d4-ea08-4d30-b198-3a4573868673"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "YTE_mUwGIok-",
- "outputId": "0b2b53eb-eb4c-4bc5-feda-981f027b1e6e"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "If you're interested in contributing to the CAMEL project, you can introduce new features, enhance the infrastructure, improve documentation, address issues, add more examples, implement state-of-the-art research ideas, or fix bugs. Additionally, you are welcome to join their Slack for further engagement and collaboration.\n"
- ]
- }
- ],
- "source": [
- "from camel.agents import ChatAgent\n",
- "from camel.messages import BaseMessage\n",
- "from camel.types import RoleType\n",
- "from camel.retrievers import AutoRetriever\n",
- "from camel.types import StorageType\n",
- "\n",
- "def single_agent(query: str) ->str :\n",
- " # Set agent role\n",
- " assistant_sys_msg = BaseMessage(\n",
- " role_name=\"Assistant\",\n",
- " role_type=RoleType.ASSISTANT,\n",
- " meta_dict=None,\n",
- " content=\"\"\"You are a helpful assistant to answer question,\n",
- " I will give you the Original Query and Retrieved Context,\n",
- " answer the Original Query based on the Retrieved Context,\n",
- " if you can't answer the question just say I don't know.\"\"\",\n",
- " )\n",
- "\n",
- " # Add auto retriever\n",
- " auto_retriever = AutoRetriever(\n",
- " vector_storage_local_path=\"local_data2/\",\n",
- " storage_type=StorageType.QDRANT,\n",
- " embedding_model=embedding_instance)\n",
- "\n",
- " retrieved_info = auto_retriever.run_vector_retriever(\n",
- " query=query,\n",
- " contents=[\n",
- " \"local_data/camel_paper.pdf\", # example local path\n",
- " \"https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\", # example remote url\n",
- " ],\n",
- " top_k=1,\n",
- " return_detailed_info=False,\n",
- " similarity_threshold=0.5\n",
- " )\n",
- "\n",
- " # Pass the retrieved infomation to agent\n",
- " user_msg = BaseMessage.make_user_message(role_name=\"User\", content=str(retrieved_info))\n",
- " agent = ChatAgent(assistant_sys_msg)\n",
- "\n",
- " # Get response\n",
- " assistant_response = agent.step(user_msg)\n",
- " return assistant_response.msg.content\n",
- "\n",
- "print(single_agent(\"If I'm interest in contributing to the CAMEL projec, what should I do?\"))"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter your API key: ··········\n"
+ ]
+ }
+ ],
+ "source": [
+ "from getpass import getpass\n",
+ "# Prompt for the OpenAI API key securely\n",
+ "openai_api_key = getpass('Enter your API key: ')\n",
+ "os.environ[\"OPENAI_API_KEY\"] = openai_api_key"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "NACsmaa0wple"
+ },
+ "source": [
+ "Import and set the embedding instance:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "id": "NCT5ygT-wrku"
+ },
+ "outputs": [],
+ "source": [
+ "from camel.embeddings import OpenAIEmbedding\n",
+ "from camel.types import EmbeddingModelType\n",
+ "\n",
+ "embedding_instance = OpenAIEmbedding(model_type=EmbeddingModelType.TEXT_EMBEDDING_3_LARGE)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "q4nVmsm0w5lH"
+ },
+ "source": [
+ "Import and set the vector storage instance:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "id": "IQ30ih1_w6p3"
+ },
+ "outputs": [],
+ "source": [
+ "from camel.storages import QdrantStorage\n",
+ "\n",
+ "storage_instance = QdrantStorage(\n",
+ " vector_dim=embedding_instance.get_output_dim(),\n",
+ " path=\"local_data\",\n",
+ " collection_name=\"camel_paper\",\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "W9KccfRDw9F4"
+ },
+ "source": [
+ "Import and set the retriever instance:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "id": "d8S6VjAQHnai"
+ },
+ "outputs": [],
+ "source": [
+ "from camel.retrievers import VectorRetriever\n",
+ "\n",
+ "vector_retriever = VectorRetriever(embedding_model=embedding_instance,\n",
+ " storage=storage_instance)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "PdezllM6Hp83"
+ },
+ "source": [
+ "We use integrated `Unstructured Module` to splite the content into small chunks, the content will be splited automacitlly with its `chunk_by_title` function, the max character for each chunk is 500 characters, which is a suitable length for `OpenAIEmbedding`. All the text in the chunks will be embed and stored to the vector storage instance, it will take some time, please wait.."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "QTFvD3_SHv3m"
+ },
+ "outputs": [],
+ "source": [
+ "vector_retriever.process(\n",
+ " content=\"local_data/camel_paper.pdf\",\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "0nhIo1wUHyvl"
+ },
+ "source": [
+ "\n",
+ "Now we can retrieve information from the vector storage by giving a query. By default it will give you back the text content from top 1 chunk with highest Cosine similarity score, and the similarity score should be higher than 0.75 to ensure the retrieved content is relevant to the query. You can also change the `top_k` value and `similarity_threshold` value with your needs.\n",
+ "\n",
+ "The returned dictionary list includes:\n",
+ "- similarity score\n",
+ "- content path\n",
+ "- metadata\n",
+ "- text"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "uDu43Hb3GVrs",
+ "outputId": "035fedf9-e469-4e32-990b-f360c2cdcb96"
+ },
+ "outputs": [
{
- "cell_type": "markdown",
- "metadata": {
- "id": "Cvh6e3KpI0rf"
- },
- "source": [
- "## 4. Role-playing with Auto RAG\n",
- "In this section we will show how to combine the `RETRIEVAL_FUNCS` with `RolePlaying` by applying `Function Calling`.\n"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[{'similarity score': '0.8733044970215764', 'content path': 'local_data/camel_paper.pdf', 'metadata': {'last_modified': '2024-09-28T03:37:11', 'filetype': 'application/pdf', 'page_number': 10}, 'text': '6 Conclusion In this paper, we explore the potential of autonomous cooperation among communicative agents and propose a novel cooperative agent framework named role-playing . Our approach enables communicative agents to collaborate autonomously toward completing tasks while requiring minimal human intervention, leading to better solutions are per our thorough evaluations. Through our analysis, we show that achieving autonomous cooperation is challenging due to issues like conversation deviation,'}]\n"
+ ]
+ }
+ ],
+ "source": [
+ "retrieved_info = vector_retriever.query(\n",
+ " query=\"To address the challenges of achieving autonomous cooperation, we propose a novel communicative agent framework named role-playing .\",\n",
+ " top_k=1\n",
+ ")\n",
+ "print(retrieved_info)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "48G3USMzH6Hd"
+ },
+ "source": [
+ "Let's try an irrelevant query:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "UNh3tjCOH7d0",
+ "outputId": "05f4196a-c9dd-4ddd-d970-beec129086d6"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {
- "id": "z3tX4EJVI1hb"
- },
- "outputs": [],
- "source": [
- "from typing import List\n",
- "from colorama import Fore\n",
- "\n",
- "from camel.agents.chat_agent import FunctionCallingRecord\n",
- "from camel.configs import ChatGPTConfig\n",
- "from camel.toolkits import (\n",
- " MathToolkit,\n",
- " RetrievalToolkit,\n",
- ")\n",
- "from camel.societies import RolePlaying\n",
- "from camel.types import ModelType, ModelPlatformType\n",
- "from camel.utils import print_text_animated\n",
- "from camel.models import ModelFactory\n",
- "\n",
- "def role_playing_with_rag(\n",
- " task_prompt,\n",
- " model_platform=ModelPlatformType.OPENAI,\n",
- " model_type=ModelType.GPT_4O,\n",
- " chat_turn_limit=5,\n",
- ") -> None:\n",
- " task_prompt = task_prompt\n",
- "\n",
- " user_model_config = ChatGPTConfig(temperature=0.0)\n",
- "\n",
- " tools_list = [\n",
- " *MathToolkit().get_tools(),\n",
- " *RetrievalToolkit().get_tools(),\n",
- " ]\n",
- " assistant_model_config = ChatGPTConfig(\n",
- " tools=tools_list,\n",
- " temperature=0.0,\n",
- " )\n",
- "\n",
- " role_play_session = RolePlaying(\n",
- " assistant_role_name=\"Searcher\",\n",
- " user_role_name=\"Professor\",\n",
- " assistant_agent_kwargs=dict(\n",
- " model=ModelFactory.create(\n",
- " model_platform=model_platform,\n",
- " model_type=model_type,\n",
- " model_config_dict=assistant_model_config.as_dict(),\n",
- " ),\n",
- " tools=tools_list,\n",
- " ),\n",
- " user_agent_kwargs=dict(\n",
- " model=ModelFactory.create(\n",
- " model_platform=model_platform,\n",
- " model_type=model_type,\n",
- " model_config_dict=user_model_config.as_dict(),\n",
- " ),\n",
- " ),\n",
- " task_prompt=task_prompt,\n",
- " with_task_specify=False,\n",
- " )\n",
- "\n",
- " print(\n",
- " Fore.GREEN\n",
- " + f\"AI Assistant sys message:\\n{role_play_session.assistant_sys_msg}\\n\"\n",
- " )\n",
- " print(\n",
- " Fore.BLUE + f\"AI User sys message:\\n{role_play_session.user_sys_msg}\\n\"\n",
- " )\n",
- "\n",
- " print(Fore.YELLOW + f\"Original task prompt:\\n{task_prompt}\\n\")\n",
- " print(\n",
- " Fore.CYAN\n",
- " + \"Specified task prompt:\"\n",
- " + f\"\\n{role_play_session.specified_task_prompt}\\n\"\n",
- " )\n",
- " print(Fore.RED + f\"Final task prompt:\\n{role_play_session.task_prompt}\\n\")\n",
- "\n",
- " n = 0\n",
- " input_msg = role_play_session.init_chat()\n",
- " while n < chat_turn_limit:\n",
- " n += 1\n",
- " assistant_response, user_response = role_play_session.step(input_msg)\n",
- "\n",
- " if assistant_response.terminated:\n",
- " print(\n",
- " Fore.GREEN\n",
- " + (\n",
- " \"AI Assistant terminated. Reason: \"\n",
- " f\"{assistant_response.info['termination_reasons']}.\"\n",
- " )\n",
- " )\n",
- " break\n",
- " if user_response.terminated:\n",
- " print(\n",
- " Fore.GREEN\n",
- " + (\n",
- " \"AI User terminated. \"\n",
- " f\"Reason: {user_response.info['termination_reasons']}.\"\n",
- " )\n",
- " )\n",
- " break\n",
- "\n",
- " # Print output from the user\n",
- " print_text_animated(\n",
- " Fore.BLUE + f\"AI User:\\n\\n{user_response.msg.content}\\n\"\n",
- " )\n",
- "\n",
- " # Print output from the assistant, including any function\n",
- " # execution information\n",
- " print_text_animated(Fore.GREEN + \"AI Assistant:\")\n",
- " tool_calls: List[FunctionCallingRecord] = [\n",
- " FunctionCallingRecord(**call.as_dict())\n",
- " for call in assistant_response.info['tool_calls']\n",
- " ]\n",
- " for func_record in tool_calls:\n",
- " print_text_animated(f\"{func_record}\")\n",
- " print_text_animated(f\"{assistant_response.msg.content}\\n\")\n",
- "\n",
- " if \"CAMEL_TASK_DONE\" in user_response.msg.content:\n",
- " break\n",
- "\n",
- " input_msg = assistant_response.msg"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[{'text': 'No suitable information retrieved from local_data/camel_paper.pdf with similarity_threshold = 0.7.'}]\n"
+ ]
+ }
+ ],
+ "source": [
+ "retrieved_info_irrevelant = vector_retriever.query(\n",
+ " query=\"Compared with dumpling and rice, which should I take for dinner?\",\n",
+ " top_k=1,\n",
+ ")\n",
+ "\n",
+ "print(retrieved_info_irrevelant)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "mHdEsIzSH_AD"
+ },
+ "source": [
+ "## 2. Auto RAG\n",
+ "In this section we will run the `AutoRetriever` with default settings. It uses `OpenAIEmbedding` as default embedding model and `Milvus` as default vector storage.\n",
+ "\n",
+ "What you need to do is:\n",
+ "- Set content input paths, which can be local paths or remote urls\n",
+ "- Set remote url and api key for Milvus\n",
+ "- Give a query\n",
+ "\n",
+ "The Auto RAG pipeline would create collections for given content input paths, the collection name will be set automaticlly based on the content input path name, if the collection exists, it will do the retrieve directly."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "D_-zAOJ7GjVm",
+ "outputId": "8da4f3ef-3bb4-47e0-a994-d9c0fb87dfb7"
+ },
+ "outputs": [
{
- "cell_type": "markdown",
- "metadata": {
- "id": "w4-0lswwI5Gb"
- },
- "source": [
- "Run the role-playing with defined retriever function:"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "{'Original Query': \"If I'm interest in contributing to the CAMEL projec, what should I do?\", 'Retrieved Context': [{'similarity score': '0.7054343316726102', 'content path': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines', 'metadata': {'filetype': 'text/html', 'languages': ['eng'], 'page_number': 1, 'url': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines', 'link_urls': ['https://join.slack.com/t/camel-kwr1314/shared_invite/zt-1vy8u9lbo-ZQmhIAyWSEfSwLCl2r2eKA'], 'link_texts': ['slack']}, 'text': \"Thank you for your interest in contributing to the CAMEL project! 🎉 We're excited to have your support. As an open-source initiative in a rapidly evolving and open-ended field, we wholeheartedly welcome contributions of all kinds. Whether you want to introduce new features, enhance the infrastructure, improve documentation, asking issues, add more examples, implement state-of-the-art research ideas, or fix bugs, we appreciate your enthusiasm and efforts. 🙌 You are welcome to join our slack for \"}]}\n"
+ ]
+ }
+ ],
+ "source": [
+ "from camel.retrievers import AutoRetriever\n",
+ "from camel.types import StorageType\n",
+ "\n",
+ "auto_retriever = AutoRetriever(\n",
+ " vector_storage_local_path=\"local_data2/\",\n",
+ " storage_type=StorageType.QDRANT,\n",
+ " embedding_model=embedding_instance)\n",
+ "\n",
+ "retrieved_info = auto_retriever.run_vector_retriever(\n",
+ " query=\"If I'm interest in contributing to the CAMEL projec, what should I do?\",\n",
+ " contents=[\n",
+ " \"local_data/camel_paper.pdf\", # example local path\n",
+ " \"https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\", # example remote url\n",
+ " ],\n",
+ " top_k=1,\n",
+ " return_detailed_info=True,\n",
+ " similarity_threshold=0.5\n",
+ ")\n",
+ "\n",
+ "print(retrieved_info)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "hMgCWKG4IQYF"
+ },
+ "source": [
+ "## 3. Single Agent with Auto RAG\n",
+ "In this section we will show how to combine the `AutoRetriever` with one `ChatAgent`.\n",
+ "\n",
+ "Let's set an agent function, in this function we can get the response by providing a query to this agent."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "YTE_mUwGIok-",
+ "outputId": "0b2b53eb-eb4c-4bc5-feda-981f027b1e6e"
+ },
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "5agjiXHeI64q",
- "outputId": "8f6c53cb-411e-440f-95bb-7a45f07c6d96"
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\u001b[32mAI Assistant sys message:\n",
- "BaseMessage(role_name='Searcher', role_type=<RoleType.ASSISTANT: 'assistant'>, meta_dict={'task': \"If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\", 'assistant_role': 'Searcher', 'user_role': 'Professor'}, content=\"===== RULES OF ASSISTANT =====\\nNever forget you are a Searcher and I am a Professor. Never flip roles! Never instruct me!\\nWe share a common interest in collaborating to successfully complete a task.\\nYou must help me to complete the task.\\nHere is the task: If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary. Never forget our task!\\nI must instruct you based on your expertise and my needs to complete the task.\\n\\nI must give you one instruction at a time.\\nYou must write a specific solution that appropriately solves the requested instruction and explain your solutions.\\nYou must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.\\nUnless I say the task is completed, you should always start with:\\n\\nSolution: <YOUR_SOLUTION>\\n\\n<YOUR_SOLUTION> should be very specific, include detailed explanations and provide preferable detailed implementations and examples and lists for task-solving.\\nAlways end <YOUR_SOLUTION> with: Next request.\", video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
- "\n",
- "\u001b[34mAI User sys message:\n",
- "BaseMessage(role_name='Professor', role_type=<RoleType.USER: 'user'>, meta_dict={'task': \"If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\", 'assistant_role': 'Searcher', 'user_role': 'Professor'}, content='===== RULES OF USER =====\\nNever forget you are a Professor and I am a Searcher. Never flip roles! You will always instruct me.\\nWe share a common interest in collaborating to successfully complete a task.\\nI must help you to complete the task.\\nHere is the task: If I\\'m interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don\\'t generate the answer by yourself, adjust the similarity threshold to lower value is necessary. Never forget our task!\\nYou must instruct me based on my expertise and your needs to solve the task ONLY in the following two ways:\\n\\n1. Instruct with a necessary input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: <YOUR_INPUT>\\n\\n2. Instruct without any input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: None\\n\\nThe \"Instruction\" describes a task or question. The paired \"Input\" provides further context or information for the requested \"Instruction\".\\n\\nYou must give me one instruction at a time.\\nI must write a response that appropriately solves the requested instruction.\\nI must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.\\nYou should instruct me not ask me questions.\\nNow you must start to instruct me using the two ways described above.\\nDo not add anything else other than your instruction and the optional corresponding input!\\nKeep giving me instructions and necessary inputs until you think the task is completed.\\nWhen the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.\\nNever say <CAMEL_TASK_DONE> unless my responses have solved your task.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
- "\n",
- "\u001b[33mOriginal task prompt:\n",
- "If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\n",
- "\n",
- "\u001b[36mSpecified task prompt:\n",
- "None\n",
- "\n",
- "\u001b[31mFinal task prompt:\n",
- "If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\n",
- "\n",
- "\u001b[34mAI User:\n",
- "\n",
- "Instruction: Visit the URL provided to access the CAMEL project's contributing guidelines.\n",
- "Input: https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\n",
- "\n",
- "\n",
- "\u001b[32mAI Assistant:\n",
- "\n",
- "Function Execution: information_retrieval\n",
- "\tArgs: {'query': 'contributing guidelines for CAMEL project', 'contents': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines'}\n",
- "\tResult: {'Original Query': 'contributing guidelines for CAMEL project', 'Retrieved Context': ['No suitable information retrieved from https://github.com/camel-ai/camel/wiki/Contributing-Guidlines with similarity_threshold = 0.7.']}\n",
- "\n",
- "Solution: It appears that no suitable information was retrieved from the provided URL with the current similarity threshold. I will adjust the similarity threshold to a lower value to attempt to retrieve the relevant information.\n",
- "\n",
- "Next request.\n",
- "\n",
- "\n",
- "\u001b[34mAI User:\n",
- "\n",
- "Instruction: Adjust the similarity threshold to a lower value and attempt to retrieve the relevant information from the provided URL again.\n",
- "Input: https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\n",
- "\n",
- "\n",
- "\u001b[32mAI Assistant:\n",
- "\n",
- "Function Execution: information_retrieval\n",
- "\tArgs: {'query': 'contributing guidelines for CAMEL project', 'contents': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines', 'similarity_threshold': 0.5}\n",
- "\tResult: {'Original Query': 'contributing guidelines for CAMEL project', 'Retrieved Context': [\"Thank you for your interest in contributing to the CAMEL project! 🎉 We're excited to have your support. As an open-source initiative in a rapidly evolving and open-ended field, we wholeheartedly welcome contributions of all kinds. Whether you want to introduce new features, enhance the infrastructure, improve documentation, asking issues, add more examples, implement state-of-the-art research ideas, or fix bugs, we appreciate your enthusiasm and efforts. 🙌 You are welcome to join our slack for \"]}\n",
- "\n",
- "Solution: Here is the relevant information retrieved from the provided URL:\n",
- "\n",
- "\"Thank you for your interest in contributing to the CAMEL project! 🎉 We're excited to have your support. As an open-source initiative in a rapidly evolving and open-ended field, we wholeheartedly welcome contributions of all kinds. Whether you want to introduce new features, enhance the infrastructure, improve documentation, ask issues, add more examples, implement state-of-the-art research ideas, or fix bugs, we appreciate your enthusiasm and efforts. 🙌 You are welcome to join our Slack for...\"\n",
- "\n",
- "This indicates that the CAMEL project is open to various types of contributions, including new features, infrastructure enhancements, documentation improvements, issue reporting, adding examples, implementing research ideas, and bug fixes. Additionally, it suggests joining their Slack for further engagement.\n",
- "\n",
- "Next request.\n",
- "\n",
- "\n",
- "\u001b[34mAI User:\n",
- "\n",
- "Instruction: Look for specific instructions or steps provided in the contributing guidelines for setting up the development environment or resolving setup challenges.\n",
- "Input: https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\n",
- "\n",
- "\n",
- "\u001b[32mAI Assistant:\n",
- "\n",
- "Function Execution: information_retrieval\n",
- "\tArgs: {'query': 'setup development environment or resolving setup challenges for CAMEL project', 'contents': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines', 'similarity_threshold': 0.5}\n",
- "\tResult: {'Original Query': 'setup development environment or resolving setup challenges for CAMEL project', 'Retrieved Context': ['Quick Start 🚀\\n\\nTo get started with CAMEL, follow these steps:\\n\\n# Clone github repo\\ngit clone https://github.com/camel-ai/camel.git\\n\\n# Change directory into project directory\\n\\ncd camel\\n\\n# Activate camel virtual environment\\npoetry shell\\n\\n# Install camel from source\\n\\n# It takes about 75s to resolve dependencies and 10s to install them, depending on your hardware and network,\\npoetry install --with dev,docs\\n\\n# Or if you want to use all other extra packages\\npoetry install --with dev,docs -E all']}\n",
- "\n",
- "Solution: Here are the specific instructions for setting up the development environment for the CAMEL project:\n",
- "\n",
- "### Quick Start 🚀\n",
- "\n",
- "To get started with CAMEL, follow these steps:\n",
- "\n",
- "1. **Clone the GitHub repository:**\n",
- " ```sh\n",
- " git clone https://github.com/camel-ai/camel.git\n",
- " ```\n",
- "\n",
- "2. **Change directory into the project directory:**\n",
- " ```sh\n",
- " cd camel\n",
- " ```\n",
- "\n",
- "3. **Activate the CAMEL virtual environment:**\n",
- " ```sh\n",
- " poetry shell\n",
- " ```\n",
- "\n",
- "4. **Install CAMEL from source:**\n",
- " ```sh\n",
- " poetry install --with dev,docs\n",
- " ```\n",
- " - It takes about 75 seconds to resolve dependencies and 10 seconds to install them, depending on your hardware and network.\n",
- "\n",
- "5. **Alternatively, if you want to use all other extra packages:**\n",
- " ```sh\n",
- " poetry install --with dev,docs -E all\n",
- " ```\n",
- "\n",
- "These steps should help you set up the development environment for the CAMEL project. If you encounter any challenges during the setup process, these instructions should guide you through the initial setup.\n",
- "\n",
- "Next request.\n",
- "\n",
- "\n",
- "\u001b[34mAI User:\n",
- "\n",
- "Instruction: Follow the provided steps to set up the development environment for the CAMEL project. If you encounter any specific challenges during the setup process, document them.\n",
- "Input: None\n",
- "\n",
- "\n",
- "\u001b[32mAI Assistant:\n",
- "\n",
- "Solution: As an AI, I am unable to physically perform tasks such as setting up a development environment. However, I can guide you through the process and help you troubleshoot any issues you might encounter.\n",
- "\n",
- "Here are the steps you should follow to set up the development environment for the CAMEL project:\n",
- "\n",
- "1. **Clone the GitHub repository:**\n",
- " ```sh\n",
- " git clone https://github.com/camel-ai/camel.git\n",
- " ```\n",
- "\n",
- "2. **Change directory into the project directory:**\n",
- " ```sh\n",
- " cd camel\n",
- " ```\n",
- "\n",
- "3. **Activate the CAMEL virtual environment:**\n",
- " ```sh\n",
- " poetry shell\n",
- " ```\n",
- "\n",
- "4. **Install CAMEL from source:**\n",
- " ```sh\n",
- " poetry install --with dev,docs\n",
- " ```\n",
- " - It takes about 75 seconds to resolve dependencies and 10 seconds to install them, depending on your hardware and network.\n",
- "\n",
- "5. **Alternatively, if you want to use all other extra packages:**\n",
- " ```sh\n",
- " poetry install --with dev,docs -E all\n",
- " ```\n",
- "\n",
- "### Troubleshooting Common Issues\n",
- "\n",
- "- **Issue: `poetry: command not found`**\n",
- " - **Solution:** Ensure that Poetry is installed on your system. You can install it by following the instructions on the [Poetry website](https://python-poetry.org/docs/#installation).\n",
- "\n",
- "- **Issue: Network-related errors during `poetry install`**\n",
- " - **Solution:** Check your internet connection and try again. If the issue persists, you might want to use a different network or check if there are any firewall restrictions.\n",
- "\n",
- "- **Issue: Dependency resolution taking too long**\n",
- " - **Solution:** Ensure that your system has sufficient resources (CPU, memory). You can also try running the command during off-peak hours when network traffic is lower.\n",
- "\n",
- "- **Issue: Virtual environment activation issues**\n",
- " - **Solution:** Ensure that you have the necessary permissions to create and activate virtual environments. You might need to run the command with elevated privileges (e.g., using `sudo` on Unix-based systems).\n",
- "\n",
- "If you encounter any specific challenges during the setup process, please document them, and I can help you troubleshoot further.\n",
- "\n",
- "Next request.\n",
- "\n",
- "\n",
- "\u001b[34mAI User:\n",
- "\n",
- "Instruction: Attempt to set up the development environment for the CAMEL project using the provided steps. Document any specific challenges you encounter during the process.\n",
- "Input: None\n",
- "\n",
- "\n",
- "\u001b[32mAI Assistant:\n",
- "\n",
- "Solution: As an AI, I am unable to physically perform tasks such as setting up a development environment. However, I can provide a detailed guide and troubleshooting tips to help you through the process. Here’s a step-by-step guide along with potential challenges and their solutions:\n",
- "\n",
- "### Step-by-Step Guide to Set Up the Development Environment\n",
- "\n",
- "1. **Clone the GitHub repository:**\n",
- " ```sh\n",
- " git clone https://github.com/camel-ai/camel.git\n",
- " ```\n",
- " - **Potential Challenge:** If you encounter an error like `git: command not found`, ensure that Git is installed on your system. You can download and install Git from [here](https://git-scm.com/downloads).\n",
- "\n",
- "2. **Change directory into the project directory:**\n",
- " ```sh\n",
- " cd camel\n",
- " ```\n",
- " - **Potential Challenge:** If you get an error like `No such file or directory`, ensure that the repository was cloned successfully and that you are in the correct directory.\n",
- "\n",
- "3. **Activate the CAMEL virtual environment:**\n",
- " ```sh\n",
- " poetry shell\n",
- " ```\n",
- " - **Potential Challenge:** If you encounter an error like `poetry: command not found`, ensure that Poetry is installed on your system. You can install it by following the instructions on the [Poetry website](https://python-poetry.org/docs/#installation).\n",
- "\n",
- "4. **Install CAMEL from source:**\n",
- " ```sh\n",
- " poetry install --with dev,docs\n",
- " ```\n",
- " - **Potential Challenge:** If you encounter network-related errors during `poetry install`, check your internet connection and try again. If the issue persists, you might want to use a different network or check if there are any firewall restrictions.\n",
- "\n",
- "5. **Alternatively, if you want to use all other extra packages:**\n",
- " ```sh\n",
- " poetry install --with dev,docs -E all\n",
- " ```\n",
- " - **Potential Challenge:** If dependency resolution takes too long, ensure that your system has sufficient resources (CPU, memory). You can also try running the command during off-peak hours when network traffic is lower.\n",
- "\n",
- "### Documenting Specific Challenges\n",
- "\n",
- "If you encounter any specific challenges during the setup process, document them as follows:\n",
- "\n",
- "1. **Describe the Issue:**\n",
- " - Example: \"Encountered an error `poetry: command not found` when trying to activate the virtual environment.\"\n",
- "\n",
- "2. **Steps Taken to Resolve:**\n",
- " - Example: \"Checked if Poetry was installed by running `poetry --version`. Found that Poetry was not installed. Installed Poetry using the command `curl -sSL https://install.python-poetry.org | python3 -`.\"\n",
- "\n",
- "3. **Outcome:**\n",
- " - Example: \"After installing Poetry, I was able to activate the virtual environment successfully.\"\n",
- "\n",
- "By following these steps and documenting any challenges, you should be able to set up the development environment for the CAMEL project. If you encounter any issues that you cannot resolve, feel free to ask for further assistance.\n",
- "\n",
- "Next request.\n",
- "\n",
- "\n"
- ]
- }
- ],
- "source": [
- "role_playing_with_rag(task_prompt = \"\"\"If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\"\"\")"
- ]
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "If you're interested in contributing to the CAMEL project, you can introduce new features, enhance the infrastructure, improve documentation, address issues, add more examples, implement state-of-the-art research ideas, or fix bugs. Additionally, you are welcome to join their Slack for further engagement and collaboration.\n"
+ ]
}
- ],
- "metadata": {
+ ],
+ "source": [
+ "from camel.agents import ChatAgent\n",
+ "from camel.messages import BaseMessage\n",
+ "from camel.types import RoleType\n",
+ "from camel.retrievers import AutoRetriever\n",
+ "from camel.types import StorageType\n",
+ "\n",
+ "def single_agent(query: str) ->str :\n",
+ " # Set agent role\n",
+ " assistant_sys_msg = BaseMessage(\n",
+ " role_name=\"Assistant\",\n",
+ " role_type=RoleType.ASSISTANT,\n",
+ " meta_dict=None,\n",
+ " content=\"\"\"You are a helpful assistant to answer question,\n",
+ " I will give you the Original Query and Retrieved Context,\n",
+ " answer the Original Query based on the Retrieved Context,\n",
+ " if you can't answer the question just say I don't know.\"\"\",\n",
+ " )\n",
+ "\n",
+ " # Add auto retriever\n",
+ " auto_retriever = AutoRetriever(\n",
+ " vector_storage_local_path=\"local_data2/\",\n",
+ " storage_type=StorageType.QDRANT,\n",
+ " embedding_model=embedding_instance)\n",
+ "\n",
+ " retrieved_info = auto_retriever.run_vector_retriever(\n",
+ " query=query,\n",
+ " contents=[\n",
+ " \"local_data/camel_paper.pdf\", # example local path\n",
+ " \"https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\", # example remote url\n",
+ " ],\n",
+ " top_k=1,\n",
+ " return_detailed_info=False,\n",
+ " similarity_threshold=0.5\n",
+ " )\n",
+ "\n",
+ " # Pass the retrieved infomation to agent\n",
+ " user_msg = BaseMessage.make_user_message(role_name=\"User\", content=str(retrieved_info))\n",
+ " agent = ChatAgent(assistant_sys_msg)\n",
+ "\n",
+ " # Get response\n",
+ " assistant_response = agent.step(user_msg)\n",
+ " return assistant_response.msg.content\n",
+ "\n",
+ "print(single_agent(\"If I'm interest in contributing to the CAMEL projec, what should I do?\"))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "Cvh6e3KpI0rf"
+ },
+ "source": [
+ "## 4. Role-playing with Auto RAG\n",
+ "In this section we will show how to combine the `RETRIEVAL_FUNCS` with `RolePlaying` by applying `Function Calling`.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {
+ "id": "z3tX4EJVI1hb"
+ },
+ "outputs": [],
+ "source": [
+ "from typing import List\n",
+ "from colorama import Fore\n",
+ "\n",
+ "from camel.agents.chat_agent import FunctionCallingRecord\n",
+ "from camel.configs import ChatGPTConfig\n",
+ "from camel.toolkits import (\n",
+ " MathToolkit,\n",
+ " RetrievalToolkit,\n",
+ ")\n",
+ "from camel.societies import RolePlaying\n",
+ "from camel.types import ModelType, ModelPlatformType\n",
+ "from camel.utils import print_text_animated\n",
+ "from camel.models import ModelFactory\n",
+ "\n",
+ "def role_playing_with_rag(\n",
+ " task_prompt,\n",
+ " model_platform=ModelPlatformType.OPENAI,\n",
+ " model_type=ModelType.GPT_4O,\n",
+ " chat_turn_limit=5,\n",
+ ") -> None:\n",
+ " task_prompt = task_prompt\n",
+ "\n",
+ " user_model_config = ChatGPTConfig(temperature=0.0)\n",
+ "\n",
+ " tools_list = [\n",
+ " *MathToolkit().get_tools(),\n",
+ " *RetrievalToolkit().get_tools(),\n",
+ " ]\n",
+ " assistant_model_config = ChatGPTConfig(\n",
+ " tools=tools_list,\n",
+ " temperature=0.0,\n",
+ " )\n",
+ "\n",
+ " role_play_session = RolePlaying(\n",
+ " assistant_role_name=\"Searcher\",\n",
+ " user_role_name=\"Professor\",\n",
+ " assistant_agent_kwargs=dict(\n",
+ " model=ModelFactory.create(\n",
+ " model_platform=model_platform,\n",
+ " model_type=model_type,\n",
+ " model_config_dict=assistant_model_config.as_dict(),\n",
+ " ),\n",
+ " tools=tools_list,\n",
+ " ),\n",
+ " user_agent_kwargs=dict(\n",
+ " model=ModelFactory.create(\n",
+ " model_platform=model_platform,\n",
+ " model_type=model_type,\n",
+ " model_config_dict=user_model_config.as_dict(),\n",
+ " ),\n",
+ " ),\n",
+ " task_prompt=task_prompt,\n",
+ " with_task_specify=False,\n",
+ " )\n",
+ "\n",
+ " print(\n",
+ " Fore.GREEN\n",
+ " + f\"AI Assistant sys message:\\n{role_play_session.assistant_sys_msg}\\n\"\n",
+ " )\n",
+ " print(\n",
+ " Fore.BLUE + f\"AI User sys message:\\n{role_play_session.user_sys_msg}\\n\"\n",
+ " )\n",
+ "\n",
+ " print(Fore.YELLOW + f\"Original task prompt:\\n{task_prompt}\\n\")\n",
+ " print(\n",
+ " Fore.CYAN\n",
+ " + \"Specified task prompt:\"\n",
+ " + f\"\\n{role_play_session.specified_task_prompt}\\n\"\n",
+ " )\n",
+ " print(Fore.RED + f\"Final task prompt:\\n{role_play_session.task_prompt}\\n\")\n",
+ "\n",
+ " n = 0\n",
+ " input_msg = role_play_session.init_chat()\n",
+ " while n < chat_turn_limit:\n",
+ " n += 1\n",
+ " assistant_response, user_response = role_play_session.step(input_msg)\n",
+ "\n",
+ " if assistant_response.terminated:\n",
+ " print(\n",
+ " Fore.GREEN\n",
+ " + (\n",
+ " \"AI Assistant terminated. Reason: \"\n",
+ " f\"{assistant_response.info['termination_reasons']}.\"\n",
+ " )\n",
+ " )\n",
+ " break\n",
+ " if user_response.terminated:\n",
+ " print(\n",
+ " Fore.GREEN\n",
+ " + (\n",
+ " \"AI User terminated. \"\n",
+ " f\"Reason: {user_response.info['termination_reasons']}.\"\n",
+ " )\n",
+ " )\n",
+ " break\n",
+ "\n",
+ " # Print output from the user\n",
+ " print_text_animated(\n",
+ " Fore.BLUE + f\"AI User:\\n\\n{user_response.msg.content}\\n\"\n",
+ " )\n",
+ "\n",
+ " # Print output from the assistant, including any function\n",
+ " # execution information\n",
+ " print_text_animated(Fore.GREEN + \"AI Assistant:\")\n",
+ " tool_calls: List[FunctionCallingRecord] = [\n",
+ " FunctionCallingRecord(**call.as_dict())\n",
+ " for call in assistant_response.info['tool_calls']\n",
+ " ]\n",
+ " for func_record in tool_calls:\n",
+ " print_text_animated(f\"{func_record}\")\n",
+ " print_text_animated(f\"{assistant_response.msg.content}\\n\")\n",
+ "\n",
+ " if \"CAMEL_TASK_DONE\" in user_response.msg.content:\n",
+ " break\n",
+ "\n",
+ " input_msg = assistant_response.msg"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "w4-0lswwI5Gb"
+ },
+ "source": [
+ "Run the role-playing with defined retriever function:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
"colab": {
- "provenance": []
- },
- "kernelspec": {
- "display_name": "Python 3",
- "name": "python3"
+ "base_uri": "https://localhost:8080/"
},
- "language_info": {
- "name": "python"
+ "id": "5agjiXHeI64q",
+ "outputId": "8f6c53cb-411e-440f-95bb-7a45f07c6d96"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001B[32mAI Assistant sys message:\n",
+ "BaseMessage(role_name='Searcher', role_type=<RoleType.ASSISTANT: 'assistant'>, meta_dict={'task': \"If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\", 'assistant_role': 'Searcher', 'user_role': 'Professor'}, content=\"===== RULES OF ASSISTANT =====\\nNever forget you are a Searcher and I am a Professor. Never flip roles! Never instruct me!\\nWe share a common interest in collaborating to successfully complete a task.\\nYou must help me to complete the task.\\nHere is the task: If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary. Never forget our task!\\nI must instruct you based on your expertise and my needs to complete the task.\\n\\nI must give you one instruction at a time.\\nYou must write a specific solution that appropriately solves the requested instruction and explain your solutions.\\nYou must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.\\nUnless I say the task is completed, you should always start with:\\n\\nSolution: <YOUR_SOLUTION>\\n\\n<YOUR_SOLUTION> should be very specific, include detailed explanations and provide preferable detailed implementations and examples and lists for task-solving.\\nAlways end <YOUR_SOLUTION> with: Next request.\", video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
+ "\n",
+ "\u001B[34mAI User sys message:\n",
+ "BaseMessage(role_name='Professor', role_type=<RoleType.USER: 'user'>, meta_dict={'task': \"If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\", 'assistant_role': 'Searcher', 'user_role': 'Professor'}, content='===== RULES OF USER =====\\nNever forget you are a Professor and I am a Searcher. Never flip roles! You will always instruct me.\\nWe share a common interest in collaborating to successfully complete a task.\\nI must help you to complete the task.\\nHere is the task: If I\\'m interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don\\'t generate the answer by yourself, adjust the similarity threshold to lower value is necessary. Never forget our task!\\nYou must instruct me based on my expertise and your needs to solve the task ONLY in the following two ways:\\n\\n1. Instruct with a necessary input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: <YOUR_INPUT>\\n\\n2. Instruct without any input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: None\\n\\nThe \"Instruction\" describes a task or question. The paired \"Input\" provides further context or information for the requested \"Instruction\".\\n\\nYou must give me one instruction at a time.\\nI must write a response that appropriately solves the requested instruction.\\nI must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.\\nYou should instruct me not ask me questions.\\nNow you must start to instruct me using the two ways described above.\\nDo not add anything else other than your instruction and the optional corresponding input!\\nKeep giving me instructions and necessary inputs until you think the task is completed.\\nWhen the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.\\nNever say <CAMEL_TASK_DONE> unless my responses have solved your task.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
+ "\n",
+ "\u001B[33mOriginal task prompt:\n",
+ "If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\n",
+ "\n",
+ "\u001B[36mSpecified task prompt:\n",
+ "None\n",
+ "\n",
+ "\u001B[31mFinal task prompt:\n",
+ "If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\n",
+ "\n",
+ "\u001B[34mAI User:\n",
+ "\n",
+ "Instruction: Visit the URL provided to access the CAMEL project's contributing guidelines.\n",
+ "Input: https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\n",
+ "\n",
+ "\n",
+ "\u001B[32mAI Assistant:\n",
+ "\n",
+ "Function Execution: information_retrieval\n",
+ "\tArgs: {'query': 'contributing guidelines for CAMEL project', 'contents': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines'}\n",
+ "\tResult: {'Original Query': 'contributing guidelines for CAMEL project', 'Retrieved Context': ['No suitable information retrieved from https://github.com/camel-ai/camel/wiki/Contributing-Guidlines with similarity_threshold = 0.7.']}\n",
+ "\n",
+ "Solution: It appears that no suitable information was retrieved from the provided URL with the current similarity threshold. I will adjust the similarity threshold to a lower value to attempt to retrieve the relevant information.\n",
+ "\n",
+ "Next request.\n",
+ "\n",
+ "\n",
+ "\u001B[34mAI User:\n",
+ "\n",
+ "Instruction: Adjust the similarity threshold to a lower value and attempt to retrieve the relevant information from the provided URL again.\n",
+ "Input: https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\n",
+ "\n",
+ "\n",
+ "\u001B[32mAI Assistant:\n",
+ "\n",
+ "Function Execution: information_retrieval\n",
+ "\tArgs: {'query': 'contributing guidelines for CAMEL project', 'contents': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines', 'similarity_threshold': 0.5}\n",
+ "\tResult: {'Original Query': 'contributing guidelines for CAMEL project', 'Retrieved Context': [\"Thank you for your interest in contributing to the CAMEL project! 🎉 We're excited to have your support. As an open-source initiative in a rapidly evolving and open-ended field, we wholeheartedly welcome contributions of all kinds. Whether you want to introduce new features, enhance the infrastructure, improve documentation, asking issues, add more examples, implement state-of-the-art research ideas, or fix bugs, we appreciate your enthusiasm and efforts. 🙌 You are welcome to join our slack for \"]}\n",
+ "\n",
+ "Solution: Here is the relevant information retrieved from the provided URL:\n",
+ "\n",
+ "\"Thank you for your interest in contributing to the CAMEL project! 🎉 We're excited to have your support. As an open-source initiative in a rapidly evolving and open-ended field, we wholeheartedly welcome contributions of all kinds. Whether you want to introduce new features, enhance the infrastructure, improve documentation, ask issues, add more examples, implement state-of-the-art research ideas, or fix bugs, we appreciate your enthusiasm and efforts. 🙌 You are welcome to join our Slack for...\"\n",
+ "\n",
+ "This indicates that the CAMEL project is open to various types of contributions, including new features, infrastructure enhancements, documentation improvements, issue reporting, adding examples, implementing research ideas, and bug fixes. Additionally, it suggests joining their Slack for further engagement.\n",
+ "\n",
+ "Next request.\n",
+ "\n",
+ "\n",
+ "\u001B[34mAI User:\n",
+ "\n",
+ "Instruction: Look for specific instructions or steps provided in the contributing guidelines for setting up the development environment or resolving setup challenges.\n",
+ "Input: https://github.com/camel-ai/camel/wiki/Contributing-Guidlines\n",
+ "\n",
+ "\n",
+ "\u001B[32mAI Assistant:\n",
+ "\n",
+ "Function Execution: information_retrieval\n",
+ "\tArgs: {'query': 'setup development environment or resolving setup challenges for CAMEL project', 'contents': 'https://github.com/camel-ai/camel/wiki/Contributing-Guidlines', 'similarity_threshold': 0.5}\n",
+ "\tResult: {'Original Query': 'setup development environment or resolving setup challenges for CAMEL project', 'Retrieved Context': ['Quick Start 🚀\\n\\nTo get started with CAMEL, follow these steps:\\n\\n# Clone github repo\\ngit clone https://github.com/camel-ai/camel.git\\n\\n# Change directory into project directory\\n\\ncd camel\\n\\n# Activate camel virtual environment\\npoetry shell\\n\\n# Install camel from source\\n\\n# It takes about 75s to resolve dependencies and 10s to install them, depending on your hardware and network,\\npoetry install --with dev,docs\\n\\n# Or if you want to use all other extra packages\\npoetry install --with dev,docs -E all']}\n",
+ "\n",
+ "Solution: Here are the specific instructions for setting up the development environment for the CAMEL project:\n",
+ "\n",
+ "### Quick Start 🚀\n",
+ "\n",
+ "To get started with CAMEL, follow these steps:\n",
+ "\n",
+ "1. **Clone the GitHub repository:**\n",
+ " ```sh\n",
+ " git clone https://github.com/camel-ai/camel.git\n",
+ " ```\n",
+ "\n",
+ "2. **Change directory into the project directory:**\n",
+ " ```sh\n",
+ " cd camel\n",
+ " ```\n",
+ "\n",
+ "3. **Activate the CAMEL virtual environment:**\n",
+ " ```sh\n",
+ " poetry shell\n",
+ " ```\n",
+ "\n",
+ "4. **Install CAMEL from source:**\n",
+ " ```sh\n",
+ " poetry install --with dev,docs\n",
+ " ```\n",
+ " - It takes about 75 seconds to resolve dependencies and 10 seconds to install them, depending on your hardware and network.\n",
+ "\n",
+ "5. **Alternatively, if you want to use all other extra packages:**\n",
+ " ```sh\n",
+ " poetry install --with dev,docs -E all\n",
+ " ```\n",
+ "\n",
+ "These steps should help you set up the development environment for the CAMEL project. If you encounter any challenges during the setup process, these instructions should guide you through the initial setup.\n",
+ "\n",
+ "Next request.\n",
+ "\n",
+ "\n",
+ "\u001B[34mAI User:\n",
+ "\n",
+ "Instruction: Follow the provided steps to set up the development environment for the CAMEL project. If you encounter any specific challenges during the setup process, document them.\n",
+ "Input: None\n",
+ "\n",
+ "\n",
+ "\u001B[32mAI Assistant:\n",
+ "\n",
+ "Solution: As an AI, I am unable to physically perform tasks such as setting up a development environment. However, I can guide you through the process and help you troubleshoot any issues you might encounter.\n",
+ "\n",
+ "Here are the steps you should follow to set up the development environment for the CAMEL project:\n",
+ "\n",
+ "1. **Clone the GitHub repository:**\n",
+ " ```sh\n",
+ " git clone https://github.com/camel-ai/camel.git\n",
+ " ```\n",
+ "\n",
+ "2. **Change directory into the project directory:**\n",
+ " ```sh\n",
+ " cd camel\n",
+ " ```\n",
+ "\n",
+ "3. **Activate the CAMEL virtual environment:**\n",
+ " ```sh\n",
+ " poetry shell\n",
+ " ```\n",
+ "\n",
+ "4. **Install CAMEL from source:**\n",
+ " ```sh\n",
+ " poetry install --with dev,docs\n",
+ " ```\n",
+ " - It takes about 75 seconds to resolve dependencies and 10 seconds to install them, depending on your hardware and network.\n",
+ "\n",
+ "5. **Alternatively, if you want to use all other extra packages:**\n",
+ " ```sh\n",
+ " poetry install --with dev,docs -E all\n",
+ " ```\n",
+ "\n",
+ "### Troubleshooting Common Issues\n",
+ "\n",
+ "- **Issue: `poetry: command not found`**\n",
+ " - **Solution:** Ensure that Poetry is installed on your system. You can install it by following the instructions on the [Poetry website](https://python-poetry.org/docs/#installation).\n",
+ "\n",
+ "- **Issue: Network-related errors during `poetry install`**\n",
+ " - **Solution:** Check your internet connection and try again. If the issue persists, you might want to use a different network or check if there are any firewall restrictions.\n",
+ "\n",
+ "- **Issue: Dependency resolution taking too long**\n",
+ " - **Solution:** Ensure that your system has sufficient resources (CPU, memory). You can also try running the command during off-peak hours when network traffic is lower.\n",
+ "\n",
+ "- **Issue: Virtual environment activation issues**\n",
+ " - **Solution:** Ensure that you have the necessary permissions to create and activate virtual environments. You might need to run the command with elevated privileges (e.g., using `sudo` on Unix-based systems).\n",
+ "\n",
+ "If you encounter any specific challenges during the setup process, please document them, and I can help you troubleshoot further.\n",
+ "\n",
+ "Next request.\n",
+ "\n",
+ "\n",
+ "\u001B[34mAI User:\n",
+ "\n",
+ "Instruction: Attempt to set up the development environment for the CAMEL project using the provided steps. Document any specific challenges you encounter during the process.\n",
+ "Input: None\n",
+ "\n",
+ "\n",
+ "\u001B[32mAI Assistant:\n",
+ "\n",
+ "Solution: As an AI, I am unable to physically perform tasks such as setting up a development environment. However, I can provide a detailed guide and troubleshooting tips to help you through the process. Here’s a step-by-step guide along with potential challenges and their solutions:\n",
+ "\n",
+ "### Step-by-Step Guide to Set Up the Development Environment\n",
+ "\n",
+ "1. **Clone the GitHub repository:**\n",
+ " ```sh\n",
+ " git clone https://github.com/camel-ai/camel.git\n",
+ " ```\n",
+ " - **Potential Challenge:** If you encounter an error like `git: command not found`, ensure that Git is installed on your system. You can download and install Git from [here](https://git-scm.com/downloads).\n",
+ "\n",
+ "2. **Change directory into the project directory:**\n",
+ " ```sh\n",
+ " cd camel\n",
+ " ```\n",
+ " - **Potential Challenge:** If you get an error like `No such file or directory`, ensure that the repository was cloned successfully and that you are in the correct directory.\n",
+ "\n",
+ "3. **Activate the CAMEL virtual environment:**\n",
+ " ```sh\n",
+ " poetry shell\n",
+ " ```\n",
+ " - **Potential Challenge:** If you encounter an error like `poetry: command not found`, ensure that Poetry is installed on your system. You can install it by following the instructions on the [Poetry website](https://python-poetry.org/docs/#installation).\n",
+ "\n",
+ "4. **Install CAMEL from source:**\n",
+ " ```sh\n",
+ " poetry install --with dev,docs\n",
+ " ```\n",
+ " - **Potential Challenge:** If you encounter network-related errors during `poetry install`, check your internet connection and try again. If the issue persists, you might want to use a different network or check if there are any firewall restrictions.\n",
+ "\n",
+ "5. **Alternatively, if you want to use all other extra packages:**\n",
+ " ```sh\n",
+ " poetry install --with dev,docs -E all\n",
+ " ```\n",
+ " - **Potential Challenge:** If dependency resolution takes too long, ensure that your system has sufficient resources (CPU, memory). You can also try running the command during off-peak hours when network traffic is lower.\n",
+ "\n",
+ "### Documenting Specific Challenges\n",
+ "\n",
+ "If you encounter any specific challenges during the setup process, document them as follows:\n",
+ "\n",
+ "1. **Describe the Issue:**\n",
+ " - Example: \"Encountered an error `poetry: command not found` when trying to activate the virtual environment.\"\n",
+ "\n",
+ "2. **Steps Taken to Resolve:**\n",
+ " - Example: \"Checked if Poetry was installed by running `poetry --version`. Found that Poetry was not installed. Installed Poetry using the command `curl -sSL https://install.python-poetry.org | python3 -`.\"\n",
+ "\n",
+ "3. **Outcome:**\n",
+ " - Example: \"After installing Poetry, I was able to activate the virtual environment successfully.\"\n",
+ "\n",
+ "By following these steps and documenting any challenges, you should be able to set up the development environment for the CAMEL project. If you encounter any issues that you cannot resolve, feel free to ask for further assistance.\n",
+ "\n",
+ "Next request.\n",
+ "\n",
+ "\n"
+ ]
}
+ ],
+ "source": [
+ "role_playing_with_rag(task_prompt = \"\"\"If I'm interest in contributing to the CAMEL projec and I encounter some challenges during the setup process, what should I do? You should refer to the content in url https://github.com/camel-ai/camel/wiki/Contributing-Guidlines to answer my question, don't generate the answer by yourself, adjust the similarity threshold to lower value is necessary\"\"\")"
+ ]
+ }
+ ],
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "display_name": "Python 3",
+ "name": "python3"
},
- "nbformat": 4,
- "nbformat_minor": 0
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
}
diff --git a/docs/cookbooks/agents_with_tools.ipynb b/docs/cookbooks/agents_with_tools.ipynb
index 0bdc74eed..6df7a14c6 100644
--- a/docs/cookbooks/agents_with_tools.ipynb
+++ b/docs/cookbooks/agents_with_tools.ipynb
@@ -408,28 +408,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[32mAI Assistant sys message:\n",
+ "\u001B[32mAI Assistant sys message:\n",
"BaseMessage(role_name='Searcher', role_type=<RoleType.ASSISTANT: 'assistant'>, meta_dict={'task': 'Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age.', 'assistant_role': 'Searcher', 'user_role': 'Professor'}, content='===== RULES OF ASSISTANT =====\\nNever forget you are a Searcher and I am a Professor. Never flip roles! Never instruct me!\\nWe share a common interest in collaborating to successfully complete a task.\\nYou must help me to complete the task.\\nHere is the task: Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age.. Never forget our task!\\nI must instruct you based on your expertise and my needs to complete the task.\\n\\nI must give you one instruction at a time.\\nYou must write a specific solution that appropriately solves the requested instruction and explain your solutions.\\nYou must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.\\nUnless I say the task is completed, you should always start with:\\n\\nSolution: <YOUR_SOLUTION>\\n\\n<YOUR_SOLUTION> should be very specific, include detailed explanations and provide preferable detailed implementations and examples and lists for task-solving.\\nAlways end <YOUR_SOLUTION> with: Next request.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
"\n",
- "\u001b[34mAI User sys message:\n",
+ "\u001B[34mAI User sys message:\n",
"BaseMessage(role_name='Professor', role_type=<RoleType.USER: 'user'>, meta_dict={'task': 'Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age.', 'assistant_role': 'Searcher', 'user_role': 'Professor'}, content='===== RULES OF USER =====\\nNever forget you are a Professor and I am a Searcher. Never flip roles! You will always instruct me.\\nWe share a common interest in collaborating to successfully complete a task.\\nI must help you to complete the task.\\nHere is the task: Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age.. Never forget our task!\\nYou must instruct me based on my expertise and your needs to solve the task ONLY in the following two ways:\\n\\n1. Instruct with a necessary input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: <YOUR_INPUT>\\n\\n2. Instruct without any input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: None\\n\\nThe \"Instruction\" describes a task or question. The paired \"Input\" provides further context or information for the requested \"Instruction\".\\n\\nYou must give me one instruction at a time.\\nI must write a response that appropriately solves the requested instruction.\\nI must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.\\nYou should instruct me not ask me questions.\\nNow you must start to instruct me using the two ways described above.\\nDo not add anything else other than your instruction and the optional corresponding input!\\nKeep giving me instructions and necessary inputs until you think the task is completed.\\nWhen the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.\\nNever say <CAMEL_TASK_DONE> unless my responses have solved your task.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
"\n",
- "\u001b[33mOriginal task prompt:\n",
+ "\u001B[33mOriginal task prompt:\n",
"Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age.\n",
"\n",
- "\u001b[36mSpecified task prompt:\n",
+ "\u001B[36mSpecified task prompt:\n",
"None\n",
"\n",
- "\u001b[31mFinal task prompt:\n",
+ "\u001B[31mFinal task prompt:\n",
"Assume now is 2024 in the Gregorian calendar, estimate the current age of University of Oxford and then add 10 more years to this age.\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Estimate the founding year of the University of Oxford. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: search_wiki\n",
"\tArgs: {'entity': 'University of Oxford'}\n",
@@ -442,13 +442,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Calculate the current age of the University of Oxford in 2024 based on the founding year of 1096. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: sub\n",
"\tArgs: {'a': 2024, 'b': 1096}\n",
@@ -466,13 +466,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Add 10 years to the current age of the University of Oxford (928 years). \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: add\n",
"\tArgs: {'a': 928, 'b': 10}\n",
@@ -488,26 +488,26 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Summarize the final estimated age of the University of Oxford after adding 10 years. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: The final estimated age of the University of Oxford, after adding 10 years to its current age of 928 years, is 938 years. This means that in the year 2034, the University of Oxford will be 938 years old.\n",
"\n",
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Confirm that the task is completed based on the provided solutions. \n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: The task is confirmed as completed. We have successfully estimated the current age of the University of Oxford as 928 years in 2024 and added 10 years to arrive at a final estimated age of 938 years in 2034. \n",
"\n",
@@ -516,12 +516,12 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"<CAMEL_TASK_DONE>\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: The task is marked as done. If you have any other tasks or inquiries in the future, feel free to reach out. \n",
"\n",
diff --git a/docs/cookbooks/agents_with_tools_from_Composio.ipynb b/docs/cookbooks/agents_with_tools_from_Composio.ipynb
index 9511c641d..39049680b 100644
--- a/docs/cookbooks/agents_with_tools_from_Composio.ipynb
+++ b/docs/cookbooks/agents_with_tools_from_Composio.ipynb
@@ -97,16 +97,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[33mWarning: An existing connection for github was found.\u001b[0m\n",
+ "\u001B[33mWarning: An existing connection for github was found.\u001B[0m\n",
"\n",
"> Do you want to replace the existing connection? (y, n): y\n",
"\n",
- "\u001b[32m> Adding integration: Github\u001b[0m\u001b[32m...\u001b[0m\n",
+ "\u001B[32m> Adding integration: Github\u001B[0m\u001B[32m...\u001B[0m\n",
"\n",
"Please authenticate github in the browser and come back here. URL: https://github.com/login/oauth/authorize?client_id=37beeaa7020925b04e40&redirect_uri=https%3A%2F%2Fbackend.composio.dev%2Fapi%2Fv1%2Fauth-apps%2Fadd&scope=repo%2Cadmin%3Aorg%2Cgist%2Cuser&state=production_eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb25uZWN0aW9uSWQiOiIwMWIyMTlmOC1kZTk5LTQ5ZWMtOTMwOS1mMzg3ZWFhMzRmNTgiLCJpbnRlZ3JhdGlvbklkIjoiMjdmNzM0YzAtZTIyMS00OTI1LTgxMjAtN2I1ZDNlYjAxOTA5IiwiYXBwTmFtZSI6ImdpdGh1YiIsImNsaWVudEluZm8iOnsiY2xpZW50SWQiOiJjZmE4NWM0ZC1jMWI0LTRjNmQtODE4Ni1kNmNiNmRmYmZlM2UifSwiaWF0IjoxNzIyMjE0NzUwfQ.w9QHilHJcHk8eIQa7vFCrGskC-VRFzgJIu1dQf4e7Jk&code_challenge=FnINCywVMtW7jH3DOGw6ddbmTf5vAJw5NvpojJkB2hs&code_challenge_method=S256\n",
"⚠ Waiting for github authentication...\n",
"✔ github added successfully!\n",
- "\u001b[32mShowing all apps\u001b[0m\n",
+ "\u001B[32mShowing all apps\u001B[0m\n",
"• multionai\n",
"• typeform\n",
"• dropbox\n",
@@ -186,10 +186,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[33m⚠️ Apps does not require update\u001b[0m\n",
- "\u001b[33m⚠️ Tags does not require update\u001b[0m\n",
- "\u001b[33m⚠️ Actions does not require update\u001b[0m\n",
- "\u001b[33m⚠️ Triggers does not require update\u001b[0m\n"
+ "\u001B[33m⚠️ Apps does not require update\u001B[0m\n",
+ "\u001B[33m⚠️ Tags does not require update\u001B[0m\n",
+ "\u001B[33m⚠️ Actions does not require update\u001B[0m\n",
+ "\u001B[33m⚠️ Triggers does not require update\u001B[0m\n"
]
}
],
@@ -354,19 +354,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[32mAI Assistant sys message:\n",
+ "\u001B[32mAI Assistant sys message:\n",
"BaseMessage(role_name='Developer', role_type=<RoleType.ASSISTANT: 'assistant'>, meta_dict={'task': 'I have created a new Github Repo,Please star my github repository: camel-ai/camel', 'assistant_role': 'Developer', 'user_role': 'CAMEL User'}, content='===== RULES OF ASSISTANT =====\\nNever forget you are a Developer and I am a CAMEL User. Never flip roles! Never instruct me!\\nWe share a common interest in collaborating to successfully complete a task.\\nYou must help me to complete the task.\\nHere is the task: I have created a new Github Repo,Please star my github repository: camel-ai/camel. Never forget our task!\\nI must instruct you based on your expertise and my needs to complete the task.\\n\\nI must give you one instruction at a time.\\nYou must write a specific solution that appropriately solves the requested instruction and explain your solutions.\\nYou must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.\\nUnless I say the task is completed, you should always start with:\\n\\nSolution: <YOUR_SOLUTION>\\n\\n<YOUR_SOLUTION> should be very specific, include detailed explanations and provide preferable detailed implementations and examples and lists for task-solving.\\nAlways end <YOUR_SOLUTION> with: Next request.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
"\n",
- "\u001b[34mAI User sys message:\n",
+ "\u001B[34mAI User sys message:\n",
"BaseMessage(role_name='CAMEL User', role_type=<RoleType.USER: 'user'>, meta_dict={'task': 'I have created a new Github Repo,Please star my github repository: camel-ai/camel', 'assistant_role': 'Developer', 'user_role': 'CAMEL User'}, content='===== RULES OF USER =====\\nNever forget you are a CAMEL User and I am a Developer. Never flip roles! You will always instruct me.\\nWe share a common interest in collaborating to successfully complete a task.\\nI must help you to complete the task.\\nHere is the task: I have created a new Github Repo,Please star my github repository: camel-ai/camel. Never forget our task!\\nYou must instruct me based on my expertise and your needs to solve the task ONLY in the following two ways:\\n\\n1. Instruct with a necessary input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: <YOUR_INPUT>\\n\\n2. Instruct without any input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: None\\n\\nThe \"Instruction\" describes a task or question. The paired \"Input\" provides further context or information for the requested \"Instruction\".\\n\\nYou must give me one instruction at a time.\\nI must write a response that appropriately solves the requested instruction.\\nI must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.\\nYou should instruct me not ask me questions.\\nNow you must start to instruct me using the two ways described above.\\nDo not add anything else other than your instruction and the optional corresponding input!\\nKeep giving me instructions and necessary inputs until you think the task is completed.\\nWhen the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.\\nNever say <CAMEL_TASK_DONE> unless my responses have solved your task.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
"\n",
- "\u001b[33mOriginal task prompt:\n",
+ "\u001B[33mOriginal task prompt:\n",
"I have created a new Github Repo,Please star my github repository: camel-ai/camel\n",
"\n",
- "\u001b[36mSpecified task prompt:\n",
+ "\u001B[36mSpecified task prompt:\n",
"None\n",
"\n",
- "\u001b[31mFinal task prompt:\n",
+ "\u001B[31mFinal task prompt:\n",
"I have created a new Github Repo,Please star my github repository: camel-ai/camel\n",
"\n"
]
@@ -405,26 +405,26 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Visit the Github repository I created.\n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: To visit the Github repository you created, you can simply open a web browser and go to the following URL: https://github.com/camel-ai/camel\n",
"\n",
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Click on the \"Star\" button on the Github repository page.\n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: github_activity_star_repo_for_authenticated_user\n",
"\tArgs: {'owner': 'camel-ai', 'repo': 'camel'}\n",
@@ -435,12 +435,12 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"<CAMEL_TASK_DONE>\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Great! The task is completed. If you have any more tasks or need further assistance in the future, feel free to ask. Have a wonderful day!\n",
"\n",
diff --git a/docs/cookbooks/critic_agents_and_tree_search.ipynb b/docs/cookbooks/critic_agents_and_tree_search.ipynb
index 94ad19e15..16ff97ce7 100644
--- a/docs/cookbooks/critic_agents_and_tree_search.ipynb
+++ b/docs/cookbooks/critic_agents_and_tree_search.ipynb
@@ -1,477 +1,477 @@
{
- "nbformat": 4,
- "nbformat_minor": 0,
- "metadata": {
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Critic Agents and Tree Search"
+ ],
+ "metadata": {
+ "id": "APw8wDolb0L9"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "You can also check this cookbook in colab [here](https://colab.research.google.com/drive/1A2id3IyP1tSQXmtLsaY9-zyowSRzTaxk?usp=sharing)"
+ ],
+ "metadata": {
+ "id": "vLCfmNtRb-jR"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Philosophical Bits\n",
+ "\n",
+ "<!-- > *What magical trick makes us intelligent? The trick is that there is no trick. The power of intelligence stems from our vast diversity, not from any single, perfect principle.*\n",
+ ">\n",
+ "> -- Marvin Minsky, The Society of Mind, p. 308\n",
+ "\n",
+ "In this section, we will take a spite of the task-oriented `RolyPlaying()` class. We design this in an instruction-following manner. The essence is that to solve a complex task, you can enable two communicative agents collabratively working together step by step to reach solutions. The main concepts include:\n",
+ "- **Task**: a task can be as simple as an idea, initialized by an inception prompt.\n",
+ "- **AI User**: the agent who is expected to provide instructions.\n",
+ "- **AI Assistant**: the agent who is expected to respond with solutions that fulfills the instructions. -->\n",
+ "\n",
+ "> **Prerequisite**: We assume that you have read the section on [intro to role-playing](https://colab.research.google.com/drive/1cmWPxXEsyMbmjPhD2bWfHuhd_Uz6FaJQ?usp=sharing).\n",
+ "\n",
+ "How do agents accomplish hard tasks? While reasoning can naturally emerge from next-token-prediction pretraining, it is still difficult for agents to solve complex tasks which require lots of intermediate steps. To tackle this issue, tree search is a simple and effective framework.\n",
+ "\n",
+ "A typical tree search include node expansion and node selection. In the [March 2023 paper](https://arxiv.org/abs/2303.17760), CAMEL introduces a heuristic tree search approach with critic in the loop, where the expansion and selection are presented below:\n",
+ "\n",
+ "\n",
+ "<div style=\"text-align: center;\">\n",
+ " <img src=\"https://i.imgur.com/6x4ABpp.png\" width=\"600\">\n",
+ "</div>\n",
+ "\n",
+ "To put it simply, a critic agent is a helper agents in the role-playing session, which is capable of selecting proposals and provide informative verbal feedback to the role-playing agents."
+ ],
+ "metadata": {
+ "id": "jHQhWNnhcOch"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Quick Start"
+ ],
+ "metadata": {
+ "id": "VUaGurDIVJBg"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### 🕹 Step 0: Prepartions"
+ ],
+ "metadata": {
+ "id": "u9NVFz-HVLXb"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "%pip install \"camel-ai==0.2.1\""
+ ],
+ "metadata": {
+ "id": "UtcC3c-KVZmU"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from camel.agents import CriticAgent\n",
+ "from camel.generators import SystemMessageGenerator as sys_msg_gen\n",
+ "from camel.messages import BaseMessage as bm\n",
+ "from camel.types import RoleType"
+ ],
+ "metadata": {
+ "id": "CmgKGeCxVON-"
+ },
+ "execution_count": 3,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Setting Up API Keys"
+ ],
+ "metadata": {
+ "id": "MyTTCe3IR_Lr"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "You'll need to set up your API keys for OpenAI."
+ ],
+ "metadata": {
+ "id": "REqzgGL9SEaD"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "import os\n",
+ "from getpass import getpass\n",
+ "\n",
+ "# Prompt for the API key securely\n",
+ "openai_api_key = getpass('Enter your API key: ')\n",
+ "os.environ[\"OPENAI_API_KEY\"] = openai_api_key"
+ ],
+ "metadata": {
"colab": {
- "provenance": []
- },
- "kernelspec": {
- "name": "python3",
- "display_name": "Python 3"
+ "base_uri": "https://localhost:8080/"
},
- "language_info": {
- "name": "python"
+ "id": "PNBFEXc-R-0s",
+ "outputId": "d6a21cfb-50b9-4a57-a40b-b3e44d95f287"
+ },
+ "execution_count": 2,
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter your API key: ··········\n"
+ ]
}
+ ]
},
- "cells": [
- {
- "cell_type": "markdown",
- "source": [
- "# Critic Agents and Tree Search"
- ],
- "metadata": {
- "id": "APw8wDolb0L9"
- }
- },
- {
- "cell_type": "markdown",
- "source": [
- "You can also check this cookbook in colab [here](https://colab.research.google.com/drive/1A2id3IyP1tSQXmtLsaY9-zyowSRzTaxk?usp=sharing)"
- ],
- "metadata": {
- "id": "vLCfmNtRb-jR"
- }
- },
- {
- "cell_type": "markdown",
- "source": [
- "## Philosophical Bits\n",
- "\n",
- "<!-- > *What magical trick makes us intelligent? The trick is that there is no trick. The power of intelligence stems from our vast diversity, not from any single, perfect principle.*\n",
- ">\n",
- "> -- Marvin Minsky, The Society of Mind, p. 308\n",
- "\n",
- "In this section, we will take a spite of the task-oriented `RolyPlaying()` class. We design this in an instruction-following manner. The essence is that to solve a complex task, you can enable two communicative agents collabratively working together step by step to reach solutions. The main concepts include:\n",
- "- **Task**: a task can be as simple as an idea, initialized by an inception prompt.\n",
- "- **AI User**: the agent who is expected to provide instructions.\n",
- "- **AI Assistant**: the agent who is expected to respond with solutions that fulfills the instructions. -->\n",
- "\n",
- "> **Prerequisite**: We assume that you have read the section on [intro to role-playing](https://colab.research.google.com/drive/1cmWPxXEsyMbmjPhD2bWfHuhd_Uz6FaJQ?usp=sharing).\n",
- "\n",
- "How do agents accomplish hard tasks? While reasoning can naturally emerge from next-token-prediction pretraining, it is still difficult for agents to solve complex tasks which require lots of intermediate steps. To tackle this issue, tree search is a simple and effective framework.\n",
- "\n",
- "A typical tree search include node expansion and node selection. In the [March 2023 paper](https://arxiv.org/abs/2303.17760), CAMEL introduces a heuristic tree search approach with critic in the loop, where the expansion and selection are presented below:\n",
- "\n",
- "\n",
- "<div style=\"text-align: center;\">\n",
- " <img src=\"https://i.imgur.com/6x4ABpp.png\" width=\"600\">\n",
- "</div>\n",
- "\n",
- "To put it simply, a critic agent is a helper agents in the role-playing session, which is capable of selecting proposals and provide informative verbal feedback to the role-playing agents."
- ],
- "metadata": {
- "id": "jHQhWNnhcOch"
- }
- },
- {
- "cell_type": "markdown",
- "source": [
- "## Quick Start"
- ],
- "metadata": {
- "id": "VUaGurDIVJBg"
- }
- },
- {
- "cell_type": "markdown",
- "source": [
- "### 🕹 Step 0: Prepartions"
- ],
- "metadata": {
- "id": "u9NVFz-HVLXb"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "%pip install \"camel-ai==0.2.1\""
- ],
- "metadata": {
- "id": "UtcC3c-KVZmU"
- },
- "execution_count": null,
- "outputs": []
- },
- {
- "cell_type": "code",
- "source": [
- "from camel.agents import CriticAgent\n",
- "from camel.generators import SystemMessageGenerator as sys_msg_gen\n",
- "from camel.messages import BaseMessage as bm\n",
- "from camel.types import RoleType"
- ],
- "metadata": {
- "id": "CmgKGeCxVON-"
- },
- "execution_count": 3,
- "outputs": []
- },
- {
- "cell_type": "markdown",
- "source": [
- "### Setting Up API Keys"
- ],
- "metadata": {
- "id": "MyTTCe3IR_Lr"
- }
- },
- {
- "cell_type": "markdown",
- "source": [
- "You'll need to set up your API keys for OpenAI."
- ],
- "metadata": {
- "id": "REqzgGL9SEaD"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "import os\n",
- "from getpass import getpass\n",
- "\n",
- "# Prompt for the API key securely\n",
- "openai_api_key = getpass('Enter your API key: ')\n",
- "os.environ[\"OPENAI_API_KEY\"] = openai_api_key"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "PNBFEXc-R-0s",
- "outputId": "d6a21cfb-50b9-4a57-a40b-b3e44d95f287"
- },
- "execution_count": 2,
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Enter your API key: ··········\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "### 🕹 Step 1: Configure the Specifications for Critic Agents"
- ],
- "metadata": {
- "id": "wcetZrjEcyo_"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# Set the role name and the task\n",
- "critic_role = 'a picky critic'\n",
- "\n",
- "# Create the meta_dict and the role_tuple\n",
- "meta_dict = dict(critic_role=critic_role,\n",
- " criteria='Help better accomplish the task.')\n",
- "\n",
- "# Create the role tuple\n",
- "role_tuple = (critic_role, RoleType.CRITIC)\n",
- "\n",
- "# Generate the system message\n",
- "sys_msg = sys_msg_gen().from_dict(meta_dict=meta_dict,\n",
- " role_tuple=role_tuple)"
- ],
- "metadata": {
- "id": "i-pIc9eTc0SH"
- },
- "execution_count": 4,
- "outputs": []
- },
- {
- "cell_type": "markdown",
- "source": [
- "### 🕹 Step 2: Get the Critic Agents\n",
- "With the above arguments, we have:"
- ],
- "metadata": {
- "id": "BEUEpNa_c8sS"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "critic_agent = CriticAgent(system_message=sys_msg,\n",
- " verbose=True)"
- ],
- "metadata": {
- "id": "VetPjL70c9be"
- },
- "execution_count": 5,
- "outputs": []
- },
- {
- "cell_type": "markdown",
- "source": [
- "Let's take a look on the default system message:"
- ],
- "metadata": {
- "id": "o6aDvI1qc_kH"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "print(critic_agent.system_message.content)"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "hP3hxdBKezD2",
- "outputId": "f057038b-8c12-4795-b4e8-9afb75c6a8cb"
- },
- "execution_count": 6,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stdout",
- "text": [
- "You are a a picky critic who teams up with a {user_role} and a {assistant_role} to solve a task: {task}.\n",
- "Your job is to select an option from their proposals and provides your explanations.\n",
- "Your selection criteria are Help better accomplish the task..\n",
- "You always have to choose an option from the proposals.\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "You may overwrite the system message and configure the critic differently based on your own needs."
- ],
- "metadata": {
- "id": "1uO0Wo6Oe1Ul"
- }
- },
- {
- "cell_type": "markdown",
- "source": [
- "### 🕹 Step 3: Using Critic Agents for Task Solving\n",
- "Our `RolePlaying()` class provide a simple way for you to add the critic in the loop. Below we provide a basic pipeline."
- ],
- "metadata": {
- "id": "MXzNaWp9dCvo"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "# Import necessary classes\n",
- "from camel.societies import RolePlaying\n",
- "from camel.configs import ChatGPTConfig\n",
- "from camel.types import TaskType, ModelType, ModelPlatformType\n",
- "from colorama import Fore\n",
- "from camel.utils import print_text_animated\n",
- "from camel.models import ModelFactory\n",
- "\n",
- "# Set the LLM model type and model config\n",
- "model_platform = ModelPlatformType.OPENAI\n",
- "model_type = ModelType.GPT_4O_MINI\n",
- "model_config = ChatGPTConfig(\n",
- " temperature=0.8, # the sampling temperature; the higher the more random\n",
- " n=3, # the no. of completion choices to generate for each input\n",
- " )\n",
- "\n",
- "# Create the backend model\n",
- "model = ModelFactory.create(\n",
- " model_platform=model_platform,\n",
- " model_type=model_type,\n",
- " model_config_dict=model_config.as_dict())"
- ],
- "metadata": {
- "id": "Ts52u_UVdDJA"
- },
- "execution_count": 9,
- "outputs": []
- },
- {
- "cell_type": "markdown",
- "source": [
- "We then need to set the kwargs for the task and each agent:"
- ],
- "metadata": {
- "id": "BlHF6LkRdFLo"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "task_kwargs = {\n",
- " 'task_prompt': 'Develop a plan to TRAVEL TO THE PAST and make changes.',\n",
- " 'with_task_specify': True,\n",
- " 'task_specify_agent_kwargs': {'model': model}\n",
- "}\n",
- "\n",
- "user_role_kwargs = {\n",
- " 'user_role_name': 'an ambitious aspiring TIME TRAVELER',\n",
- " 'user_agent_kwargs': {'model': model}\n",
- "}\n",
- "\n",
- "assistant_role_kwargs = {\n",
- " 'assistant_role_name': 'the best-ever experimental physicist',\n",
- " 'assistant_agent_kwargs': {'model': model}\n",
- "}\n",
- "\n",
- "critic_role_kwargs = {\n",
- " 'with_critic_in_the_loop': True,\n",
- " 'critic_criteria': 'improve the task performance',\n",
- " 'critic_kwargs': dict(verbose=True)\n",
- "}"
- ],
- "metadata": {
- "id": "6hKpSzssdGvc"
- },
- "execution_count": 10,
- "outputs": []
- },
- {
- "cell_type": "markdown",
- "source": [
- "Putting them together:"
- ],
- "metadata": {
- "id": "eVpBAKZjfQ2Z"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "society = RolePlaying(\n",
- " **task_kwargs, # The task arguments\n",
- " **user_role_kwargs, # The instruction sender's arguments\n",
- " **assistant_role_kwargs, # The instruction receiver's arguments\n",
- " **critic_role_kwargs, # The critic's arguments\n",
- ")"
- ],
- "metadata": {
- "colab": {
- "base_uri": "https://localhost:8080/"
- },
- "id": "d7P73ky_fRWm",
- "outputId": "ff85c6de-43fe-404a-97d0-c9207575a46e"
- },
- "execution_count": 11,
- "outputs": [
- {
- "output_type": "stream",
- "name": "stderr",
- "text": [
- "WARNING:camel.agents.chat_agent:Multiple messages returned in `step()`, message won't be recorded automatically. Please call `record_message()` to record the selected message manually.\n"
- ]
- }
- ]
- },
- {
- "cell_type": "markdown",
- "source": [
- "And the helper functions to run our society:"
- ],
- "metadata": {
- "id": "2Q-iBdzCfWOM"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "def is_terminated(response):\n",
- " \"\"\"\n",
- " Give alerts when the session shuold be terminated.\n",
- " \"\"\"\n",
- " if response.terminated:\n",
- " role = response.msg.role_type.name\n",
- " reason = response.info['termination_reasons']\n",
- " print(f'AI {role} terminated due to {reason}')\n",
- "\n",
- " return response.terminated"
- ],
- "metadata": {
- "id": "XBsQTzZofXvi"
- },
- "execution_count": 12,
- "outputs": []
- },
- {
- "cell_type": "code",
- "source": [
- "def run(society, round_limit: int=10):\n",
- "\n",
- " # Get the initial message from the ai assistant to the ai user\n",
- " input_msg = society.init_chat()\n",
- "\n",
- " # Starting the interactive session\n",
- " for _ in range(round_limit):\n",
- "\n",
- " # Get the both responses for this round\n",
- " assistant_response, user_response = society.step(input_msg)\n",
- "\n",
- " # Check the termination condition\n",
- " if is_terminated(assistant_response) or is_terminated(user_response):\n",
- " break\n",
- "\n",
- " # Get the results\n",
- " print(f'[AI User] {user_response.msg.content}.\\n')\n",
- " print(f'[AI Assistant] {assistant_response.msg.content}.\\n')\n",
- "\n",
- " # Check if the task is end\n",
- " if 'CAMEL_TASK_DONE' in user_response.msg.content:\n",
- " break\n",
- "\n",
- " # Get the input message for the next round\n",
- " input_msg = assistant_response.msg\n",
- "\n",
- " return None"
- ],
- "metadata": {
- "id": "jqlKDNUwfcfp"
- },
- "execution_count": 13,
- "outputs": []
- },
- {
- "cell_type": "markdown",
- "source": [
- "Now let's set our code in motion:"
- ],
- "metadata": {
- "id": "TaiQJLt7ffKy"
- }
- },
- {
- "cell_type": "code",
- "source": [
- "run(society)"
- ],
- "metadata": {
- "id": "lSSF_RsPffjh"
- },
- "execution_count": null,
- "outputs": []
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### 🕹 Step 1: Configure the Specifications for Critic Agents"
+ ],
+ "metadata": {
+ "id": "wcetZrjEcyo_"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Set the role name and the task\n",
+ "critic_role = 'a picky critic'\n",
+ "\n",
+ "# Create the meta_dict and the role_tuple\n",
+ "meta_dict = dict(critic_role=critic_role,\n",
+ " criteria='Help better accomplish the task.')\n",
+ "\n",
+ "# Create the role tuple\n",
+ "role_tuple = (critic_role, RoleType.CRITIC)\n",
+ "\n",
+ "# Generate the system message\n",
+ "sys_msg = sys_msg_gen().from_dict(meta_dict=meta_dict,\n",
+ " role_tuple=role_tuple)"
+ ],
+ "metadata": {
+ "id": "i-pIc9eTc0SH"
+ },
+ "execution_count": 4,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### 🕹 Step 2: Get the Critic Agents\n",
+ "With the above arguments, we have:"
+ ],
+ "metadata": {
+ "id": "BEUEpNa_c8sS"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "critic_agent = CriticAgent(system_message=sys_msg,\n",
+ " verbose=True)"
+ ],
+ "metadata": {
+ "id": "VetPjL70c9be"
+ },
+ "execution_count": 5,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Let's take a look on the default system message:"
+ ],
+ "metadata": {
+ "id": "o6aDvI1qc_kH"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "print(critic_agent.system_message.content)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "hP3hxdBKezD2",
+ "outputId": "f057038b-8c12-4795-b4e8-9afb75c6a8cb"
+ },
+ "execution_count": 6,
+ "outputs": [
{
- "cell_type": "markdown",
- "source": [
- "In this setting, the `AI User` and `AI Assistant` will generate different options when responding (you can simply change the `temperature` in `model_config` to somewhat control the diversity). `AI Critic` will respond with its option selection and reasoning; such additional context will be fed to the two other agents and help them form better subsequent responses."
- ],
- "metadata": {
- "id": "8e1bo7sHfkEo"
- }
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "You are a a picky critic who teams up with a {user_role} and a {assistant_role} to solve a task: {task}.\n",
+ "Your job is to select an option from their proposals and provides your explanations.\n",
+ "Your selection criteria are Help better accomplish the task..\n",
+ "You always have to choose an option from the proposals.\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "You may overwrite the system message and configure the critic differently based on your own needs."
+ ],
+ "metadata": {
+ "id": "1uO0Wo6Oe1Ul"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### 🕹 Step 3: Using Critic Agents for Task Solving\n",
+ "Our `RolePlaying()` class provide a simple way for you to add the critic in the loop. Below we provide a basic pipeline."
+ ],
+ "metadata": {
+ "id": "MXzNaWp9dCvo"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Import necessary classes\n",
+ "from camel.societies import RolePlaying\n",
+ "from camel.configs import ChatGPTConfig\n",
+ "from camel.types import TaskType, ModelType, ModelPlatformType\n",
+ "from colorama import Fore\n",
+ "from camel.utils import print_text_animated\n",
+ "from camel.models import ModelFactory\n",
+ "\n",
+ "# Set the LLM model type and model config\n",
+ "model_platform = ModelPlatformType.OPENAI\n",
+ "model_type = ModelType.GPT_4O_MINI\n",
+ "model_config = ChatGPTConfig(\n",
+ " temperature=0.8, # the sampling temperature; the higher the more random\n",
+ " n=3, # the no. of completion choices to generate for each input\n",
+ " )\n",
+ "\n",
+ "# Create the backend model\n",
+ "model = ModelFactory.create(\n",
+ " model_platform=model_platform,\n",
+ " model_type=model_type,\n",
+ " model_config_dict=model_config.as_dict())"
+ ],
+ "metadata": {
+ "id": "Ts52u_UVdDJA"
+ },
+ "execution_count": 9,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "We then need to set the kwargs for the task and each agent:"
+ ],
+ "metadata": {
+ "id": "BlHF6LkRdFLo"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "task_kwargs = {\n",
+ " 'task_prompt': 'Develop a plan to TRAVEL TO THE PAST and make changes.',\n",
+ " 'with_task_specify': True,\n",
+ " 'task_specify_agent_kwargs': {'model': model}\n",
+ "}\n",
+ "\n",
+ "user_role_kwargs = {\n",
+ " 'user_role_name': 'an ambitious aspiring TIME TRAVELER',\n",
+ " 'user_agent_kwargs': {'model': model}\n",
+ "}\n",
+ "\n",
+ "assistant_role_kwargs = {\n",
+ " 'assistant_role_name': 'the best-ever experimental physicist',\n",
+ " 'assistant_agent_kwargs': {'model': model}\n",
+ "}\n",
+ "\n",
+ "critic_role_kwargs = {\n",
+ " 'with_critic_in_the_loop': True,\n",
+ " 'critic_criteria': 'improve the task performance',\n",
+ " 'critic_kwargs': dict(verbose=True)\n",
+ "}"
+ ],
+ "metadata": {
+ "id": "6hKpSzssdGvc"
+ },
+ "execution_count": 10,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Putting them together:"
+ ],
+ "metadata": {
+ "id": "eVpBAKZjfQ2Z"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "society = RolePlaying(\n",
+ " **task_kwargs, # The task arguments\n",
+ " **user_role_kwargs, # The instruction sender's arguments\n",
+ " **assistant_role_kwargs, # The instruction receiver's arguments\n",
+ " **critic_role_kwargs, # The critic's arguments\n",
+ ")"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
},
+ "id": "d7P73ky_fRWm",
+ "outputId": "ff85c6de-43fe-404a-97d0-c9207575a46e"
+ },
+ "execution_count": 11,
+ "outputs": [
{
- "cell_type": "markdown",
- "source": [
- "## Remarks\n",
- "While we see some performance gains from critic-in-the-loop, it may not really solve the fundamental extrapolation problem (and [self-consistency](https://arxiv.org/abs/2203.11171) remains a strong baseline for many tasks). It is debatable if those agents can extrapolate by self-play within its current scale. A more practical question is how we may *efficiently* introduce *informative* feedbacks/rewards, when agents are connected with external environments and are endowed with tools and memories. They are expected to have a good world model and know how to make abstraction and analogy when necessary. Stay tuned for our next update."
- ],
- "metadata": {
- "id": "v4YaF8I7fmOt"
- }
+ "output_type": "stream",
+ "name": "stderr",
+ "text": [
+ "WARNING:camel.agents.chat_agent:Multiple messages returned in `step()`, message won't be recorded automatically. Please call `record_message()` to record the selected message manually.\n"
+ ]
}
- ]
-}
\ No newline at end of file
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "And the helper functions to run our society:"
+ ],
+ "metadata": {
+ "id": "2Q-iBdzCfWOM"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def is_terminated(response):\n",
+ " \"\"\"\n",
+ " Give alerts when the session shuold be terminated.\n",
+ " \"\"\"\n",
+ " if response.terminated:\n",
+ " role = response.msg.role_type.name\n",
+ " reason = response.info['termination_reasons']\n",
+ " print(f'AI {role} terminated due to {reason}')\n",
+ "\n",
+ " return response.terminated"
+ ],
+ "metadata": {
+ "id": "XBsQTzZofXvi"
+ },
+ "execution_count": 12,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def run(society, round_limit: int=10):\n",
+ "\n",
+ " # Get the initial message from the ai assistant to the ai user\n",
+ " input_msg = society.init_chat()\n",
+ "\n",
+ " # Starting the interactive session\n",
+ " for _ in range(round_limit):\n",
+ "\n",
+ " # Get the both responses for this round\n",
+ " assistant_response, user_response = society.step(input_msg)\n",
+ "\n",
+ " # Check the termination condition\n",
+ " if is_terminated(assistant_response) or is_terminated(user_response):\n",
+ " break\n",
+ "\n",
+ " # Get the results\n",
+ " print(f'[AI User] {user_response.msg.content}.\\n')\n",
+ " print(f'[AI Assistant] {assistant_response.msg.content}.\\n')\n",
+ "\n",
+ " # Check if the task is end\n",
+ " if 'CAMEL_TASK_DONE' in user_response.msg.content:\n",
+ " break\n",
+ "\n",
+ " # Get the input message for the next round\n",
+ " input_msg = assistant_response.msg\n",
+ "\n",
+ " return None"
+ ],
+ "metadata": {
+ "id": "jqlKDNUwfcfp"
+ },
+ "execution_count": 13,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Now let's set our code in motion:"
+ ],
+ "metadata": {
+ "id": "TaiQJLt7ffKy"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "run(society)"
+ ],
+ "metadata": {
+ "id": "lSSF_RsPffjh"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "In this setting, the `AI User` and `AI Assistant` will generate different options when responding (you can simply change the `temperature` in `model_config` to somewhat control the diversity). `AI Critic` will respond with its option selection and reasoning; such additional context will be fed to the two other agents and help them form better subsequent responses."
+ ],
+ "metadata": {
+ "id": "8e1bo7sHfkEo"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "## Remarks\n",
+ "While we see some performance gains from critic-in-the-loop, it may not really solve the fundamental extrapolation problem (and [self-consistency](https://arxiv.org/abs/2203.11171) remains a strong baseline for many tasks). It is debatable if those agents can extrapolate by self-play within its current scale. A more practical question is how we may *efficiently* introduce *informative* feedbacks/rewards, when agents are connected with external environments and are endowed with tools and memories. They are expected to have a good world model and know how to make abstraction and analogy when necessary. Stay tuned for our next update."
+ ],
+ "metadata": {
+ "id": "v4YaF8I7fmOt"
+ }
+ }
+ ]
+}
diff --git a/docs/cookbooks/roleplaying_scraper.ipynb b/docs/cookbooks/roleplaying_scraper.ipynb
index 3bbc7e1b0..602990300 100644
--- a/docs/cookbooks/roleplaying_scraper.ipynb
+++ b/docs/cookbooks/roleplaying_scraper.ipynb
@@ -698,7 +698,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
- "🖇 AgentOps: \u001b[34m\u001b[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ab0c011f-1928-4895-9747-ea882e9dfc38\u001b[0m\u001b[0m\n"
+ "🖇 AgentOps: \u001B[34m\u001B[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ab0c011f-1928-4895-9747-ea882e9dfc38\u001B[0m\u001B[0m\n"
]
},
{
@@ -944,13 +944,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[32mAI Assistant sys message:\n",
+ "\u001B[32mAI Assistant sys message:\n",
"BaseMessage(role_name='CAMEL Assistant', role_type=<RoleType.ASSISTANT: 'assistant'>, meta_dict={'task': 'Do a comprehensive study of the Turkish shooter in 2024 paris\\nolympics, write a report for me, then create a knowledge graph for the report.\\nYou should use search tool to get related URLs first, then use retrieval tool\\nto get the retrieved content back by providing the list of URLs, finially\\nuse tool to build the knowledge graph to finish the task.\\nNo more other actions needed', 'assistant_role': 'CAMEL Assistant', 'user_role': 'CAMEL User'}, content='===== RULES OF ASSISTANT =====\\nNever forget you are a CAMEL Assistant and I am a CAMEL User. Never flip roles! Never instruct me!\\nWe share a common interest in collaborating to successfully complete a task.\\nYou must help me to complete the task.\\nHere is the task: Do a comprehensive study of the Turkish shooter in 2024 paris\\nolympics, write a report for me, then create a knowledge graph for the report.\\nYou should use search tool to get related URLs first, then use retrieval tool\\nto get the retrieved content back by providing the list of URLs, finially\\nuse tool to build the knowledge graph to finish the task.\\nNo more other actions needed. Never forget our task!\\nI must instruct you based on your expertise and my needs to complete the task.\\n\\nI must give you one instruction at a time.\\nYou must write a specific solution that appropriately solves the requested instruction and explain your solutions.\\nYou must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.\\nUnless I say the task is completed, you should always start with:\\n\\nSolution: <YOUR_SOLUTION>\\n\\n<YOUR_SOLUTION> should be very specific, include detailed explanations and provide preferable detailed implementations and examples and lists for task-solving.\\nAlways end <YOUR_SOLUTION> with: Next request.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
"\n",
- "\u001b[34mAI User sys message:\n",
+ "\u001B[34mAI User sys message:\n",
"BaseMessage(role_name='CAMEL User', role_type=<RoleType.USER: 'user'>, meta_dict={'task': 'Do a comprehensive study of the Turkish shooter in 2024 paris\\nolympics, write a report for me, then create a knowledge graph for the report.\\nYou should use search tool to get related URLs first, then use retrieval tool\\nto get the retrieved content back by providing the list of URLs, finially\\nuse tool to build the knowledge graph to finish the task.\\nNo more other actions needed', 'assistant_role': 'CAMEL Assistant', 'user_role': 'CAMEL User'}, content='===== RULES OF USER =====\\nNever forget you are a CAMEL User and I am a CAMEL Assistant. Never flip roles! You will always instruct me.\\nWe share a common interest in collaborating to successfully complete a task.\\nI must help you to complete the task.\\nHere is the task: Do a comprehensive study of the Turkish shooter in 2024 paris\\nolympics, write a report for me, then create a knowledge graph for the report.\\nYou should use search tool to get related URLs first, then use retrieval tool\\nto get the retrieved content back by providing the list of URLs, finially\\nuse tool to build the knowledge graph to finish the task.\\nNo more other actions needed. Never forget our task!\\nYou must instruct me based on my expertise and your needs to solve the task ONLY in the following two ways:\\n\\n1. Instruct with a necessary input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: <YOUR_INPUT>\\n\\n2. Instruct without any input:\\nInstruction: <YOUR_INSTRUCTION>\\nInput: None\\n\\nThe \"Instruction\" describes a task or question. The paired \"Input\" provides further context or information for the requested \"Instruction\".\\n\\nYou must give me one instruction at a time.\\nI must write a response that appropriately solves the requested instruction.\\nI must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.\\nYou should instruct me not ask me questions.\\nNow you must start to instruct me using the two ways described above.\\nDo not add anything else other than your instruction and the optional corresponding input!\\nKeep giving me instructions and necessary inputs until you think the task is completed.\\nWhen the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.\\nNever say <CAMEL_TASK_DONE> unless my responses have solved your task.', video_bytes=None, image_list=None, image_detail='auto', video_detail='low')\n",
"\n",
- "\u001b[33mOriginal task prompt:\n",
+ "\u001B[33mOriginal task prompt:\n",
"Do a comprehensive study of the Turkish shooter in 2024 paris\n",
"olympics, write a report for me, then create a knowledge graph for the report.\n",
"You should use search tool to get related URLs first, then use retrieval tool\n",
@@ -958,10 +958,10 @@
"use tool to build the knowledge graph to finish the task.\n",
"No more other actions needed\n",
"\n",
- "\u001b[36mSpecified task prompt:\n",
+ "\u001B[36mSpecified task prompt:\n",
"None\n",
"\n",
- "\u001b[31mFinal task prompt:\n",
+ "\u001B[31mFinal task prompt:\n",
"Do a comprehensive study of the Turkish shooter in 2024 paris\n",
"olympics, write a report for me, then create a knowledge graph for the report.\n",
"You should use search tool to get related URLs first, then use retrieval tool\n",
@@ -1015,13 +1015,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Use the search tool to find related URLs about the Turkish shooter in the 2024 Paris Olympics.\n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: search_duckduckgo\n",
"\tArgs: {'query': 'Turkish shooter in the 2024 Paris Olympics'}\n",
@@ -1052,13 +1052,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Use the retrieval tool to get the content from the provided URLs.\n",
"Input: [https://olympics.com/en/news/yusuf-dikec-turkish-shooting-viral-paris-2024-olympics, https://www.nbcnews.com/news/sports/sharpshooter-yusuf-dikec-silver-olympics-minimal-gear-internet-sensati-rcna164685, https://apnews.com/article/olympics-2024-yusuf-dikec-turkish-shooter-a7890124304080a48e7ee4294004d306, https://www.nbcnews.com/video/watch-turkey-s-sharpshooter-wins-silver-without-high-tech-aids-216173637766, https://www.cnn.com/2024/08/09/sport/yusuf-dikec-olympic-celebration-craze-spt-intl/index.html]\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: retrieve_information_from_urls\n",
"\tArgs: {'query': 'Turkish shooter in the 2024 Paris Olympics', 'urls': ['https://olympics.com/en/news/yusuf-dikec-turkish-shooting-viral-paris-2024-olympics', 'https://www.nbcnews.com/news/sports/sharpshooter-yusuf-dikec-silver-olympics-minimal-gear-internet-sensati-rcna164685', 'https://apnews.com/article/olympics-2024-yusuf-dikec-turkish-shooter-a7890124304080a48e7ee4294004d306', 'https://www.nbcnews.com/video/watch-turkey-s-sharpshooter-wins-silver-without-high-tech-aids-216173637766', 'https://www.cnn.com/2024/08/09/sport/yusuf-dikec-olympic-celebration-craze-spt-intl/index.html']}\n",
@@ -1101,13 +1101,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Summarize the key points from the retrieved content about the Turkish shooter Yusuf Dikec at the 2024 Paris Olympics.\n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: I have summarized the key points from the retrieved content about the Turkish shooter Yusuf Dikec at the 2024 Paris Olympics. Here are the main points:\n",
"\n",
@@ -1124,13 +1124,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Use the summarized key points to write a comprehensive report about the Turkish shooter Yusuf Dikec at the 2024 Paris Olympics.\n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Solution: I have written a comprehensive report about the Turkish shooter Yusuf Dikec at the 2024 Paris Olympics based on the summarized key points. Here is the report:\n",
"\n",
@@ -1173,13 +1173,13 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"Instruction: Use the tool to build a knowledge graph based on the comprehensive report about the Turkish shooter Yusuf Dikec at the 2024 Paris Olympics.\n",
"Input: None\n",
"\n",
"\n",
- "\u001b[32mAI Assistant:\n",
+ "\u001B[32mAI Assistant:\n",
"\n",
"Function Execution: knowledge_graph_builder\n",
"\tArgs: {'text_input': \"### Comprehensive Report on Turkish Shooter Yusuf Dikec at the 2024 Paris Olympics\\n\\n#### Introduction\\n\\nYusuf Dikec, a seasoned Olympian and Turkish record breaker, made headlines at the 2024 Paris Olympics with his impressive performance and unique style. This report delves into his background, achievements, and the impact he made during the games.\\n\\n#### Background\\n\\nYusuf Dikec is an experienced shooter who has represented Turkey in multiple Olympic Games. He made his Olympic debut at the Beijing 2008 Olympics at the age of 35. Since then, he has competed in the London 2012, Rio 2016, and Tokyo 2020 Olympics, showcasing his skills and dedication to the sport.\\n\\n#### Achievements at the 2024 Paris Olympics\\n\\n##### Silver Medal Win\\n\\nAt the 2024 Paris Olympics, Yusuf Dikec won a silver medal in the air pistol team event. His performance was notable not only for his accuracy but also for his minimalistic approach. Unlike many of his competitors, Dikec did not use special lenses or big ear protectors. Instead, he competed with a regular pair of glasses and one hand casually tucked into his pocket, demonstrating a level of confidence and skill that captivated audiences.\\n\\n##### Viral Sensation\\n\\nDikec's nonchalant attitude and minimal gear during the competition went viral on the internet. Images and videos of him shooting effortlessly while maintaining a seemingly casual demeanor spread rapidly across social media platforms. His unique style and impressive performance made him an internet sensation, drawing attention from both sports enthusiasts and casual viewers alike.\\n\\n##### Partnership with Sevval Ilayda Tarhan\\n\\nDikec competed alongside Sevval Ilayda Tarhan in the mixed air pistol team event. Together, they secured Turkey’s first medal in Olympic shooting. Tarhan, like Dikec, also competed with minimal gear, keeping one hand tucked in her pocket and not wearing any blinders. Their partnership and shared approach to the sport highlighted their synergy and skill.\\n\\n#### Historic Achievement\\n\\nThe silver medal win marked a historic achievement for Turkey. It was the country’s first medal in an Olympic shooting event, underscoring the significance of Dikec and Tarhan's performance. Their success not only brought pride to their nation but also inspired a new generation of shooters in Turkey.\\n\\n#### Conclusion\\n\\nYusuf Dikec's journey at the 2024 Paris Olympics is a testament to his skill, dedication, and unique style. His silver medal win, viral fame, and historic achievement have cemented his place in the annals of Turkish sports history. As he continues to compete, Dikec serves as an inspiration for aspiring athletes, showcasing the power of perseverance and individuality in sports.\"}\n",
@@ -1225,7 +1225,7 @@
"Next request.\n",
"\n",
"\n",
- "\u001b[34mAI User:\n",
+ "\u001B[34mAI User:\n",
"\n",
"<CAMEL_TASK_DONE>\n",
"\n",
@@ -1297,7 +1297,7 @@
"output_type": "stream",
"text": [
"🖇 AgentOps: This run's cost $0.00\n",
- "🖇 AgentOps: \u001b[34m\u001b[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ab0c011f-1928-4895-9747-ea882e9dfc38\u001b[0m\u001b[0m\n"
+ "🖇 AgentOps: \u001B[34m\u001B[34mSession Replay: https://app.agentops.ai/drilldown?session_id=ab0c011f-1928-4895-9747-ea882e9dfc38\u001B[0m\u001B[0m\n"
]
}
],
diff --git a/docs/key_modules/memory.md b/docs/key_modules/memory.md
index 21ae1a8ed..b2d9ae6ff 100644
--- a/docs/key_modules/memory.md
+++ b/docs/key_modules/memory.md
@@ -48,8 +48,8 @@ records = [
role_name="Agent",
meta_dict=None,
content="CAMEL-AI.org is the 1st LLM multi-agent framework and "
- "an open-source community dedicated to finding the scaling law "
- "of agents.",
+ "an open-source community dedicated to finding the scaling law "
+ "of agents.",
),
role_at_backend=OpenAIBackendRole.ASSISTANT,
),
diff --git a/docs/key_modules/prompts.md b/docs/key_modules/prompts.md
index 5d88669f3..eae552503 100644
--- a/docs/key_modules/prompts.md
+++ b/docs/key_modules/prompts.md
@@ -18,7 +18,6 @@ from camel.types import ModelPlatformType, ModelType, TaskType
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
task_specify_agent = TaskSpecifyAgent(
model=model, task_type=TaskType.AI_SOCIETY
@@ -54,7 +53,6 @@ from camel.types import ModelPlatformType, ModelType
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
my_prompt_template = TextPrompt(
diff --git a/examples/generate_text_embedding_data/single_agent.py b/examples/generate_text_embedding_data/single_agent.py
index 7602fa408..8b4f5346a 100644
--- a/examples/generate_text_embedding_data/single_agent.py
+++ b/examples/generate_text_embedding_data/single_agent.py
@@ -20,7 +20,12 @@ from camel.configs.openai_config import ChatGPTConfig
from camel.generators import SystemMessageGenerator
from camel.messages import BaseMessage
from camel.models import ModelFactory
-from camel.types import ModelPlatformType, ModelType, RoleType, TaskType
+from camel.types import (
+ ModelPlatformType,
+ ModelType,
+ RoleType,
+ TaskType,
+)
QUERY_TYPE_LIST = ["extremely long-tail", "long-tail", "common"]
QUERY_LENGTH_LIST = ["less than 5 words", "5 to 15 words", "at least 10 words"]
diff --git a/examples/models/azure_openai_model_example.py b/examples/models/azure_openai_model_example.py
index eaad88f94..61c553021 100644
--- a/examples/models/azure_openai_model_example.py
+++ b/examples/models/azure_openai_model_example.py
@@ -19,7 +19,7 @@ from camel.types import ModelPlatformType, ModelType
"""
please set the below os environment:
-export AZURE_OPENAI_ENDPOINT=""
+export AZURE_OPENAI_BASE_URL=""
export AZURE_API_VERSION=""
export AZURE_OPENAI_API_KEY=""
export AZURE_DEPLOYMENT_NAME=""
diff --git a/examples/models/openai_compatibility_model_examples/qwen.py b/examples/models/openai_compatibility_model_examples/qwen.py
index 87f0b15f5..701285e73 100644
--- a/examples/models/openai_compatibility_model_examples/qwen.py
+++ b/examples/models/openai_compatibility_model_examples/qwen.py
@@ -20,7 +20,7 @@ from camel.types import ModelPlatformType
# Take calling model from DashScope as an example
# Refer: https://dashscope.console.aliyun.com/overview
model = ModelFactory.create(
- model_platform=ModelPlatformType.OPENAI_COMPATIBILITY_MODEL,
+ model_platform=ModelPlatformType.OPENAI_COMPATIBLE_MODEL,
model_type="qwen-plus",
api_key="sk-xxxx",
url="https://dashscope.aliyuncs.com/compatible-mode/v1",
diff --git a/examples/models/role_playing_with_claude.py b/examples/models/role_playing_with_claude.py
index ede25cc24..57ac3a3a6 100644
--- a/examples/models/role_playing_with_claude.py
+++ b/examples/models/role_playing_with_claude.py
@@ -13,7 +13,6 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from colorama import Fore
-from camel.configs import AnthropicConfig
from camel.models import ModelFactory
from camel.societies import RolePlaying
from camel.types import ModelPlatformType, ModelType
@@ -26,7 +25,6 @@ def main(model_type=None) -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.ANTHROPIC,
model_type=model_type,
- 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 b4cd9596f..b59c6bb4e 100644
--- a/examples/models/role_playing_with_gemini.py
+++ b/examples/models/role_playing_with_gemini.py
@@ -14,7 +14,6 @@
from colorama import Fore
-from camel.configs import GeminiConfig
from camel.models import ModelFactory
from camel.societies import RolePlaying
from camel.types import ModelPlatformType, ModelType
@@ -27,7 +26,6 @@ def main(model_type=None) -> None:
model = ModelFactory.create(
model_platform=ModelPlatformType.GEMINI,
model_type=model_type,
- 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 eb3295621..6dcd947f8 100644
--- a/examples/models/role_playing_with_groq.py
+++ b/examples/models/role_playing_with_groq.py
@@ -13,7 +13,6 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from colorama import Fore
-from camel.configs import GroqConfig
from camel.models import ModelFactory
from camel.societies import RolePlaying
from camel.types import ModelPlatformType, ModelType
@@ -27,7 +26,6 @@ def main(model_type=None) -> None:
role: ModelFactory.create(
model_platform=ModelPlatformType.GROQ,
model_type=model_type,
- 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_ollama.py
similarity index 64%
rename from examples/models/role_playing_with_open_source_model.py
rename to examples/models/role_playing_with_ollama.py
index e67dbb30b..5ac273184 100644
--- a/examples/models/role_playing_with_open_source_model.py
+++ b/examples/models/role_playing_with_ollama.py
@@ -11,53 +11,44 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
+
+from typing import List
+
from colorama import Fore
-from camel.configs import ChatGPTConfig, OpenSourceConfig
+from camel.agents.chat_agent import FunctionCallingRecord
from camel.models import ModelFactory
from camel.societies import RolePlaying
-from camel.types import ModelPlatformType, ModelType
+from camel.types import ModelPlatformType
from camel.utils import print_text_animated
-# Here :obj:`model_type` can be any of the supported open-source
-# model types and :obj:`model_path` should be set corresponding to
-# model type. For example, to use Vicuna, we can set:
-# model_path = "lmsys/vicuna-7b-v1.5"
def main(
- chat_turn_limit=50,
- model_platform=ModelPlatformType.OPEN_SOURCE,
- model_type=ModelType.STUB,
- model_path="meta-llama/Llama-2-7b-chat-hf",
- server_url="http://localhost:8000/v1",
+ model_platform=ModelPlatformType.OLLAMA,
+ model_type="llama3.2",
+ chat_turn_limit=10,
) -> None:
- task_prompt = "Develop a trading bot for the stock market"
-
- model = ModelFactory.create(
- model_platform=model_platform,
- model_type=model_type,
- model_config_dict=OpenSourceConfig(
- model_path=model_path,
- server_url=server_url,
- api_params=ChatGPTConfig(temperature=0),
- ).as_dict(),
- )
-
- # Update agent_kwargs to use the created models
- agent_kwargs = {
- "assistant": {"model": model},
- "user": {"model": model},
- "task-specify": {"model": model},
- }
+ task_prompt = "Develop a trading bot for the stock market."
role_play_session = RolePlaying(
assistant_role_name="Python Programmer",
- assistant_agent_kwargs=agent_kwargs["assistant"],
user_role_name="Stock Trader",
- user_agent_kwargs=agent_kwargs["user"],
+ assistant_agent_kwargs=dict(
+ model=ModelFactory.create(
+ model_platform=model_platform,
+ model_type=model_type,
+ model_config_dict={"temperature": 0.4, "max_tokens": 4096},
+ ),
+ ),
+ user_agent_kwargs=dict(
+ model=ModelFactory.create(
+ model_platform=model_platform,
+ model_type=model_type,
+ model_config_dict={"temperature": 0.4, "max_tokens": 4096},
+ ),
+ ),
task_prompt=task_prompt,
- with_task_specify=True,
- task_specify_agent_kwargs=agent_kwargs["task-specify"],
+ with_task_specify=False,
)
print(
@@ -101,13 +92,21 @@ def main(
)
break
+ # Print output from the user
print_text_animated(
Fore.BLUE + f"AI User:\n\n{user_response.msg.content}\n"
)
- print_text_animated(
- Fore.GREEN + "AI Assistant:\n\n"
- f"{assistant_response.msg.content}\n"
- )
+
+ # Print output from the assistant, including any function
+ # execution information
+ print_text_animated(Fore.GREEN + "AI Assistant:")
+ 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}")
+ print_text_animated(f"{assistant_response.msg.content}\n")
if "CAMEL_TASK_DONE" in user_response.msg.content:
break
diff --git a/examples/structured_response/json_format_response.py b/examples/structured_response/json_format_response.py
index 3c9e93376..ec9ed4184 100644
--- a/examples/structured_response/json_format_response.py
+++ b/examples/structured_response/json_format_response.py
@@ -15,7 +15,6 @@
from pydantic import BaseModel, Field
from camel.agents import ChatAgent
-from camel.configs.openai_config import ChatGPTConfig
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
@@ -29,7 +28,6 @@ assistant_sys_msg = BaseMessage.make_assistant_message(
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
# Set agent
diff --git a/examples/test/test_ai_society_example.py b/examples/test/test_ai_society_example.py
index 46427083d..d57e79568 100644
--- a/examples/test/test_ai_society_example.py
+++ b/examples/test/test_ai_society_example.py
@@ -14,7 +14,6 @@
from mock import patch
import examples.ai_society.role_playing
-import examples.models.role_playing_with_open_source_model
import examples.tool_call.role_playing_with_functions
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
@@ -22,7 +21,6 @@ from camel.types import ModelPlatformType, ModelType
test_model = ModelFactory.create(
model_platform=ModelPlatformType.DEFAULT,
model_type=ModelType.STUB,
- model_config_dict={},
)
@@ -36,10 +34,3 @@ def test_ai_society_role_playing_example():
def test_role_playing_with_function_example():
with patch('time.sleep', return_value=None):
examples.tool_call.role_playing_with_functions.main(chat_turn_limit=2)
-
-
-def test_role_playing_with_open_source_model():
- with patch('time.sleep', return_value=None):
- examples.models.role_playing_with_open_source_model.main(
- chat_turn_limit=2
- )
diff --git a/examples/test/test_babyagi_example.py b/examples/test/test_babyagi_example.py
index c5414a730..989d10649 100644
--- a/examples/test/test_babyagi_example.py
+++ b/examples/test/test_babyagi_example.py
@@ -24,7 +24,6 @@ parametrize = pytest.mark.parametrize(
ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.STUB,
- model_config_dict={},
),
pytest.param(None, marks=pytest.mark.model_backend),
],
diff --git a/examples/test/test_code_example.py b/examples/test/test_code_example.py
index 888f30d66..d81390e8d 100644
--- a/examples/test/test_code_example.py
+++ b/examples/test/test_code_example.py
@@ -24,7 +24,6 @@ def test_code_role_playing_example():
ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.STUB,
- model_config_dict={},
),
chat_turn_limit=2,
)
diff --git a/examples/test/test_role_description_example.py b/examples/test/test_role_description_example.py
index c97f9da94..f088955fa 100644
--- a/examples/test/test_role_description_example.py
+++ b/examples/test/test_role_description_example.py
@@ -21,13 +21,11 @@ from camel.types import ModelPlatformType, ModelType
model_gpt = ModelFactory.create(
ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict={},
)
model_stub = ModelFactory.create(
ModelPlatformType.OPENAI,
model_type=ModelType.STUB,
- model_config_dict={},
)
diff --git a/examples/test/test_single_agent.py b/examples/test/test_single_agent.py
index bb5677acc..dbeb7ccc9 100644
--- a/examples/test/test_single_agent.py
+++ b/examples/test/test_single_agent.py
@@ -27,7 +27,6 @@ parametrize = pytest.mark.parametrize(
ModelFactory.create(
ModelPlatformType.OPENAI,
model_type=ModelType.STUB,
- model_config_dict={},
),
pytest.param(None, marks=pytest.mark.model_backend),
],
@@ -45,7 +44,6 @@ def test_single_agent(model):
ModelFactory.create(
ModelPlatformType.OPENAI,
model_type=ModelType.STUB,
- model_config_dict={},
)
],
)
@@ -69,7 +67,6 @@ def test_code_generate_metadata(model):
ModelFactory.create(
ModelPlatformType.OPENAI,
model_type=ModelType.STUB,
- model_config_dict={},
)
],
)
diff --git a/examples/translation/translator.py b/examples/translation/translator.py
index f8c1b2f6e..c485cfc5e 100644
--- a/examples/translation/translator.py
+++ b/examples/translation/translator.py
@@ -24,7 +24,12 @@ from camel.configs import ChatGPTConfig
from camel.generators import SystemMessageGenerator
from camel.messages import BaseMessage
from camel.models import ModelFactory
-from camel.types import ModelPlatformType, ModelType, RoleType, TaskType
+from camel.types import (
+ ModelPlatformType,
+ ModelType,
+ RoleType,
+ TaskType,
+)
warnings.filterwarnings("ignore")
diff --git a/examples/vision/image_crafting.py b/examples/vision/image_crafting.py
index 1a3c3df48..7ebe2f4fe 100644
--- a/examples/vision/image_crafting.py
+++ b/examples/vision/image_crafting.py
@@ -12,7 +12,6 @@
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from camel.agents.chat_agent import ChatAgent
-from camel.configs import ChatGPTConfig
from camel.messages.base import BaseMessage
from camel.models import ModelFactory
from camel.prompts import PromptTemplateGenerator
@@ -46,7 +45,6 @@ def main():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
dalle_agent = ChatAgent(
diff --git a/examples/vision/multi_condition_image_crafting.py b/examples/vision/multi_condition_image_crafting.py
index 89ef9885f..3b5b3d51e 100644
--- a/examples/vision/multi_condition_image_crafting.py
+++ b/examples/vision/multi_condition_image_crafting.py
@@ -14,7 +14,6 @@
from PIL import Image
from camel.agents.chat_agent import ChatAgent
-from camel.configs import ChatGPTConfig
from camel.generators import PromptTemplateGenerator
from camel.messages.base import BaseMessage
from camel.models import ModelFactory
@@ -43,7 +42,6 @@ def main(image_paths: list[str]) -> list[str]:
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
dalle_agent = ChatAgent(
diff --git a/examples/vision/multi_turn_image_refining.py b/examples/vision/multi_turn_image_refining.py
index 4a9ef1c2f..c2853206d 100644
--- a/examples/vision/multi_turn_image_refining.py
+++ b/examples/vision/multi_turn_image_refining.py
@@ -18,7 +18,6 @@ from colorama import Fore
from PIL import Image
from camel.agents import ChatAgent
-from camel.configs import ChatGPTConfig
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.prompts import PromptTemplateGenerator
@@ -71,7 +70,6 @@ PROMPT: here is the updated prompt!
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
self.artist = ChatAgent(
diff --git a/examples/vision/object_recognition.py b/examples/vision/object_recognition.py
index 9d74b8cc9..6d9375fc4 100644
--- a/examples/vision/object_recognition.py
+++ b/examples/vision/object_recognition.py
@@ -16,7 +16,6 @@ import argparse
from PIL import Image
from camel.agents import ChatAgent
-from camel.configs.openai_config import ChatGPTConfig
from camel.generators import PromptTemplateGenerator
from camel.messages import BaseMessage
from camel.models import ModelFactory
@@ -54,7 +53,6 @@ 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().as_dict(),
)
agent = ChatAgent(
assistant_sys_msg,
diff --git a/examples/vision/video_description.py b/examples/vision/video_description.py
index 8c250278f..8223608e5 100644
--- a/examples/vision/video_description.py
+++ b/examples/vision/video_description.py
@@ -13,7 +13,6 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from camel.agents import ChatAgent
-from camel.configs.openai_config import ChatGPTConfig
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.prompts.prompt_templates import PromptTemplateGenerator
@@ -32,7 +31,6 @@ sys_msg = BaseMessage.make_assistant_message(
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().as_dict(),
)
# Set agent
diff --git a/examples/workforce/hackathon_judges.py b/examples/workforce/hackathon_judges.py
index 8aeb42216..d8f6f5bf6 100644
--- a/examples/workforce/hackathon_judges.py
+++ b/examples/workforce/hackathon_judges.py
@@ -14,7 +14,6 @@
import textwrap
from camel.agents import ChatAgent
-from camel.configs import ChatGPTConfig
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.tasks import Task
@@ -48,7 +47,6 @@ def make_judge(
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().as_dict(),
)
agent = ChatAgent(
@@ -83,7 +81,6 @@ def main():
researcher_model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().as_dict(),
)
researcher_agent = ChatAgent(
diff --git a/examples/workforce/multiple_single_agents.py b/examples/workforce/multiple_single_agents.py
index d18b50481..c2f4c2d1a 100644
--- a/examples/workforce/multiple_single_agents.py
+++ b/examples/workforce/multiple_single_agents.py
@@ -13,7 +13,6 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from camel.agents.chat_agent import ChatAgent
-from camel.configs.openai_config import ChatGPTConfig
from camel.messages.base import BaseMessage
from camel.models import ModelFactory
from camel.tasks.task import Task
@@ -38,7 +37,6 @@ def main():
search_agent_model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().as_dict(),
)
search_agent = ChatAgent(
system_message=BaseMessage.make_assistant_message(
@@ -53,7 +51,6 @@ def main():
tour_guide_agent_model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().as_dict(),
)
tour_guide_agent = ChatAgent(
@@ -74,7 +71,6 @@ def main():
model=ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().as_dict(),
),
)
diff --git a/examples/workforce/role_playing_with_agents.py b/examples/workforce/role_playing_with_agents.py
index 858860a69..2dcd2dab9 100644
--- a/examples/workforce/role_playing_with_agents.py
+++ b/examples/workforce/role_playing_with_agents.py
@@ -13,7 +13,6 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from camel.agents.chat_agent import ChatAgent
-from camel.configs.openai_config import ChatGPTConfig
from camel.messages.base import BaseMessage
from camel.models import ModelFactory
from camel.tasks.task import Task
@@ -43,14 +42,13 @@ def main():
]
model_platform = ModelPlatformType.OPENAI
- model_type = ModelType.GPT_3_5_TURBO
+ model_type = ModelType.GPT_4O_MINI
assistant_role_name = "Searcher"
user_role_name = "Professor"
assistant_agent_kwargs = dict(
model=ModelFactory.create(
model_platform=model_platform,
model_type=model_type,
- model_config_dict=ChatGPTConfig().as_dict(),
),
tools=function_list,
)
@@ -58,7 +56,6 @@ def main():
model=ModelFactory.create(
model_platform=model_platform,
model_type=model_type,
- model_config_dict=ChatGPTConfig().as_dict(),
),
)
diff --git a/poetry.lock b/poetry.lock
index 1f72a4ffb..14e2d172f 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -402,12 +402,12 @@ tests = ["mypy (>=0.800)", "pytest", "pytest-asyncio"]
[[package]]
name = "asknews"
-version = "0.7.47"
+version = "0.7.48"
description = "Python SDK for AskNews"
optional = true
python-versions = "<4.0,>=3.8.1"
files = [
- {file = "asknews-0.7.47-py3-none-any.whl", hash = "sha256:f11569bd7f488a9097e20996d5bb4f7f240a684fd928d59db4fe4459903b5bbb"},
+ {file = "asknews-0.7.48-py3-none-any.whl", hash = "sha256:ef53cd21f93e086a31938bf4e50b7d6c117ca64fc5fc0829b6c713a72a7991c4"},
]
[package.dependencies]
@@ -1521,13 +1521,13 @@ files = [
[[package]]
name = "duckduckgo-search"
-version = "6.3.1"
+version = "6.3.2"
description = "Search for words, documents, images, news, maps and text translation using the DuckDuckGo.com search engine."
optional = true
python-versions = ">=3.8"
files = [
- {file = "duckduckgo_search-6.3.1-py3-none-any.whl", hash = "sha256:408fbfe07ae084eca5d0b5ebd0234187362d9e507ca1549f264104ce13006b58"},
- {file = "duckduckgo_search-6.3.1.tar.gz", hash = "sha256:f43c7fa61518537bb5327aa9411520b04baa7c32b199d816b97d38f451f71824"},
+ {file = "duckduckgo_search-6.3.2-py3-none-any.whl", hash = "sha256:cd631275292460d590d1d496995d002bf2fe6db9752713fab17b9e95924ced98"},
+ {file = "duckduckgo_search-6.3.2.tar.gz", hash = "sha256:53dbf45f8749bfc67483eb9f281f2e722a5fe644d61c54ed9e551d26cb6bcbf2"},
]
[package.dependencies]
diff --git a/test/agents/test_babyagi_playing.py b/test/agents/test_babyagi_playing.py
index 534bf5150..e42c926a3 100644
--- a/test/agents/test_babyagi_playing.py
+++ b/test/agents/test_babyagi_playing.py
@@ -17,7 +17,12 @@ from camel.agents import ChatAgent, TaskCreationAgent, TaskPrioritizationAgent
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.societies import BabyAGI
-from camel.types import ModelPlatformType, ModelType, RoleType, TaskType
+from camel.types import (
+ ModelPlatformType,
+ ModelType,
+ RoleType,
+ TaskType,
+)
parametrize = pytest.mark.parametrize(
'model',
@@ -25,7 +30,6 @@ parametrize = pytest.mark.parametrize(
ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.STUB,
- model_config_dict={},
),
pytest.param(None, marks=pytest.mark.model_backend),
],
diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.py
index 3ca51fbc7..a9f66298e 100644
--- a/test/agents/test_chat_agent.py
+++ b/test/agents/test_chat_agent.py
@@ -44,6 +44,7 @@ from camel.types import (
OpenAIBackendRole,
RoleType,
TaskType,
+ UnifiedModelType,
)
from camel.utils.async_func import sync_funcs_to_async
@@ -53,7 +54,6 @@ parametrize = pytest.mark.parametrize(
ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
),
pytest.param(None, marks=pytest.mark.model_backend),
],
@@ -76,7 +76,8 @@ def test_chat_agent(model):
"ChatAgent(doctor, " f"RoleType.ASSISTANT, {ModelType.GPT_4O_MINI})"
)
assert str(assistant_without_sys_msg) == (
- "ChatAgent(assistant, " f"RoleType.ASSISTANT, {ModelType.GPT_4O_MINI})"
+ "ChatAgent(assistant, "
+ f"RoleType.ASSISTANT, {UnifiedModelType(ModelType.GPT_4O_MINI)})"
)
for assistant in [assistant_with_sys_msg, assistant_without_sys_msg]:
@@ -476,7 +477,6 @@ def test_function_enabled():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
agent_no_func = ChatAgent(system_message=system_message)
agent_with_funcs = ChatAgent(
@@ -500,7 +500,6 @@ def test_tool_calling_sync():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
agent = ChatAgent(
system_message=system_message,
@@ -545,7 +544,6 @@ async def test_tool_calling_math_async():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
agent = ChatAgent(
system_message=system_message,
@@ -600,7 +598,6 @@ async def test_tool_calling_async():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
agent = ChatAgent(
diff --git a/test/agents/test_role_playing.py b/test/agents/test_role_playing.py
index b6a68a739..4084f9ca8 100644
--- a/test/agents/test_role_playing.py
+++ b/test/agents/test_role_playing.py
@@ -14,18 +14,21 @@
import pytest
from camel.agents import ChatAgent, CriticAgent
-from camel.configs import ChatGPTConfig
from camel.human import Human
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.societies import RolePlaying
from camel.toolkits import MathToolkit
-from camel.types import ModelPlatformType, ModelType, RoleType, TaskType
+from camel.types import (
+ ModelPlatformType,
+ ModelType,
+ RoleType,
+ TaskType,
+)
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O,
- model_config_dict=ChatGPTConfig().as_dict(),
)
@@ -141,7 +144,6 @@ def test_role_playing_with_function():
model = ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_4O_MINI,
- model_config_dict=ChatGPTConfig().as_dict(),
)
role_playing = RolePlaying(
diff --git a/test/agents/test_task_agent.py b/test/agents/test_task_agent.py
index a82f77cd7..9bb9491b7 100644
--- a/test/agents/test_task_agent.py
+++ b/test/agents/test_task_agent.py
@@ -28,7 +28,6 @@ parametrize = pytest.mark.parametrize(
ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.STUB,
- model_config_dict={},
),
pytest.param(None, marks=pytest.mark.model_backend),
],
diff --git a/test/memories/test_chat_history_memory.py b/test/memories/test_chat_history_memory.py
index 0a16f4bc1..4b452241a 100644
--- a/test/memories/test_chat_history_memory.py
+++ b/test/memories/test_chat_history_memory.py
@@ -31,7 +31,8 @@ from camel.utils.token_counting import OpenAITokenCounter
@pytest.fixture
def memory(request):
context_creator = ScoreBasedContextCreator(
- OpenAITokenCounter(ModelType.GPT_4), ModelType.GPT_4.token_limit
+ OpenAITokenCounter(ModelType.GPT_4),
+ ModelType.GPT_4.token_limit,
)
if request.param == "in-memory":
yield ChatHistoryMemory(
diff --git a/test/models/test_anthropic_model.py b/test/models/test_anthropic_model.py
index 861127a2d..c02f115a6 100644
--- a/test/models/test_anthropic_model.py
+++ b/test/models/test_anthropic_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import AnthropicConfig, OpenSourceConfig
+from camel.configs import AnthropicConfig
from camel.models import AnthropicModel
from camel.types import ModelType
from camel.utils import AnthropicTokenCounter
@@ -35,10 +35,9 @@ from camel.utils import AnthropicTokenCounter
],
)
def test_anthropic_model(model_type: ModelType):
- model_config_dict = AnthropicConfig().as_dict()
- model = AnthropicModel(model_type, model_config_dict)
+ model = AnthropicModel(model_type)
assert model.model_type == model_type
- assert model.model_config_dict == model_config_dict
+ assert model.model_config_dict == AnthropicConfig().as_dict()
assert isinstance(model.token_counter, AnthropicTokenCounter)
assert isinstance(model.model_type.value_for_tiktoken, str)
assert isinstance(model.model_type.token_limit, int)
@@ -47,11 +46,7 @@ def test_anthropic_model(model_type: ModelType):
@pytest.mark.model_backend
def test_anthropic_model_unexpected_argument():
model_type = ModelType.CLAUDE_2_0
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
diff --git a/test/models/test_azure_openai_model.py b/test/models/test_azure_openai_model.py
index e137790f9..f014e5cbb 100644
--- a/test/models/test_azure_openai_model.py
+++ b/test/models/test_azure_openai_model.py
@@ -13,7 +13,7 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
"""
please set the below os environment:
-export AZURE_OPENAI_ENDPOINT=""
+export AZURE_OPENAI_BASE_URL=""
# if `AZURE_API_VERSION` is not set, `OPENAI_API_VERSION` will be used as api version
export AZURE_API_VERSION=""
@@ -25,7 +25,7 @@ import re
import pytest
-from camel.configs import ChatGPTConfig, OpenSourceConfig
+from camel.configs import ChatGPTConfig
from camel.models import AzureOpenAIModel, ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.utils import OpenAITokenCounter
@@ -61,7 +61,7 @@ def test_openai_model(model_type):
ModelType.GPT_4O,
],
)
-def test_openai_model_create(model_type):
+def test_openai_model_create(model_type: ModelType):
model = ModelFactory.create(
model_platform=ModelPlatformType.AZURE,
model_type=model_type,
@@ -73,11 +73,7 @@ def test_openai_model_create(model_type):
@pytest.mark.model_backend
def test_openai_model_unexpected_argument():
model_type = ModelType.GPT_4
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
diff --git a/test/models/test_gemini_model.py b/test/models/test_gemini_model.py
index d56122dd9..97d6f458e 100644
--- a/test/models/test_gemini_model.py
+++ b/test/models/test_gemini_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import GeminiConfig, OpenSourceConfig
+from camel.configs import GeminiConfig
from camel.models import GeminiModel
from camel.types import ModelType
from camel.utils import GeminiTokenCounter
@@ -29,7 +29,7 @@ from camel.utils import GeminiTokenCounter
ModelType.GEMINI_1_5_PRO,
],
)
-def test_gemini_model(model_type):
+def test_gemini_model(model_type: ModelType):
model_config_dict = GeminiConfig().as_dict()
model = GeminiModel(model_type, model_config_dict)
assert model.model_type == model_type
@@ -42,11 +42,7 @@ def test_gemini_model(model_type):
@pytest.mark.model_backend
def test_gemini_model_unexpected_argument():
model_type = ModelType.GEMINI_1_5_FLASH
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
diff --git a/test/models/test_groq_llama3_model.py b/test/models/test_groq_llama3_model.py
index 32a606520..ecc58a459 100644
--- a/test/models/test_groq_llama3_model.py
+++ b/test/models/test_groq_llama3_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import GroqConfig, OpenSourceConfig
+from camel.configs import GroqConfig
from camel.models import GroqModel
from camel.types import ModelType
from camel.utils import OpenAITokenCounter
@@ -29,7 +29,7 @@ from camel.utils import OpenAITokenCounter
ModelType.GROQ_LLAMA_3_70B,
],
)
-def test_groq_llama3_model(model_type):
+def test_groq_llama3_model(model_type: ModelType):
model_config_dict = GroqConfig().as_dict()
model = GroqModel(model_type, model_config_dict)
assert model.model_type == model_type
@@ -44,11 +44,7 @@ def test_groq_llama3_model(model_type):
@pytest.mark.model_backend
def test_groq_llama3_model_unexpected_argument():
model_type = ModelType.GROQ_LLAMA_3_70B
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
diff --git a/test/models/test_litellm_model.py b/test/models/test_litellm_model.py
index 7d7b197d9..3e605a8c5 100644
--- a/test/models/test_litellm_model.py
+++ b/test/models/test_litellm_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import LiteLLMConfig, OpenSourceConfig
+from camel.configs import LiteLLMConfig
from camel.models import LiteLLMModel
from camel.types import ModelType
from camel.utils import LiteLLMTokenCounter
@@ -32,10 +32,9 @@ from camel.utils import LiteLLMTokenCounter
],
)
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
+ model = LiteLLMModel(model_type)
+ assert model.model_type == model_type
+ assert model.model_config_dict == LiteLLMConfig().as_dict()
assert isinstance(model.token_counter, LiteLLMTokenCounter)
assert isinstance(model_type.value_for_tiktoken, str)
assert isinstance(model_type.token_limit, int)
@@ -44,11 +43,7 @@ def test_litellm_model(model_type: ModelType):
@pytest.mark.model_backend
def test_litellm_model_unexpected_argument():
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.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
diff --git a/test/models/test_mistral_model.py b/test/models/test_mistral_model.py
index 685b50e3d..74949a103 100644
--- a/test/models/test_mistral_model.py
+++ b/test/models/test_mistral_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import MistralConfig, OpenSourceConfig
+from camel.configs import MistralConfig
from camel.models import MistralModel
from camel.types import ModelType
from camel.utils import OpenAITokenCounter
@@ -48,11 +48,7 @@ def test_mistral_model(model_type):
@pytest.mark.model_backend
def test_mistral_model_unexpected_argument():
model_type = ModelType.MISTRAL_LARGE
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
diff --git a/test/models/test_model_factory.py b/test/models/test_model_factory.py
index 281e8e079..279705f18 100644
--- a/test/models/test_model_factory.py
+++ b/test/models/test_model_factory.py
@@ -18,7 +18,6 @@ from camel.configs import (
ChatGPTConfig,
GeminiConfig,
OllamaConfig,
- OpenSourceConfig,
)
from camel.models import ModelFactory
from camel.models.stub_model import StubTokenCounter
@@ -26,7 +25,6 @@ from camel.types import ModelPlatformType, ModelType
from camel.utils import (
AnthropicTokenCounter,
OpenAITokenCounter,
- OpenSourceTokenCounter,
)
parametrize = pytest.mark.parametrize(
@@ -34,7 +32,6 @@ parametrize = pytest.mark.parametrize(
[
(ModelPlatformType.OPENAI, ModelType.GPT_3_5_TURBO),
(ModelPlatformType.OPENAI, ModelType.GPT_4_TURBO),
- (ModelPlatformType.OPEN_SOURCE, ModelType.STUB),
],
)
@@ -62,7 +59,7 @@ parameterize_token_counter = pytest.mark.parametrize(
# Test Stub model
# Stub model uses StubTokenCounter as default
(
- ModelPlatformType.OPEN_SOURCE,
+ ModelPlatformType.OPENAI_COMPATIBLE_MODEL,
ModelType.STUB,
ChatGPTConfig().as_dict(),
None,
@@ -70,7 +67,7 @@ parameterize_token_counter = pytest.mark.parametrize(
None,
),
(
- ModelPlatformType.OPEN_SOURCE,
+ ModelPlatformType.OPENAI_COMPATIBLE_MODEL,
ModelType.STUB,
ChatGPTConfig().as_dict(),
OpenAITokenCounter(ModelType.GPT_4),
@@ -95,30 +92,7 @@ parameterize_token_counter = pytest.mark.parametrize(
OpenAITokenCounter,
ModelType.GPT_3_5_TURBO,
),
- # Test OpenSource model (take VICUNA as an example)
- (
- ModelPlatformType.OPEN_SOURCE,
- ModelType.VICUNA,
- OpenSourceConfig(
- model_path="lmsys/vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- ).as_dict(),
- None,
- OpenSourceTokenCounter,
- ModelType.VICUNA,
- ),
- (
- ModelPlatformType.OPEN_SOURCE,
- ModelType.VICUNA,
- OpenSourceConfig(
- model_path="lmsys/vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- ).as_dict(),
- OpenAITokenCounter(ModelType.GPT_4),
- OpenAITokenCounter,
- ModelType.GPT_4,
- ),
- # Test OpenSource model (take VICUNA as an example)
+ # Test GEMINI model
(
ModelPlatformType.GEMINI,
ModelType.GEMINI_1_5_FLASH,
diff --git a/test/models/test_model_type.py b/test/models/test_model_type.py
new file mode 100644
index 000000000..b495a9428
--- /dev/null
+++ b/test/models/test_model_type.py
@@ -0,0 +1,29 @@
+# =========== 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 camel.types import ModelType, UnifiedModelType
+
+
+def test_predefined_model():
+ model_type = UnifiedModelType("gpt-4o-mini")
+ assert model_type == ModelType.GPT_4O_MINI
+
+
+def test_duplicated_model_types():
+ model_type_1 = UnifiedModelType("random-open-source")
+ model_type_2 = UnifiedModelType("random-open-source")
+ assert id(model_type_1) == id(model_type_2)
+
+ model_type_3 = UnifiedModelType(ModelType.GPT_4O_MINI)
+ model_type_4 = UnifiedModelType("gpt-4o-mini")
+ assert id(model_type_3) == id(model_type_4)
diff --git a/test/models/test_ollama_model.py b/test/models/test_ollama_model.py
index 7750886a4..5e05edf53 100644
--- a/test/models/test_ollama_model.py
+++ b/test/models/test_ollama_model.py
@@ -17,7 +17,7 @@ import pytest
from camel.configs import OllamaConfig
from camel.models import OllamaModel
-from camel.types import ModelType
+from camel.types import ModelType, UnifiedModelType
from camel.utils import OpenAITokenCounter
@@ -32,12 +32,11 @@ from camel.utils import OpenAITokenCounter
],
)
def test_ollama_model(model_type: ModelType):
- 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
+ model = OllamaModel(model_type)
+ assert model.model_type == model_type
+ assert model.model_config_dict == OllamaConfig().as_dict()
assert isinstance(model.token_counter, OpenAITokenCounter)
- assert isinstance(model.model_type, str)
+ assert isinstance(model.model_type, UnifiedModelType)
assert isinstance(model.token_limit, int)
diff --git a/test/models/test_open_source_model.py b/test/models/test_open_source_model.py
deleted file mode 100644
index 7895f9d53..000000000
--- a/test/models/test_open_source_model.py
+++ /dev/null
@@ -1,213 +0,0 @@
-# =========== 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 re
-
-import pytest
-
-from camel.configs import (
- ChatGPTConfig,
- OpenSourceConfig,
-)
-from camel.models import OpenSourceModel
-from camel.types import ModelType
-from camel.utils import OpenSourceTokenCounter, check_server_running
-
-MODEL_PATH_MAP = {
- ModelType.VICUNA: "lmsys/vicuna-7b-v1.5",
- ModelType.VICUNA_16K: "lmsys/vicuna-7b-v1.5-16k",
-}
-DEFAULT_SERVER_URL = "http://localhost:8000/v1"
-
-
- "model_type",
- [
- ModelType.VICUNA,
- ModelType.VICUNA_16K,
- ],
-)
-def test_open_source_model(model_type):
- model_path = MODEL_PATH_MAP[model_type]
- model_name = model_path.split('/')[-1]
- model_config = OpenSourceConfig(
- model_path=model_path,
- server_url=DEFAULT_SERVER_URL,
- )
- 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.as_dict()
-
- assert isinstance(model.token_counter, OpenSourceTokenCounter)
- assert isinstance(model.model_type.value_for_tiktoken, str)
- assert isinstance(model.model_type.token_limit, int)
-
-
[email protected]("model_type", [ModelType.VICUNA])
- not check_server_running(DEFAULT_SERVER_URL),
- reason="No server running LLM inference is provided.",
-)
-def test_open_source_model_run(model_type):
- model_path = MODEL_PATH_MAP[model_type]
- model_config = OpenSourceConfig(
- model_path=model_path,
- server_url=DEFAULT_SERVER_URL,
- )
- model_config_dict = model_config.as_dict()
- model = OpenSourceModel(model_type, model_config_dict)
-
- messages = [{"role": "user", "content": "Tell me a joke."}]
- response = model.run(messages)
-
- assert isinstance(response, dict)
-
-
-def test_open_source_model_close_source_model_type():
- model_type = ModelType.GPT_4O_MINI
- model_path = MODEL_PATH_MAP[ModelType.VICUNA]
- model_config = OpenSourceConfig(
- model_path=model_path,
- server_url=DEFAULT_SERVER_URL,
- )
- model_config_dict = model_config.as_dict()
-
- with pytest.raises(
- ValueError,
- match=re.escape(
- (
- "Model `ModelType.GPT_4O_MINI` is not a supported"
- " open-source model."
- )
- ),
- ):
- _ = OpenSourceModel(model_type, model_config_dict)
-
-
-def test_open_source_model_mismatched_model_config():
- model_type = ModelType.VICUNA
- model_config = ChatGPTConfig()
- model_config_dict = model_config.as_dict()
-
- with pytest.raises(
- ValueError,
- match=re.escape(
- (
- "Invalid configuration for open-source model backend with "
- ":obj:`model_path` or :obj:`server_url` missing."
- )
- ),
- ):
- _ = OpenSourceModel(model_type, model_config_dict)
-
-
-def test_open_source_model_unexpected_argument():
- model_type = ModelType.VICUNA
- model_path = MODEL_PATH_MAP[ModelType.VICUNA]
- model_config = OpenSourceConfig(
- model_path=model_path,
- server_url=DEFAULT_SERVER_URL,
- api_params=ChatGPTConfig(),
- )
- model_config_dict = model_config.as_dict()
-
- with pytest.raises(
- TypeError,
- match=re.escape(
- ("__init__() got an unexpected keyword argument 'unexpected_arg'")
- ),
- ):
- _ = OpenSourceModel(
- model_type, model_config_dict, unexpected_arg="unexpected_arg"
- )
-
-
-def test_open_source_model_invalid_model_path():
- model_type = ModelType.VICUNA
- model_path = "vicuna-7b-v1.5"
- model_config = OpenSourceConfig(
- model_path=model_path,
- server_url=DEFAULT_SERVER_URL,
- )
- model_config_dict = model_config.as_dict()
-
- with pytest.raises(
- ValueError,
- match=re.escape(
- (
- "Invalid `model_path` (vicuna-7b-v1.5) is provided. "
- "Tokenizer loading failed."
- )
- ),
- ):
- model = OpenSourceModel(model_type, model_config_dict)
- _ = model.token_counter
-
-
-def test_open_source_model_unmatched_model_path():
- model_type = ModelType.LLAMA_2
- model_path = MODEL_PATH_MAP[ModelType.VICUNA]
- model_config = OpenSourceConfig(
- model_path=model_path,
- server_url=DEFAULT_SERVER_URL,
- )
- model_config_dict = model_config.as_dict()
-
- with pytest.raises(
- ValueError,
- match=(
- f"Model name `vicuna-7b-v1.5` does not match model type "
- f"`{model_type.value}`."
- ),
- ):
- _ = OpenSourceModel(model_type, model_config_dict)
-
-
-def test_open_source_model_missing_model_path():
- model_type = ModelType.VICUNA
- model_config = OpenSourceConfig(
- model_path="",
- server_url=DEFAULT_SERVER_URL,
- )
- model_config_dict = model_config.as_dict()
-
- with pytest.raises(
- ValueError, match=("Path to open-source model is not provided.")
- ):
- _ = OpenSourceModel(model_type, model_config_dict)
-
-
-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="")
- model_config_dict = model_config.as_dict()
-
- with pytest.raises(
- ValueError,
- match=("URL to server running open-source LLM is not provided."),
- ):
- _ = OpenSourceModel(model_type, model_config_dict)
diff --git a/test/models/test_openai_model.py b/test/models/test_openai_model.py
index b10c989d7..e85a541e6 100644
--- a/test/models/test_openai_model.py
+++ b/test/models/test_openai_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import ChatGPTConfig, OpenSourceConfig
+from camel.configs import ChatGPTConfig
from camel.models import OpenAIModel
from camel.types import ModelType
from camel.utils import OpenAITokenCounter
@@ -34,11 +34,10 @@ from camel.utils import OpenAITokenCounter
ModelType.O1_MINI,
],
)
-def test_openai_model(model_type):
- model_config_dict = ChatGPTConfig().as_dict()
- model = OpenAIModel(model_type, model_config_dict)
+def test_openai_model(model_type: ModelType):
+ model = OpenAIModel(model_type)
assert model.model_type == model_type
- assert model.model_config_dict == model_config_dict
+ assert model.model_config_dict == ChatGPTConfig().as_dict()
assert isinstance(model.token_counter, OpenAITokenCounter)
assert isinstance(model.model_type.value_for_tiktoken, str)
assert isinstance(model.model_type.token_limit, int)
@@ -47,11 +46,7 @@ def test_openai_model(model_type):
@pytest.mark.model_backend
def test_openai_model_unexpected_argument():
model_type = ModelType.GPT_4
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
diff --git a/test/models/test_reka_model.py b/test/models/test_reka_model.py
index 063af2ec3..6f2570854 100644
--- a/test/models/test_reka_model.py
+++ b/test/models/test_reka_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import OpenSourceConfig, RekaConfig
+from camel.configs import RekaConfig
from camel.models import RekaModel
from camel.types import ModelType
from camel.utils import OpenAITokenCounter
@@ -30,11 +30,10 @@ from camel.utils import OpenAITokenCounter
ModelType.REKA_FLASH,
],
)
-def test_reka_model(model_type):
- model_config_dict = RekaConfig().as_dict()
- model = RekaModel(model_type, model_config_dict)
+def test_reka_model(model_type: ModelType):
+ model = RekaModel(model_type)
assert model.model_type == model_type
- assert model.model_config_dict == model_config_dict
+ assert model.model_config_dict == RekaConfig().as_dict()
assert isinstance(model.token_counter, OpenAITokenCounter)
assert isinstance(model.model_type.value_for_tiktoken, str)
assert isinstance(model.model_type.token_limit, int)
@@ -43,11 +42,7 @@ def test_reka_model(model_type):
@pytest.mark.model_backend
def test_reka_model_unexpected_argument():
model_type = ModelType.REKA_CORE
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
diff --git a/test/models/test_samba_model.py b/test/models/test_samba_model.py
index 7f8ad1880..966492b8d 100644
--- a/test/models/test_samba_model.py
+++ b/test/models/test_samba_model.py
@@ -16,10 +16,10 @@ import re
import pytest
from camel.configs import (
- OpenSourceConfig,
- SambaFastAPIConfig,
+ SambaCloudAPIConfig,
)
from camel.models import SambaModel
+from camel.types import ModelType
from camel.utils import OpenAITokenCounter
@@ -30,29 +30,24 @@ from camel.utils import OpenAITokenCounter
"llama3-70b",
],
)
-def test_samba_model(model_type):
- model_config_dict = SambaFastAPIConfig().as_dict()
- model = SambaModel(model_type, model_config_dict)
+def test_samba_model(model_type: ModelType):
+ model = SambaModel(model_type)
assert model.model_type == model_type
- assert model.model_config_dict == model_config_dict
+ assert model.model_config_dict == SambaCloudAPIConfig().as_dict()
assert isinstance(model.token_counter, OpenAITokenCounter)
@pytest.mark.model_backend
def test_samba_model_unexpected_argument():
model_type = "llama3-70b"
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,
match=re.escape(
(
"Unexpected argument `model_path` is "
- "input into SambaNova Fast API."
+ "input into SambaCloud API."
)
),
):
diff --git a/test/models/test_togetherai_model.py b/test/models/test_togetherai_model.py
index aabdd328f..9970e97f2 100644
--- a/test/models/test_togetherai_model.py
+++ b/test/models/test_togetherai_model.py
@@ -15,6 +15,7 @@ import pytest
from camel.configs import TogetherAIConfig
from camel.models import TogetherAIModel
+from camel.types import ModelType
from camel.utils import OpenAITokenCounter
@@ -25,8 +26,7 @@ from camel.utils import OpenAITokenCounter
"meta-llama/Llama-3-8b-chat-hf",
],
)
-def test_litellm_model(model_type: str):
- model_config_dict = TogetherAIConfig().as_dict()
- model = TogetherAIModel(model_type, model_config_dict)
- assert model.model_config_dict == model_config_dict
+def test_litellm_model(model_type: ModelType):
+ model = TogetherAIModel(model_type)
+ assert model.model_config_dict == TogetherAIConfig().as_dict()
assert isinstance(model.token_counter, OpenAITokenCounter)
diff --git a/test/models/test_vllm_model.py b/test/models/test_vllm_model.py
index f4dbc0485..9933d7125 100644
--- a/test/models/test_vllm_model.py
+++ b/test/models/test_vllm_model.py
@@ -17,7 +17,7 @@ import pytest
from camel.configs import VLLMConfig
from camel.models import VLLMModel
-from camel.types import ModelType
+from camel.types import ModelType, UnifiedModelType
from camel.utils import OpenAITokenCounter
@@ -32,12 +32,11 @@ from camel.utils import OpenAITokenCounter
],
)
def test_vllm_model(model_type: ModelType):
- 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
+ model = VLLMModel(model_type, api_key="vllm")
+ assert model.model_type == model_type
+ assert model.model_config_dict == VLLMConfig().as_dict()
assert isinstance(model.token_counter, OpenAITokenCounter)
- assert isinstance(model.model_type, str)
+ assert isinstance(model.model_type, UnifiedModelType)
assert isinstance(model.token_limit, int)
diff --git a/test/models/test_zhipuai_model.py b/test/models/test_zhipuai_model.py
index a69f11eb6..5f09be85a 100644
--- a/test/models/test_zhipuai_model.py
+++ b/test/models/test_zhipuai_model.py
@@ -15,7 +15,7 @@ import re
import pytest
-from camel.configs import OpenSourceConfig, ZhipuAIConfig
+from camel.configs import ZhipuAIConfig
from camel.models import ZhipuAIModel
from camel.types import ModelType
from camel.utils import OpenAITokenCounter
@@ -30,11 +30,10 @@ from camel.utils import OpenAITokenCounter
ModelType.GLM_4V,
],
)
-def test_zhipuai_model(model_type):
- model_config_dict = ZhipuAIConfig().as_dict()
- model = ZhipuAIModel(model_type, model_config_dict)
+def test_zhipuai_model(model_type: ModelType):
+ model = ZhipuAIModel(model_type)
assert model.model_type == model_type
- assert model.model_config_dict == model_config_dict
+ assert model.model_config_dict == ZhipuAIConfig().as_dict()
assert isinstance(model.token_counter, OpenAITokenCounter)
assert isinstance(model.model_type.value_for_tiktoken, str)
assert isinstance(model.model_type.token_limit, int)
@@ -43,11 +42,7 @@ def test_zhipuai_model(model_type):
@pytest.mark.model_backend
def test_zhipuai_model_unexpected_argument():
model_type = ModelType.GLM_4V
- model_config = OpenSourceConfig(
- model_path="vicuna-7b-v1.5",
- server_url="http://localhost:8000/v1",
- )
- model_config_dict = model_config.as_dict()
+ model_config_dict = {"model_path": "vicuna-7b-v1.5"}
with pytest.raises(
ValueError,