Skip to content

Commit

Permalink
Merge branch 'master' into dockerfile-improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Wendong-Fan committed Jul 23, 2024
2 parents 9b44bb8 + fd944b7 commit 1dc2c62
Show file tree
Hide file tree
Showing 47 changed files with 1,861 additions and 121 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ jobs:
OPENWEATHERMAP_API_KEY: "${{ secrets.OPENWEATHERMAP_API_KEY }}"
ANTHROPIC_API_KEY: "${{ secrets.ANTHROPIC_API_KEY }}"
COHERE_API_KEY: "${{ secrets.COHERE_API_KEY }}"
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 }}"
run: pytest --fast-test-mode ./test
12 changes: 12 additions & 0 deletions .github/workflows/pytest_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
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/"
run: poetry run pytest --fast-test-mode test/

pytest_package_llm_test:
Expand All @@ -55,6 +59,10 @@ jobs:
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
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/"
run: poetry run pytest --llm-test-only test/

pytest_package_very_slow_test:
Expand All @@ -77,4 +85,8 @@ jobs:
NVIDIA_API_KEY: "${{ secrets.NVIDIA_API_KEY }}"
ZHIPUAI_API_BASE_URL: "${{ secrets.ZHIPUAI_API_BASE_URL }}"
ZHIPUAI_API_KEY: "${{ secrets.ZHIPUAI_API_KEY }}"
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 }}"
run: poetry run pytest --very-slow-test-only test/
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,41 @@ Please note that the environment variable is session-specific. If you open a new
print(assistant_response.msg.content)
```

## Use Open-Source Models as Backends (ex. using vLLM to set Phi-3 locally)
- [Install vLLM](https://docs.vllm.ai/en/latest/getting_started/installation.html)
- After setting up vLLM, start an OpenAI compatible server for example by
```bash
python -m vllm.entrypoints.openai.api_server --model microsoft/Phi-3-mini-4k-instruct --api-key vllm --dtype bfloat16
```
- Create and run following script (more details please refer to this [example](https://github.com/camel-ai/camel/blob/master/examples/models/vllm_model_example.py))
```python
from camel.agents import ChatAgent
from camel.messages import BaseMessage
from camel.models import ModelFactory
from camel.types import ModelPlatformType
vllm_model = ModelFactory.create(
model_platform=ModelPlatformType.VLLM,
model_type="microsoft/Phi-3-mini-4k-instruct",
url="http://localhost:8000/v1",
model_config_dict={"temperature": 0.0},
api_key="vllm",
)
assistant_sys_msg = BaseMessage.make_assistant_message(
role_name="Assistant",
content="You are a helpful assistant.",
)
agent = ChatAgent(assistant_sys_msg, model=vllm_model, token_limit=4096)
user_msg = BaseMessage.make_user_message(
role_name="User",
content="Say hi to CAMEL AI",
)
assistant_response = agent.step(user_msg)
print(assistant_response.msg.content)
```

## Data (Hosted on Hugging Face)
| Dataset | Chat format | Instruction format | Chat format (translated) |
|----------------|-----------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------|
Expand Down
2 changes: 1 addition & 1 deletion camel/agents/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __init__(
if model is not None
else ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
model_type=ModelType.GPT_3_5_TURBO,
model_type=ModelType.GPT_4O_MINI,
model_config_dict=ChatGPTConfig().__dict__,
api_key=self._api_key,
)
Expand Down
2 changes: 1 addition & 1 deletion camel/agents/critic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CriticAgent(ChatAgent):
agent.
model (BaseModelBackend, optional): The model backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
message_window_size (int, optional): The maximum number of previous
messages to include in the context window. If `None`, no windowing
is performed. (default: :obj:`6`)
Expand Down
2 changes: 1 addition & 1 deletion camel/agents/deductive_reasoner_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DeductiveReasonerAgent(ChatAgent):
Args:
model (BaseModelBackend, optional): The model backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
"""

def __init__(
Expand Down
2 changes: 1 addition & 1 deletion camel/agents/embodied_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EmbodiedAgent(ChatAgent):
system_message (BaseMessage): The system message for the chat agent.
model (BaseModelBackend, optional): The model backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
message_window_size (int, optional): The maximum number of previous
messages to include in the context window. If `None`, no windowing
is performed. (default: :obj:`None`)
Expand Down
2 changes: 1 addition & 1 deletion camel/agents/knowledge_graph_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __init__(
Args:
model (BaseModelBackend, optional): The model backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
"""
system_message = BaseMessage(
role_name="Graphify",
Expand Down
2 changes: 1 addition & 1 deletion camel/agents/role_assignment_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RoleAssignmentAgent(ChatAgent):
Args:
model (BaseModelBackend, optional): The model backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
Attributes:
role_assignment_prompt (TextPrompt): A prompt for the agent to generate
Expand Down
7 changes: 3 additions & 4 deletions camel/agents/search_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ class SearchAgent(ChatAgent):
relevance of an answer.
Args:
model_type (ModelType, optional): The type of model to use for the
agent. (default: :obj:`ModelType.GPT_3_5_TURBO`)
model_config (Any, optional): The configuration for the model.
(default: :obj:`None`)
model (BaseModelBackend, optional): The model backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_4O_MINI`)
"""

def __init__(
Expand Down
8 changes: 4 additions & 4 deletions camel/agents/task_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TaskSpecifyAgent(ChatAgent):
Args:
model (BaseModelBackend, optional): The model backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
task_type (TaskType, optional): The type of task for which to generate
a prompt. (default: :obj:`TaskType.AI_SOCIETY`)
task_specify_prompt (Union[str, TextPrompt], optional): The prompt for
Expand Down Expand Up @@ -126,7 +126,7 @@ class TaskPlannerAgent(ChatAgent):
Args:
model (BaseModelBackend, optional): The model backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
output_language (str, optional): The language to be output by the
agent. (default: :obj:`None`)
"""
Expand Down Expand Up @@ -201,7 +201,7 @@ class TaskCreationAgent(ChatAgent):
perform the task.
model (BaseModelBackend, optional): The LLM backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
output_language (str, optional): The language to be output by the
agent. (default: :obj:`None`)
message_window_size (int, optional): The maximum number of previous
Expand Down Expand Up @@ -312,7 +312,7 @@ class TaskPrioritizationAgent(ChatAgent):
perform the task.
model (BaseModelBackend, optional): The LLM backend to use for
generating responses. (default: :obj:`OpenAIModel` with
`GPT_3_5_TURBO`)
`GPT_4O_MINI`)
output_language (str, optional): The language to be output by the
agent. (default: :obj:`None`)
message_window_size (int, optional): The maximum number of previous
Expand Down
3 changes: 3 additions & 0 deletions camel/configs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ChatGPTConfig,
OpenSourceConfig,
)
from .vllm_config import VLLM_API_PARAMS, VLLMConfig
from .zhipuai_config import ZHIPUAI_API_PARAMS, ZhipuAIConfig

