srush commited on
Commit
81cdfe3
·
1 Parent(s): 17a97dd

Upload with huggingface_hub

Browse files
Files changed (3) hide show
  1. app.py +48 -73
  2. requirements.txt +2 -1
  3. selfask.pmpt.tpl +3 -3
app.py CHANGED
@@ -1,83 +1,58 @@
1
- # Notebook implementation of the self-ask + Google tool use prompt.
2
- # Adapted from https://github.com/ofirpress/self-ask
3
 
4
- from dataclasses import dataclass
 
 
 
5
 
6
- from parsita import *
 
 
7
 
8
- import minichain
9
-
10
- # Define the state of the bot.
11
-
12
- @dataclass
13
- class IntermediateState:
14
- s: str
15
-
16
- @dataclass
17
- class FinalState:
18
- s: str
19
 
20
  @dataclass
21
- class Out:
22
- echo: str
23
- state: FinalState | IntermediateState
24
-
25
-
26
- # Self Ask Prompt
27
-
28
- class SelfAsk(minichain.TemplatePrompt[Out]):
29
- template_file = "selfask.pmpt.tpl"
30
- stop_template = "\nIntermediate answer:"
31
-
32
- # Parsita parser.
33
- class Parser(TextParsers):
34
- follow = (lit("Follow up:") >> reg(r".*")) > IntermediateState
35
- finish = (lit("So the final answer is: ") >> reg(r".*")) > FinalState
36
- response = follow | finish
37
-
38
- def parse(self, response: str, inp) -> Out:
39
- return Out(
40
- self.prompt(inp).prompt + response,
41
- self.Parser.response.parse(response).or_die(),
42
- )
43
-
44
- # Runtime loop
45
-
46
- def selfask(inp: str, openai, google) -> str:
47
- prompt1 = SelfAsk(openai)
48
- prompt2 = minichain.SimplePrompt(google)
49
- suffix = ""
50
  for i in range(3):
51
- out = prompt1(dict(input=inp, suffix=suffix, agent_scratchpad=True))
52
-
53
- if isinstance(out.state, FinalState):
54
- break
55
- suffix += out.echo
56
- out2 = prompt2(out.state.s)
57
- suffix += "\nIntermediate answer: " + out2 + "\n"
58
- return out.state.s
59
-
60
-
61
- with minichain.start_chain("selfask") as backend:
62
- result = selfask(
63
- "What is the zip code of the city where George Washington was born?",
64
- backend.OpenAI(),
65
- backend.Google(),
66
- )
67
- print(result)
68
 
69
- # View prompt examples.
70
 
71
- # + tags=["hide_inp"]
72
- SelfAsk().show(
73
- {
74
- "input": "What is the zip code of the city where George Washington was born?",
75
- "agent_scratchpad": True,
76
- },
77
- "Follow up: Where was George Washington born?",
78
- )
79
- # -
80
 
81
- # View log.
82
 
83
- minichain.show_log("selfask.log")
 
 
 
1
 
2
+ desc = """
3
+ Notebook implementation of the self-ask + Google tool use prompt.
4
+ Adapted from https://github.com/ofirpress/self-ask
5
+ """
6
 
7
+ from dataclasses import dataclass, replace
8
+ from typing import Optional
9
+ from minichain import prompt, show, OpenAI, Google
10
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  @dataclass
13
+ class State:
14
+ question: str
15
+ history: str = ""
16
+ next_query: Optional[str] = None
17
+ final_answer: Optional[str] = None
18
+
19
+
20
+ @prompt(OpenAI(),
21
+ template_file = "selfask.pmpt.tpl",
22
+ stop_template = "\nIntermediate answer:")
23
+ def self_ask(model, state):
24
+ out = model(state)
25
+ res = out.split(":", 1)[1]
26
+ if out.startswith("Follow up:"):
27
+ return replace(state, next_query=res)
28
+ elif out.startswith("So the final answer is:"):
29
+ return replace(state, final_answer=res)
30
+
31
+ @prompt(Google())
32
+ def google(model, state):
33
+ if state.next_query is None:
34
+ return state
35
+
36
+ result = model(state.next_query)
37
+ return State(state.question,
38
+ state.history + "\nIntermediate answer: " + result + "\n")
39
+
40
+ def selfask(question):
41
+ state = State(question)
42
  for i in range(3):
43
+ state = self_ask(state)
44
+ state = google(state)
45
+ return state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
 
47
 
48
+ gradio = show(selfask,
49
+ examples=["What is the zip code of the city where George Washington was born?"],
50
+ subprompts=[self_ask, google] * 3,
51
+ description=desc,
52
+ code=open("selfask.py", "r").read().split("$")[1].strip().strip("#").strip(),
53
+ out_type="json"
54
+ )
55
+ if __name__ == "__main__":
56
+ gradio.launch()
57
 
 
58
 
 
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
- gradio
2
  git+https://github.com/srush/minichain@gradio
3
  manifest-ml
 
 
1
+ gradio==3.21.0
2
  git+https://github.com/srush/minichain@gradio
3
  manifest-ml
4
+ faiss-cpu
selfask.pmpt.tpl CHANGED
@@ -34,6 +34,6 @@ Follow up: Where is Martin Campbell from?
34
  Intermediate answer: New Zealand.
35
  So the final answer is: No
36
 
37
- Question: {{input}}
38
- Are followup questions needed here: {% if agent_scratchpad %}Yes{%else%}No{% endif %}.
39
- {{suffix}}
 
34
  Intermediate answer: New Zealand.
35
  So the final answer is: No
36
 
37
+ Question: {{question}}
38
+ Are followup questions needed here: Yes
39
+ {{history}}