guidance icon indicating copy to clipboard operation
guidance copied to clipboard

Model-specific prompts

Open motaatmo opened this issue 2 years ago • 4 comments

Hi,

when browsing huggingface it always says that a specific model works best with this or that prompt. I know (or rather: assume) that these prompts are generated by the with system(): etc clauses. How can I be sure that such a prompt is good for a given model? Greetings!

Moritz

PS: As an aside: The reason why I post this under "issues" (which does not seem to fit perfectly) is that I don't know of a different platform to ask questions regarding guidance (e.g. I didn't find a lot on stack overflow), is there such a place?

motaatmo avatar Jan 04 '24 08:01 motaatmo

Hi @motaatmo . The best place for discussion questions in the GitHub discussion panel. I'll leave this as an issue though because having default system prompts for models is not a bad thing. We could have something like a .default_system_prompt class attribute for some of the model subclasses if anyone wants to add it.

slundberg avatar Jan 05 '24 04:01 slundberg

@motaatmo On my end, I've been trying Orca mini 3B (HF Hub link) today and I came up with this to inject my own prompt for now:

import torch
from guidance import models, gen, system, user, assistant, select

# Custom prompt implementation
from guidance.models.transformers._transformers import Transformers, TransformersChat

class Orca(Transformers):
    pass

class OrcaChat(TransformersChat, Orca):
    def get_role_start(self, role_name, **kwargs):
        if role_name == "system":
            return "### System:\n"
        elif role_name == "user":
            if str(self).endswith("\n\n### User:\n"):
                return "" # we don't need to start anything if we are starting with a top level unnested system tag
            else:
                return "### System:\n"
        else:
            return " "

    def get_role_end(self, role_name=None):
        if role_name == "system":
            return "\n\n### User:\n"
        elif role_name == "user":
            return "\n\n### Response:\n"
        else:
            return " "

# Usage
orca = OrcaChat('psmathur/orca_mini_3b', torch_dtype=torch.float16, device_map='auto')

with system():
    lm = orca + "You are a cat expert."

with user():
    lm += "What are the smallest cats?"

with assistant():
    lm += gen("answer", stop=".")

I tested it and also extracted the underlying text produced to see if it correctly injects the prompt and it works. I used the following code to verify:

display_out = lm._state
for context in reversed(lm.opened_blocks):
    display_out += lm.opened_blocks[context][1]

Look at all the <||_#NODISP_||> ... <||_/NODISP_||> blocks that contain the prompt that is hidden. It'd be nice to have a debug version of the final HTML print for when we are integrating a new model.

@slundberg Do you think it is relevant for me to contribute the above Orca implementation (or any other models I work with that has custom prompts)? Also, if I have time in another PR, even a debug print for the system/user/assistant blocks?

cpcdoy avatar Jan 05 '24 10:01 cpcdoy

Hi! I'd like to work on this issue. Please let me know if I can be assigned. Thanks!

LeonxLJX avatar Aug 27 '26 01:08 LeonxLJX

Hi! I'd like to add model-specific prompts support. Plan: design the API, implement, add tests. May I be assigned?

LeonxLJX avatar Sep 01 '26 10:09 LeonxLJX