Spaces:
Runtime error
Runtime error
Upload with huggingface_hub
Browse files- app.py +83 -0
- requirements.txt +3 -0
- selfask.pmpt.tpl +39 -0
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
git+https://github.com/srush/minichain
|
3 |
+
manifest-ml
|
selfask.pmpt.tpl
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Question: Who lived longer, Muhammad Ali or Alan Turing?
|
2 |
+
Are follow up questions needed here: Yes.
|
3 |
+
Follow up: How old was Muhammad Ali when he died?
|
4 |
+
Intermediate answer: Muhammad Ali was 74 years old when he died.
|
5 |
+
Follow up: How old was Alan Turing when he died?
|
6 |
+
Intermediate answer: Alan Turing was 41 years old when he died.
|
7 |
+
So the final answer is: Muhammad Ali
|
8 |
+
|
9 |
+
Question: When was the founder of craigslist born?
|
10 |
+
Are follow up questions needed here: Yes.
|
11 |
+
Follow up: Who was the founder of craigslist?
|
12 |
+
Intermediate answer: Craigslist was founded by Craig Newmark.
|
13 |
+
Follow up: When was Craig Newmark born?
|
14 |
+
Intermediate answer: Craig Newmark was born on December 6, 1952.
|
15 |
+
So the final answer is: December 6, 1952
|
16 |
+
|
17 |
+
Question: Who was the maternal grandfather of George Washington?
|
18 |
+
Are follow up questions needed here: Yes.
|
19 |
+
Follow up: Who was the mother of George Washington?
|
20 |
+
Intermediate answer: The mother of George Washington was Mary Ball Washington.
|
21 |
+
Follow up: Who was the father of Mary Ball Washington?
|
22 |
+
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
|
23 |
+
So the final answer is: Joseph Ball
|
24 |
+
|
25 |
+
Question: Are both the directors of Jaws and Casino Royale from the same country?
|
26 |
+
Are follow up questions needed here: Yes.
|
27 |
+
Follow up: Who is the director of Jaws?
|
28 |
+
Intermediate answer: The director of Jaws is Steven Spielberg.
|
29 |
+
Follow up: Where is Steven Spielberg from?
|
30 |
+
Intermediate answer: The United States.
|
31 |
+
Follow up: Who is the director of Casino Royale?
|
32 |
+
Intermediate answer: The director of Casino Royale is Martin Campbell.
|
33 |
+
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}}
|