Skip to content

Commit

Permalink
Add BaseAgent, EmbodiedAgent, BaseToolAgent, `HuggingFaceToolAg…
Browse files Browse the repository at this point in the history
…ent` and full and fast test mode (#123)
  • Loading branch information
lightaime committed Jun 4, 2023
1 parent 3c15afa commit 86a978e
Show file tree
Hide file tree
Showing 36 changed files with 974 additions and 101 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ jobs:
pip install sphinx sphinx_book_theme recommonmark numpy openai tenacity tiktoken colorama
- name: Sphinx build
run: |
sphinx-build docs docs/_build
cd docs
make html
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master'}}
Expand Down
58 changes: 57 additions & 1 deletion .github/workflows/pytest_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ permissions:
contents: read

jobs:
pytest_package:
pytest_package_test:

runs-on: ubuntu-latest

Expand All @@ -40,3 +40,59 @@ jobs:
OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"
run: |
pytest test/
pytest_package_full_test:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v3
with:
python-version: "3.8"
- uses: actions/cache@v3
id: cache
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.*') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install -e .
- name: Test with pytest
env:
OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"
run: |
pytest --full-test-mode test/
pytest_package_fast_test:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v3
with:
python-version: "3.8"
- uses: actions/cache@v3
id: cache
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.*') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install -e .
- name: Test with pytest
env:
OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"
run: |
pytest --fast-test-mode test/
16 changes: 14 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ We kindly request that you provide comprehensive documentation for all classes a
To build the documentation locally, follow these steps:

```bash
sphinx-build docs docs/_build
cd docs
make html
```

More guidelines about building and hosting documentations locally can be found [here](https://github.com/camel-ai/camel/blob/master/docs/README.md).
Expand All @@ -130,7 +131,18 @@ As of now, CAMEL is actively in development and not published to PyPI yet.

CAMEL follows the [semver](https://semver.org/) versioning standard. As pre-1.0 software, even patch releases may contain [non-backwards-compatible changes](https://semver.org/#spec-item-4). Currently, the major version is 0, and the minor version is incremented. Releases are made once the maintainers feel that a significant body of changes has accumulated.

### Giving Credit 🎉

## License 📜

The source code of the CAMEL project is licensed under Apache 2.0. Your contributed code will be also licensed under Apache 2.0 by default. To add license to you code, you can manually copy-paste it from `license_template.txt` to the head of your files or run the `update_license.py` script to automate the process:

```bash
python licenses/update_license.py . licenses/license_template.txt
```

This script will add licenses to all the `*.py` files or update the licenses if the existing licenses are not the same as `license_template.txt`.

## Giving Credit 🎉

If your contribution has been included in a release, we'd love to give you credit on Twitter, but only if you're comfortable with it!

Expand Down
8 changes: 8 additions & 0 deletions camel/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from .base import BaseAgent
from .chat_agent import ChatAgent
from .task_agent import TaskPlannerAgent, TaskSpecifyAgent
from .critic_agent import CriticAgent
from .tool_agents.base import BaseToolAgent
from .tool_agents.hugging_face_tool_agent import HuggingFaceToolAgent
from .embodied_agent import EmbodiedAgent
from .role_playing import RolePlaying

__all__ = [
'BaseAgent',
'ChatAgent',
'TaskSpecifyAgent',
'TaskPlannerAgent',
'CriticAgent',
'BaseToolAgent',
'HuggingFaceToolAgent',
'EmbodiedAgent',
'RolePlaying',
]
28 changes: 28 additions & 0 deletions camel/agents/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from abc import ABC, abstractmethod


class BaseAgent(ABC):
r"""An abstract base class for all CAMEL agents."""

@abstractmethod
def reset(self) -> None:
r"""Resets the agent to its initial state."""
pass

@abstractmethod
def step(self) -> None:
r"""Performs a single step of the agent."""
pass
12 changes: 9 additions & 3 deletions camel/agents/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
import openai
from tenacity import retry, stop_after_attempt, wait_exponential

from camel.agents import BaseAgent
from camel.configs import ChatGPTConfig
from camel.messages import ChatMessage, MessageType, SystemMessage
from camel.typing import ModelType
from camel.utils import get_model_token_limit, num_tokens_from_messages
from camel.utils import (
get_model_token_limit,
num_tokens_from_messages,
openai_api_key_required,
)


class ChatAgent:
class ChatAgent(BaseAgent):
r"""Class for managing conversations of CAMEL Chat Agents.
Args:
Expand All @@ -40,7 +45,7 @@ def __init__(
self,
system_message: SystemMessage,
model: ModelType = ModelType.GPT_3_5_TURBO,
model_config: Any = None,
model_config: Optional[Any] = None,
message_window_size: Optional[int] = None,
) -> None:

Expand Down Expand Up @@ -115,6 +120,7 @@ def update_messages(self, message: ChatMessage) -> List[MessageType]:
return self.stored_messages

@retry(wait=wait_exponential(min=5, max=60), stop=stop_after_attempt(5))
@openai_api_key_required
def step(
self,
input_message: ChatMessage,
Expand Down
14 changes: 7 additions & 7 deletions camel/agents/critic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ class CriticAgent(ChatAgent):
retry_attempts (int, optional): The number of retry attempts if the
critic fails to return a valid option. (default: :obj:`2`)
verbose (bool, optional): Whether to print the critic's messages.
menu_color (Any): The color of the menu options displayed to the user.
(default: :obj:`Fore.MAGENTA`)
logger_color (Any): The color of the menu options displayed to the
user. (default: :obj:`Fore.MAGENTA`)
"""

def __init__(
self,
system_message: SystemMessage,
model: ModelType = ModelType.GPT_3_5_TURBO,
model_config: Any = None,
model_config: Optional[Any] = None,
message_window_size: int = 6,
retry_attempts: int = 2,
verbose: bool = False,
menu_color: Any = Fore.MAGENTA,
logger_color: Any = Fore.MAGENTA,
) -> None:
super().__init__(system_message, model, model_config,
message_window_size)
self.options_dict: Dict[str, str] = dict()
self.retry_attempts = retry_attempts
self.verbose = verbose
self.menu_color = menu_color
self.logger_color = logger_color

def flatten_options(self, messages: List[ChatMessage]) -> str:
r"""Flattens the options to the critic.
Expand Down Expand Up @@ -107,7 +107,7 @@ def get_option(self, input_message: ChatMessage) -> str:
critic_msg = critic_msgs[0]
self.update_messages(critic_msg)
if self.verbose:
print_text_animated(self.menu_color + "\n> Critic response: "
print_text_animated(self.logger_color + "\n> Critic response: "
f"\x1b[3m{critic_msg.content}\x1b[0m\n")
choice = self.parse_critic(critic_msg)

Expand Down Expand Up @@ -163,7 +163,7 @@ def step(self, messages: List[ChatMessage]) -> ChatMessage:

flatten_options = self.flatten_options(messages)
if self.verbose:
print_text_animated(self.menu_color +
print_text_animated(self.logger_color +
f"\x1b[3m{flatten_options}\x1b[0m\n")
input_msg = copy.deepcopy(meta_chat_message)
input_msg.content = flatten_options
Expand Down
Loading

0 comments on commit 86a978e

Please sign in to comment.