Skip to content

Commit 8082e80

Browse files
feat: New open source models (#649)
Co-authored-by: Wendong-Fan <[email protected]> Co-authored-by: Wendong <[email protected]>
1 parent fedfa0a commit 8082e80

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

camel/types/enums.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ class ModelType(Enum):
3030
GPT_4_TURBO = "gpt-4-turbo"
3131
GPT_4O = "gpt-4o"
3232
GLM_4 = "glm-4"
33+
GLM_4_OPEN_SOURCE = "glm-4-open-source"
3334
GLM_4V = 'glm-4v'
3435
GLM_3_TURBO = "glm-3-turbo"
3536

3637
STUB = "stub"
3738

3839
LLAMA_2 = "llama-2"
40+
LLAMA_3 = "llama-3"
3941
VICUNA = "vicuna"
4042
VICUNA_16K = "vicuna-16k"
4143

44+
QWEN_2 = "qwen-2"
45+
4246
# Legacy anthropic models
4347
# NOTE: anthropic lagecy models only Claude 2.1 has system prompt support
4448
CLAUDE_2_1 = "claude-2.1"
@@ -87,6 +91,9 @@ def is_open_source(self) -> bool:
8791
r"""Returns whether this type of models is open-source."""
8892
return self in {
8993
ModelType.LLAMA_2,
94+
ModelType.LLAMA_3,
95+
ModelType.QWEN_2,
96+
ModelType.GLM_4_OPEN_SOURCE,
9097
ModelType.VICUNA,
9198
ModelType.VICUNA_16K,
9299
}
@@ -135,7 +142,7 @@ def token_limit(self) -> int:
135142
return 128000
136143
elif self is ModelType.GPT_4O:
137144
return 128000
138-
elif self == ModelType.GLM_4:
145+
elif self == ModelType.GLM_4_OPEN_SOURCE:
139146
return 8192
140147
elif self == ModelType.GLM_3_TURBO:
141148
return 8192
@@ -145,6 +152,12 @@ def token_limit(self) -> int:
145152
return 4096
146153
elif self is ModelType.LLAMA_2:
147154
return 4096
155+
elif self is ModelType.LLAMA_3:
156+
return 8192
157+
elif self is ModelType.QWEN_2:
158+
return 128000
159+
elif self is ModelType.GLM_4:
160+
return 8192
148161
elif self is ModelType.VICUNA:
149162
# reference: https://lmsys.org/blog/2023-03-30-vicuna/
150163
return 2048
@@ -184,6 +197,20 @@ def validate_model_name(self, model_name: str) -> bool:
184197
self.value in model_name.lower()
185198
or "llama2" in model_name.lower()
186199
)
200+
elif self is ModelType.LLAMA_3:
201+
return (
202+
self.value in model_name.lower()
203+
or "llama3" in model_name.lower()
204+
)
205+
elif self is ModelType.QWEN_2:
206+
return (
207+
self.value in model_name.lower()
208+
or "qwen2" in model_name.lower()
209+
)
210+
elif self is ModelType.GLM_4_OPEN_SOURCE:
211+
return (
212+
'glm-4' in model_name.lower() or "glm4" in model_name.lower()
213+
)
187214
else:
188215
return self.value in model_name.lower()
189216

camel/utils/token_counting.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def messages_to_prompt(messages: List[OpenAIMessage], model: ModelType) -> str:
5151
system_message = messages[0]["content"]
5252

5353
ret: str
54-
if model == ModelType.LLAMA_2:
54+
if model == ModelType.LLAMA_2 or model == ModelType.LLAMA_3:
5555
# reference: https://github.com/facebookresearch/llama/blob/cfc3fc8c1968d390eb830e65c63865e980873a06/llama/generation.py#L212
5656
seps = [" ", " </s><s>"]
5757
role_map = {"user": "[INST]", "assistant": "[/INST]"}
@@ -93,6 +93,45 @@ def messages_to_prompt(messages: List[OpenAIMessage], model: ModelType) -> str:
9393
else:
9494
ret += role + ":"
9595
return ret
96+
elif model == ModelType.GLM_4_OPEN_SOURCE:
97+
system_prompt = f"[gMASK]<sop><|system|>\n{system_message}"
98+
ret = system_prompt
99+
for msg in messages[1:]:
100+
role = msg["role"]
101+
content = msg["content"]
102+
if not isinstance(content, str):
103+
raise ValueError(
104+
"Currently multimodal context is not "
105+
"supported by the token counter."
106+
)
107+
if content:
108+
ret += "<|" + role + "|>" + "\n" + content
109+
else:
110+
ret += "<|" + role + "|>" + "\n"
111+
return ret
112+
elif model == ModelType.QWEN_2:
113+
system_prompt = f"<|im_start|>system\n{system_message}<|im_end|>"
114+
ret = system_prompt + "\n"
115+
for msg in messages[1:]:
116+
role = msg["role"]
117+
content = msg["content"]
118+
if not isinstance(content, str):
119+
raise ValueError(
120+
"Currently multimodal context is not "
121+
"supported by the token counter."
122+
)
123+
if content:
124+
ret += (
125+
'<|im_start|>'
126+
+ role
127+
+ '\n'
128+
+ content
129+
+ '<|im_end|>'
130+
+ '\n'
131+
)
132+
else:
133+
ret += '<|im_start|>' + role + '\n'
134+
return ret
96135
else:
97136
raise ValueError(f"Invalid model type: {model}")
98137

0 commit comments

Comments
 (0)