Generate text with guided decoding#
Source NVIDIA/TensorRT-LLM.
1from tensorrt_llm import LLM, SamplingParams
2from tensorrt_llm.llmapi import GuidedDecodingParams
3
4
5def main():
6
7 # Specify the guided decoding backend; xgrammar is supported currently.
8 llm = LLM(
9 model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
10 guided_decoding_backend='xgrammar',
11 disable_overlap_scheduler=True # Not supported by xgrammar mode
12 )
13
14 # An example from json-mode-eval
15 schema = '{"title": "WirelessAccessPoint", "type": "object", "properties": {"ssid": {"title": "SSID", "type": "string"}, "securityProtocol": {"title": "SecurityProtocol", "type": "string"}, "bandwidth": {"title": "Bandwidth", "type": "string"}}, "required": ["ssid", "securityProtocol", "bandwidth"]}'
16
17 prompt = [{
18 'role':
19 'system',
20 'content':
21 "You are a helpful assistant that answers in JSON. Here's the json schema you must adhere to:\n<schema>\n{'title': 'WirelessAccessPoint', 'type': 'object', 'properties': {'ssid': {'title': 'SSID', 'type': 'string'}, 'securityProtocol': {'title': 'SecurityProtocol', 'type': 'string'}, 'bandwidth': {'title': 'Bandwidth', 'type': 'string'}}, 'required': ['ssid', 'securityProtocol', 'bandwidth']}\n</schema>\n"
22 }, {
23 'role':
24 'user',
25 'content':
26 "I'm currently configuring a wireless access point for our office network and I need to generate a JSON object that accurately represents its settings. The access point's SSID should be 'OfficeNetSecure', it uses WPA2-Enterprise as its security protocol, and it's capable of a bandwidth of up to 1300 Mbps on the 5 GHz band. This JSON object will be used to document our network configurations and to automate the setup process for additional access points in the future. Please provide a JSON object that includes these details."
27 }]
28 prompt = llm.tokenizer.apply_chat_template(prompt, tokenize=False)
29 print(f"Prompt: {prompt!r}")
30
31 output = llm.generate(prompt, sampling_params=SamplingParams(max_tokens=50))
32 print(f"Generated text (unguided): {output.outputs[0].text!r}")
33
34 output = llm.generate(
35 prompt,
36 sampling_params=SamplingParams(
37 max_tokens=50, guided_decoding=GuidedDecodingParams(json=schema)))
38 print(f"Generated text (guided): {output.outputs[0].text!r}")
39
40 # Got output like
41 # Prompt: "<|system|>\nYou are a helpful assistant that answers in JSON. Here's the json schema you must adhere to:\n<schema>\n{'title': 'WirelessAccessPoint', 'type': 'object', 'properties': {'ssid': {'title': 'SSID', 'type': 'string'}, 'securityProtocol': {'title': 'SecurityProtocol', 'type': 'string'}, 'bandwidth': {'title': 'Bandwidth', 'type': 'string'}}, 'required': ['ssid', 'securityProtocol', 'bandwidth']}\n</schema>\n</s>\n<|user|>\nI'm currently configuring a wireless access point for our office network and I need to generate a JSON object that accurately represents its settings. The access point's SSID should be 'OfficeNetSecure', it uses WPA2-Enterprise as its security protocol, and it's capable of a bandwidth of up to 1300 Mbps on the 5 GHz band. This JSON object will be used to document our network configurations and to automate the setup process for additional access points in the future. Please provide a JSON object that includes these details.</s>\n"
42 # Generated text (unguided): '<|assistant|>\nHere\'s a JSON object that accurately represents the settings of a wireless access point for our office network:\n\n```json\n{\n "title": "WirelessAccessPoint",\n "'
43 # Generated text (guided): '{"ssid": "OfficeNetSecure", "securityProtocol": "WPA2-Enterprise", "bandwidth": "1300 Mbps"}'
44
45
46if __name__ == '__main__':
47 main()