-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
218 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# =========== 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 .base_model import BaseModelBackend | ||
from .openai_model import OpenAIModel | ||
from .stub_model import StubModel | ||
from .model_factory import ModelFactory | ||
|
||
__all__ = [ | ||
'BaseModelBackend', | ||
'OpenAIModel', | ||
'StubModel', | ||
'ModelFactory', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# =========== 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 | ||
from typing import Any, Dict, List | ||
|
||
|
||
class BaseModelBackend(ABC): | ||
r"""Base class for different model backends. | ||
May be OpenAI API, a local LLM, a stub for unit tests, etc.""" | ||
|
||
@abstractmethod | ||
def run(self, messages: List[Dict]) -> Dict[str, Any]: | ||
r"""Runs the query to the backend model. | ||
Args: | ||
messages (List[Dict]): message list with the chat history | ||
in OpenAI API format. | ||
Raises: | ||
RuntimeError: if the return value from OpenAI API | ||
is not a dict that is expected. | ||
Returns: | ||
Dict[str, Any]: All backends must return a dict in OpenAI format. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# =========== 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 | ||
|
||
from camel.models import BaseModelBackend, OpenAIModel, StubModel | ||
from camel.typing import ModelType | ||
|
||
|
||
class ModelFactory: | ||
r"""Factory of backend models. | ||
Raises: | ||
ValueError: in case the provided model type is unknown. | ||
""" | ||
|
||
@staticmethod | ||
def create(model_type: ModelType, | ||
model_config_dict: Dict) -> BaseModelBackend: | ||
r"""Creates an instance of `BaseModelBackend` of the specified type. | ||
Args: | ||
model_type (ModelType): Model for which a backend is created. | ||
model_config_dict (Dict): a dictionary that will be fed into | ||
the backend constructor. | ||
Raises: | ||
ValueError: If there is not backend for the model. | ||
Returns: | ||
BaseModelBackend: The initialized backend. | ||
""" | ||
model_class: Any | ||
if model_type in { | ||
ModelType.GPT_3_5_TURBO, ModelType.GPT_4, ModelType.GPT_4_32k | ||
}: | ||
model_class = OpenAIModel | ||
elif model_type == ModelType.STUB: | ||
model_class = StubModel | ||
else: | ||
raise ValueError("Unknown model") | ||
|
||
inst = model_class(model_type, model_config_dict) | ||
return inst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# =========== 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 | ||
|
||
from camel.messages import OpenAIMessage | ||
from camel.models import BaseModelBackend | ||
from camel.typing import ModelType | ||
|
||
|
||
class OpenAIModel(BaseModelBackend): | ||
r"""OpenAI API in a unified BaseModelBackend interface.""" | ||
|
||
def __init__(self, model_type: ModelType, | ||
model_config_dict: Dict[str, Any]) -> 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(). | ||
""" | ||
super().__init__() | ||
self.model_type = model_type | ||
self.model_config_dict = model_config_dict | ||
|
||
def run(self, messages: List[Dict]) -> Dict[str, Any]: | ||
r"""Run inference of OpenAI chat completion. | ||
Args: | ||
messages (List[Dict]): message list with the chat history | ||
in OpenAI API format. | ||
Returns: | ||
Dict[str, Any]: Response in the OpenAI API format. | ||
""" | ||
import openai | ||
messages_openai: List[OpenAIMessage] = messages | ||
response = openai.ChatCompletion.create(messages=messages_openai, | ||
model=self.model_type.value, | ||
**self.model_config_dict) | ||
if not isinstance(response, Dict): | ||
raise RuntimeError("Unexpected return from OpenAI API") | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# =========== 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 | ||
|
||
from camel.models import BaseModelBackend | ||
|
||
|
||
class StubModel(BaseModelBackend): | ||
r"""A dummy model used for unit tests.""" | ||
|
||
def __init__(self, *args, **kwargs) -> None: | ||
r"""All arguments are unused for the dummy model.""" | ||
super().__init__() | ||
|
||
def run(self, messages: List[Dict]) -> Dict[str, Any]: | ||
r"""Run fake inference by returning a fixed string. | ||
All arguments are unused for the dummy model. | ||
Returns: | ||
Dict[str, Any]: Response in the OpenAI API format. | ||
""" | ||
ARBITRARY_STRING = "Lorem Ipsum" | ||
|
||
return dict( | ||
id="stub_model_id", | ||
usage=dict(), | ||
choices=[ | ||
dict(finish_reason="stop", | ||
message=dict(content=ARBITRARY_STRING, role="assistant")) | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters