Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test regex in vision api #926

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions test/srt/test_openai_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ def run_chat_completion(self, logprobs, parallel_sample_num):
model=self.model,
messages=[
{"role": "system", "content": "You are a helpful AI assistant"},
{"role": "user", "content": "What is the capital of France?"},
{
"role": "user",
"content": "What is the capital of France? Answer in a few words.",
},
],
temperature=0,
max_tokens=32,
logprobs=logprobs is not None and logprobs > 0,
top_logprobs=logprobs,
n=parallel_sample_num,
Expand Down Expand Up @@ -178,7 +180,6 @@ def run_chat_completion_stream(self, logprobs):
{"role": "user", "content": "What is the capital of France?"},
],
temperature=0,
max_tokens=32,
logprobs=logprobs is not None and logprobs > 0,
top_logprobs=logprobs,
stream=True,
Expand Down
54 changes: 50 additions & 4 deletions test/srt/test_vision_openai_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,71 @@ def test_chat_completion(self):
{
"type": "image_url",
"image_url": {
"url": "https://github.com/sgl-project/sglang/blob/main/assets/logo.png?raw=true"
"url": "https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true"
},
},
{"type": "text", "text": "Describe this image"},
{
"type": "text",
"text": "Describe this image in a very short sentence.",
},
],
},
],
temperature=0,
max_tokens=32,
)

assert response.choices[0].message.role == "assistant"
assert isinstance(response.choices[0].message.content, str)
text = response.choices[0].message.content
assert isinstance(text, str)
assert "car" in text or "taxi" in text, text
assert response.id
assert response.created
assert response.usage.prompt_tokens > 0
assert response.usage.completion_tokens > 0
assert response.usage.total_tokens > 0

def test_regex(self):
client = openai.Client(api_key=self.api_key, base_url=self.base_url)

regex = (
r"""\{\n"""
+ r""" "color": "[\w]+",\n"""
+ r""" "number_of_cars": [\d]+\n"""
+ r"""\}"""
)

response = client.chat.completions.create(
model="default",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true"
},
},
{
"type": "text",
"text": "Describe this image in the JSON format.",
},
],
},
],
temperature=0,
extra_body={"regex": regex},
)
text = response.choices[0].message.content

try:
js_obj = json.loads(text)
except (TypeError, json.decoder.JSONDecodeError):
print("JSONDecodeError", text)
raise
assert isinstance(js_obj["color"], str)
assert isinstance(js_obj["number_of_cars"], int)


if __name__ == "__main__":
unittest.main(warnings="ignore")
Expand Down
Loading