Skip to content

Commit

Permalink
Use Anthropic messages API (sgl-project#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
janimo authored Mar 22, 2024
1 parent 08df63a commit e57f079
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/quick_start/anthropic_example_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def batch():


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
print("\n========== single ==========\n")
Expand Down
4 changes: 2 additions & 2 deletions examples/quick_start/anthropic_example_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def few_shot_qa(s, question):
\n\nAssistant: Rome
""")
s += "\n\nHuman: " + question + "\n"
s += "\n\nAssistant:" + sgl.gen("answer", stop="\n", temperature=0)
s += "\n\nAssistant:" + sgl.gen("answer", stop="\\n", temperature=0)


def single():
Expand Down Expand Up @@ -52,7 +52,7 @@ def batch():


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
print("\n========== single ==========\n")
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ srt = ["aiohttp", "fastapi", "psutil", "rpyc", "torch", "uvloop", "uvicorn",
"zmq", "vllm>=0.3.3", "interegular", "lark", "numba",
"pydantic", "referencing", "diskcache", "cloudpickle", "pillow", "outlines>=0.0.27"]
openai = ["openai>=1.0", "numpy"]
anthropic = ["anthropic", "numpy"]
anthropic = ["anthropic>=0.20.0", "numpy"]
all = ["sglang[srt]", "sglang[openai]", "sglang[anthropic]"]

[project.urls]
Expand Down
30 changes: 18 additions & 12 deletions python/sglang/backend/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ def generate(
s: StreamExecutor,
sampling_params: SglSamplingParams,
):
prompt = s.text_
ret = anthropic.Anthropic().completions.create(
if s.messages_:
messages = s.messages_
else:
messages = [{"role": "user", "content": s.text_}]

ret = anthropic.Anthropic().messages.create(
model=self.model_name,
prompt=prompt,
messages=messages,
**sampling_params.to_anthropic_kwargs(),
)
comp = ret.completion
comp = ret.content[0].text

return comp, {}

Expand All @@ -45,13 +49,15 @@ def generate_stream(
s: StreamExecutor,
sampling_params: SglSamplingParams,
):
prompt = s.text_
generator = anthropic.Anthropic().completions.create(
if s.messages_:
messages = s.messages_
else:
messages = [{"role": "user", "content": s.text_}]

with anthropic.Anthropic().messages.stream(
model=self.model_name,
prompt=prompt,
stream=True,
messages=messages,
**sampling_params.to_anthropic_kwargs(),
)

for ret in generator:
yield ret.completion, {}
) as stream:
for text in stream.text_stream:
yield text, {}
2 changes: 1 addition & 1 deletion python/sglang/lang/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def to_anthropic_kwargs(self):
"Regular expression is not supported in the Anthropic backend."
)
return {
"max_tokens_to_sample": self.max_new_tokens,
"max_tokens": self.max_new_tokens,
"stop_sequences": (
self.stop if isinstance(self.stop, (list, tuple)) else [self.stop]
),
Expand Down
2 changes: 1 addition & 1 deletion test/lang/test_anthropic_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUp(self):
cls = type(self)

if cls.backend is None:
cls.backend = Anthropic("claude-2")
cls.backend = Anthropic("claude-3-haiku-20240307")
set_default_backend(cls.backend)

def test_mt_bench(self):
Expand Down

0 comments on commit e57f079

Please sign in to comment.