Generate text with guided decoding#

SourceNVIDIA/TensorRT-LLM.

 1fromtensorrt_llmimportLLM,SamplingParams 2fromtensorrt_llm.llmapiimportGuidedDecodingParams 3 4 5defmain(): 6 7# Specify the guided decoding backend; xgrammar and llguidance are supported currently. 8llm=LLM(model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", 9guided_decoding_backend='xgrammar')1011# An example from json-mode-eval12schema='{"title": "WirelessAccessPoint", "type": "object", "properties": {"ssid": {"title": "SSID", "type": "string"}, "securityProtocol": {"title": "SecurityProtocol", "type": "string"}, "bandwidth": {"title": "Bandwidth", "type": "string"}}, "required": ["ssid", "securityProtocol", "bandwidth"]}'1314prompt=[{15'role':16'system',17'content':18"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"19},{20'role':21'user',22'content':23"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."24}]25prompt=llm.tokenizer.apply_chat_template(prompt,tokenize=False)26print(f"Prompt:{prompt!r}")2728output=llm.generate(prompt,sampling_params=SamplingParams(max_tokens=50))29print(f"Generated text (unguided):{output.outputs[0].text!r}")3031output=llm.generate(32prompt,33sampling_params=SamplingParams(34max_tokens=50,guided_decoding=GuidedDecodingParams(json=schema)))35print(f"Generated text (guided):{output.outputs[0].text!r}")3637# Got output like38# 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"39# 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  "'40# Generated text (guided): '{"ssid": "OfficeNetSecure", "securityProtocol": "WPA2-Enterprise", "bandwidth": "1300 Mbps"}'414243if__name__=='__main__':44main()