Selfask
In [ ]:
Copied!
In [1]:
Copied!
desc = """
### Self-Ask
Notebook implementation of the self-ask + Google tool use prompt.
(Adapted from [Self-Ask repo](https://github.com/ofirpress/self-ask))
"""
desc = """
### Self-Ask
Notebook implementation of the self-ask + Google tool use prompt.
(Adapted from [Self-Ask repo](https://github.com/ofirpress/self-ask))
"""
$
In [2]:
Copied!
from dataclasses import dataclass, replace
from typing import Optional
from minichain import prompt, show, OpenAI, Google
from dataclasses import dataclass, replace
from typing import Optional
from minichain import prompt, show, OpenAI, Google
In [3]:
Copied!
@dataclass
class State:
question: str
history: str = ""
next_query: Optional[str] = None
final_answer: Optional[str] = None
@dataclass
class State:
question: str
history: str = ""
next_query: Optional[str] = None
final_answer: Optional[str] = None
In [4]:
Copied!
@prompt(OpenAI(),
template_file = "selfask.pmpt.tpl",
stop_template = "\nIntermediate answer:")
def self_ask(model, state):
out = model(state)
res = out.split(":", 1)[1]
if out.startswith("Follow up:"):
return replace(state, next_query=res)
elif out.startswith("So the final answer is:"):
return replace(state, final_answer=res)
@prompt(OpenAI(),
template_file = "selfask.pmpt.tpl",
stop_template = "\nIntermediate answer:")
def self_ask(model, state):
out = model(state)
res = out.split(":", 1)[1]
if out.startswith("Follow up:"):
return replace(state, next_query=res)
elif out.startswith("So the final answer is:"):
return replace(state, final_answer=res)
In [5]:
Copied!
@prompt(Google())
def google(model, state):
if state.next_query is None:
return state
result = model(state.next_query)
return State(state.question,
state.history + "\nIntermediate answer: " + result + "\n")
@prompt(Google())
def google(model, state):
if state.next_query is None:
return state
result = model(state.next_query)
return State(state.question,
state.history + "\nIntermediate answer: " + result + "\n")
In [6]:
Copied!
def selfask(question):
state = State(question)
for i in range(3):
state = self_ask(state)
state = google(state)
return state
def selfask(question):
state = State(question)
for i in range(3):
state = self_ask(state)
state = google(state)
return state
$
In [7]:
Copied!
gradio = show(selfask,
examples=["What is the zip code of the city where George Washington was born?"],
subprompts=[self_ask, google] * 3,
description=desc,
code=open("selfask.py", "r").read().split("$")[1].strip().strip("#").strip(),
out_type="json"
)
if __name__ == "__main__":
gradio.launch()
gradio = show(selfask,
examples=["What is the zip code of the city where George Washington was born?"],
subprompts=[self_ask, google] * 3,
description=desc,
code=open("selfask.py", "r").read().split("$")[1].strip().strip("#").strip(),
out_type="json"
)
if __name__ == "__main__":
gradio.launch()
Running on local URL: http://127.0.0.1:7861 To create a public link, set `share=True` in `launch()`.
In [ ]:
Copied!