|
| 1 | +import json |
| 2 | +import re |
| 3 | +import weakref |
| 4 | + |
| 5 | +import jsonschema |
| 6 | +import pytest |
| 7 | + |
| 8 | +from vllm.entrypoints.llm import LLM |
| 9 | +from vllm.outputs import RequestOutput |
| 10 | +from vllm.sampling_params import SamplingParams |
| 11 | + |
| 12 | +from ...conftest import cleanup |
| 13 | + |
| 14 | +MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta" |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(scope="module") |
| 18 | +def llm(): |
| 19 | + # pytest caches the fixture so we use weakref.proxy to |
| 20 | + # enable garbage collection |
| 21 | + llm = LLM(model=MODEL_NAME, max_model_len=1024) |
| 22 | + |
| 23 | + with llm.deprecate_legacy_api(): |
| 24 | + yield weakref.proxy(llm) |
| 25 | + del llm |
| 26 | + cleanup() |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.skip_global_cleanup |
| 30 | +def test_guided_regex(sample_regex, llm): |
| 31 | + sampling_params = SamplingParams( |
| 32 | + temperature=0.8, |
| 33 | + top_p=0.95, |
| 34 | + ) |
| 35 | + outputs = llm.generate( |
| 36 | + prompts=[ |
| 37 | + f"Give an example IPv4 address with this regex: {sample_regex}" |
| 38 | + ] * 2, |
| 39 | + sampling_params=sampling_params, |
| 40 | + use_tqdm=True, |
| 41 | + guided_options_request=dict(guided_regex=sample_regex)) |
| 42 | + |
| 43 | + assert outputs is not None |
| 44 | + for output in outputs: |
| 45 | + assert output is not None |
| 46 | + assert isinstance(output, RequestOutput) |
| 47 | + prompt = output.prompt |
| 48 | + generated_text = output.outputs[0].text |
| 49 | + print(generated_text) |
| 50 | + assert generated_text is not None |
| 51 | + assert re.fullmatch(sample_regex, generated_text) is not None |
| 52 | + print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.skip_global_cleanup |
| 56 | +def test_guided_json_completion(sample_json_schema, llm): |
| 57 | + sampling_params = SamplingParams( |
| 58 | + temperature=1.0, |
| 59 | + max_tokens=1000, |
| 60 | + ) |
| 61 | + outputs = llm.generate( |
| 62 | + prompts=[ |
| 63 | + f"Give an example JSON for an employee profile " |
| 64 | + f"that fits this schema: {sample_json_schema}" |
| 65 | + ] * 2, |
| 66 | + sampling_params=sampling_params, |
| 67 | + use_tqdm=True, |
| 68 | + guided_options_request=dict(guided_json=sample_json_schema)) |
| 69 | + |
| 70 | + assert outputs is not None |
| 71 | + |
| 72 | + for output in outputs: |
| 73 | + assert output is not None |
| 74 | + assert isinstance(output, RequestOutput) |
| 75 | + prompt = output.prompt |
| 76 | + |
| 77 | + generated_text = output.outputs[0].text |
| 78 | + assert generated_text is not None |
| 79 | + print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") |
| 80 | + output_json = json.loads(generated_text) |
| 81 | + jsonschema.validate(instance=output_json, schema=sample_json_schema) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.skip_global_cleanup |
| 85 | +def test_guided_choice_completion(sample_guided_choice, llm): |
| 86 | + sampling_params = SamplingParams( |
| 87 | + temperature=0.8, |
| 88 | + top_p=0.95, |
| 89 | + ) |
| 90 | + outputs = llm.generate( |
| 91 | + prompts="The best language for type-safe systems programming is ", |
| 92 | + sampling_params=sampling_params, |
| 93 | + use_tqdm=True, |
| 94 | + guided_options_request=dict(guided_choice=sample_guided_choice)) |
| 95 | + |
| 96 | + assert outputs is not None |
| 97 | + for output in outputs: |
| 98 | + assert output is not None |
| 99 | + assert isinstance(output, RequestOutput) |
| 100 | + prompt = output.prompt |
| 101 | + generated_text = output.outputs[0].text |
| 102 | + print(generated_text) |
| 103 | + assert generated_text is not None |
| 104 | + assert generated_text in sample_guided_choice |
| 105 | + print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.skip_global_cleanup |
| 109 | +def test_guided_grammar(sample_sql_statements, llm): |
| 110 | + |
| 111 | + sampling_params = SamplingParams( |
| 112 | + temperature=0.8, |
| 113 | + top_p=0.95, |
| 114 | + max_tokens=1000, |
| 115 | + ) |
| 116 | + outputs = llm.generate( |
| 117 | + prompts=("Generate a sql state that select col_1 from " |
| 118 | + "table_1 where it is equals to 1"), |
| 119 | + sampling_params=sampling_params, |
| 120 | + use_tqdm=True, |
| 121 | + guided_options_request=dict(guided_grammar=sample_sql_statements)) |
| 122 | + |
| 123 | + assert outputs is not None |
| 124 | + for output in outputs: |
| 125 | + assert output is not None |
| 126 | + assert isinstance(output, RequestOutput) |
| 127 | + prompt = output.prompt |
| 128 | + |
| 129 | + generated_text = output.outputs[0].text |
| 130 | + assert generated_text is not None |
| 131 | + # use Lark to parse the output, and make sure it's a valid parse tree |
| 132 | + from lark import Lark |
| 133 | + parser = Lark(sample_sql_statements) |
| 134 | + parser.parse(generated_text) |
| 135 | + |
| 136 | + # remove spaces for comparison b/c we removed them in the grammar |
| 137 | + ground_truth = "SELECT col_1 from table_1 where col_1 = 1".replace( |
| 138 | + " ", "") |
| 139 | + |
| 140 | + assert generated_text.strip() == ground_truth |
| 141 | + |
| 142 | + print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") |
0 commit comments