Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
change
sglang
Commits
6767e222
Unverified
Commit
6767e222
authored
Aug 14, 2024
by
Ying Sheng
Committed by
GitHub
Aug 14, 2024
Browse files
Support jinja as chat template file (#1104)
parent
73cf6834
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
26 deletions
+34
-26
python/sglang/srt/openai_api/adapter.py
python/sglang/srt/openai_api/adapter.py
+32
-21
python/sglang/srt/server.py
python/sglang/srt/server.py
+2
-5
No files found.
python/sglang/srt/openai_api/adapter.py
View file @
6767e222
...
...
@@ -117,7 +117,7 @@ def create_streaming_error_response(
return
json_str
def
load_chat_template_for_openai_api
(
chat_template_arg
):
def
load_chat_template_for_openai_api
(
tokenizer_manager
,
chat_template_arg
):
global
chat_template_name
print
(
f
"Use chat template:
{
chat_template_arg
}
"
)
...
...
@@ -127,27 +127,38 @@ def load_chat_template_for_openai_api(chat_template_arg):
f
"Chat template
{
chat_template_arg
}
is not a built-in template name "
"or a valid chat template file path."
)
with
open
(
chat_template_arg
,
"r"
)
as
filep
:
template
=
json
.
load
(
filep
)
try
:
sep_style
=
SeparatorStyle
[
template
[
"sep_style"
]]
except
KeyError
:
raise
ValueError
(
f
"Unknown separator style:
{
template
[
'sep_style'
]
}
"
)
from
None
register_conv_template
(
Conversation
(
name
=
template
[
"name"
],
system_template
=
template
[
"system"
]
+
"
\n
{system_message}"
,
system_message
=
template
.
get
(
"system_message"
,
""
),
roles
=
(
template
[
"user"
],
template
[
"assistant"
]),
sep_style
=
sep_style
,
sep
=
template
.
get
(
"sep"
,
"
\n
"
),
stop_str
=
template
[
"stop_str"
],
),
override
=
True
,
if
chat_template_arg
.
endswith
(
".jinja"
):
with
open
(
chat_template_arg
,
"r"
)
as
f
:
chat_template
=
""
.
join
(
f
.
readlines
()).
strip
(
"
\n
"
)
tokenizer_manager
.
tokenizer
.
chat_template
=
chat_template
.
replace
(
"
\\
n"
,
"
\n
"
)
chat_template_name
=
template
[
"name"
]
chat_template_name
=
None
else
:
assert
chat_template_arg
.
endswith
(
".json"
),
"unrecognized format of chat template file"
with
open
(
chat_template_arg
,
"r"
)
as
filep
:
template
=
json
.
load
(
filep
)
try
:
sep_style
=
SeparatorStyle
[
template
[
"sep_style"
]]
except
KeyError
:
raise
ValueError
(
f
"Unknown separator style:
{
template
[
'sep_style'
]
}
"
)
from
None
register_conv_template
(
Conversation
(
name
=
template
[
"name"
],
system_template
=
template
[
"system"
]
+
"
\n
{system_message}"
,
system_message
=
template
.
get
(
"system_message"
,
""
),
roles
=
(
template
[
"user"
],
template
[
"assistant"
]),
sep_style
=
sep_style
,
sep
=
template
.
get
(
"sep"
,
"
\n
"
),
stop_str
=
template
[
"stop_str"
],
),
override
=
True
,
)
chat_template_name
=
template
[
"name"
]
else
:
chat_template_name
=
chat_template_arg
...
...
python/sglang/srt/server.py
View file @
6767e222
...
...
@@ -288,6 +288,8 @@ def launch_server(
# Launch processes
tokenizer_manager
=
TokenizerManager
(
server_args
,
port_args
,
model_overide_args
)
if
server_args
.
chat_template
:
load_chat_template_for_openai_api
(
tokenizer_manager
,
server_args
.
chat_template
)
pipe_controller_reader
,
pipe_controller_writer
=
mp
.
Pipe
(
duplex
=
False
)
pipe_detoken_reader
,
pipe_detoken_writer
=
mp
.
Pipe
(
duplex
=
False
)
...
...
@@ -375,11 +377,6 @@ def _set_envs_and_config(server_args: ServerArgs):
# FIXME: remove this after https://github.com/triton-lang/triton/pull/4295 is used as a dependency.
maybe_set_triton_cache_manager
()
# Set global chat template
if
server_args
.
chat_template
:
# TODO: replace this with huggingface transformers template
load_chat_template_for_openai_api
(
server_args
.
chat_template
)
# Check flashinfer version
if
not
server_args
.
disable_flashinfer
:
assert_pkg_version
(
...
...
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