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

Co-authored-by: Ying Sheng <sqy1415@gmail.com>
This commit is contained in:
Fronx
2024-04-16 20:18:24 +02:00
committed by GitHub
parent 65501a9cf1
commit 2b6d999191
3 changed files with 13 additions and 0 deletions

View File

@@ -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:

View File

@@ -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"))