Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
1e8f4252
Unverified
Commit
1e8f4252
authored
Apr 24, 2024
by
Cyrus Leung
Committed by
GitHub
Apr 23, 2024
Browse files
[Bugfix][Frontend] Raise exception when file-like chat template fails to be opened (#4292)
parent
2b7949c1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
13 deletions
+29
-13
tests/async_engine/test_chat_template.py
tests/async_engine/test_chat_template.py
+14
-5
vllm/entrypoints/openai/serving_chat.py
vllm/entrypoints/openai/serving_chat.py
+15
-8
No files found.
tests/async_engine/test_chat_template.py
View file @
1e8f4252
...
...
@@ -76,20 +76,29 @@ def test_load_chat_template():
{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}{{ '<|im_start|>assistant
\\
n' }}{% endif %}"""
# noqa: E501
def
test_no_load_chat_template
():
def
test_no_load_chat_template
_filelike
():
# Testing chatml template
template
=
"../../examples/does_not_exist"
tokenizer
=
MockTokenizer
()
mock_serving_chat
=
MockServingChat
(
tokenizer
)
with
pytest
.
raises
(
ValueError
,
match
=
"looks like a file path"
):
OpenAIServingChat
.
_load_chat_template
(
mock_serving_chat
,
chat_template
=
template
)
def
test_no_load_chat_template_literallike
():
# Testing chatml template
template
=
"{{ messages }}"
tokenizer
=
MockTokenizer
()
mock_serving_chat
=
MockServingChat
(
tokenizer
)
OpenAIServingChat
.
_load_chat_template
(
mock_serving_chat
,
chat_template
=
template
)
template_content
=
tokenizer
.
chat_template
# Test assertions
assert
template_content
is
not
None
# Hard coded value for template_chatml.jinja
assert
template_content
==
"""../../examples/does_not_exist"""
assert
template_content
==
template
@
pytest
.
mark
.
asyncio
...
...
vllm/entrypoints/openai/serving_chat.py
View file @
1e8f4252
...
...
@@ -319,23 +319,30 @@ class OpenAIServingChat(OpenAIServing):
return
response
def
_load_chat_template
(
self
,
chat_template
):
tokenizer
=
self
.
tokenizer
if
chat_template
is
not
None
:
try
:
with
open
(
chat_template
,
"r"
)
as
f
:
self
.
tokenizer
.
chat_template
=
f
.
read
()
except
OSError
:
tokenizer
.
chat_template
=
f
.
read
()
except
OSError
as
e
:
JINJA_CHARS
=
"{}
\n
"
if
not
any
(
c
in
chat_template
for
c
in
JINJA_CHARS
):
msg
=
(
f
"The supplied chat template (
{
chat_template
}
) "
f
"looks like a file path, but it failed to be "
f
"opened. Reason:
{
e
}
"
)
raise
ValueError
(
msg
)
from
e
# If opening a file fails, set chat template to be args to
# ensure we decode so our escape are interpreted correctly
self
.
tokenizer
.
chat_template
=
codecs
.
decode
(
tokenizer
.
chat_template
=
codecs
.
decode
(
chat_template
,
"unicode_escape"
)
logger
.
info
(
f
"Using supplied chat template:
\n
{
self
.
tokenizer
.
chat_template
}
"
)
elif
self
.
tokenizer
.
chat_template
is
not
None
:
f
"Using supplied chat template:
\n
{
tokenizer
.
chat_template
}
"
)
elif
tokenizer
.
chat_template
is
not
None
:
logger
.
info
(
f
"Using default chat template:
\n
{
self
.
tokenizer
.
chat_template
}
"
)
f
"Using default chat template:
\n
{
tokenizer
.
chat_template
}
"
)
else
:
logger
.
warning
(
"No chat template provided. Chat API will not work."
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment