Unverified Commit 2b6d9991 authored by Fronx's avatar Fronx Committed by GitHub
Browse files

Fix issue #367 – System message not supported for Anthropic (anthropic.BadRequestError) (#368)


Co-authored-by: default avatarYing Sheng <sqy1415@gmail.com>
parent 65501a9c
...@@ -35,8 +35,14 @@ class Anthropic(BaseBackend): ...@@ -35,8 +35,14 @@ class Anthropic(BaseBackend):
else: else:
messages = [{"role": "user", "content": s.text_}] 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( ret = anthropic.Anthropic().messages.create(
model=self.model_name, model=self.model_name,
system=system,
messages=messages, messages=messages,
**sampling_params.to_anthropic_kwargs(), **sampling_params.to_anthropic_kwargs(),
) )
...@@ -54,8 +60,14 @@ class Anthropic(BaseBackend): ...@@ -54,8 +60,14 @@ class Anthropic(BaseBackend):
else: else:
messages = [{"role": "user", "content": s.text_}] 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( with anthropic.Anthropic().messages.stream(
model=self.model_name, model=self.model_name,
system=system,
messages=messages, messages=messages,
**sampling_params.to_anthropic_kwargs(), **sampling_params.to_anthropic_kwargs(),
) as stream: ) as stream:
......
...@@ -313,6 +313,7 @@ def test_image_qa(): ...@@ -313,6 +313,7 @@ def test_image_qa():
def test_stream(): def test_stream():
@sgl.function @sgl.function
def qa(s, question): def qa(s, question):
s += sgl.system("You are a helpful assistant.")
s += sgl.user(question) s += sgl.user(question)
s += sgl.assistant(sgl.gen("answer")) s += sgl.assistant(sgl.gen("answer"))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment