From 2b6d99919143080b84db5fb1a8cb5ea504e5fabe Mon Sep 17 00:00:00 2001 From: Fronx Date: Tue, 16 Apr 2024 20:18:24 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20issue=20#367=20=E2=80=93=20System=20messa?= =?UTF-8?q?ge=20not=20supported=20for=20Anthropic=20(anthropic.BadRequestE?= =?UTF-8?q?rror)=20(#368)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ying Sheng --- python/sglang/backend/anthropic.py | 12 ++++++++++++ python/sglang/test/test_programs.py | 1 + test/__init__.py | 0 3 files changed, 13 insertions(+) create mode 100644 test/__init__.py 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