camel-ai 0.2.45__py3-none-any.whl → 0.2.47__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/configs/__init__.py +6 -0
- camel/configs/bedrock_config.py +73 -0
- camel/configs/lmstudio_config.py +94 -0
- camel/configs/qwen_config.py +3 -3
- camel/datasets/few_shot_generator.py +19 -3
- camel/datasets/models.py +1 -1
- camel/loaders/__init__.py +2 -0
- camel/loaders/scrapegraph_reader.py +96 -0
- camel/models/__init__.py +4 -0
- camel/models/aiml_model.py +11 -104
- camel/models/anthropic_model.py +11 -76
- camel/models/aws_bedrock_model.py +112 -0
- camel/models/deepseek_model.py +11 -44
- camel/models/gemini_model.py +10 -72
- camel/models/groq_model.py +11 -131
- camel/models/internlm_model.py +11 -61
- camel/models/lmstudio_model.py +82 -0
- camel/models/model_factory.py +7 -1
- camel/models/modelscope_model.py +11 -122
- camel/models/moonshot_model.py +10 -76
- camel/models/nemotron_model.py +4 -60
- camel/models/nvidia_model.py +11 -111
- camel/models/ollama_model.py +12 -205
- camel/models/openai_compatible_model.py +51 -12
- camel/models/openai_model.py +3 -1
- camel/models/openrouter_model.py +12 -131
- camel/models/ppio_model.py +10 -99
- camel/models/qwen_model.py +11 -122
- camel/models/reka_model.py +1 -1
- camel/models/sglang_model.py +5 -3
- camel/models/siliconflow_model.py +10 -58
- camel/models/togetherai_model.py +10 -177
- camel/models/vllm_model.py +11 -218
- camel/models/volcano_model.py +1 -15
- camel/models/yi_model.py +11 -98
- camel/models/zhipuai_model.py +11 -102
- camel/storages/__init__.py +2 -0
- camel/storages/vectordb_storages/__init__.py +2 -0
- camel/storages/vectordb_storages/oceanbase.py +458 -0
- camel/toolkits/__init__.py +4 -0
- camel/toolkits/browser_toolkit.py +4 -7
- camel/toolkits/jina_reranker_toolkit.py +231 -0
- camel/toolkits/pyautogui_toolkit.py +428 -0
- camel/toolkits/search_toolkit.py +167 -0
- camel/toolkits/video_analysis_toolkit.py +215 -80
- camel/toolkits/video_download_toolkit.py +10 -3
- camel/types/enums.py +70 -0
- camel/types/unified_model_type.py +10 -0
- camel/utils/token_counting.py +7 -3
- {camel_ai-0.2.45.dist-info → camel_ai-0.2.47.dist-info}/METADATA +13 -1
- {camel_ai-0.2.45.dist-info → camel_ai-0.2.47.dist-info}/RECORD +54 -46
- {camel_ai-0.2.45.dist-info → camel_ai-0.2.47.dist-info}/WHEEL +0 -0
- {camel_ai-0.2.45.dist-info → camel_ai-0.2.47.dist-info}/licenses/LICENSE +0 -0
camel/models/anthropic_model.py
CHANGED
|
@@ -12,14 +12,11 @@
|
|
|
12
12
|
# limitations under the License.
|
|
13
13
|
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
14
14
|
import os
|
|
15
|
-
from typing import Any, Dict,
|
|
16
|
-
|
|
17
|
-
from pydantic import BaseModel
|
|
15
|
+
from typing import Any, Dict, Optional, Union
|
|
18
16
|
|
|
19
17
|
from camel.configs import ANTHROPIC_API_PARAMS, AnthropicConfig
|
|
20
|
-
from camel.
|
|
21
|
-
from camel.
|
|
22
|
-
from camel.types import ChatCompletion, ModelType
|
|
18
|
+
from camel.models.openai_compatible_model import OpenAICompatibleModel
|
|
19
|
+
from camel.types import ModelType
|
|
23
20
|
from camel.utils import (
|
|
24
21
|
AnthropicTokenCounter,
|
|
25
22
|
BaseTokenCounter,
|
|
@@ -28,8 +25,8 @@ from camel.utils import (
|
|
|
28
25
|
)
|
|
29
26
|
|
|
30
27
|
|
|
31
|
-
class AnthropicModel(
|
|
32
|
-
r"""Anthropic API in a unified
|
|
28
|
+
class AnthropicModel(OpenAICompatibleModel):
|
|
29
|
+
r"""Anthropic API in a unified OpenAICompatibleModel interface.
|
|
33
30
|
|
|
34
31
|
Args:
|
|
35
32
|
model_type (Union[ModelType, str]): Model for which a backend is
|
|
@@ -66,8 +63,6 @@ class AnthropicModel(BaseModelBackend):
|
|
|
66
63
|
token_counter: Optional[BaseTokenCounter] = None,
|
|
67
64
|
timeout: Optional[float] = None,
|
|
68
65
|
) -> None:
|
|
69
|
-
from openai import AsyncOpenAI, OpenAI
|
|
70
|
-
|
|
71
66
|
if model_config_dict is None:
|
|
72
67
|
model_config_dict = AnthropicConfig().as_dict()
|
|
73
68
|
api_key = api_key or os.environ.get("ANTHROPIC_API_KEY")
|
|
@@ -78,14 +73,12 @@ class AnthropicModel(BaseModelBackend):
|
|
|
78
73
|
)
|
|
79
74
|
timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
|
|
80
75
|
super().__init__(
|
|
81
|
-
model_type,
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
self.async_client = AsyncOpenAI(
|
|
88
|
-
api_key=self._api_key, base_url=self._url, timeout=self._timeout
|
|
76
|
+
model_type=model_type,
|
|
77
|
+
model_config_dict=model_config_dict,
|
|
78
|
+
api_key=api_key,
|
|
79
|
+
url=url,
|
|
80
|
+
token_counter=token_counter,
|
|
81
|
+
timeout=timeout,
|
|
89
82
|
)
|
|
90
83
|
|
|
91
84
|
@property
|
|
@@ -100,54 +93,6 @@ class AnthropicModel(BaseModelBackend):
|
|
|
100
93
|
self._token_counter = AnthropicTokenCounter(self.model_type)
|
|
101
94
|
return self._token_counter
|
|
102
95
|
|
|
103
|
-
def _run(
|
|
104
|
-
self,
|
|
105
|
-
messages: List[OpenAIMessage],
|
|
106
|
-
response_format: Optional[Type[BaseModel]] = None,
|
|
107
|
-
tools: Optional[List[Dict[str, Any]]] = None,
|
|
108
|
-
):
|
|
109
|
-
r"""Run inference of Anthropic chat completion.
|
|
110
|
-
|
|
111
|
-
Args:
|
|
112
|
-
messages (List[OpenAIMessage]): Message list with the chat history
|
|
113
|
-
in OpenAI API format.
|
|
114
|
-
|
|
115
|
-
Returns:
|
|
116
|
-
ChatCompletion: Response in the OpenAI API format.
|
|
117
|
-
"""
|
|
118
|
-
response = self.client.chat.completions.create(
|
|
119
|
-
model=self.model_type,
|
|
120
|
-
messages=messages,
|
|
121
|
-
**self.model_config_dict,
|
|
122
|
-
tools=tools, # type: ignore[arg-type]
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
return response
|
|
126
|
-
|
|
127
|
-
async def _arun(
|
|
128
|
-
self,
|
|
129
|
-
messages: List[OpenAIMessage],
|
|
130
|
-
response_format: Optional[Type[BaseModel]] = None,
|
|
131
|
-
tools: Optional[List[Dict[str, Any]]] = None,
|
|
132
|
-
) -> ChatCompletion:
|
|
133
|
-
r"""Run inference of Anthropic chat completion.
|
|
134
|
-
|
|
135
|
-
Args:
|
|
136
|
-
messages (List[OpenAIMessage]): Message list with the chat history
|
|
137
|
-
in OpenAI API format.
|
|
138
|
-
|
|
139
|
-
Returns:
|
|
140
|
-
ChatCompletion: Response in the OpenAI API format.
|
|
141
|
-
"""
|
|
142
|
-
response = await self.async_client.chat.completions.create(
|
|
143
|
-
model=self.model_type,
|
|
144
|
-
messages=messages,
|
|
145
|
-
**self.model_config_dict,
|
|
146
|
-
tools=tools, # type: ignore[arg-type]
|
|
147
|
-
)
|
|
148
|
-
|
|
149
|
-
return response
|
|
150
|
-
|
|
151
96
|
def check_model_config(self):
|
|
152
97
|
r"""Check whether the model configuration is valid for anthropic
|
|
153
98
|
model backends.
|
|
@@ -162,13 +107,3 @@ class AnthropicModel(BaseModelBackend):
|
|
|
162
107
|
f"Unexpected argument `{param}` is "
|
|
163
108
|
"input into Anthropic model backend."
|
|
164
109
|
)
|
|
165
|
-
|
|
166
|
-
@property
|
|
167
|
-
def stream(self) -> bool:
|
|
168
|
-
r"""Returns whether the model is in stream mode, which sends partial
|
|
169
|
-
results each time.
|
|
170
|
-
|
|
171
|
-
Returns:
|
|
172
|
-
bool: Whether the model is in stream mode.
|
|
173
|
-
"""
|
|
174
|
-
return self.model_config_dict.get("stream", False)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
14
|
+
|
|
15
|
+
import os
|
|
16
|
+
from typing import Any, Dict, List, Optional, Type, Union
|
|
17
|
+
|
|
18
|
+
from openai import AsyncStream
|
|
19
|
+
from pydantic import BaseModel
|
|
20
|
+
|
|
21
|
+
from camel.configs import BEDROCK_API_PARAMS, BedrockConfig
|
|
22
|
+
from camel.messages import OpenAIMessage
|
|
23
|
+
from camel.models.openai_compatible_model import OpenAICompatibleModel
|
|
24
|
+
from camel.types import (
|
|
25
|
+
ChatCompletion,
|
|
26
|
+
ChatCompletionChunk,
|
|
27
|
+
ModelType,
|
|
28
|
+
)
|
|
29
|
+
from camel.utils import BaseTokenCounter, api_keys_required
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class AWSBedrockModel(OpenAICompatibleModel):
|
|
33
|
+
r"""AWS Bedrock API in a unified OpenAICompatibleModel interface.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
model_type (Union[ModelType, str]): Model for which a backend is
|
|
37
|
+
created.
|
|
38
|
+
model_config_dict (Dict[str, Any], optional): A dictionary
|
|
39
|
+
that will be fed into:obj:`openai.ChatCompletion.create()`.
|
|
40
|
+
If:obj:`None`, :obj:`BedrockConfig().as_dict()` will be used.
|
|
41
|
+
(default: :obj:`None`)
|
|
42
|
+
api_key (str, optional): The API key for authenticating with
|
|
43
|
+
the AWS Bedrock service. (default: :obj:`None`)
|
|
44
|
+
url (str, optional): The url to the AWS Bedrock service.
|
|
45
|
+
token_counter (BaseTokenCounter, optional): Token counter to
|
|
46
|
+
use for the model. If not provided, :obj:`OpenAITokenCounter(
|
|
47
|
+
ModelType.GPT_4O_MINI)` will be used.
|
|
48
|
+
(default: :obj:`None`)
|
|
49
|
+
timeout (Optional[float], optional): The timeout value in seconds for
|
|
50
|
+
API calls. If not provided, will fall back to the MODEL_TIMEOUT
|
|
51
|
+
environment variable or default to 180 seconds.
|
|
52
|
+
(default: :obj:`None`)
|
|
53
|
+
|
|
54
|
+
References:
|
|
55
|
+
https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
@api_keys_required(
|
|
59
|
+
[
|
|
60
|
+
("url", "BEDROCK_API_BASE_URL"),
|
|
61
|
+
("api_key", "BEDROCK_API_KEY"),
|
|
62
|
+
]
|
|
63
|
+
)
|
|
64
|
+
def __init__(
|
|
65
|
+
self,
|
|
66
|
+
model_type: Union[ModelType, str],
|
|
67
|
+
model_config_dict: Optional[Dict[str, Any]] = None,
|
|
68
|
+
api_key: Optional[str] = None,
|
|
69
|
+
url: Optional[str] = None,
|
|
70
|
+
token_counter: Optional[BaseTokenCounter] = None,
|
|
71
|
+
timeout: Optional[float] = None,
|
|
72
|
+
) -> None:
|
|
73
|
+
if model_config_dict is None:
|
|
74
|
+
model_config_dict = BedrockConfig().as_dict()
|
|
75
|
+
api_key = api_key or os.environ.get("BEDROCK_API_KEY")
|
|
76
|
+
url = url or os.environ.get(
|
|
77
|
+
"BEDROCK_API_BASE_URL",
|
|
78
|
+
)
|
|
79
|
+
timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
|
|
80
|
+
super().__init__(
|
|
81
|
+
model_type=model_type,
|
|
82
|
+
model_config_dict=model_config_dict,
|
|
83
|
+
api_key=api_key,
|
|
84
|
+
url=url,
|
|
85
|
+
token_counter=token_counter,
|
|
86
|
+
timeout=timeout,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
async def _arun(
|
|
90
|
+
self,
|
|
91
|
+
messages: List[OpenAIMessage],
|
|
92
|
+
response_format: Optional[Type[BaseModel]] = None,
|
|
93
|
+
tools: Optional[List[Dict[str, Any]]] = None,
|
|
94
|
+
) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
|
|
95
|
+
raise NotImplementedError(
|
|
96
|
+
"AWS Bedrock does not support async inference."
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
def check_model_config(self):
|
|
100
|
+
r"""Check whether the input model configuration contains unexpected
|
|
101
|
+
arguments.
|
|
102
|
+
|
|
103
|
+
Raises:
|
|
104
|
+
ValueError: If the model configuration dictionary contains any
|
|
105
|
+
unexpected argument for this model class.
|
|
106
|
+
"""
|
|
107
|
+
for param in self.model_config_dict:
|
|
108
|
+
if param not in BEDROCK_API_PARAMS:
|
|
109
|
+
raise ValueError(
|
|
110
|
+
f"Invalid parameter '{param}' in model_config_dict. "
|
|
111
|
+
f"Valid parameters are: {BEDROCK_API_PARAMS}"
|
|
112
|
+
)
|
camel/models/deepseek_model.py
CHANGED
|
@@ -15,20 +15,20 @@
|
|
|
15
15
|
import os
|
|
16
16
|
from typing import Any, Dict, List, Optional, Type, Union
|
|
17
17
|
|
|
18
|
-
from openai import
|
|
18
|
+
from openai import AsyncStream, Stream
|
|
19
19
|
from pydantic import BaseModel
|
|
20
20
|
|
|
21
21
|
from camel.configs import DEEPSEEK_API_PARAMS, DeepSeekConfig
|
|
22
22
|
from camel.logger import get_logger
|
|
23
23
|
from camel.messages import OpenAIMessage
|
|
24
24
|
from camel.models._utils import try_modify_message_with_format
|
|
25
|
-
from camel.models.
|
|
25
|
+
from camel.models.openai_compatible_model import OpenAICompatibleModel
|
|
26
26
|
from camel.types import (
|
|
27
27
|
ChatCompletion,
|
|
28
28
|
ChatCompletionChunk,
|
|
29
29
|
ModelType,
|
|
30
30
|
)
|
|
31
|
-
from camel.utils import BaseTokenCounter,
|
|
31
|
+
from camel.utils import BaseTokenCounter, api_keys_required
|
|
32
32
|
|
|
33
33
|
logger = get_logger(__name__)
|
|
34
34
|
|
|
@@ -43,8 +43,8 @@ REASONSER_UNSUPPORTED_PARAMS = [
|
|
|
43
43
|
]
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
class DeepSeekModel(
|
|
47
|
-
r"""DeepSeek API in a unified
|
|
46
|
+
class DeepSeekModel(OpenAICompatibleModel):
|
|
47
|
+
r"""DeepSeek API in a unified OpenAICompatibleModel interface.
|
|
48
48
|
|
|
49
49
|
Args:
|
|
50
50
|
model_type (Union[ModelType, str]): Model for which a backend is
|
|
@@ -92,37 +92,14 @@ class DeepSeekModel(BaseModelBackend):
|
|
|
92
92
|
)
|
|
93
93
|
timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
|
|
94
94
|
super().__init__(
|
|
95
|
-
model_type,
|
|
95
|
+
model_type=model_type,
|
|
96
|
+
model_config_dict=model_config_dict,
|
|
97
|
+
api_key=api_key,
|
|
98
|
+
url=url,
|
|
99
|
+
token_counter=token_counter,
|
|
100
|
+
timeout=timeout,
|
|
96
101
|
)
|
|
97
102
|
|
|
98
|
-
self._client = OpenAI(
|
|
99
|
-
timeout=self._timeout,
|
|
100
|
-
max_retries=3,
|
|
101
|
-
api_key=self._api_key,
|
|
102
|
-
base_url=self._url,
|
|
103
|
-
)
|
|
104
|
-
|
|
105
|
-
self._async_client = AsyncOpenAI(
|
|
106
|
-
timeout=self._timeout,
|
|
107
|
-
max_retries=3,
|
|
108
|
-
api_key=self._api_key,
|
|
109
|
-
base_url=self._url,
|
|
110
|
-
)
|
|
111
|
-
|
|
112
|
-
@property
|
|
113
|
-
def token_counter(self) -> BaseTokenCounter:
|
|
114
|
-
r"""Initialize the token counter for the model backend.
|
|
115
|
-
|
|
116
|
-
Returns:
|
|
117
|
-
BaseTokenCounter: The token counter following the model's
|
|
118
|
-
tokenization style.
|
|
119
|
-
"""
|
|
120
|
-
if not self._token_counter:
|
|
121
|
-
self._token_counter = OpenAITokenCounter(
|
|
122
|
-
model=ModelType.GPT_4O_MINI
|
|
123
|
-
)
|
|
124
|
-
return self._token_counter
|
|
125
|
-
|
|
126
103
|
def _prepare_request(
|
|
127
104
|
self,
|
|
128
105
|
messages: List[OpenAIMessage],
|
|
@@ -270,13 +247,3 @@ class DeepSeekModel(BaseModelBackend):
|
|
|
270
247
|
f"Unexpected argument `{param}` is "
|
|
271
248
|
"input into DeepSeek model backend."
|
|
272
249
|
)
|
|
273
|
-
|
|
274
|
-
@property
|
|
275
|
-
def stream(self) -> bool:
|
|
276
|
-
r"""Returns whether the model is in stream mode, which sends partial
|
|
277
|
-
results each time.
|
|
278
|
-
|
|
279
|
-
Returns:
|
|
280
|
-
bool: Whether the model is in stream mode.
|
|
281
|
-
"""
|
|
282
|
-
return self.model_config_dict.get("stream", False)
|
camel/models/gemini_model.py
CHANGED
|
@@ -14,12 +14,12 @@
|
|
|
14
14
|
import os
|
|
15
15
|
from typing import Any, Dict, List, Optional, Type, Union
|
|
16
16
|
|
|
17
|
-
from openai import
|
|
17
|
+
from openai import AsyncStream, Stream
|
|
18
18
|
from pydantic import BaseModel
|
|
19
19
|
|
|
20
20
|
from camel.configs import Gemini_API_PARAMS, GeminiConfig
|
|
21
21
|
from camel.messages import OpenAIMessage
|
|
22
|
-
from camel.models import
|
|
22
|
+
from camel.models.openai_compatible_model import OpenAICompatibleModel
|
|
23
23
|
from camel.types import (
|
|
24
24
|
ChatCompletion,
|
|
25
25
|
ChatCompletionChunk,
|
|
@@ -27,13 +27,12 @@ from camel.types import (
|
|
|
27
27
|
)
|
|
28
28
|
from camel.utils import (
|
|
29
29
|
BaseTokenCounter,
|
|
30
|
-
OpenAITokenCounter,
|
|
31
30
|
api_keys_required,
|
|
32
31
|
)
|
|
33
32
|
|
|
34
33
|
|
|
35
|
-
class GeminiModel(
|
|
36
|
-
r"""Gemini API in a unified
|
|
34
|
+
class GeminiModel(OpenAICompatibleModel):
|
|
35
|
+
r"""Gemini API in a unified OpenAICompatibleModel interface.
|
|
37
36
|
|
|
38
37
|
Args:
|
|
39
38
|
model_type (Union[ModelType, str]): Model for which a backend is
|
|
@@ -80,19 +79,12 @@ class GeminiModel(BaseModelBackend):
|
|
|
80
79
|
)
|
|
81
80
|
timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
|
|
82
81
|
super().__init__(
|
|
83
|
-
model_type,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
base_url=self._url,
|
|
90
|
-
)
|
|
91
|
-
self._async_client = AsyncOpenAI(
|
|
92
|
-
timeout=self._timeout,
|
|
93
|
-
max_retries=3,
|
|
94
|
-
api_key=self._api_key,
|
|
95
|
-
base_url=self._url,
|
|
82
|
+
model_type=model_type,
|
|
83
|
+
model_config_dict=model_config_dict,
|
|
84
|
+
api_key=api_key,
|
|
85
|
+
url=url,
|
|
86
|
+
token_counter=token_counter,
|
|
87
|
+
timeout=timeout,
|
|
96
88
|
)
|
|
97
89
|
|
|
98
90
|
def _process_messages(self, messages) -> List[OpenAIMessage]:
|
|
@@ -247,50 +239,6 @@ class GeminiModel(BaseModelBackend):
|
|
|
247
239
|
**request_config,
|
|
248
240
|
)
|
|
249
241
|
|
|
250
|
-
def _request_parse(
|
|
251
|
-
self,
|
|
252
|
-
messages: List[OpenAIMessage],
|
|
253
|
-
response_format: Type[BaseModel],
|
|
254
|
-
) -> ChatCompletion:
|
|
255
|
-
request_config = self.model_config_dict.copy()
|
|
256
|
-
|
|
257
|
-
request_config["response_format"] = response_format
|
|
258
|
-
request_config.pop("stream", None)
|
|
259
|
-
|
|
260
|
-
return self._client.beta.chat.completions.parse(
|
|
261
|
-
messages=messages,
|
|
262
|
-
model=self.model_type,
|
|
263
|
-
**request_config,
|
|
264
|
-
)
|
|
265
|
-
|
|
266
|
-
async def _arequest_parse(
|
|
267
|
-
self,
|
|
268
|
-
messages: List[OpenAIMessage],
|
|
269
|
-
response_format: Type[BaseModel],
|
|
270
|
-
) -> ChatCompletion:
|
|
271
|
-
request_config = self.model_config_dict.copy()
|
|
272
|
-
|
|
273
|
-
request_config["response_format"] = response_format
|
|
274
|
-
request_config.pop("stream", None)
|
|
275
|
-
|
|
276
|
-
return await self._async_client.beta.chat.completions.parse(
|
|
277
|
-
messages=messages,
|
|
278
|
-
model=self.model_type,
|
|
279
|
-
**request_config,
|
|
280
|
-
)
|
|
281
|
-
|
|
282
|
-
@property
|
|
283
|
-
def token_counter(self) -> BaseTokenCounter:
|
|
284
|
-
r"""Initialize the token counter for the model backend.
|
|
285
|
-
|
|
286
|
-
Returns:
|
|
287
|
-
BaseTokenCounter: The token counter following the model's
|
|
288
|
-
tokenization style.
|
|
289
|
-
"""
|
|
290
|
-
if not self._token_counter:
|
|
291
|
-
self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)
|
|
292
|
-
return self._token_counter
|
|
293
|
-
|
|
294
242
|
def check_model_config(self):
|
|
295
243
|
r"""Check whether the model configuration contains any
|
|
296
244
|
unexpected arguments to Gemini API.
|
|
@@ -305,13 +253,3 @@ class GeminiModel(BaseModelBackend):
|
|
|
305
253
|
f"Unexpected argument `{param}` is "
|
|
306
254
|
"input into Gemini model backend."
|
|
307
255
|
)
|
|
308
|
-
|
|
309
|
-
@property
|
|
310
|
-
def stream(self) -> bool:
|
|
311
|
-
r"""Returns whether the model is in stream mode, which sends partial
|
|
312
|
-
results each time.
|
|
313
|
-
|
|
314
|
-
Returns:
|
|
315
|
-
bool: Whether the model is in stream mode.
|
|
316
|
-
"""
|
|
317
|
-
return self.model_config_dict.get('stream', False)
|
camel/models/groq_model.py
CHANGED
|
@@ -12,29 +12,19 @@
|
|
|
12
12
|
# limitations under the License.
|
|
13
13
|
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
14
14
|
import os
|
|
15
|
-
from typing import Any, Dict,
|
|
16
|
-
|
|
17
|
-
from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream
|
|
18
|
-
from pydantic import BaseModel
|
|
15
|
+
from typing import Any, Dict, Optional, Union
|
|
19
16
|
|
|
20
17
|
from camel.configs import GROQ_API_PARAMS, GroqConfig
|
|
21
|
-
from camel.
|
|
22
|
-
from camel.
|
|
23
|
-
from camel.models._utils import try_modify_message_with_format
|
|
24
|
-
from camel.types import (
|
|
25
|
-
ChatCompletion,
|
|
26
|
-
ChatCompletionChunk,
|
|
27
|
-
ModelType,
|
|
28
|
-
)
|
|
18
|
+
from camel.models.openai_compatible_model import OpenAICompatibleModel
|
|
19
|
+
from camel.types import ModelType
|
|
29
20
|
from camel.utils import (
|
|
30
21
|
BaseTokenCounter,
|
|
31
|
-
OpenAITokenCounter,
|
|
32
22
|
api_keys_required,
|
|
33
23
|
)
|
|
34
24
|
|
|
35
25
|
|
|
36
|
-
class GroqModel(
|
|
37
|
-
r"""LLM API served by Groq in a unified
|
|
26
|
+
class GroqModel(OpenAICompatibleModel):
|
|
27
|
+
r"""LLM API served by Groq in a unified OpenAICompatibleModel interface.
|
|
38
28
|
|
|
39
29
|
Args:
|
|
40
30
|
model_type (Union[ModelType, str]): Model for which a backend is
|
|
@@ -75,114 +65,14 @@ class GroqModel(BaseModelBackend):
|
|
|
75
65
|
)
|
|
76
66
|
timeout = timeout or float(os.environ.get("MODEL_TIMEOUT", 180))
|
|
77
67
|
super().__init__(
|
|
78
|
-
model_type,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
base_url=self._url,
|
|
85
|
-
)
|
|
86
|
-
self._async_client = AsyncOpenAI(
|
|
87
|
-
timeout=self._timeout,
|
|
88
|
-
max_retries=3,
|
|
89
|
-
api_key=self._api_key,
|
|
90
|
-
base_url=self._url,
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
@property
|
|
94
|
-
def token_counter(self) -> BaseTokenCounter:
|
|
95
|
-
r"""Initialize the token counter for the model backend.
|
|
96
|
-
|
|
97
|
-
Returns:
|
|
98
|
-
BaseTokenCounter: The token counter following the model's
|
|
99
|
-
tokenization style.
|
|
100
|
-
"""
|
|
101
|
-
if not self._token_counter:
|
|
102
|
-
self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)
|
|
103
|
-
return self._token_counter
|
|
104
|
-
|
|
105
|
-
def _prepare_request(
|
|
106
|
-
self,
|
|
107
|
-
messages: List[OpenAIMessage],
|
|
108
|
-
response_format: Optional[Type[BaseModel]] = None,
|
|
109
|
-
tools: Optional[List[Dict[str, Any]]] = None,
|
|
110
|
-
) -> Dict[str, Any]:
|
|
111
|
-
request_config = self.model_config_dict.copy()
|
|
112
|
-
if tools:
|
|
113
|
-
request_config["tools"] = tools
|
|
114
|
-
elif response_format:
|
|
115
|
-
try_modify_message_with_format(messages[-1], response_format)
|
|
116
|
-
request_config["response_format"] = {"type": "json_object"}
|
|
117
|
-
|
|
118
|
-
return request_config
|
|
119
|
-
|
|
120
|
-
def _run(
|
|
121
|
-
self,
|
|
122
|
-
messages: List[OpenAIMessage],
|
|
123
|
-
response_format: Optional[type[BaseModel]] = None,
|
|
124
|
-
tools: Optional[List[Dict[str, Any]]] = None,
|
|
125
|
-
) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
|
|
126
|
-
r"""Runs inference of Groq chat completion.
|
|
127
|
-
|
|
128
|
-
Args:
|
|
129
|
-
messages (List[OpenAIMessage]): Message list with the chat history
|
|
130
|
-
in OpenAI API format.
|
|
131
|
-
response_format (Optional[Type[BaseModel]]): The format of the
|
|
132
|
-
response.
|
|
133
|
-
tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
|
|
134
|
-
use for the request.
|
|
135
|
-
|
|
136
|
-
Returns:
|
|
137
|
-
Union[ChatCompletion, Stream[ChatCompletionChunk]]:
|
|
138
|
-
`ChatCompletion` in the non-stream mode, or
|
|
139
|
-
`Stream[ChatCompletionChunk]` in the stream mode.
|
|
140
|
-
"""
|
|
141
|
-
request_config = self._prepare_request(
|
|
142
|
-
messages, response_format, tools
|
|
68
|
+
model_type=model_type,
|
|
69
|
+
model_config_dict=model_config_dict,
|
|
70
|
+
api_key=api_key,
|
|
71
|
+
url=url,
|
|
72
|
+
token_counter=token_counter,
|
|
73
|
+
timeout=timeout,
|
|
143
74
|
)
|
|
144
75
|
|
|
145
|
-
response = self._client.chat.completions.create(
|
|
146
|
-
messages=messages,
|
|
147
|
-
model=self.model_type,
|
|
148
|
-
**request_config,
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
return response
|
|
152
|
-
|
|
153
|
-
async def _arun(
|
|
154
|
-
self,
|
|
155
|
-
messages: List[OpenAIMessage],
|
|
156
|
-
response_format: Optional[type[BaseModel]] = None,
|
|
157
|
-
tools: Optional[List[Dict[str, Any]]] = None,
|
|
158
|
-
) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
|
|
159
|
-
r"""Runs inference of Groq chat completion asynchronously.
|
|
160
|
-
|
|
161
|
-
Args:
|
|
162
|
-
messages (List[OpenAIMessage]): Message list with the chat history
|
|
163
|
-
in OpenAI API format.
|
|
164
|
-
response_format (Optional[Type[BaseModel]]): The format of the
|
|
165
|
-
response.
|
|
166
|
-
tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
|
|
167
|
-
use for the request.
|
|
168
|
-
|
|
169
|
-
Returns:
|
|
170
|
-
Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
|
|
171
|
-
`ChatCompletion` in the non-stream mode, or
|
|
172
|
-
`AsyncStream[ChatCompletionChunk]` in the stream mode.
|
|
173
|
-
"""
|
|
174
|
-
request_config = self._prepare_request(
|
|
175
|
-
messages, response_format, tools
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
response = await self._async_client.chat.completions.create(
|
|
179
|
-
messages=messages,
|
|
180
|
-
model=self.model_type,
|
|
181
|
-
**request_config,
|
|
182
|
-
)
|
|
183
|
-
|
|
184
|
-
return response
|
|
185
|
-
|
|
186
76
|
def check_model_config(self):
|
|
187
77
|
r"""Check whether the model configuration contains any unexpected
|
|
188
78
|
arguments to Groq API. But Groq API does not have any additional
|
|
@@ -198,13 +88,3 @@ class GroqModel(BaseModelBackend):
|
|
|
198
88
|
f"Unexpected argument `{param}` is "
|
|
199
89
|
"input into Groq model backend."
|
|
200
90
|
)
|
|
201
|
-
|
|
202
|
-
@property
|
|
203
|
-
def stream(self) -> bool:
|
|
204
|
-
r"""Returns whether the model is in stream mode, which sends partial
|
|
205
|
-
results each time.
|
|
206
|
-
|
|
207
|
-
Returns:
|
|
208
|
-
bool: Whether the model is in stream mode.
|
|
209
|
-
"""
|
|
210
|
-
return self.model_config_dict.get("stream", False)
|