sgl-project / sglang

SGLang is a fast serving framework for large language models and vision language models.
https://sgl-project.github.io/
Apache License 2.0
5.9k stars 477 forks source link

Add support for scalar values #145

Closed tom-doerr closed 9 months ago

tom-doerr commented 9 months ago

I often need an LLM to generate scalar values. I look at the logprobs of True and False and do True - False. Works very well for me:

f'{output_text}\n\nRate whether the text is well written.', '{"is_well_written_bool": '

Also the values are nicely distributed:

is_great_comment_bool: 0.72  
is_funny_bool_opt: -0.79  
neg_sounds_like_chatgpt_bool: -0.49  
neg_contains_placeholders_bool: -0.86  
neg_sounds_awkward_bool: -0.97 
 is_written_by_human: 0.94  
neg_sounds_robotic: -0.89

Could you add something like this into sglang?

merrymercy commented 9 months ago

Could you try one of the following options?

  1. The select operator https://github.com/sgl-project/sglang/blob/322421fae36424cdcef16ecc913e7f6e92d4b7d2/examples/usage/choices_logprob.py#L9-L12
  2. The regex decoding. You can use (True|False) to specify a selection. https://github.com/sgl-project/sglang/tree/main?tab=readme-ov-file#json-decoding

The difference is documented here https://github.com/sgl-project/sglang?tab=readme-ov-file#tips-and-implementation-details

tom-doerr commented 9 months ago

Oh wow, that's great. Just for documentation, the relevant code is:

    meta_info = state.get_meta_info("tool")
    print("logprobs of choice 1", meta_info["prompt_logprob"][0])
    print("logprobs of choice 2", meta_info["prompt_logprob"][1])