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

added gpt-4 and gpt-4-32k support (#58)

Christopher T Clark9f1e9eb8d8c8
diff --git a/.github/workflows/linting.yaml b/.github/workflows/linting.yaml
index b80cbe0d8..f73c8f5c8 100644
--- a/.github/workflows/linting.yaml
+++ b/.github/workflows/linting.yaml
@@ -44,7 +44,7 @@ jobs:
- name: Install dependencies
run: |
- pip install isort
+ pip install isort==5.12.0
- name: Run linting
run: |
diff --git a/camel/agent/chat_agent.py b/camel/agent/chat_agent.py
index a04176c12..5c349b2d4 100644
--- a/camel/agent/chat_agent.py
+++ b/camel/agent/chat_agent.py
@@ -14,7 +14,7 @@ class ChatAgent:
def __init__(
self,
system_message: SystemMessage,
- model: ModeType,
+ model: ModeType = ModeType.GPT_3_5_TURBO,
model_config: Any = None,
message_window_size: Optional[int] = None,
) -> None:
diff --git a/camel/agent/role_playing.py b/camel/agent/role_playing.py
index a94b513bd..bf72bcef5 100644
--- a/camel/agent/role_playing.py
+++ b/camel/agent/role_playing.py
@@ -28,10 +28,11 @@ class RolePlaying:
) -> None:
self.with_task_specify = with_task_specify
self.with_task_planner = with_task_planner
+ self.mode_type = mode_type
if with_task_specify:
task_specify_agent = TaskSpecifyAgent(
- ModeType.GPT_3_5_TURBO,
+ self.mode_type,
task_type=task_type,
**(task_specify_agent_kwargs or {}),
)
@@ -46,7 +47,7 @@ class RolePlaying:
if with_task_planner:
task_planner_agent = TaskPlannerAgent(
- ModeType.GPT_3_5_TURBO,
+ self.mode_type,
**(task_planner_agent_kwargs or {}),
)
self.planned_task_prompt = task_planner_agent.plan_task(
diff --git a/camel/agent/task_agent.py b/camel/agent/task_agent.py
index 51cf79dfe..5f6632bd4 100644
--- a/camel/agent/task_agent.py
+++ b/camel/agent/task_agent.py
@@ -10,7 +10,7 @@ class TaskSpecifyAgent(ChatAgent):
def __init__(
self,
- model: ModeType,
+ model: ModeType = ModeType.GPT_3_5_TURBO,
task_type: TaskType = TaskType.AI_SOCIETY,
model_config: Any = None,
task_specify_prompt: Optional[str] = None,
@@ -70,7 +70,7 @@ class TaskPlannerAgent(ChatAgent):
def __init__(
self,
- model: ModeType,
+ model: ModeType = ModeType.GPT_3_5_TURBO,
model_config: Any = None,
) -> None:
diff --git a/camel/typing.py b/camel/typing.py
index 82142306f..c67450c5c 100644
--- a/camel/typing.py
+++ b/camel/typing.py
@@ -9,6 +9,8 @@ class RoleType(Enum):
class ModeType(Enum):
GPT_3_5_TURBO = "gpt-3.5-turbo"
+ GPT_4 = "gpt-4"
+ GPT_4_32k = "gpt-4-32k"
class TaskType(Enum):
diff --git a/camel/utils.py b/camel/utils.py
index 258989785..f102cbcc9 100644
--- a/camel/utils.py
+++ b/camel/utils.py
@@ -1,4 +1,4 @@
-from typing import List
+from typing import Any, List
import tiktoken
@@ -6,32 +6,53 @@ from camel.message import OpenAIMessage
from camel.typing import ModeType
-def num_tokens_from_messages(messages: List[OpenAIMessage], model: ModeType):
+def count_tokens_openai_chat_models(
+ messages: List[OpenAIMessage],
+ encoding: Any,
+) -> int:
+ num_tokens = 0
+ for message in messages:
+ # message follows <im_start>{role/name}\n{content}<im_end>\n
+ num_tokens += 4
+ for key, value in message.items():
+ num_tokens += len(encoding.encode(value))
+ if key == "name": # if there's a name, the role is omitted
+ num_tokens += -1 # role is always 1 token
+ num_tokens += 2 # every reply is primed with <im_start>assistant
+ return num_tokens
+
+
+def num_tokens_from_messages(
+ messages: List[OpenAIMessage],
+ model: ModeType,
+) -> int:
"""Returns the number of tokens used by a list of messages."""
try:
encoding = tiktoken.encoding_for_model(model.value)
except KeyError:
encoding = tiktoken.get_encoding("cl100k_base")
if model == ModeType.GPT_3_5_TURBO:
- num_tokens = 0
- for message in messages:
- # message follows <im_start>{role/name}\n{content}<im_end>\n
- num_tokens += 4
- for key, value in message.items():
- num_tokens += len(encoding.encode(value))
- if key == "name": # if there's a name, the role is omitted
- num_tokens += -1 # role is always 1 token
- num_tokens += 2 # every reply is primed with <im_start>assistant
- return num_tokens
+ return count_tokens_openai_chat_models(messages, encoding)
+ elif model == ModeType.GPT_4:
+ return count_tokens_openai_chat_models(messages, encoding)
+ elif model == ModeType.GPT_4_32k:
+ return count_tokens_openai_chat_models(messages, encoding)
else:
raise NotImplementedError(
f"`num_tokens_from_messages`` is not presently implemented "
f"for model {model}. "
f"See https://github.com/openai/openai-python/blob/main/chatml.md "
- f"for information on how messages are converted to tokens.")
+ f"for information on how messages are converted to tokens. "
+ f"See https://platform.openai.com/docs/models/gpt-4"
+ f"or https://platform.openai.com/docs/models/gpt-3-5"
+ f"for information about openai chat models.")
def get_model_token_limit(model: ModeType) -> int:
"""Returns the maximum number of tokens for a given model."""
if model == ModeType.GPT_3_5_TURBO:
return 4096
+ if model == ModeType.GPT_4:
+ return 8192
+ if model == ModeType.GPT_4_32k:
+ return 32768
diff --git a/examples/ai_society/role_generation.py b/examples/ai_society/role_generation.py
index adc6afdaf..c851ee79a 100644
--- a/examples/ai_society/role_generation.py
+++ b/examples/ai_society/role_generation.py
@@ -1,6 +1,5 @@
from camel.agent import ChatAgent
from camel.message import AssistantSystemMessage, UserChatMessage
-from camel.typing import ModeType
def main(role_type: str = "user", num_roles: int = 50):
@@ -11,7 +10,7 @@ def main(role_type: str = "user", num_roles: int = 50):
role_name="Assistant",
content="You are a helpful assistant.",
)
- agent = ChatAgent(assistant_sys_msg, model=ModeType.GPT_3_5_TURBO)
+ agent = ChatAgent(assistant_sys_msg)
agent.reset()
user_msg = UserChatMessage(
diff --git a/examples/ai_society/task_generation.py b/examples/ai_society/task_generation.py
index f8ad690ac..6e1ba63e9 100644
--- a/examples/ai_society/task_generation.py
+++ b/examples/ai_society/task_generation.py
@@ -7,7 +7,7 @@ from camel.generator import (
RoleNameGenerator,
SystemMessageGenerator,
)
-from camel.typing import ModeType, RoleType, TaskType
+from camel.typing import RoleType, TaskType
def generate_tasks(role_names: str, task_generator_prompt: str,
@@ -16,7 +16,7 @@ def generate_tasks(role_names: str, task_generator_prompt: str,
assistant_sys_msg = sys_msg_generator.from_dict(
dict(), role_tuple=("Task Generator", RoleType.DEFAULT))
- assistant_agent = ChatAgent(assistant_sys_msg, ModeType.GPT_3_5_TURBO)
+ assistant_agent = ChatAgent(assistant_sys_msg)
assistant_sys_msg.content = task_generator_prompt
user_msgs, _, _ = assistant_agent.step(assistant_sys_msg)
diff --git a/examples/code/generate_meta_data.py b/examples/code/generate_meta_data.py
index 0acdfc904..cd4df8b48 100644
--- a/examples/code/generate_meta_data.py
+++ b/examples/code/generate_meta_data.py
@@ -1,6 +1,5 @@
from camel.agent import ChatAgent
from camel.message import AssistantSystemMessage, UserChatMessage
-from camel.typing import ModeType
def generate_meta_data(meta_data: str, num: int = 50):
@@ -11,7 +10,7 @@ def generate_meta_data(meta_data: str, num: int = 50):
role_name="Assistant",
content="You are a helpful assistant.",
)
- agent = ChatAgent(assistant_sys_msg, model=ModeType.GPT_3_5_TURBO)
+ agent = ChatAgent(assistant_sys_msg)
agent.reset()
user_msg = UserChatMessage(
diff --git a/examples/code/role_playing_multiprocess.py b/examples/code/role_playing_multiprocess.py
index 3b11ce061..aeb7327b2 100644
--- a/examples/code/role_playing_multiprocess.py
+++ b/examples/code/role_playing_multiprocess.py
@@ -11,7 +11,7 @@ from camel.message import (
UserChatMessage,
UserSystemMessage,
)
-from camel.typing import ModeType, RoleType, TaskType
+from camel.typing import RoleType, TaskType
def init_chat(
@@ -47,7 +47,6 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
original_task_prompt = task_prompt.replace(f"{task_idx+1}. ", "")
task_specify_agent = TaskSpecifyAgent(
- ModeType.GPT_3_5_TURBO,
task_type=TaskType.CODE,
model_config=ChatGPTConfig(temperature=1.4),
)
@@ -74,10 +73,9 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
],
)
- assistant_agent = ChatAgent(assistant_sys_msg, ModeType.GPT_3_5_TURBO,
+ assistant_agent = ChatAgent(assistant_sys_msg,
message_window_size=max_num_messages)
- user_agent = ChatAgent(user_sys_msg, ModeType.GPT_3_5_TURBO,
- message_window_size=max_num_messages)
+ user_agent = ChatAgent(user_sys_msg, message_window_size=max_num_messages)
assistant_msg, _ = init_chat(assistant_agent, user_agent, user_sys_msg,
assistant_sys_msg)
diff --git a/examples/code/task_generation.py b/examples/code/task_generation.py
index c92798b2e..cd41532a5 100644
--- a/examples/code/task_generation.py
+++ b/examples/code/task_generation.py
@@ -3,7 +3,7 @@ import os
from camel.agent import ChatAgent
from camel.generator import CodeTaskPromptGenerator, SystemMessageGenerator
-from camel.typing import ModeType, RoleType, TaskType
+from camel.typing import RoleType, TaskType
def generate_tasks(task_generator_prompt: str, language: str, domain: str,
@@ -11,7 +11,7 @@ def generate_tasks(task_generator_prompt: str, language: str, domain: str,
sys_msg_generator = SystemMessageGenerator(task_type=TaskType.DEFAULT)
assistant_sys_msg = sys_msg_generator.from_dict(
dict(), role_tuple=("Task Generator", RoleType.DEFAULT))
- assistant_agent = ChatAgent(assistant_sys_msg, ModeType.GPT_3_5_TURBO)
+ assistant_agent = ChatAgent(assistant_sys_msg)
assistant_sys_msg.content = task_generator_prompt
user_msgs, _, _ = assistant_agent.step(assistant_sys_msg)
diff --git a/examples/misalignment/single_agent.py b/examples/misalignment/single_agent.py
index 6095b9303..26e9eff52 100644
--- a/examples/misalignment/single_agent.py
+++ b/examples/misalignment/single_agent.py
@@ -1,6 +1,5 @@
from camel.agent import ChatAgent
from camel.message import AssistantSystemMessage, UserChatMessage
-from camel.typing import ModeType
def main() -> None:
@@ -12,7 +11,7 @@ def main() -> None:
role_name="Assistant",
content=sys_prompt,
)
- agent = ChatAgent(assistant_sys_msg, model=ModeType.GPT_3_5_TURBO)
+ agent = ChatAgent(assistant_sys_msg)
agent.reset()
user_msg = UserChatMessage(role_name="User", content=prompt)
diff --git a/examples/single_agent.py b/examples/single_agent.py
index 13eaa6363..9e0f8814d 100644
--- a/examples/single_agent.py
+++ b/examples/single_agent.py
@@ -1,6 +1,5 @@
from camel.agent import ChatAgent
from camel.message import AssistantSystemMessage, UserChatMessage
-from camel.typing import ModeType
def main(role_type: str = "user", num_roles: int = 50):
@@ -11,7 +10,7 @@ def main(role_type: str = "user", num_roles: int = 50):
role_name="Assistant",
content="You are a helpful assistant.",
)
- agent = ChatAgent(assistant_sys_msg, model=ModeType.GPT_3_5_TURBO)
+ agent = ChatAgent(assistant_sys_msg)
agent.reset()
user_msg = UserChatMessage(role_name="User", content=prompt)
diff --git a/test/agent/test_chat_agent.py b/test/agent/test_chat_agent.py
index af613bbfc..974fb0a2a 100644
--- a/test/agent/test_chat_agent.py
+++ b/test/agent/test_chat_agent.py
@@ -1,29 +1,31 @@
import os
+import pytest
+
from camel.agent import ChatAgent
from camel.configs import ChatGPTConfig
from camel.generator import SystemMessageGenerator
from camel.message import ChatMessage
from camel.typing import ModeType, RoleType, TaskType
+from camel.utils import get_model_token_limit
assert os.environ.get("OPENAI_API_KEY") is not None, "Missing OPENAI_API_KEY"
-def test_chat_agent():
- chat_gpt_args = ChatGPTConfig()
+ 'model', [ModeType.GPT_3_5_TURBO, ModeType.GPT_4, ModeType.GPT_4_32k])
+def test_chat_agent(model):
+ model_config = ChatGPTConfig()
system_message = SystemMessageGenerator(
task_type=TaskType.AI_SOCIETY).from_dict(
{"<ASSISTANT_ROLE>": "doctor"},
role_tuple=("doctor", RoleType.ASSISTANT),
)
- assistant = ChatAgent(
- system_message,
- ModeType.GPT_3_5_TURBO,
- chat_gpt_args,
- )
+ assistant = ChatAgent(system_message, model=model,
+ model_config=model_config)
assert str(assistant) == ("ChatAgent(doctor, "
- "RoleType.ASSISTANT, ModeType.GPT_3_5_TURBO)")
+ f"RoleType.ASSISTANT, {str(model)})")
assistant.reset()
user_msg = ChatMessage(role_name="Patient", role_type=RoleType.USER,
@@ -35,9 +37,10 @@ def test_chat_agent():
assert info['id'] is not None
assistant.reset()
+ token_limit = get_model_token_limit(model)
user_msg = ChatMessage(role_name="Patient", role_type=RoleType.USER,
meta_dict=dict(), role="user",
- content="Hello!" * 4096)
+ content="token" * (token_limit + 1))
messages, terminated, info = assistant.step(user_msg)
assert terminated is True
diff --git a/test/agent/test_task_agent.py b/test/agent/test_task_agent.py
index 59e7a05bf..e3a9aad5e 100644
--- a/test/agent/test_task_agent.py
+++ b/test/agent/test_task_agent.py
@@ -2,7 +2,7 @@ import os
from camel.agent import TaskPlannerAgent, TaskSpecifyAgent
from camel.configs import ChatGPTConfig
-from camel.typing import ModeType, TaskType
+from camel.typing import TaskType
assert os.environ.get("OPENAI_API_KEY") is not None, "Missing OPENAI_API_KEY"
@@ -10,8 +10,8 @@ assert os.environ.get("OPENAI_API_KEY") is not None, "Missing OPENAI_API_KEY"
def test_task_specify_ai_society_agent():
original_task_prompt = "Improving stage presence and performance skills"
print(f"Original task prompt:\n{original_task_prompt}\n")
- task_specify_agent = TaskSpecifyAgent(
- ModeType.GPT_3_5_TURBO, model_config=ChatGPTConfig(temperature=1.0))
+ task_specify_agent = TaskSpecifyAgent(model_config=ChatGPTConfig(
+ temperature=1.0))
specified_task_prompt = task_specify_agent.specify_task(
original_task_prompt, [
("<ASSISTANT_ROLE>", "Musician"),
@@ -24,7 +24,6 @@ def test_task_specify_code_agent():
original_task_prompt = "Modeling molecular dynamics"
print(f"Original task prompt:\n{original_task_prompt}\n")
task_specify_agent = TaskSpecifyAgent(
- ModeType.GPT_3_5_TURBO,
task_type=TaskType.CODE,
model_config=ChatGPTConfig(temperature=1.0),
)
@@ -40,7 +39,6 @@ def test_task_planner_agent():
original_task_prompt = "Modeling molecular dynamics"
print(f"Original task prompt:\n{original_task_prompt}\n")
task_specify_agent = TaskSpecifyAgent(
- ModeType.GPT_3_5_TURBO,
task_type=TaskType.CODE,
model_config=ChatGPTConfig(temperature=1.0),
)
@@ -50,7 +48,7 @@ def test_task_planner_agent():
("<LANGUAGE>", "Python"),
])
print(f"Specified task prompt:\n{specified_task_prompt}\n")
- task_planner_agent = TaskPlannerAgent(
- ModeType.GPT_3_5_TURBO, model_config=ChatGPTConfig(temperature=1.0))
+ task_planner_agent = TaskPlannerAgent(model_config=ChatGPTConfig(
+ temperature=1.0))
planned_task_prompt = task_planner_agent.plan_task(specified_task_prompt)
print(f"Planned task prompt:\n{planned_task_prompt}\n")