camel-ai 0.2.3a1__py3-none-any.whl → 0.2.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of camel-ai might be problematic. Click here for more details.
- camel/__init__.py +1 -1
- camel/agents/chat_agent.py +93 -69
- camel/agents/knowledge_graph_agent.py +4 -6
- camel/bots/__init__.py +16 -2
- camel/bots/discord_app.py +138 -0
- camel/bots/slack/__init__.py +30 -0
- camel/bots/slack/models.py +158 -0
- camel/bots/slack/slack_app.py +255 -0
- camel/configs/__init__.py +1 -2
- camel/configs/anthropic_config.py +2 -5
- camel/configs/base_config.py +6 -6
- camel/configs/groq_config.py +2 -3
- camel/configs/ollama_config.py +1 -2
- camel/configs/openai_config.py +2 -23
- camel/configs/samba_config.py +2 -2
- camel/configs/togetherai_config.py +1 -1
- camel/configs/vllm_config.py +1 -1
- camel/configs/zhipuai_config.py +2 -3
- camel/embeddings/openai_embedding.py +2 -2
- camel/loaders/__init__.py +2 -0
- camel/loaders/chunkr_reader.py +163 -0
- camel/loaders/firecrawl_reader.py +3 -3
- camel/loaders/unstructured_io.py +35 -33
- camel/messages/__init__.py +1 -0
- camel/models/__init__.py +2 -4
- camel/models/anthropic_model.py +32 -26
- camel/models/azure_openai_model.py +39 -36
- camel/models/base_model.py +31 -20
- camel/models/gemini_model.py +37 -29
- camel/models/groq_model.py +29 -23
- camel/models/litellm_model.py +44 -61
- camel/models/mistral_model.py +32 -29
- camel/models/model_factory.py +66 -76
- camel/models/nemotron_model.py +33 -23
- camel/models/ollama_model.py +42 -47
- camel/models/{openai_compatibility_model.py → openai_compatible_model.py} +31 -49
- camel/models/openai_model.py +48 -29
- camel/models/reka_model.py +30 -28
- camel/models/samba_model.py +82 -177
- camel/models/stub_model.py +2 -2
- camel/models/togetherai_model.py +37 -43
- camel/models/vllm_model.py +43 -50
- camel/models/zhipuai_model.py +33 -27
- camel/retrievers/auto_retriever.py +29 -97
- camel/retrievers/vector_retriever.py +58 -47
- camel/societies/babyagi_playing.py +6 -3
- camel/societies/role_playing.py +5 -3
- camel/storages/graph_storages/graph_element.py +2 -2
- camel/storages/key_value_storages/json.py +6 -1
- camel/toolkits/__init__.py +20 -7
- camel/toolkits/arxiv_toolkit.py +155 -0
- camel/toolkits/ask_news_toolkit.py +653 -0
- camel/toolkits/base.py +2 -3
- camel/toolkits/code_execution.py +6 -7
- camel/toolkits/dalle_toolkit.py +6 -6
- camel/toolkits/{openai_function.py → function_tool.py} +34 -11
- camel/toolkits/github_toolkit.py +9 -10
- camel/toolkits/google_maps_toolkit.py +7 -7
- camel/toolkits/google_scholar_toolkit.py +146 -0
- camel/toolkits/linkedin_toolkit.py +7 -7
- camel/toolkits/math_toolkit.py +8 -8
- camel/toolkits/open_api_toolkit.py +5 -5
- camel/toolkits/reddit_toolkit.py +7 -7
- camel/toolkits/retrieval_toolkit.py +5 -5
- camel/toolkits/search_toolkit.py +9 -9
- camel/toolkits/slack_toolkit.py +11 -11
- camel/toolkits/twitter_toolkit.py +378 -452
- camel/toolkits/weather_toolkit.py +6 -6
- camel/toolkits/whatsapp_toolkit.py +177 -0
- camel/types/__init__.py +6 -1
- camel/types/enums.py +40 -85
- camel/types/openai_types.py +3 -0
- camel/types/unified_model_type.py +104 -0
- camel/utils/__init__.py +0 -2
- camel/utils/async_func.py +7 -7
- camel/utils/commons.py +32 -3
- camel/utils/token_counting.py +30 -212
- camel/workforce/role_playing_worker.py +1 -1
- camel/workforce/single_agent_worker.py +1 -1
- camel/workforce/task_channel.py +4 -3
- camel/workforce/workforce.py +4 -4
- camel_ai-0.2.4.dist-info/LICENSE +201 -0
- {camel_ai-0.2.3a1.dist-info → camel_ai-0.2.4.dist-info}/METADATA +27 -56
- {camel_ai-0.2.3a1.dist-info → camel_ai-0.2.4.dist-info}/RECORD +85 -76
- {camel_ai-0.2.3a1.dist-info → camel_ai-0.2.4.dist-info}/WHEEL +1 -1
- camel/bots/discord_bot.py +0 -206
- camel/models/open_source_model.py +0 -170
camel/models/model_factory.py
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
# See the License for the specific language governing permissions and
|
|
12
12
|
# limitations under the License.
|
|
13
13
|
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
-
from typing import
|
|
14
|
+
from typing import Dict, Optional, Type, Union
|
|
15
15
|
|
|
16
16
|
from camel.models.anthropic_model import AnthropicModel
|
|
17
17
|
from camel.models.azure_openai_model import AzureOpenAIModel
|
|
@@ -21,8 +21,7 @@ from camel.models.groq_model import GroqModel
|
|
|
21
21
|
from camel.models.litellm_model import LiteLLMModel
|
|
22
22
|
from camel.models.mistral_model import MistralModel
|
|
23
23
|
from camel.models.ollama_model import OllamaModel
|
|
24
|
-
from camel.models.
|
|
25
|
-
from camel.models.openai_compatibility_model import OpenAICompatibilityModel
|
|
24
|
+
from camel.models.openai_compatible_model import OpenAICompatibleModel
|
|
26
25
|
from camel.models.openai_model import OpenAIModel
|
|
27
26
|
from camel.models.reka_model import RekaModel
|
|
28
27
|
from camel.models.samba_model import SambaModel
|
|
@@ -30,7 +29,7 @@ from camel.models.stub_model import StubModel
|
|
|
30
29
|
from camel.models.togetherai_model import TogetherAIModel
|
|
31
30
|
from camel.models.vllm_model import VLLMModel
|
|
32
31
|
from camel.models.zhipuai_model import ZhipuAIModel
|
|
33
|
-
from camel.types import ModelPlatformType, ModelType
|
|
32
|
+
from camel.types import ModelPlatformType, ModelType, UnifiedModelType
|
|
34
33
|
from camel.utils import BaseTokenCounter
|
|
35
34
|
|
|
36
35
|
|
|
@@ -45,7 +44,7 @@ class ModelFactory:
|
|
|
45
44
|
def create(
|
|
46
45
|
model_platform: ModelPlatformType,
|
|
47
46
|
model_type: Union[ModelType, str],
|
|
48
|
-
model_config_dict: Dict,
|
|
47
|
+
model_config_dict: Optional[Dict] = None,
|
|
49
48
|
token_counter: Optional[BaseTokenCounter] = None,
|
|
50
49
|
api_key: Optional[str] = None,
|
|
51
50
|
url: Optional[str] = None,
|
|
@@ -55,80 +54,71 @@ class ModelFactory:
|
|
|
55
54
|
Args:
|
|
56
55
|
model_platform (ModelPlatformType): Platform from which the model
|
|
57
56
|
originates.
|
|
58
|
-
model_type (Union[ModelType, str]): Model for which a
|
|
59
|
-
created
|
|
60
|
-
model_config_dict (Dict): A dictionary that will be fed
|
|
61
|
-
the backend constructor.
|
|
62
|
-
token_counter (Optional[BaseTokenCounter]): Token
|
|
63
|
-
for the model. If not provided,
|
|
64
|
-
|
|
65
|
-
provide official
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
ValueError: If there is not backend for the model.
|
|
57
|
+
model_type (Union[ModelType, str]): Model for which a
|
|
58
|
+
backend is created. Can be a `str` for open source platforms.
|
|
59
|
+
model_config_dict (Optional[Dict]): A dictionary that will be fed
|
|
60
|
+
into the backend constructor. (default: :obj:`None`)
|
|
61
|
+
token_counter (Optional[BaseTokenCounter], optional): Token
|
|
62
|
+
counter to use for the model. If not provided,
|
|
63
|
+
:obj:`OpenAITokenCounter(ModelType.GPT_4O_MINI)`
|
|
64
|
+
will be used if the model platform didn't provide official
|
|
65
|
+
token counter. (default: :obj:`None`)
|
|
66
|
+
api_key (Optional[str], optional): The API key for authenticating
|
|
67
|
+
with the model service. (default: :obj:`None`)
|
|
68
|
+
url (Optional[str], optional): The url to the model service.
|
|
69
|
+
(default: :obj:`None`)
|
|
72
70
|
|
|
73
71
|
Returns:
|
|
74
72
|
BaseModelBackend: The initialized backend.
|
|
73
|
+
|
|
74
|
+
Raises:
|
|
75
|
+
ValueError: If there is no backend for the model.
|
|
75
76
|
"""
|
|
76
|
-
model_class:
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
elif
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
model_class = OpenAICompatibilityModel
|
|
118
|
-
elif model_platform.is_samba:
|
|
119
|
-
model_class = SambaModel
|
|
120
|
-
elif model_platform.is_together:
|
|
121
|
-
model_class = TogetherAIModel
|
|
122
|
-
return model_class(
|
|
123
|
-
model_type, model_config_dict, api_key, token_counter
|
|
124
|
-
)
|
|
125
|
-
else:
|
|
126
|
-
raise ValueError(
|
|
127
|
-
f"Unknown pair of model platform `{model_platform}` "
|
|
128
|
-
f"and model type `{model_type}`."
|
|
129
|
-
)
|
|
130
|
-
else:
|
|
131
|
-
raise ValueError(f"Invalid model type `{model_type}` provided.")
|
|
77
|
+
model_class: Optional[Type[BaseModelBackend]] = None
|
|
78
|
+
model_type = UnifiedModelType(model_type)
|
|
79
|
+
|
|
80
|
+
if model_platform.is_ollama:
|
|
81
|
+
model_class = OllamaModel
|
|
82
|
+
elif model_platform.is_vllm:
|
|
83
|
+
model_class = VLLMModel
|
|
84
|
+
elif model_platform.is_openai_compatible_model:
|
|
85
|
+
model_class = OpenAICompatibleModel
|
|
86
|
+
elif model_platform.is_samba:
|
|
87
|
+
model_class = SambaModel
|
|
88
|
+
elif model_platform.is_together:
|
|
89
|
+
model_class = TogetherAIModel
|
|
90
|
+
elif model_platform.is_litellm:
|
|
91
|
+
model_class = LiteLLMModel
|
|
92
|
+
|
|
93
|
+
elif model_platform.is_openai and model_type.is_openai:
|
|
94
|
+
model_class = OpenAIModel
|
|
95
|
+
elif model_platform.is_azure and model_type.is_azure_openai:
|
|
96
|
+
model_class = AzureOpenAIModel
|
|
97
|
+
elif model_platform.is_anthropic and model_type.is_anthropic:
|
|
98
|
+
model_class = AnthropicModel
|
|
99
|
+
elif model_platform.is_groq and model_type.is_groq:
|
|
100
|
+
model_class = GroqModel
|
|
101
|
+
elif model_platform.is_zhipuai and model_type.is_zhipuai:
|
|
102
|
+
model_class = ZhipuAIModel
|
|
103
|
+
elif model_platform.is_gemini and model_type.is_gemini:
|
|
104
|
+
model_class = GeminiModel
|
|
105
|
+
elif model_platform.is_mistral and model_type.is_mistral:
|
|
106
|
+
model_class = MistralModel
|
|
107
|
+
elif model_platform.is_reka and model_type.is_reka:
|
|
108
|
+
model_class = RekaModel
|
|
109
|
+
elif model_type == ModelType.STUB:
|
|
110
|
+
model_class = StubModel
|
|
111
|
+
|
|
112
|
+
if model_class is None:
|
|
113
|
+
raise ValueError(
|
|
114
|
+
f"Unknown pair of model platform `{model_platform}` "
|
|
115
|
+
f"and model type `{model_type}`."
|
|
116
|
+
)
|
|
117
|
+
|
|
132
118
|
return model_class(
|
|
133
|
-
model_type,
|
|
119
|
+
model_type=model_type,
|
|
120
|
+
model_config_dict=model_config_dict,
|
|
121
|
+
api_key=api_key,
|
|
122
|
+
url=url,
|
|
123
|
+
token_counter=token_counter,
|
|
134
124
|
)
|
camel/models/nemotron_model.py
CHANGED
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
# limitations under the License.
|
|
13
13
|
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
14
|
import os
|
|
15
|
-
from typing import List, Optional
|
|
15
|
+
from typing import List, Optional, Union
|
|
16
16
|
|
|
17
17
|
from openai import OpenAI
|
|
18
18
|
|
|
19
19
|
from camel.messages import OpenAIMessage
|
|
20
|
+
from camel.models import BaseModelBackend
|
|
20
21
|
from camel.types import ChatCompletion, ModelType
|
|
21
22
|
from camel.utils import (
|
|
22
23
|
BaseTokenCounter,
|
|
@@ -24,40 +25,38 @@ from camel.utils import (
|
|
|
24
25
|
)
|
|
25
26
|
|
|
26
27
|
|
|
27
|
-
class NemotronModel:
|
|
28
|
-
r"""Nemotron model API backend with OpenAI compatibility.
|
|
28
|
+
class NemotronModel(BaseModelBackend):
|
|
29
|
+
r"""Nemotron model API backend with OpenAI compatibility.
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
Args:
|
|
32
|
+
model_type (Union[ModelType, str]): Model for which a backend is
|
|
33
|
+
created.
|
|
34
|
+
api_key (Optional[str], optional): The API key for authenticating with
|
|
35
|
+
the Nvidia service. (default: :obj:`None`)
|
|
36
|
+
url (Optional[str], optional): The url to the Nvidia service.
|
|
37
|
+
(default: :obj:`https://integrate.api.nvidia.com/v1`)
|
|
38
|
+
|
|
39
|
+
Notes:
|
|
40
|
+
Nemotron model doesn't support additional model config like OpenAI.
|
|
41
|
+
"""
|
|
31
42
|
|
|
32
43
|
def __init__(
|
|
33
44
|
self,
|
|
34
|
-
model_type: ModelType,
|
|
45
|
+
model_type: Union[ModelType, str],
|
|
35
46
|
api_key: Optional[str] = None,
|
|
36
47
|
url: Optional[str] = None,
|
|
37
48
|
) -> None:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
Nvidia service. (default: :obj:`None`)
|
|
44
|
-
url (Optional[str]): The url to the Nvidia service. (default:
|
|
45
|
-
:obj:`None`)
|
|
46
|
-
"""
|
|
47
|
-
self.model_type = model_type
|
|
48
|
-
self._url = url or os.environ.get("NVIDIA_API_BASE_URL")
|
|
49
|
-
self._api_key = api_key or os.environ.get("NVIDIA_API_KEY")
|
|
50
|
-
if not self._url or not self._api_key:
|
|
51
|
-
raise ValueError(
|
|
52
|
-
"NVIDIA_API_BASE_URL and NVIDIA_API_KEY should be set."
|
|
53
|
-
)
|
|
49
|
+
url = url or os.environ.get(
|
|
50
|
+
"NVIDIA_API_BASE_URL", "https://integrate.api.nvidia.com/v1"
|
|
51
|
+
)
|
|
52
|
+
api_key = api_key or os.environ.get("NVIDIA_API_KEY")
|
|
53
|
+
super().__init__(model_type, {}, api_key, url)
|
|
54
54
|
self._client = OpenAI(
|
|
55
55
|
timeout=60,
|
|
56
56
|
max_retries=3,
|
|
57
57
|
base_url=self._url,
|
|
58
58
|
api_key=self._api_key,
|
|
59
59
|
)
|
|
60
|
-
self._token_counter: Optional[BaseTokenCounter] = None
|
|
61
60
|
|
|
62
61
|
@api_keys_required("NVIDIA_API_KEY")
|
|
63
62
|
def run(
|
|
@@ -74,6 +73,17 @@ class NemotronModel:
|
|
|
74
73
|
"""
|
|
75
74
|
response = self._client.chat.completions.create(
|
|
76
75
|
messages=messages,
|
|
77
|
-
model=self.model_type
|
|
76
|
+
model=self.model_type,
|
|
78
77
|
)
|
|
79
78
|
return response
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def token_counter(self) -> BaseTokenCounter:
|
|
82
|
+
raise NotImplementedError(
|
|
83
|
+
"Nemotron model doesn't support token counter."
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
def check_model_config(self):
|
|
87
|
+
raise NotImplementedError(
|
|
88
|
+
"Nemotron model doesn't support model config."
|
|
89
|
+
)
|
camel/models/ollama_model.py
CHANGED
|
@@ -17,54 +17,64 @@ from typing import Any, Dict, List, Optional, Union
|
|
|
17
17
|
|
|
18
18
|
from openai import OpenAI, Stream
|
|
19
19
|
|
|
20
|
-
from camel.configs import OLLAMA_API_PARAMS
|
|
20
|
+
from camel.configs import OLLAMA_API_PARAMS, OllamaConfig
|
|
21
21
|
from camel.messages import OpenAIMessage
|
|
22
|
-
from camel.
|
|
22
|
+
from camel.models import BaseModelBackend
|
|
23
|
+
from camel.types import (
|
|
24
|
+
ChatCompletion,
|
|
25
|
+
ChatCompletionChunk,
|
|
26
|
+
ModelType,
|
|
27
|
+
)
|
|
23
28
|
from camel.utils import BaseTokenCounter, OpenAITokenCounter
|
|
24
29
|
|
|
25
30
|
|
|
26
|
-
class OllamaModel:
|
|
27
|
-
r"""Ollama service interface.
|
|
31
|
+
class OllamaModel(BaseModelBackend):
|
|
32
|
+
r"""Ollama service interface.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
model_type (Union[ModelType, str]): Model for which a backend is
|
|
36
|
+
created.
|
|
37
|
+
model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
|
|
38
|
+
that will be fed into:obj:`openai.ChatCompletion.create()`.
|
|
39
|
+
If:obj:`None`, :obj:`OllamaConfig().as_dict()` will be used.
|
|
40
|
+
(default: :obj:`None`)
|
|
41
|
+
api_key (Optional[str], optional): The API key for authenticating with
|
|
42
|
+
the model service. Ollama doesn't need API key, it would be
|
|
43
|
+
ignored if set. (default: :obj:`None`)
|
|
44
|
+
url (Optional[str], optional): The url to the model service.
|
|
45
|
+
(default: :obj:`None`)
|
|
46
|
+
token_counter (Optional[BaseTokenCounter], optional): Token counter to
|
|
47
|
+
use for the model. If not provided, :obj:`OpenAITokenCounter(
|
|
48
|
+
ModelType.GPT_4O_MINI)` will be used.
|
|
49
|
+
(default: :obj:`None`)
|
|
50
|
+
|
|
51
|
+
References:
|
|
52
|
+
https://github.com/ollama/ollama/blob/main/docs/openai.md
|
|
53
|
+
"""
|
|
28
54
|
|
|
29
55
|
def __init__(
|
|
30
56
|
self,
|
|
31
|
-
model_type: str,
|
|
32
|
-
model_config_dict: Dict[str, Any],
|
|
57
|
+
model_type: Union[ModelType, str],
|
|
58
|
+
model_config_dict: Optional[Dict[str, Any]] = None,
|
|
59
|
+
api_key: Optional[str] = None,
|
|
33
60
|
url: Optional[str] = None,
|
|
34
61
|
token_counter: Optional[BaseTokenCounter] = None,
|
|
35
62
|
) -> None:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
model_type (str): Model for which a backend is created.
|
|
42
|
-
model_config_dict (Dict[str, Any]): A dictionary that will
|
|
43
|
-
be fed into openai.ChatCompletion.create().
|
|
44
|
-
url (Optional[str]): The url to the model service. (default:
|
|
45
|
-
:obj:`"http://localhost:11434/v1"`)
|
|
46
|
-
token_counter (Optional[BaseTokenCounter]): Token counter to use
|
|
47
|
-
for the model. If not provided, `OpenAITokenCounter(ModelType.
|
|
48
|
-
GPT_4O_MINI)` will be used.
|
|
49
|
-
"""
|
|
50
|
-
self.model_type = model_type
|
|
51
|
-
self.model_config_dict = model_config_dict
|
|
52
|
-
self._url = (
|
|
53
|
-
url
|
|
54
|
-
or os.environ.get("OLLAMA_BASE_URL")
|
|
55
|
-
or "http://localhost:11434/v1"
|
|
63
|
+
if model_config_dict is None:
|
|
64
|
+
model_config_dict = OllamaConfig().as_dict()
|
|
65
|
+
url = url or os.environ.get("OLLAMA_BASE_URL")
|
|
66
|
+
super().__init__(
|
|
67
|
+
model_type, model_config_dict, api_key, url, token_counter
|
|
56
68
|
)
|
|
57
|
-
if not
|
|
69
|
+
if not self._url:
|
|
58
70
|
self._start_server()
|
|
59
71
|
# Use OpenAI client as interface call Ollama
|
|
60
72
|
self._client = OpenAI(
|
|
61
73
|
timeout=60,
|
|
62
74
|
max_retries=3,
|
|
75
|
+
api_key="Set-but-ignored", # required but ignored
|
|
63
76
|
base_url=self._url,
|
|
64
|
-
api_key="ollama", # required but ignored
|
|
65
77
|
)
|
|
66
|
-
self._token_counter = token_counter
|
|
67
|
-
self.check_model_config()
|
|
68
78
|
|
|
69
79
|
def _start_server(self) -> None:
|
|
70
80
|
r"""Starts the Ollama server in a subprocess."""
|
|
@@ -74,8 +84,9 @@ class OllamaModel:
|
|
|
74
84
|
stdout=subprocess.PIPE,
|
|
75
85
|
stderr=subprocess.PIPE,
|
|
76
86
|
)
|
|
87
|
+
self._url = "http://localhost:11434/v1"
|
|
77
88
|
print(
|
|
78
|
-
f"Ollama server started on
|
|
89
|
+
f"Ollama server started on {self._url} "
|
|
79
90
|
f"for {self.model_type} model."
|
|
80
91
|
)
|
|
81
92
|
except Exception as e:
|
|
@@ -131,22 +142,6 @@ class OllamaModel:
|
|
|
131
142
|
)
|
|
132
143
|
return response
|
|
133
144
|
|
|
134
|
-
@property
|
|
135
|
-
def token_limit(self) -> int:
|
|
136
|
-
r"""Returns the maximum token limit for the given model.
|
|
137
|
-
|
|
138
|
-
Returns:
|
|
139
|
-
int: The maximum token limit for the given model.
|
|
140
|
-
"""
|
|
141
|
-
max_tokens = self.model_config_dict.get("max_tokens")
|
|
142
|
-
if isinstance(max_tokens, int):
|
|
143
|
-
return max_tokens
|
|
144
|
-
print(
|
|
145
|
-
"Must set `max_tokens` as an integer in `model_config_dict` when"
|
|
146
|
-
" setting up the model. Using 4096 as default value."
|
|
147
|
-
)
|
|
148
|
-
return 4096
|
|
149
|
-
|
|
150
145
|
@property
|
|
151
146
|
def stream(self) -> bool:
|
|
152
147
|
r"""Returns whether the model is in stream mode, which sends partial
|
|
@@ -18,59 +18,54 @@ from typing import Any, Dict, List, Optional, Union
|
|
|
18
18
|
from openai import OpenAI, Stream
|
|
19
19
|
|
|
20
20
|
from camel.messages import OpenAIMessage
|
|
21
|
-
from camel.
|
|
21
|
+
from camel.models import BaseModelBackend
|
|
22
|
+
from camel.types import (
|
|
23
|
+
ChatCompletion,
|
|
24
|
+
ChatCompletionChunk,
|
|
25
|
+
ModelType,
|
|
26
|
+
)
|
|
22
27
|
from camel.utils import (
|
|
23
28
|
BaseTokenCounter,
|
|
24
29
|
OpenAITokenCounter,
|
|
25
30
|
)
|
|
26
31
|
|
|
27
32
|
|
|
28
|
-
class
|
|
29
|
-
r"""
|
|
33
|
+
class OpenAICompatibleModel(BaseModelBackend):
|
|
34
|
+
r"""Constructor for model backend supporting OpenAI compatibility.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
model_type (Union[ModelType, str]): Model for which a backend is
|
|
38
|
+
created.
|
|
39
|
+
model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
|
|
40
|
+
that will be fed into:obj:`openai.ChatCompletion.create()`. If
|
|
41
|
+
:obj:`None`, :obj:`{}` will be used. (default: :obj:`None`)
|
|
42
|
+
api_key (str): The API key for authenticating with the model service.
|
|
43
|
+
url (str): The url to the model service.
|
|
44
|
+
token_counter (Optional[BaseTokenCounter], optional): Token counter to
|
|
45
|
+
use for the model. If not provided, :obj:`OpenAITokenCounter(
|
|
46
|
+
ModelType.GPT_4O_MINI)` will be used.
|
|
47
|
+
(default: :obj:`None`)
|
|
48
|
+
"""
|
|
30
49
|
|
|
31
50
|
def __init__(
|
|
32
51
|
self,
|
|
33
|
-
model_type: str,
|
|
34
|
-
model_config_dict: Dict[str, Any],
|
|
52
|
+
model_type: Union[ModelType, str],
|
|
53
|
+
model_config_dict: Optional[Dict[str, Any]] = None,
|
|
35
54
|
api_key: Optional[str] = None,
|
|
36
55
|
url: Optional[str] = None,
|
|
37
56
|
token_counter: Optional[BaseTokenCounter] = None,
|
|
38
57
|
) -> None:
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
model_type
|
|
43
|
-
model_config_dict (Dict[str, Any]): A dictionary that will
|
|
44
|
-
be fed into openai.ChatCompletion.create().
|
|
45
|
-
api_key (str): The API key for authenticating with the
|
|
46
|
-
model service. (default: :obj:`None`)
|
|
47
|
-
url (str): The url to the model service. (default:
|
|
48
|
-
:obj:`None`)
|
|
49
|
-
token_counter (Optional[BaseTokenCounter]): Token counter to use
|
|
50
|
-
for the model. If not provided, `OpenAITokenCounter(ModelType.
|
|
51
|
-
GPT_4O_MINI)` will be used.
|
|
52
|
-
"""
|
|
53
|
-
self.model_type = model_type
|
|
54
|
-
self.model_config_dict = model_config_dict
|
|
55
|
-
self._url = url or os.environ.get("OPENAI_COMPATIBILIY_API_BASE_URL")
|
|
56
|
-
self._api_key = api_key or os.environ.get(
|
|
57
|
-
"OPENAI_COMPATIBILIY_API_KEY"
|
|
58
|
+
self.api_key = api_key or os.environ.get("OPENAI_COMPATIBILIY_API_KEY")
|
|
59
|
+
self.url = url or os.environ.get("OPENAI_COMPATIBILIY_API_BASE_URL")
|
|
60
|
+
super().__init__(
|
|
61
|
+
model_type, model_config_dict, api_key, url, token_counter
|
|
58
62
|
)
|
|
59
|
-
if self._url is None:
|
|
60
|
-
raise ValueError(
|
|
61
|
-
"For OpenAI-compatible models, you must provide the `url`."
|
|
62
|
-
)
|
|
63
|
-
if self._api_key is None:
|
|
64
|
-
raise ValueError(
|
|
65
|
-
"For OpenAI-compatible models, you must provide the `api_key`."
|
|
66
|
-
)
|
|
67
63
|
self._client = OpenAI(
|
|
68
64
|
timeout=60,
|
|
69
65
|
max_retries=3,
|
|
70
|
-
base_url=self._url,
|
|
71
66
|
api_key=self._api_key,
|
|
67
|
+
base_url=self._url,
|
|
72
68
|
)
|
|
73
|
-
self._token_counter = token_counter
|
|
74
69
|
|
|
75
70
|
def run(
|
|
76
71
|
self,
|
|
@@ -117,18 +112,5 @@ class OpenAICompatibilityModel:
|
|
|
117
112
|
"""
|
|
118
113
|
return self.model_config_dict.get('stream', False)
|
|
119
114
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
r"""Returns the maximum token limit for the given model.
|
|
123
|
-
|
|
124
|
-
Returns:
|
|
125
|
-
int: The maximum token limit for the given model.
|
|
126
|
-
"""
|
|
127
|
-
max_tokens = self.model_config_dict.get("max_tokens")
|
|
128
|
-
if isinstance(max_tokens, int):
|
|
129
|
-
return max_tokens
|
|
130
|
-
print(
|
|
131
|
-
"Must set `max_tokens` as an integer in `model_config_dict` when"
|
|
132
|
-
" setting up the model. Using 4096 as default value."
|
|
133
|
-
)
|
|
134
|
-
return 4096
|
|
115
|
+
def check_model_config(self):
|
|
116
|
+
pass
|
camel/models/openai_model.py
CHANGED
|
@@ -12,14 +12,19 @@
|
|
|
12
12
|
# limitations under the License.
|
|
13
13
|
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
14
|
import os
|
|
15
|
+
import warnings
|
|
15
16
|
from typing import Any, Dict, List, Optional, Union
|
|
16
17
|
|
|
17
18
|
from openai import OpenAI, Stream
|
|
18
19
|
|
|
19
|
-
from camel.configs import OPENAI_API_PARAMS
|
|
20
|
+
from camel.configs import OPENAI_API_PARAMS, ChatGPTConfig
|
|
20
21
|
from camel.messages import OpenAIMessage
|
|
21
22
|
from camel.models import BaseModelBackend
|
|
22
|
-
from camel.types import
|
|
23
|
+
from camel.types import (
|
|
24
|
+
ChatCompletion,
|
|
25
|
+
ChatCompletionChunk,
|
|
26
|
+
ModelType,
|
|
27
|
+
)
|
|
23
28
|
from camel.utils import (
|
|
24
29
|
BaseTokenCounter,
|
|
25
30
|
OpenAITokenCounter,
|
|
@@ -28,36 +33,39 @@ from camel.utils import (
|
|
|
28
33
|
|
|
29
34
|
|
|
30
35
|
class OpenAIModel(BaseModelBackend):
|
|
31
|
-
r"""OpenAI API in a unified BaseModelBackend interface.
|
|
36
|
+
r"""OpenAI API in a unified BaseModelBackend interface.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
model_type (Union[ModelType, str]): Model for which a backend is
|
|
40
|
+
created, one of GPT_* series.
|
|
41
|
+
model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
|
|
42
|
+
that will be fed into:obj:`openai.ChatCompletion.create()`. If
|
|
43
|
+
:obj:`None`, :obj:`ChatGPTConfig().as_dict()` will be used.
|
|
44
|
+
(default: :obj:`None`)
|
|
45
|
+
api_key (Optional[str], optional): The API key for authenticating
|
|
46
|
+
with the OpenAI service. (default: :obj:`None`)
|
|
47
|
+
url (Optional[str], optional): The url to the OpenAI service.
|
|
48
|
+
(default: :obj:`None`)
|
|
49
|
+
token_counter (Optional[BaseTokenCounter], optional): Token counter to
|
|
50
|
+
use for the model. If not provided, :obj:`OpenAITokenCounter` will
|
|
51
|
+
be used. (default: :obj:`None`)
|
|
52
|
+
"""
|
|
32
53
|
|
|
33
54
|
def __init__(
|
|
34
55
|
self,
|
|
35
|
-
model_type: ModelType,
|
|
36
|
-
model_config_dict: Dict[str, Any],
|
|
56
|
+
model_type: Union[ModelType, str],
|
|
57
|
+
model_config_dict: Optional[Dict[str, Any]] = None,
|
|
37
58
|
api_key: Optional[str] = None,
|
|
38
59
|
url: Optional[str] = None,
|
|
39
60
|
token_counter: Optional[BaseTokenCounter] = None,
|
|
40
61
|
) -> None:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
one of GPT_* series.
|
|
46
|
-
model_config_dict (Dict[str, Any]): A dictionary that will
|
|
47
|
-
be fed into openai.ChatCompletion.create().
|
|
48
|
-
api_key (Optional[str]): The API key for authenticating with the
|
|
49
|
-
OpenAI service. (default: :obj:`None`)
|
|
50
|
-
url (Optional[str]): The url to the OpenAI service. (default:
|
|
51
|
-
:obj:`None`)
|
|
52
|
-
token_counter (Optional[BaseTokenCounter]): Token counter to use
|
|
53
|
-
for the model. If not provided, `OpenAITokenCounter` will
|
|
54
|
-
be used.
|
|
55
|
-
"""
|
|
62
|
+
if model_config_dict is None:
|
|
63
|
+
model_config_dict = ChatGPTConfig().as_dict()
|
|
64
|
+
api_key = api_key or os.environ.get("OPENAI_API_KEY")
|
|
65
|
+
url = url or os.environ.get("OPENAI_API_BASE_URL")
|
|
56
66
|
super().__init__(
|
|
57
67
|
model_type, model_config_dict, api_key, url, token_counter
|
|
58
68
|
)
|
|
59
|
-
self._url = url or os.environ.get("OPENAI_API_BASE_URL")
|
|
60
|
-
self._api_key = api_key or os.environ.get("OPENAI_API_KEY")
|
|
61
69
|
self._client = OpenAI(
|
|
62
70
|
timeout=60,
|
|
63
71
|
max_retries=3,
|
|
@@ -96,13 +104,23 @@ class OpenAIModel(BaseModelBackend):
|
|
|
96
104
|
# o1-preview and o1-mini have Beta limitations
|
|
97
105
|
# reference: https://platform.openai.com/docs/guides/reasoning
|
|
98
106
|
if self.model_type in [ModelType.O1_MINI, ModelType.O1_PREVIEW]:
|
|
107
|
+
warnings.warn(
|
|
108
|
+
"Warning: You are using an O1 model (O1_MINI or O1_PREVIEW), "
|
|
109
|
+
"which has certain limitations, reference: "
|
|
110
|
+
"`https://platform.openai.com/docs/guides/reasoning`.",
|
|
111
|
+
UserWarning,
|
|
112
|
+
)
|
|
113
|
+
|
|
99
114
|
# Remove system message that is not supported in o1 model.
|
|
100
115
|
messages = [msg for msg in messages if msg.get("role") != "system"]
|
|
101
116
|
|
|
102
|
-
#
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
117
|
+
# Check and remove unsupported parameters and reset the fixed
|
|
118
|
+
# parameters
|
|
119
|
+
unsupported_keys = ["stream", "tools", "tool_choice"]
|
|
120
|
+
for key in unsupported_keys:
|
|
121
|
+
if key in self.model_config_dict:
|
|
122
|
+
del self.model_config_dict[key]
|
|
123
|
+
|
|
106
124
|
self.model_config_dict["temperature"] = 1.0
|
|
107
125
|
self.model_config_dict["top_p"] = 1.0
|
|
108
126
|
self.model_config_dict["n"] = 1.0
|
|
@@ -111,7 +129,7 @@ class OpenAIModel(BaseModelBackend):
|
|
|
111
129
|
|
|
112
130
|
response = self._client.chat.completions.create(
|
|
113
131
|
messages=messages,
|
|
114
|
-
model=self.model_type
|
|
132
|
+
model=self.model_type,
|
|
115
133
|
**self.model_config_dict,
|
|
116
134
|
)
|
|
117
135
|
return response
|
|
@@ -133,8 +151,9 @@ class OpenAIModel(BaseModelBackend):
|
|
|
133
151
|
|
|
134
152
|
@property
|
|
135
153
|
def stream(self) -> bool:
|
|
136
|
-
r"""Returns whether the model is in stream mode,
|
|
137
|
-
|
|
154
|
+
r"""Returns whether the model is in stream mode, which sends partial
|
|
155
|
+
results each time.
|
|
156
|
+
|
|
138
157
|
Returns:
|
|
139
158
|
bool: Whether the model is in stream mode.
|
|
140
159
|
"""
|