最后活跃于 1718132229

kristofer's Avatar kristofer 修订了这个 Gist 1718132229. 跳至此修订

1 file changed, 70 insertions

guidance-example.py(file created)

@@ -0,0 +1,70 @@
1 + import guidance
2 + gpt35 = guidance.models.OpenAI("gpt-3.5-turbo")
3 +
4 + import re
5 + from guidance import gen, select, system, user, assistant
6 +
7 + @guidance
8 + def plan_for_goal(lm, goal: str):
9 +
10 + # This is a helper function which we will use below
11 + def parse_best(prosandcons, options):
12 + best = re.search(r'Best=(\d+)', prosandcons)
13 + if not best:
14 + best = re.search(r'Best.*?(\d+)', 'Best= option is 3')
15 + if best:
16 + best = int(best.group(1))
17 + else:
18 + best = 0
19 + return options[best]
20 +
21 + # Some general instruction to the model
22 + with system():
23 + lm += "You are a helpful assistant."
24 +
25 + # Simulate a simple request from the user
26 + # Note that we switch to using 'lm2' here, because these are intermediate steps (so we don't want to overwrite the current lm object)
27 + with user():
28 + lm2 = lm + f"""\
29 + I want to {goal}
30 + Can you please generate one option for how to accomplish this?
31 + Please make the option very short, at most one line."""
32 +
33 + # Generate several options. Note that this means several sequential generation requests
34 + n_options = 5
35 + with assistant():
36 + options = []
37 + for i in range(n_options):
38 + options.append((lm2 + gen(name='option', temperature=1.0, max_tokens=50))["option"])
39 +
40 + # Have the user request pros and cons
41 + with user():
42 + lm2 += f"""\
43 + I want to {goal}
44 + Can you please comment on the pros and cons of each of the following options, and then pick the best option?
45 + ---
46 + """
47 + for i, opt in enumerate(options):
48 + lm2 += f"Option {i}: {opt}\n"
49 + lm2 += f"""\
50 + ---
51 + Please discuss each option very briefly (one line for pros, one for cons), and end by saying Best=X, where X is the number of the best option."""
52 +
53 + # Get the pros and cons from the model
54 + with assistant():
55 + lm2 += gen(name='prosandcons', temperature=0.0, max_tokens=600, stop="Best=") + "Best=" + gen("best", regex="[0-9]+")
56 +
57 + # The user now extracts the one selected as the best, and asks for a full plan
58 + # We switch back to 'lm' because this is the final result we want
59 + with user():
60 + lm += f"""\
61 + I want to {goal}
62 + Here is my plan: {options[int(lm2["best"])]}
63 + Please elaborate on this plan, and tell me how to best accomplish it."""
64 +
65 + # The plan is generated
66 + with assistant():
67 + lm += gen(name='plan', max_tokens=500)
68 +
69 + return lm
70 +
更新 更早