Skip to content

Commit

Permalink
feat: Integrate Reka model (#845)
Browse files Browse the repository at this point in the history
Co-authored-by: Isaac Jin <[email protected]>
  • Loading branch information
Wendong-Fan and WHALEEYE authored Sep 2, 2024
1 parent 4c3b023 commit c36cb5c
Show file tree
Hide file tree
Showing 21 changed files with 1,053 additions and 589 deletions.
2 changes: 1 addition & 1 deletion .github/actions/camel_install/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ inputs:
python-version:
description: 'Python version.'
required: true
default: '3.9'
default: '3.10'
runs:
using: "composite"
steps:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/build_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Python environment and install dependencies
uses: actions/setup-python@v3
with:
python-version: "3.9"
python-version: "3.10"

- name: Install dependencies
run: |
Expand Down Expand Up @@ -61,6 +61,7 @@ jobs:
AZURE_DEPLOYMENT_NAME: "${{ secrets.AZURE_DEPLOYMENT_NAME }}"
AZURE_OPENAI_ENDPOINT: "${{ secrets.AZURE_OPENAI_ENDPOINT }}"
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
NEO4J_USERNAME: "${{ secrets.NEO4J_USERNAME }}"
NEO4J_PASSWORD: "${{ secrets.NEO4J_PASSWORD }}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Python environment and install dependencies
uses: ./.github/actions/camel_install
with:
python-version: "3.9"
python-version: "3.10"
- name: Sphinx build
run: |
cd docs
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pre_commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
- name: Set up Python environment and install dependencies
uses: ./.github/actions/camel_install
with:
python-version: "3.9"
python-version: "3.10"
- name: Run pre-commit
run: poetry run pre-commit run --all-files
4 changes: 2 additions & 2 deletions .github/workflows/pytest_apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set up Python environment and install dependencies
uses: ./.github/actions/camel_install
with:
python-version: "3.9"
python-version: "3.10"
- name: Run pytest
env:
OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"
Expand All @@ -40,7 +40,7 @@ jobs:
- name: Set up Python environment and install dependencies
uses: ./.github/actions/camel_install
with:
python-version: "3.9"
python-version: "3.10"
- name: Run pytest
env:
OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/pytest_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up Python environment and install dependencies
uses: ./.github/actions/camel_install
with:
python-version: "3.9"
python-version: "3.10"
- name: Run pytest
env:
OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"
Expand All @@ -40,6 +40,7 @@ jobs:
AZURE_DEPLOYMENT_NAME: "to-be-filled"
AZURE_OPENAI_ENDPOINT: "https://camel.openai.azure.com/"
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
NEO4J_USERNAME: "${{ secrets.NEO4J_USERNAME }}"
NEO4J_PASSWORD: "${{ secrets.NEO4J_PASSWORD }}"
Expand All @@ -52,7 +53,7 @@ jobs:
- name: Set up Python environment and install dependencies
uses: ./.github/actions/camel_install
with:
python-version: "3.9"
python-version: "3.10"
- name: Run pytest
env:
OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"
Expand All @@ -72,6 +73,7 @@ jobs:
AZURE_DEPLOYMENT_NAME: "to-be-filled"
AZURE_OPENAI_ENDPOINT: "https://camel.openai.azure.com/"
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
NEO4J_USERNAME: "${{ secrets.NEO4J_USERNAME }}"
NEO4J_PASSWORD: "${{ secrets.NEO4J_PASSWORD }}"
Expand All @@ -84,7 +86,7 @@ jobs:
- name: Set up Python environment and install dependencies
uses: ./.github/actions/camel_install
with:
python-version: "3.9"
python-version: "3.10"
- name: Run pytest
env:
OPENAI_API_KEY: "${{ secrets.OPENAI_API_KEY }}"
Expand All @@ -104,6 +106,7 @@ jobs:
AZURE_DEPLOYMENT_NAME: ${{ secrets.AZURE_DEPLOYMENT_NAME }}"
AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }}"
MISTRAL_API_KEY: "${{ secrets.MISTRAL_API_KEY }}"
REKA_API_KEY: "${{ secrets.REKA_API_KEY }}"
NEO4J_URI: "${{ secrets.NEO4J_URI }}"
NEO4J_USERNAME: "${{ secrets.NEO4J_USERNAME }}"
NEO4J_PASSWORD: "${{ secrets.NEO4J_PASSWORD }}"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Some features require extra dependencies:

