diff --git a/python/sglang/backend/anthropic.py b/python/sglang/backend/anthropic.py index aa03cb5b6..ea3213e9d 100644 --- a/python/sglang/backend/anthropic.py +++ b/python/sglang/backend/anthropic.py @@ -35,8 +35,14 @@ class Anthropic(BaseBackend): else: messages = [{"role": "user", "content": s.text_}] + if messages and messages[0]["role"] == "system": + system = messages.pop(0)["content"] + else: + system = "" + ret = anthropic.Anthropic().messages.create( model=self.model_name, + system=system, messages=messages, **sampling_params.to_anthropic_kwargs(), ) @@ -54,8 +60,14 @@ class Anthropic(BaseBackend): else: messages = [{"role": "user", "content": s.text_}] + if messages and messages[0]["role"] == "system": + system = messages.pop(0)["content"] + else: + system = "" + with anthropic.Anthropic().messages.stream( model=self.model_name, + system=system, messages=messages, **sampling_params.to_anthropic_kwargs(), ) as stream: diff --git a/python/sglang/test/test_programs.py b/python/sglang/test/test_programs.py index 9fed0b7b3..a1f29e4c3 100644 --- a/python/sglang/test/test_programs.py +++ b/python/sglang/test/test_programs.py @@ -313,6 +313,7 @@ def test_image_qa(): def test_stream(): @sgl.function def qa(s, question): + s += sgl.system("You are a helpful assistant.") s += sgl.user(question) s += sgl.assistant(sgl.gen("answer")) diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 000000000..e69de29bb