camel-ai 0.2.11__py3-none-any.whl → 0.2.12__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 +13 -1
- camel/benchmarks/__init__.py +18 -0
- camel/benchmarks/base.py +152 -0
- camel/benchmarks/gaia.py +478 -0
- camel/configs/__init__.py +3 -0
- camel/configs/ollama_config.py +4 -2
- camel/configs/sglang_config.py +71 -0
- camel/data_collector/__init__.py +19 -0
- camel/data_collector/alpaca_collector.py +127 -0
- camel/data_collector/base.py +211 -0
- camel/data_collector/sharegpt_collector.py +205 -0
- camel/datahubs/__init__.py +23 -0
- camel/datahubs/base.py +136 -0
- camel/datahubs/huggingface.py +433 -0
- camel/datahubs/models.py +22 -0
- camel/interpreters/__init__.py +2 -0
- camel/interpreters/e2b_interpreter.py +136 -0
- camel/loaders/__init__.py +3 -1
- camel/loaders/base_io.py +41 -41
- camel/messages/__init__.py +2 -0
- camel/models/__init__.py +2 -0
- camel/models/anthropic_model.py +14 -4
- camel/models/base_model.py +28 -0
- camel/models/groq_model.py +1 -1
- camel/models/model_factory.py +3 -0
- camel/models/ollama_model.py +12 -0
- camel/models/openai_model.py +0 -26
- camel/models/reward/__init__.py +22 -0
- camel/models/reward/base_reward_model.py +58 -0
- camel/models/reward/evaluator.py +63 -0
- camel/models/reward/nemotron_model.py +112 -0
- camel/models/sglang_model.py +225 -0
- camel/models/vllm_model.py +1 -1
- camel/personas/persona_hub.py +2 -2
- camel/schemas/openai_converter.py +2 -2
- camel/societies/workforce/role_playing_worker.py +2 -2
- camel/societies/workforce/single_agent_worker.py +2 -2
- camel/societies/workforce/workforce.py +3 -3
- camel/storages/object_storages/amazon_s3.py +2 -2
- camel/storages/object_storages/azure_blob.py +2 -2
- camel/storages/object_storages/google_cloud.py +2 -2
- camel/toolkits/__init__.py +2 -0
- camel/toolkits/code_execution.py +5 -1
- camel/toolkits/function_tool.py +41 -0
- camel/toolkits/math_toolkit.py +47 -16
- camel/toolkits/search_toolkit.py +154 -2
- camel/toolkits/stripe_toolkit.py +273 -0
- camel/types/__init__.py +2 -0
- camel/types/enums.py +27 -2
- camel/utils/token_counting.py +22 -10
- {camel_ai-0.2.11.dist-info → camel_ai-0.2.12.dist-info}/METADATA +13 -6
- {camel_ai-0.2.11.dist-info → camel_ai-0.2.12.dist-info}/RECORD +55 -36
- {camel_ai-0.2.11.dist-info → camel_ai-0.2.12.dist-info}/LICENSE +0 -0
- {camel_ai-0.2.11.dist-info → camel_ai-0.2.12.dist-info}/WHEEL +0 -0
camel/loaders/base_io.py
CHANGED
|
@@ -22,6 +22,47 @@ from typing import Any, Dict, List, Optional
|
|
|
22
22
|
from camel.utils import dependencies_required
|
|
23
23
|
|
|
24
24
|
|
|
25
|
+
def create_file(file: BytesIO, filename: str) -> "File":
|
|
26
|
+
r"""Reads an uploaded file and returns a File object.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
file (BytesIO): A BytesIO object representing the contents of the
|
|
30
|
+
file.
|
|
31
|
+
filename (str): The name of the file.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
File: A File object.
|
|
35
|
+
"""
|
|
36
|
+
ext_to_cls = {
|
|
37
|
+
"docx": DocxFile,
|
|
38
|
+
"pdf": PdfFile,
|
|
39
|
+
"txt": TxtFile,
|
|
40
|
+
"json": JsonFile,
|
|
41
|
+
"html": HtmlFile,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
ext = filename.split(".")[-1].lower()
|
|
45
|
+
if ext not in ext_to_cls:
|
|
46
|
+
raise NotImplementedError(f"File type {ext} not supported")
|
|
47
|
+
|
|
48
|
+
out_file = ext_to_cls[ext].from_bytes(file, filename)
|
|
49
|
+
return out_file
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def create_file_from_raw_bytes(raw_bytes: bytes, filename: str) -> "File":
|
|
53
|
+
r"""Reads raw bytes and returns a File object.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
raw_bytes (bytes): The raw bytes content of the file.
|
|
57
|
+
filename (str): The name of the file.
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
File: A File object.
|
|
61
|
+
"""
|
|
62
|
+
file = BytesIO(raw_bytes)
|
|
63
|
+
return create_file(file, filename)
|
|
64
|
+
|
|
65
|
+
|
|
25
66
|
class File(ABC):
|
|
26
67
|
r"""Represents an uploaded file comprised of Documents.
|
|
27
68
|
|
|
@@ -79,47 +120,6 @@ class File(ABC):
|
|
|
79
120
|
file = BytesIO(raw_bytes)
|
|
80
121
|
return cls.from_bytes(file, filename)
|
|
81
122
|
|
|
82
|
-
@staticmethod
|
|
83
|
-
def create_file(file: BytesIO, filename: str) -> "File":
|
|
84
|
-
r"""Reads an uploaded file and returns a File object.
|
|
85
|
-
|
|
86
|
-
Args:
|
|
87
|
-
file (BytesIO): A BytesIO object representing the contents of the
|
|
88
|
-
file.
|
|
89
|
-
filename (str): The name of the file.
|
|
90
|
-
|
|
91
|
-
Returns:
|
|
92
|
-
File: A File object.
|
|
93
|
-
"""
|
|
94
|
-
ext_to_cls = {
|
|
95
|
-
"docx": DocxFile,
|
|
96
|
-
"pdf": PdfFile,
|
|
97
|
-
"txt": TxtFile,
|
|
98
|
-
"json": JsonFile,
|
|
99
|
-
"html": HtmlFile,
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
ext = filename.split(".")[-1].lower()
|
|
103
|
-
if ext not in ext_to_cls:
|
|
104
|
-
raise NotImplementedError(f"File type {ext} not supported")
|
|
105
|
-
|
|
106
|
-
out_file = ext_to_cls[ext].from_bytes(file, filename)
|
|
107
|
-
return out_file
|
|
108
|
-
|
|
109
|
-
@staticmethod
|
|
110
|
-
def create_file_from_raw_bytes(raw_bytes: bytes, filename: str) -> "File":
|
|
111
|
-
r"""Reads raw bytes and returns a File object.
|
|
112
|
-
|
|
113
|
-
Args:
|
|
114
|
-
raw_bytes (bytes): The raw bytes content of the file.
|
|
115
|
-
filename (str): The name of the file.
|
|
116
|
-
|
|
117
|
-
Returns:
|
|
118
|
-
File: A File object.
|
|
119
|
-
"""
|
|
120
|
-
file = BytesIO(raw_bytes)
|
|
121
|
-
return File.create_file(file, filename)
|
|
122
|
-
|
|
123
123
|
def __repr__(self) -> str:
|
|
124
124
|
return (
|
|
125
125
|
f"File(name={self.name}, id={self.file_id}, "
|
camel/messages/__init__.py
CHANGED
|
@@ -20,6 +20,7 @@ from camel.types import (
|
|
|
20
20
|
)
|
|
21
21
|
|
|
22
22
|
from .conversion import (
|
|
23
|
+
AlpacaItem,
|
|
23
24
|
HermesFunctionFormatter,
|
|
24
25
|
ShareGPTMessage,
|
|
25
26
|
)
|
|
@@ -52,4 +53,5 @@ __all__ = [
|
|
|
52
53
|
'ShareGPTMessage',
|
|
53
54
|
'BaseMessage',
|
|
54
55
|
'FunctionCallingMessage',
|
|
56
|
+
'AlpacaItem',
|
|
55
57
|
]
|
camel/models/__init__.py
CHANGED
|
@@ -31,6 +31,7 @@ from .openai_model import OpenAIModel
|
|
|
31
31
|
from .qwen_model import QwenModel
|
|
32
32
|
from .reka_model import RekaModel
|
|
33
33
|
from .samba_model import SambaModel
|
|
34
|
+
from .sglang_model import SGLangModel
|
|
34
35
|
from .stub_model import StubModel
|
|
35
36
|
from .togetherai_model import TogetherAIModel
|
|
36
37
|
from .vllm_model import VLLMModel
|
|
@@ -55,6 +56,7 @@ __all__ = [
|
|
|
55
56
|
'NvidiaModel',
|
|
56
57
|
'OllamaModel',
|
|
57
58
|
'VLLMModel',
|
|
59
|
+
'SGLangModel',
|
|
58
60
|
'GeminiModel',
|
|
59
61
|
'OpenAICompatibleModel',
|
|
60
62
|
'RekaModel',
|
camel/models/anthropic_model.py
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
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, List, Optional, Union
|
|
15
|
+
from typing import Any, Dict, List, Literal, Optional, Union
|
|
16
16
|
|
|
17
17
|
from camel.configs import ANTHROPIC_API_PARAMS, AnthropicConfig
|
|
18
18
|
from camel.messages import OpenAIMessage
|
|
@@ -94,19 +94,29 @@ class AnthropicModel(BaseModelBackend):
|
|
|
94
94
|
tokenization style.
|
|
95
95
|
"""
|
|
96
96
|
if not self._token_counter:
|
|
97
|
-
self._token_counter = AnthropicTokenCounter()
|
|
97
|
+
self._token_counter = AnthropicTokenCounter(self.model_type)
|
|
98
98
|
return self._token_counter
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
@dependencies_required('anthropic')
|
|
101
|
+
def count_tokens_from_prompt(
|
|
102
|
+
self, prompt: str, role: Literal["user", "assistant"]
|
|
103
|
+
) -> int:
|
|
101
104
|
r"""Count the number of tokens from a prompt.
|
|
102
105
|
|
|
103
106
|
Args:
|
|
104
107
|
prompt (str): The prompt string.
|
|
108
|
+
role (Literal["user", "assistant"]): The role of the message
|
|
109
|
+
sender, either "user" or "assistant".
|
|
105
110
|
|
|
106
111
|
Returns:
|
|
107
112
|
int: The number of tokens in the prompt.
|
|
108
113
|
"""
|
|
109
|
-
|
|
114
|
+
from anthropic.types.beta import BetaMessageParam
|
|
115
|
+
|
|
116
|
+
return self.client.beta.messages.count_tokens(
|
|
117
|
+
messages=[BetaMessageParam(content=prompt, role=role)],
|
|
118
|
+
model=self.model_type,
|
|
119
|
+
).input_tokens
|
|
110
120
|
|
|
111
121
|
@api_keys_required("ANTHROPIC_API_KEY")
|
|
112
122
|
def run(
|
camel/models/base_model.py
CHANGED
|
@@ -21,6 +21,7 @@ from camel.types import (
|
|
|
21
21
|
ChatCompletion,
|
|
22
22
|
ChatCompletionChunk,
|
|
23
23
|
ModelType,
|
|
24
|
+
ParsedChatCompletion,
|
|
24
25
|
UnifiedModelType,
|
|
25
26
|
)
|
|
26
27
|
from camel.utils import BaseTokenCounter
|
|
@@ -114,6 +115,33 @@ class BaseModelBackend(ABC):
|
|
|
114
115
|
"""
|
|
115
116
|
return self.token_counter.count_tokens_from_messages(messages)
|
|
116
117
|
|
|
118
|
+
def _to_chat_completion(
|
|
119
|
+
self, response: ParsedChatCompletion
|
|
120
|
+
) -> ChatCompletion:
|
|
121
|
+
if len(response.choices) > 1:
|
|
122
|
+
print("Warning: Multiple response choices detected")
|
|
123
|
+
|
|
124
|
+
choice = dict(
|
|
125
|
+
index=response.choices[0].index,
|
|
126
|
+
message={
|
|
127
|
+
"role": response.choices[0].message.role,
|
|
128
|
+
"content": response.choices[0].message.content,
|
|
129
|
+
"tool_calls": response.choices[0].message.tool_calls,
|
|
130
|
+
"parsed": response.choices[0].message.parsed,
|
|
131
|
+
},
|
|
132
|
+
finish_reason=response.choices[0].finish_reason,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
obj = ChatCompletion.construct(
|
|
136
|
+
id=response.id,
|
|
137
|
+
choices=[choice],
|
|
138
|
+
created=response.created,
|
|
139
|
+
model=response.model,
|
|
140
|
+
object="chat.completion",
|
|
141
|
+
usage=response.usage,
|
|
142
|
+
)
|
|
143
|
+
return obj
|
|
144
|
+
|
|
117
145
|
@property
|
|
118
146
|
def token_limit(self) -> int:
|
|
119
147
|
r"""Returns the maximum token limit for a given model.
|
camel/models/groq_model.py
CHANGED
|
@@ -63,7 +63,7 @@ class GroqModel(BaseModelBackend):
|
|
|
63
63
|
model_config_dict = GroqConfig().as_dict()
|
|
64
64
|
api_key = api_key or os.environ.get("GROQ_API_KEY")
|
|
65
65
|
url = url or os.environ.get(
|
|
66
|
-
"GROQ_API_BASE_URL"
|
|
66
|
+
"GROQ_API_BASE_URL", "https://api.groq.com/openai/v1"
|
|
67
67
|
)
|
|
68
68
|
super().__init__(
|
|
69
69
|
model_type, model_config_dict, api_key, url, token_counter
|
camel/models/model_factory.py
CHANGED
|
@@ -29,6 +29,7 @@ from camel.models.openai_model import OpenAIModel
|
|
|
29
29
|
from camel.models.qwen_model import QwenModel
|
|
30
30
|
from camel.models.reka_model import RekaModel
|
|
31
31
|
from camel.models.samba_model import SambaModel
|
|
32
|
+
from camel.models.sglang_model import SGLangModel
|
|
32
33
|
from camel.models.stub_model import StubModel
|
|
33
34
|
from camel.models.togetherai_model import TogetherAIModel
|
|
34
35
|
from camel.models.vllm_model import VLLMModel
|
|
@@ -86,6 +87,8 @@ class ModelFactory:
|
|
|
86
87
|
model_class = OllamaModel
|
|
87
88
|
elif model_platform.is_vllm:
|
|
88
89
|
model_class = VLLMModel
|
|
90
|
+
elif model_platform.is_sglang:
|
|
91
|
+
model_class = SGLangModel
|
|
89
92
|
elif model_platform.is_openai_compatible_model:
|
|
90
93
|
model_class = OpenAICompatibleModel
|
|
91
94
|
elif model_platform.is_samba:
|
camel/models/ollama_model.py
CHANGED
|
@@ -134,6 +134,18 @@ class OllamaModel(BaseModelBackend):
|
|
|
134
134
|
`ChatCompletion` in the non-stream mode, or
|
|
135
135
|
`Stream[ChatCompletionChunk]` in the stream mode.
|
|
136
136
|
"""
|
|
137
|
+
if self.model_config_dict.get("response_format"):
|
|
138
|
+
# stream is not supported in beta.chat.completions.parse
|
|
139
|
+
if "stream" in self.model_config_dict:
|
|
140
|
+
del self.model_config_dict["stream"]
|
|
141
|
+
|
|
142
|
+
response = self._client.beta.chat.completions.parse(
|
|
143
|
+
messages=messages,
|
|
144
|
+
model=self.model_type,
|
|
145
|
+
**self.model_config_dict,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
return self._to_chat_completion(response)
|
|
137
149
|
|
|
138
150
|
response = self._client.chat.completions.create(
|
|
139
151
|
messages=messages,
|
camel/models/openai_model.py
CHANGED
|
@@ -24,7 +24,6 @@ from camel.types import (
|
|
|
24
24
|
ChatCompletion,
|
|
25
25
|
ChatCompletionChunk,
|
|
26
26
|
ModelType,
|
|
27
|
-
ParsedChatCompletion,
|
|
28
27
|
)
|
|
29
28
|
from camel.utils import (
|
|
30
29
|
BaseTokenCounter,
|
|
@@ -148,31 +147,6 @@ class OpenAIModel(BaseModelBackend):
|
|
|
148
147
|
)
|
|
149
148
|
return response
|
|
150
149
|
|
|
151
|
-
def _to_chat_completion(
|
|
152
|
-
self, response: "ParsedChatCompletion"
|
|
153
|
-
) -> ChatCompletion:
|
|
154
|
-
# TODO: Handle n > 1 or warn consumers it's not supported
|
|
155
|
-
choice = dict(
|
|
156
|
-
index=response.choices[0].index,
|
|
157
|
-
message={
|
|
158
|
-
"role": response.choices[0].message.role,
|
|
159
|
-
"content": response.choices[0].message.content,
|
|
160
|
-
"tool_calls": response.choices[0].message.tool_calls,
|
|
161
|
-
"parsed": response.choices[0].message.parsed,
|
|
162
|
-
},
|
|
163
|
-
finish_reason=response.choices[0].finish_reason,
|
|
164
|
-
)
|
|
165
|
-
|
|
166
|
-
obj = ChatCompletion.construct(
|
|
167
|
-
id=response.id,
|
|
168
|
-
choices=[choice],
|
|
169
|
-
created=response.created,
|
|
170
|
-
model=response.model,
|
|
171
|
-
object="chat.completion",
|
|
172
|
-
usage=response.usage,
|
|
173
|
-
)
|
|
174
|
-
return obj
|
|
175
|
-
|
|
176
150
|
def check_model_config(self):
|
|
177
151
|
r"""Check whether the model configuration contains any
|
|
178
152
|
unexpected arguments to OpenAI API.
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
from .base_reward_model import BaseRewardModel
|
|
15
|
+
from .evaluator import Evaluator
|
|
16
|
+
from .nemotron_model import NemotronRewardModel
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
'BaseRewardModel',
|
|
20
|
+
'NemotronRewardModel',
|
|
21
|
+
'Evaluator',
|
|
22
|
+
]
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
from abc import ABC, abstractmethod
|
|
15
|
+
from typing import Dict, List, Optional, Union
|
|
16
|
+
|
|
17
|
+
from camel.types import ModelType
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class BaseRewardModel(ABC):
|
|
21
|
+
r"""Abstract base class for reward models. Reward models are used to
|
|
22
|
+
evaluate messages and return scores based on different criteria.
|
|
23
|
+
|
|
24
|
+
Subclasses should implement the 'evaluate' and 'get_scores_types' methods.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
model_type: Union[ModelType, str],
|
|
30
|
+
api_key: Optional[str] = None,
|
|
31
|
+
url: Optional[str] = None,
|
|
32
|
+
) -> None:
|
|
33
|
+
self.model_type = model_type
|
|
34
|
+
self.api_key = api_key
|
|
35
|
+
self.url = url
|
|
36
|
+
|
|
37
|
+
@abstractmethod
|
|
38
|
+
def evaluate(self, messages: List[Dict[str, str]]) -> Dict[str, float]:
|
|
39
|
+
r"""Evaluate the messages and return scores based on different
|
|
40
|
+
criteria.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
messages (List[Dict[str, str]]): A list of messages where each
|
|
44
|
+
message is a dictionary with 'role' and 'content'.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
Dict[str, float]: A dictionary mapping score types to their values.
|
|
48
|
+
"""
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
@abstractmethod
|
|
52
|
+
def get_scores_types(self) -> List[str]:
|
|
53
|
+
r"""Get the list of score types that the reward model can return.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
List[str]: A list of score types that the reward model can return.
|
|
57
|
+
"""
|
|
58
|
+
pass
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
from typing import Dict, List
|
|
15
|
+
|
|
16
|
+
from camel.models.reward import BaseRewardModel
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Evaluator:
|
|
20
|
+
r"""Evaluator class to evaluate messages using a reward model and filter
|
|
21
|
+
data based on the scores.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
reward_model (BaseRewardModel): A reward model to evaluate messages.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(self, reward_model: BaseRewardModel):
|
|
28
|
+
self.reward_model = reward_model
|
|
29
|
+
|
|
30
|
+
def evaluate(self, messages: List[Dict[str, str]]) -> Dict[str, float]:
|
|
31
|
+
r"""Evaluate the messages using the reward model.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
messages (List[Dict[str, str]]): A list of messages where each
|
|
35
|
+
message is a dictionary with 'role' and 'content'.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
Dict[str, float]: A dictionary mapping score types to their values.
|
|
39
|
+
"""
|
|
40
|
+
scores = self.reward_model.evaluate(messages)
|
|
41
|
+
return scores
|
|
42
|
+
|
|
43
|
+
def filter_data(
|
|
44
|
+
self, messages: List[Dict[str, str]], thresholds: Dict[str, float]
|
|
45
|
+
) -> bool:
|
|
46
|
+
r"""Filter messages based on the scores.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
messages (List[Dict[str, str]]): A list of messages where each
|
|
50
|
+
message is a dictionary with 'role' and 'content'.
|
|
51
|
+
thresholds (Dict[str, float]): A dictionary mapping score types to
|
|
52
|
+
their values.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
bool: A boolean indicating whether the messages pass the filter.
|
|
56
|
+
"""
|
|
57
|
+
scores = self.evaluate(messages)
|
|
58
|
+
for score_type, threshold in thresholds.items():
|
|
59
|
+
if score_type not in scores:
|
|
60
|
+
raise ValueError(f"Score type {score_type} not found.")
|
|
61
|
+
if scores.get(score_type, 0) < threshold:
|
|
62
|
+
return False
|
|
63
|
+
return True
|
|
@@ -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
|
+
import os
|
|
15
|
+
from typing import Dict, List, Optional, Union
|
|
16
|
+
|
|
17
|
+
from openai import OpenAI
|
|
18
|
+
|
|
19
|
+
from camel.models.reward import BaseRewardModel
|
|
20
|
+
from camel.types import ChatCompletion, ModelType
|
|
21
|
+
from camel.utils import api_keys_required
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class NemotronRewardModel(BaseRewardModel):
|
|
25
|
+
r"""Reward model based on the Nemotron model with OpenAI compatibility.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
model_type (Union[ModelType, str]): Model for which a backend is
|
|
29
|
+
created.
|
|
30
|
+
api_key (Optional[str], optional): The API key for authenticating
|
|
31
|
+
with the model service. (default: :obj:`None`)
|
|
32
|
+
url (Optional[str], optional): The url to the model service.
|
|
33
|
+
|
|
34
|
+
Note:
|
|
35
|
+
The Nemotron model does not support model config.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
model_type: Union[ModelType, str],
|
|
41
|
+
api_key: Optional[str] = None,
|
|
42
|
+
url: Optional[str] = None,
|
|
43
|
+
) -> None:
|
|
44
|
+
url = url or os.environ.get(
|
|
45
|
+
"NVIDIA_API_BASE_URL", "https://integrate.api.nvidia.com/v1"
|
|
46
|
+
)
|
|
47
|
+
api_key = api_key or os.environ.get("NVIDIA_API_KEY")
|
|
48
|
+
super().__init__(model_type, api_key, url)
|
|
49
|
+
self._client = OpenAI(
|
|
50
|
+
timeout=60,
|
|
51
|
+
max_retries=3,
|
|
52
|
+
base_url=self.url,
|
|
53
|
+
api_key=self.api_key,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@api_keys_required("NVIDIA_API_KEY")
|
|
57
|
+
def evaluate(self, messages: List[Dict[str, str]]) -> Dict[str, float]:
|
|
58
|
+
r"""Evaluate the messages using the Nemotron model.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
messages (List[Dict[str, str]]): A list of messages where each
|
|
62
|
+
message is a dictionary format.
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
Dict[str, float]: A dictionary mapping score types to their
|
|
66
|
+
values.
|
|
67
|
+
"""
|
|
68
|
+
response = self._client.chat.completions.create(
|
|
69
|
+
messages=messages, # type: ignore[arg-type]
|
|
70
|
+
model=self.model_type,
|
|
71
|
+
)
|
|
72
|
+
scores = self._parse_scores(response)
|
|
73
|
+
return scores
|
|
74
|
+
|
|
75
|
+
def get_scores_types(self) -> List[str]:
|
|
76
|
+
r"""Get the list of score types that the reward model can return.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
List[str]: A list of score types that the reward model can return.
|
|
80
|
+
"""
|
|
81
|
+
return [
|
|
82
|
+
"helpfulness",
|
|
83
|
+
"correctness",
|
|
84
|
+
"coherence",
|
|
85
|
+
"complexity",
|
|
86
|
+
"verbosity",
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
def _parse_scores(self, response: ChatCompletion) -> Dict[str, float]:
|
|
90
|
+
r"""Parse the scores from the response.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
response (ChatCompletion): A ChatCompletion object with the scores.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
Dict[str, float]: A dictionary mapping score types to their values.
|
|
97
|
+
"""
|
|
98
|
+
try:
|
|
99
|
+
choices = response.choices
|
|
100
|
+
logprobs = (
|
|
101
|
+
choices[0].logprobs.content
|
|
102
|
+
if choices and choices[0].logprobs
|
|
103
|
+
else None
|
|
104
|
+
)
|
|
105
|
+
scores = (
|
|
106
|
+
{entry.token: entry.logprob for entry in logprobs if entry}
|
|
107
|
+
if logprobs
|
|
108
|
+
else {}
|
|
109
|
+
)
|
|
110
|
+
return scores
|
|
111
|
+
except Exception as e:
|
|
112
|
+
raise ValueError(f"Failed to parse scores: {e}")
|