Unverified Commit e57f0792 authored by Jani Monoses's avatar Jani Monoses Committed by GitHub
Browse files

Use Anthropic messages API (#304)

parent 08df63a6
...@@ -52,7 +52,7 @@ def batch(): ...@@ -52,7 +52,7 @@ def batch():
if __name__ == "__main__": 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 # Run a single request
print("\n========== single ==========\n") print("\n========== single ==========\n")
......
...@@ -19,7 +19,7 @@ def few_shot_qa(s, question): ...@@ -19,7 +19,7 @@ def few_shot_qa(s, question):
\n\nAssistant: Rome \n\nAssistant: Rome
""") """)
s += "\n\nHuman: " + question + "\n" 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(): def single():
...@@ -52,7 +52,7 @@ def batch(): ...@@ -52,7 +52,7 @@ def batch():
if __name__ == "__main__": 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 # Run a single request
print("\n========== single ==========\n") print("\n========== single ==========\n")
......
...@@ -22,7 +22,7 @@ srt = ["aiohttp", "fastapi", "psutil", "rpyc", "torch", "uvloop", "uvicorn", ...@@ -22,7 +22,7 @@ srt = ["aiohttp", "fastapi", "psutil", "rpyc", "torch", "uvloop", "uvicorn",
"zmq", "vllm>=0.3.3", "interegular", "lark", "numba", "zmq", "vllm>=0.3.3", "interegular", "lark", "numba",
"pydantic", "referencing", "diskcache", "cloudpickle", "pillow", "outlines>=0.0.27"] "pydantic", "referencing", "diskcache", "cloudpickle", "pillow", "outlines>=0.0.27"]
openai = ["openai>=1.0", "numpy"] openai = ["openai>=1.0", "numpy"]
anthropic = ["anthropic", "numpy"] anthropic = ["anthropic>=0.20.0", "numpy"]
all = ["sglang[srt]", "sglang[openai]", "sglang[anthropic]"] all = ["sglang[srt]", "sglang[openai]", "sglang[anthropic]"]
[project.urls] [project.urls]
......
...@@ -30,13 +30,17 @@ class Anthropic(BaseBackend): ...@@ -30,13 +30,17 @@ class Anthropic(BaseBackend):
s: StreamExecutor, s: StreamExecutor,
sampling_params: SglSamplingParams, sampling_params: SglSamplingParams,
): ):
prompt = s.text_ if s.messages_:
ret = anthropic.Anthropic().completions.create( messages = s.messages_
else:
messages = [{"role": "user", "content": s.text_}]
ret = anthropic.Anthropic().messages.create(
model=self.model_name, model=self.model_name,
prompt=prompt, messages=messages,
**sampling_params.to_anthropic_kwargs(), **sampling_params.to_anthropic_kwargs(),
) )
comp = ret.completion comp = ret.content[0].text
return comp, {} return comp, {}
...@@ -45,13 +49,15 @@ class Anthropic(BaseBackend): ...@@ -45,13 +49,15 @@ class Anthropic(BaseBackend):
s: StreamExecutor, s: StreamExecutor,
sampling_params: SglSamplingParams, sampling_params: SglSamplingParams,
): ):
prompt = s.text_ if s.messages_:
generator = anthropic.Anthropic().completions.create( messages = s.messages_
else:
messages = [{"role": "user", "content": s.text_}]
with anthropic.Anthropic().messages.stream(
model=self.model_name, model=self.model_name,
prompt=prompt, messages=messages,
stream=True,
**sampling_params.to_anthropic_kwargs(), **sampling_params.to_anthropic_kwargs(),
) ) as stream:
for text in stream.text_stream:
for ret in generator: yield text, {}
yield ret.completion, {}
...@@ -73,7 +73,7 @@ class SglSamplingParams: ...@@ -73,7 +73,7 @@ class SglSamplingParams:
"Regular expression is not supported in the Anthropic backend." "Regular expression is not supported in the Anthropic backend."
) )
return { return {
"max_tokens_to_sample": self.max_new_tokens, "max_tokens": self.max_new_tokens,
"stop_sequences": ( "stop_sequences": (
self.stop if isinstance(self.stop, (list, tuple)) else [self.stop] self.stop if isinstance(self.stop, (list, tuple)) else [self.stop]
), ),
......
...@@ -14,7 +14,7 @@ class TestAnthropicBackend(unittest.TestCase): ...@@ -14,7 +14,7 @@ class TestAnthropicBackend(unittest.TestCase):
cls = type(self) cls = type(self)
if cls.backend is None: if cls.backend is None:
cls.backend = Anthropic("claude-2") cls.backend = Anthropic("claude-3-haiku-20240307")
set_default_backend(cls.backend) set_default_backend(cls.backend)
def test_mt_bench(self): def test_mt_bench(self):
......
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