Install `CAMEL` from source with poetry (Recommended):
```sh
# Make sure your python version is later than 3.9
# Make sure your python version is later than 3.10
# You can use pyenv to manage multiple python verisons in your sytstem
# Clone github repo
Expand Down Expand Up @@ -104,7 +104,7 @@ exit
Install `CAMEL` from source with conda and pip:
```sh
# Create a conda virtual environment
conda create --name camel python=3.9
conda create --name camel python=3.10
# Activate CAMEL conda environment
conda activate camel
Expand Down Expand Up @@ -323,8 +323,8 @@ We appreciate your interest in contributing to our open-source initiative. We pr
## Contact
For more information please contact [email protected].
[python-image]: https://img.shields.io/badge/Python-3.9%2B-brightgreen.svg
[python-url]: https://docs.python.org/3.9/
[python-image]: https://img.shields.io/badge/Python-3.10%2B-brightgreen.svg
[python-url]: https://docs.python.org/3.10/
[pytest-image]: https://github.com/camel-ai/camel/actions/workflows/pytest_package.yml/badge.svg
[pytest-url]: https://github.com/camel-ai/camel/actions/workflows/pytest_package.yml
[docs-image]: https://img.shields.io/badge/Documentation-grey.svg?logo=github
Expand Down
14 changes: 13 additions & 1 deletion camel/agents/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,9 @@ def handle_batch_response(
str(choice.finish_reason) for choice in response.choices
]
usage = (
response.usage.model_dump() if response.usage is not None else {}
self._safe_model_dump(response.usage)
if response.usage is not None
else {}
)
return (
output_messages,
Expand All @@ -774,6 +776,16 @@ def handle_batch_response(
response.id,
)

def _safe_model_dump(self, obj):
# Check if the `model_dump` method exists (Pydantic v2)
if hasattr(obj, 'model_dump'):
return obj.model_dump()
# Fallback to `dict()` method (Pydantic v1)
elif hasattr(obj, 'dict'):
return obj.dict()
else:
raise TypeError("The object is not a Pydantic model")

def handle_stream_response(
self,
response: Stream[ChatCompletionChunk],
Expand Down
3 changes: 3 additions & 0 deletions camel/configs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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 .reka_config import REKA_API_PARAMS, RekaConfig
from .samba_config import SAMBA_API_PARAMS, SambaConfig
from .togetherai_config import TOGETHERAI_API_PARAMS, TogetherAIConfig
from .vllm_config import VLLM_API_PARAMS, VLLMConfig
Expand All @@ -45,6 +46,8 @@
'VLLM_API_PARAMS',
'MistralConfig',
'MISTRAL_API_PARAMS',
'RekaConfig',
'REKA_API_PARAMS',
'SambaConfig',
'SAMBA_API_PARAMS',
'TogetherAIConfig',
Expand Down
74 changes: 74 additions & 0 deletions camel/configs/reka_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# =========== 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 typing import Any, Optional, Union

from camel.configs.base_config import BaseConfig


class RekaConfig(BaseConfig):
r"""Defines the parameters for generating chat completions using the
Reka API.
Reference: https://docs.reka.ai/api-reference/chat/create
Args:
temperature (Optional[float], optional): temperature the temperature
to use for sampling, e.g. 0.5.
top_p (Optional[float], optional): the cumulative probability of
tokens to generate, e.g. 0.9. Defaults to None.
top_k (Optional[int], optional): Parameter which forces the model to
only consider the tokens with the `top_k` highest probabilities at
the next step. Defaults to 1024.
max_tokens (Optional[int], optional): the maximum number of tokens to
generate, e.g. 100. Defaults to None.
stop (Optional[Union[str,list[str]]]): Stop generation if this token
is detected. Or if one of these tokens is detected when providing
a string list.
seed (Optional[int], optional): the random seed to use for sampling, e.
g. 42. Defaults to 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`)
use_search_engine (Optional[bool]): Whether to consider using search
engine to complete the request. Note that even if this is set to
`True`, the model might decide to not use search.
"""

temperature: Optional[float] = None
top_p: Optional[float] = None
top_k: Optional[int] = None
max_tokens: Optional[int] = None
stop: Optional[Union[str, list[str]]] = None
seed: Optional[int] = None
frequency_penalty: float = 0.0
presence_penalty: float = 0.0
use_search_engine: Optional[bool] = False

def as_dict(self) -> dict[str, Any]:
config_dict = super().as_dict()
if "tools" in config_dict:
del config_dict["tools"] # Reka does not support tool calling
return config_dict


REKA_API_PARAMS = {param for param in RekaConfig().model_fields.keys()}
2 changes: 2 additions & 0 deletions camel/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .openai_audio_models import OpenAIAudioModels
from .openai_compatibility_model import OpenAICompatibilityModel
from .openai_model import OpenAIModel
from .reka_model import RekaModel
from .samba_model import SambaModel
from .stub_model import StubModel
from .togetherai_model import TogetherAIModel
Expand All @@ -49,6 +50,7 @@
'VLLMModel',
'GeminiModel',
'OpenAICompatibilityModel',
'RekaModel',
'SambaModel',
'TogetherAIModel',
]
3 changes: 3 additions & 0 deletions camel/models/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from camel.models.open_source_model import OpenSourceModel
from camel.models.openai_compatibility_model import OpenAICompatibilityModel
from camel.models.openai_model import OpenAIModel
from camel.models.reka_model import RekaModel
from camel.models.samba_model import SambaModel
from camel.models.stub_model import StubModel
from camel.models.togetherai_model import TogetherAIModel
Expand Down Expand Up @@ -93,6 +94,8 @@ def create(
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_platform.is_samba and model_type.is_samba:
model_class = SambaModel
elif model_type == ModelType.STUB:
Expand Down
Loading

0 comments on commit c36cb5c

Please sign in to comment.