Spaces:
Runtime error
Runtime error
Upload with huggingface_hub
Browse files- app.py +48 -73
- requirements.txt +2 -1
- 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 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
from
|
|
|
|
|
|
|
| 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
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
for i in range(3):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 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 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 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: {{
|
| 38 |
-
Are followup questions needed here:
|
| 39 |
-
{{
|
|
|
|
| 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}}
|