MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT-LLM / main

Function main

examples/llm-api/llm_guided_decoding.py:8–43  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6
7
8def main():
9
10 # Specify the guided decoding backend; xgrammar and llguidance are supported currently.
11 llm = LLM(model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
12 guided_decoding_backend='xgrammar')
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__':

Callers 1

Calls 5

LLMClass · 0.90
SamplingParamsClass · 0.90
apply_chat_templateMethod · 0.45
generateMethod · 0.45

Tested by

no test coverage detected