sebastienros/fluid

Question: is this library suited for implement something like Microsoft Guidance?

Opened this issue · 0 comments

Hello,
first of all thanks for this library, I've started using this to experiment with LLM prompt engine and it's promising.
I've one question tho:
I've found a python library made from microsoft: https://github.com/microsoft/guidance
In this library they have a prompt that's interleaved by "methods" that make the prompt built progressively.

Let see an example from their home page:

# connect to a chat model like GPT-4 or Vicuna
gpt4 = guidance.llms.OpenAI("gpt-4")
# vicuna = guidance.llms.transformers.Vicuna("your_path/vicuna_13B", device_map="auto")

experts = guidance('''
{{#system~}}
You are a helpful and terse assistant.
{{~/system}}

{{#user~}}
I want a response to the following question:
{{query}}
Name 3 world-class experts (past or present) who would be great at answering this?
Don't answer the question yet.
{{~/user}}

{{#assistant~}}
{{gen 'expert_names' temperature=0 max_tokens=300}}
{{~/assistant}}

{{#user~}}
Great, now please answer the question as if these experts had collaborated in writing a joint anonymous answer.
{{~/user}}

{{#assistant~}}
{{gen 'answer' temperature=0 max_tokens=500}}
{{~/assistant}}
''', llm=gpt4)

experts(query='How can I be more productive?')

they surround some text with a tag system

{{#system~}}
You are a helpful and terse assistant.
{{~/system}}

(this is possible, so move one)

Then they have another tag that specify the user prompt

{{#user~}}
I want a response to the following question:
{{query}}
Name 3 world-class experts (past or present) who would be great at answering this?
Don't answer the question yet.
{{~/user}}

Then they have an assistant block and within it there is a call to their gen method

{{#assistant~}}
{{gen 'expert_names' temperature=0 max_tokens=300}}
{{~/assistant}}

From my understanding, that library builds the text generated in the prompt so far, and use it as a LLM prompt to a specific model (this is just a technical details, what I mean is that they build the prompt up to that gen function call), then they place the result of that gen call and place it within the assistant tag, and keep build up to the next gen call, if any.

This is a very effective way to build a prompt for LLM and I was trying to understand if Fluid allows this kind of behavior out of the box, or where I should look into to implement this kind of feature.

Thanks

UPDATE:
Just to add more context: while a simple prompt could be created by using capture, this example may show a more advanced use case where capture isn't viable

# we use LLaMA here, but any GPT-style model will do
llama = guidance.llms.Transformers("your_path/llama-7b", device=0)

# we can pre-define valid option sets
valid_weapons = ["sword", "axe", "mace", "spear", "bow", "crossbow"]

# define the prompt
character_maker = guidance("""The following is a character profile for an RPG game in JSON format.
```json
{
    "id": "{{id}}",
    "description": "{{description}}",
    "name": "{{gen 'name'}}",
    "age": {{gen 'age' pattern='[0-9]+' stop=','}},
    "armor": "{{#select 'armor'}}leather{{or}}chainmail{{or}}plate{{/select}}",
    "weapon": "{{select 'weapon' options=valid_weapons}}",
    "class": "{{gen 'class'}}",
    "mantra": "{{gen 'mantra' temperature=0.7}}",
    "strength": {{gen 'strength' pattern='[0-9]+' stop=','}},
    "items": [{{#geneach 'items' num_iterations=5 join=', '}}"{{gen 'this' temperature=0.7}}"{{/geneach}}]
}```""")

# generate a character
character_maker(
    id="e1f491f7-7ab8-4dac-8c20-c92b5e7d883d",
    description="A quick and nimble fighter.",
    valid_weapons=valid_weapons, llm=llama
)

In this example the prompt generates progressively a JSON, so having nested capture, even if technically may be possible (I don't know if it is) would result in an unreadable template.

Maybe I should append a custom block at the prompt start and inject it as a variable that my gen filter(?) can then use?
But still I should be able to detect the parent opening tag where gen lies to not have it into the generated prompt (e.g. the first example where it's placed within assistant tag