guidance-ai / guidance

A guidance language for controlling large language models.
MIT License
18.88k stars 1.04k forks source link

Return the thing matched by stop_regex #762

Open wendlerc opened 6 months ago

wendlerc commented 6 months ago

Is it possible to access the string that has been matched by stop regex?

I am working on a gsm8k tool-use example.

prompt = lm + '''
Solve the following math problems using the following tools: add, subtract, multiply, divide.
Each tool requires precisely two arguments. For example:
1 + 1 = add(1, 1) = 2
2 - 3 = subtract(2, 3) = -1
2 * 3 = multiply(2, 3) = 6.0
2 / 3 = divide(2, 3) = 0.6666666666666666
Questions are denoted by ### Question. For each question provide a caluclation denoted by ### Calculation and a consise answer denoted by ### Answer.

### Question 
Kylie and Robert enjoy going to the beach to collect shells. On Monday, Kylie collects 5 more shells than Robert, who collects 20 shells. On Tuesday, Kylie collects 2 times more shells than she did on Monday. How many shells does Kylie collect on Tuesday?
### Calculation 
Natalia sold 48/2 = divide(48,2) = 24 clips in May.
Natalia sold 48+24 = add(48,24) = 72 clips altogether in April and May.
### Answer
72
### Question
Weng earns $12 an hour for babysitting. Yesterday, she just did 50 minutes of babysitting. How much did she earn?
### Calculation
Weng earns 12/60 = divide(12/60) = $ 0.2 per minute.
Working 50 minutes, she earned 0.2 x 50 = multiply(0.2,50) = $ 10.
### Answer
10
### Question
'''
prompt + question + gen(max_tokens=500, tools=[add, subtract, multiply, divide], stop_regex="### Question")

Right now I have to use a hack and use the next ### Question, but would be nicer to parse the answer using stop_regex instead and return it.

Best, Chris

Harsha-Nori commented 6 months ago

Hey @wendlerc -- not sure if this is quite what you're asking, but you can pass in a name argument to any guidance library function like gen or select and then index the lm object to get back the generated text.

For example:

prompt = lm + '''
Solve the following math problems using the following tools: add, subtract, multiply, divide.
Each tool requires precisely two arguments. For example:
1 + 1 = add(1, 1) = 2
2 - 3 = subtract(2, 3) = -1
2 * 3 = multiply(2, 3) = 6.0
2 / 3 = divide(2, 3) = 0.6666666666666666
Questions are denoted by ### Question. For each question provide a caluclation denoted by ### Calculation and a consise answer denoted by ### Answer.

### Question 
Kylie and Robert enjoy going to the beach to collect shells. On Monday, Kylie collects 5 more shells than Robert, who collects 20 shells. On Tuesday, Kylie collects 2 times more shells than she did on Monday. How many shells does Kylie collect on Tuesday?
### Calculation 
Natalia sold 48/2 = divide(48,2) = 24 clips in May.
Natalia sold 48+24 = add(48,24) = 72 clips altogether in April and May.
### Answer
72
### Question
Weng earns $12 an hour for babysitting. Yesterday, she just did 50 minutes of babysitting. How much did she earn?
### Calculation
Weng earns 12/60 = divide(12/60) = $ 0.2 per minute.
Working 50 minutes, she earned 0.2 x 50 = multiply(0.2,50) = $ 10.
### Answer
10
### Question
'''
prompt += question + gen(name="answer", max_tokens=500, tools=[add, subtract, multiply, divide], stop_regex="### Question")
prompt['answer'] # contains the output from only the `gen` call here, not including the stop string ### Question

Let me know if I misinterpreted your question here!

wendlerc commented 6 months ago

No I would like to use something like...

prompt = lm + '''
Solve the following math problems using the following tools: add, subtract, multiply, divide.
Each tool requires precisely two arguments. For example:
1 + 1 = add(1, 1) = 2
2 - 3 = subtract(2, 3) = -1
2 * 3 = multiply(2, 3) = 6.0
2 / 3 = divide(2, 3) = 0.6666666666666666
Questions are denoted by ### Question. For each question provide a caluclation denoted by ### Calculation and a consise answer denoted by ### Answer.

### Question 
Kylie and Robert enjoy going to the beach to collect shells. On Monday, Kylie collects 5 more shells than Robert, who collects 20 shells. On Tuesday, Kylie collects 2 times more shells than she did on Monday. How many shells does Kylie collect on Tuesday?
### Calculation 
Natalia sold 48/2 = divide(48,2) = 24 clips in May.
Natalia sold 48+24 = add(48,24) = 72 clips altogether in April and May.
### Answer
72
### Question
Weng earns $12 an hour for babysitting. Yesterday, she just did 50 minutes of babysitting. How much did she earn?
### Calculation
Weng earns 12/60 = divide(12/60) = $ 0.2 per minute.
Working 50 minutes, she earned 0.2 x 50 = multiply(0.2,50) = $ 10.
### Answer
10
### Question
'''
prompt + question + gen(max_tokens=500, tools=[add, subtract, multiply, divide], stop_regex="### Answer\n\d+")

and then access the thing that is matched and removed by stop_regex... that would feel a lot cleaner to me than matching ### Question and hoping the thing before that was a number.