__all__ = [
Expand All @@ -41,4 +42,6 @@
'ZHIPUAI_API_PARAMS',
'GeminiConfig',
'Gemini_API_PARAMS',
'VLLMConfig',
'VLLM_API_PARAMS',
]
103 changes: 103 additions & 0 deletions camel/configs/vllm_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
from __future__ import annotations

from dataclasses import asdict, dataclass, field
from typing import Sequence

from openai._types import NOT_GIVEN, NotGiven

from camel.configs.base_config import BaseConfig


# flake8: noqa: E501
@dataclass(frozen=True)
class VLLMConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using the
OpenAI API.
Reference: https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html
Args:
temperature (float, optional): Sampling temperature to use, between
:obj:`0` and :obj:`2`. Higher values make the output more random,
while lower values make it more focused and deterministic.
(default: :obj:`0.2`)
top_p (float, optional): An alternative to sampling with temperature,
called nucleus sampling, where the model considers the results of
the tokens with top_p probability mass. So :obj:`0.1` means only
the tokens comprising the top 10% probability mass are considered.
(default: :obj:`1.0`)
n (int, optional): How many chat completion choices to generate for
each input message. (default: :obj:`1`)
response_format (object, optional): An object specifying the format
that the model must output. Compatible with GPT-4 Turbo and all
GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
{"type": "json_object"} enables JSON mode, which guarantees the
message the model generates is valid JSON. Important: when using
JSON mode, you must also instruct the model to produce JSON
yourself via a system or user message. Without this, the model
may generate an unending stream of whitespace until the generation
reaches the token limit, resulting in a long-running and seemingly
"stuck" request. Also note that the message content may be
partially cut off if finish_reason="length", which indicates the
generation exceeded max_tokens or the conversation exceeded the
max context length.
stream (bool, optional): If True, partial message deltas will be sent
as data-only server-sent events as they become available.
(default: :obj:`False`)
stop (str or list, optional): Up to :obj:`4` sequences where the API
will stop generating further tokens. (default: :obj:`None`)
max_tokens (int, optional): The maximum number of tokens to generate
in the chat completion. The total length of input tokens and
generated tokens is limited by the model's context length.
(default: :obj:`None`)
presence_penalty (float, optional): Number between :obj:`-2.0` and
:obj:`2.0`. Positive values penalize new tokens based on whether
they appear in the text so far, increasing the model's likelihood
to talk about new topics. See more information about frequency and
presence penalties. (default: :obj:`0.0`)
frequency_penalty (float, optional): Number between :obj:`-2.0` and
:obj:`2.0`. Positive values penalize new tokens based on their
existing frequency in the text so far, decreasing the model's
likelihood to repeat the same line verbatim. See more information
about frequency and presence penalties. (default: :obj:`0.0`)
logit_bias (dict, optional): Modify the likelihood of specified tokens
appearing in the completion. Accepts a json object that maps tokens
(specified by their token ID in the tokenizer) to an associated
bias value from :obj:`-100` to :obj:`100`. Mathematically, the bias
is added to the logits generated by the model prior to sampling.
The exact effect will vary per model, but values between:obj:` -1`
and :obj:`1` should decrease or increase likelihood of selection;
values like :obj:`-100` or :obj:`100` should result in a ban or
exclusive selection of the relevant token. (default: :obj:`{}`)
user (str, optional): A unique identifier representing your end-user,
which can help OpenAI to monitor and detect abuse.
(default: :obj:`""`)
"""

temperature: float = 0.2 # openai default: 1.0
top_p: float = 1.0
n: int = 1
stream: bool = False
stop: str | Sequence[str] | NotGiven = NOT_GIVEN
max_tokens: int | NotGiven = NOT_GIVEN
presence_penalty: float = 0.0
response_format: dict | NotGiven = NOT_GIVEN
frequency_penalty: float = 0.0
logit_bias: dict = field(default_factory=dict)
user: str = ""


VLLM_API_PARAMS = {param for param in asdict(VLLMConfig()).keys()}
2 changes: 2 additions & 0 deletions camel/interpreters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .docker_interpreter import DockerInterpreter
from .internal_python_interpreter import InternalPythonInterpreter
from .interpreter_error import InterpreterError
from .ipython_interpreter import JupyterKernelInterpreter
from .subprocess_interpreter import SubprocessInterpreter

__all__ = [
Expand All @@ -24,4 +25,5 @@
'InternalPythonInterpreter',
'SubprocessInterpreter',
'DockerInterpreter',
'JupyterKernelInterpreter',
]
Loading

0 comments on commit 1dc2c62

Please sign in to comment.