Skip to content

Commit 94fef76

Browse files
committed
优化极爱阿狗
1 parent f8e39fd commit 94fef76

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

gpt_server/model_backend/lmdeploy_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ async def stream_chat(self, params: Dict[str, Any]) -> AsyncGenerator:
138138
temperature = float(params.get("temperature", 0.8))
139139
top_p = float(params.get("top_p", 0.8))
140140
top_k = params.get("top_k", 50)
141-
141+
chat_template = params["chat_template"]
142142
max_new_tokens = int(params.get("max_new_tokens", 1024 * 8))
143143
stop_str = params.get("stop", None)
144144
stop_token_ids = params.get("stop_words_ids", None) or []

gpt_server/model_worker/base/model_worker_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def __init__(
124124
self.model = None
125125
self.backend = None
126126
self.chat_template = None
127+
self.vl_chat_template = None
127128
self.tokenizer: PreTrainedTokenizer | None = None
128129
self.load_model_tokenizer(model_path)
129130
self.context_len = self.get_context_length()
@@ -137,6 +138,10 @@ def __init__(
137138
def preprocess_params(self, params: dict) -> dict:
138139
"""预处理 params"""
139140
messages = params["messages"]
141+
params["chat_template"] = self.chat_template
142+
if self.vision_config:
143+
params["multimodal"] = True
144+
params["chat_template"] = self.vl_chat_template
140145
if isinstance(messages, str):
141146
messages = [{"role": "user", "content": messages}]
142147
params["messages"] = messages

gpt_server/model_worker/qwen.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def __init__(
4141
logger.warning(f"{model_names[0]} 停用词: {self.stop}")
4242

4343
self.chat_template = get_chat_template(model_name="qwen", lang="zh")
44+
# from https://github.com/xorbitsai/inference/blob/c70ea74fa820a613f8d577047ef1818da20a96b3/xinference/model/llm/llm_family_modelscope.json
45+
self.vl_chat_template = "{% set image_count = namespace(value=0) %}{% set video_count = namespace(value=0) %}{% for message in messages %}{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}<|im_start|>{{ message['role'] }}\n{% if message['content'] is string %}{{ message['content'] }}<|im_end|>\n{% else %}{% for content in message['content'] %}{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}{% set image_count.value = image_count.value + 1 %}{% if add_vision_id %}Picture {{ image_count.value }}: {% endif %}<|vision_start|><|image_pad|><|vision_end|>{% elif content['type'] == 'video' or 'video' in content %}{% set video_count.value = video_count.value + 1 %}{% if add_vision_id %}Video {{ video_count.value }}: {% endif %}<|vision_start|><|video_pad|><|vision_end|>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}<|im_end|>\n{% endif %}{% endfor %}{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
4446
self.tool_parser = ToolParserManager.module_dict["qwen2_5"](
4547
tokenizer=self.tokenizer
4648
)
47-
# from https://github.com/xorbitsai/inference/blob/c70ea74fa820a613f8d577047ef1818da20a96b3/xinference/model/llm/llm_family_modelscope.json
48-
self.vl_chat_template = "{% set image_count = namespace(value=0) %}{% set video_count = namespace(value=0) %}{% for message in messages %}{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}<|im_start|>{{ message['role'] }}\n{% if message['content'] is string %}{{ message['content'] }}<|im_end|>\n{% else %}{% for content in message['content'] %}{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}{% set image_count.value = image_count.value + 1 %}{% if add_vision_id %}Picture {{ image_count.value }}: {% endif %}<|vision_start|><|image_pad|><|vision_end|>{% elif content['type'] == 'video' or 'video' in content %}{% set video_count.value = video_count.value + 1 %}{% if add_vision_id %}Video {{ video_count.value }}: {% endif %}<|vision_start|><|video_pad|><|vision_end|>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}<|im_end|>\n{% endif %}{% endfor %}{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
4949

5050
async def generate_stream_gate(self, params):
5151
self.call_ct += 1
@@ -60,13 +60,12 @@ async def generate_stream_gate(self, params):
6060
text = await asyncio.to_thread(
6161
self.tokenizer.apply_chat_template,
6262
messages,
63-
# chat_template=self.vl_chat_template,
63+
chat_template=params["chat_template"],
6464
tokenize=False,
6565
add_generation_prompt=True,
6666
tools=tools,
6767
enable_thinking=bool(params.get("enable_thinking", True)),
6868
)
69-
7069
params["prompt"] = text
7170
# 多模态不需要传入input_ids
7271
# ---------------添加额外的参数------------------------

0 commit comments

Comments
 (0)