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
e57f0792
Unverified
Commit
e57f0792
authored
Mar 22, 2024
by
Jani Monoses
Committed by
GitHub
Mar 22, 2024
Browse files
Use Anthropic messages API (#304)
parent
08df63a6
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
24 additions
and
18 deletions
+24
-18
examples/quick_start/anthropic_example_chat.py
examples/quick_start/anthropic_example_chat.py
+1
-1
examples/quick_start/anthropic_example_complete.py
examples/quick_start/anthropic_example_complete.py
+2
-2
python/pyproject.toml
python/pyproject.toml
+1
-1
python/sglang/backend/anthropic.py
python/sglang/backend/anthropic.py
+18
-12
python/sglang/lang/ir.py
python/sglang/lang/ir.py
+1
-1
test/lang/test_anthropic_backend.py
test/lang/test_anthropic_backend.py
+1
-1
No files found.
examples/quick_start/anthropic_example_chat.py
View file @
e57f0792
...
@@ -52,7 +52,7 @@ def batch():
...
@@ -52,7 +52,7 @@ def batch():
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
sgl
.
set_default_backend
(
sgl
.
Anthropic
(
"claude-
2
"
))
sgl
.
set_default_backend
(
sgl
.
Anthropic
(
"claude-
3-haiku-20240307
"
))
# Run a single request
# Run a single request
print
(
"
\n
========== single ==========
\n
"
)
print
(
"
\n
========== single ==========
\n
"
)
...
...
examples/quick_start/anthropic_example_complete.py
View file @
e57f0792
...
@@ -19,7 +19,7 @@ def few_shot_qa(s, question):
...
@@ -19,7 +19,7 @@ def few_shot_qa(s, question):
\n\n
Assistant: Rome
\n\n
Assistant: Rome
"""
)
"""
)
s
+=
"
\n\n
Human: "
+
question
+
"
\n
"
s
+=
"
\n\n
Human: "
+
question
+
"
\n
"
s
+=
"
\n\n
Assistant:"
+
sgl
.
gen
(
"answer"
,
stop
=
"
\n
"
,
temperature
=
0
)
s
+=
"
\n\n
Assistant:"
+
sgl
.
gen
(
"answer"
,
stop
=
"
\
\
n"
,
temperature
=
0
)
def
single
():
def
single
():
...
@@ -52,7 +52,7 @@ def batch():
...
@@ -52,7 +52,7 @@ def batch():
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
sgl
.
set_default_backend
(
sgl
.
Anthropic
(
"claude-
2
"
))
sgl
.
set_default_backend
(
sgl
.
Anthropic
(
"claude-
3-haiku-20240307
"
))
# Run a single request
# Run a single request
print
(
"
\n
========== single ==========
\n
"
)
print
(
"
\n
========== single ==========
\n
"
)
...
...
python/pyproject.toml
View file @
e57f0792
...
@@ -22,7 +22,7 @@ srt = ["aiohttp", "fastapi", "psutil", "rpyc", "torch", "uvloop", "uvicorn",
...
@@ -22,7 +22,7 @@ srt = ["aiohttp", "fastapi", "psutil", "rpyc", "torch", "uvloop", "uvicorn",
"zmq"
,
"vllm>=0.3.3"
,
"interegular"
,
"lark"
,
"numba"
,
"zmq"
,
"vllm>=0.3.3"
,
"interegular"
,
"lark"
,
"numba"
,
"pydantic"
,
"referencing"
,
"diskcache"
,
"cloudpickle"
,
"pillow"
,
"outlines>=0.0.27"
]
"pydantic"
,
"referencing"
,
"diskcache"
,
"cloudpickle"
,
"pillow"
,
"outlines>=0.0.27"
]
openai
=
[
"openai>=1.0"
,
"numpy"
]
openai
=
[
"openai>=1.0"
,
"numpy"
]
anthropic
=
[
"anthropic"
,
"numpy"
]
anthropic
=
[
"anthropic
>=0.20.0
"
,
"numpy"
]
all
=
["sglang[srt]
", "
sglang
[openai]
", "
sglang
[anthropic]"]
all
=
["sglang[srt]
", "
sglang
[openai]
", "
sglang
[anthropic]"]
[project.urls]
[project.urls]
...
...
python/sglang/backend/anthropic.py
View file @
e57f0792
...
@@ -30,13 +30,17 @@ class Anthropic(BaseBackend):
...
@@ -30,13 +30,17 @@ class Anthropic(BaseBackend):
s
:
StreamExecutor
,
s
:
StreamExecutor
,
sampling_params
:
SglSamplingParams
,
sampling_params
:
SglSamplingParams
,
):
):
prompt
=
s
.
text_
if
s
.
messages_
:
ret
=
anthropic
.
Anthropic
().
completions
.
create
(
messages
=
s
.
messages_
else
:
messages
=
[{
"role"
:
"user"
,
"content"
:
s
.
text_
}]
ret
=
anthropic
.
Anthropic
().
messages
.
create
(
model
=
self
.
model_name
,
model
=
self
.
model_name
,
prompt
=
prompt
,
messages
=
messages
,
**
sampling_params
.
to_anthropic_kwargs
(),
**
sampling_params
.
to_anthropic_kwargs
(),
)
)
comp
=
ret
.
co
mpletion
comp
=
ret
.
co
ntent
[
0
].
text
return
comp
,
{}
return
comp
,
{}
...
@@ -45,13 +49,15 @@ class Anthropic(BaseBackend):
...
@@ -45,13 +49,15 @@ class Anthropic(BaseBackend):
s
:
StreamExecutor
,
s
:
StreamExecutor
,
sampling_params
:
SglSamplingParams
,
sampling_params
:
SglSamplingParams
,
):
):
prompt
=
s
.
text_
if
s
.
messages_
:
generator
=
anthropic
.
Anthropic
().
completions
.
create
(
messages
=
s
.
messages_
else
:
messages
=
[{
"role"
:
"user"
,
"content"
:
s
.
text_
}]
with
anthropic
.
Anthropic
().
messages
.
stream
(
model
=
self
.
model_name
,
model
=
self
.
model_name
,
prompt
=
prompt
,
messages
=
messages
,
stream
=
True
,
**
sampling_params
.
to_anthropic_kwargs
(),
**
sampling_params
.
to_anthropic_kwargs
(),
)
)
as
stream
:
for
text
in
stream
.
text_stream
:
for
ret
in
generator
:
yield
text
,
{}
yield
ret
.
completion
,
{}
python/sglang/lang/ir.py
View file @
e57f0792
...
@@ -73,7 +73,7 @@ class SglSamplingParams:
...
@@ -73,7 +73,7 @@ class SglSamplingParams:
"Regular expression is not supported in the Anthropic backend."
"Regular expression is not supported in the Anthropic backend."
)
)
return
{
return
{
"max_tokens
_to_sample
"
:
self
.
max_new_tokens
,
"max_tokens"
:
self
.
max_new_tokens
,
"stop_sequences"
:
(
"stop_sequences"
:
(
self
.
stop
if
isinstance
(
self
.
stop
,
(
list
,
tuple
))
else
[
self
.
stop
]
self
.
stop
if
isinstance
(
self
.
stop
,
(
list
,
tuple
))
else
[
self
.
stop
]
),
),
...
...
test/lang/test_anthropic_backend.py
View file @
e57f0792
...
@@ -14,7 +14,7 @@ class TestAnthropicBackend(unittest.TestCase):
...
@@ -14,7 +14,7 @@ class TestAnthropicBackend(unittest.TestCase):
cls
=
type
(
self
)
cls
=
type
(
self
)
if
cls
.
backend
is
None
:
if
cls
.
backend
is
None
:
cls
.
backend
=
Anthropic
(
"claude-
2
"
)
cls
.
backend
=
Anthropic
(
"claude-
3-haiku-20240307
"
)
set_default_backend
(
cls
.
backend
)
set_default_backend
(
cls
.
backend
)
def
test_mt_bench
(
self
):
def
test_mt_bench
(
self
):
...
...
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