In [1]:
# Prompt from ...
#
from dataclasses import dataclass
In [2]:
from parsita import TextParsers, lit, reg
In [3]:
from minichain import Backend, JinjaPrompt, SimplePrompt, start_chain
In [4]:
# Define the states of the bot.
@dataclass
class IntermediateState:
    s: str
@dataclass
class FinalState:
    s: str
States = FinalState | IntermediateState
In [5]:
class SelfAsk(JinjaPrompt[States]):
    template_file = "selfask.pmpt.tpl"
    stop_template = "\nIntermediate answer:"

    class SelfAskParser(TextParsers):  # type: ignore
        follow = (lit("Follow up:") >> reg(r".*")) > IntermediateState
        finish = (lit("So the final answer is: ") >> reg(r".*")) > FinalState
        response = follow | finish

    def parse(self, inp: str) -> States:
        return self.SelfAskParser.response.parse(inp).or_die()  # type: ignore
In [6]:
SelfAsk().show({"input": "What is the zip code of the city where George Washington was born?",
                "agent_scratchpad":True},
               "Follow up: Where was George Washington born?")
Out[6]:

SelfAsk

Input:
{'input': 'What is the zip code of the city where George Washington was born?', 'agent_scratchpad': True}
Full Prompt:

Question: Who lived longer, Muhammad Ali or Alan Turing?
Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali

Question: When was the founder of craigslist born?
Are follow up questions needed here: Yes.
Follow up: Who was the founder of craigslist?
Intermediate answer: Craigslist was founded by Craig Newmark.
Follow up: When was Craig Newmark born?
Intermediate answer: Craig Newmark was born on December 6, 1952.
So the final answer is: December 6, 1952

Question: Who was the maternal grandfather of George Washington?
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball

Question: Are both the directors of Jaws and Casino Royale from the same country?
Are follow up questions needed here: Yes.
Follow up: Who is the director of Jaws?
Intermediate answer: The director of Jaws is Steven Spielberg.
Follow up: Where is Steven Spielberg from?
Intermediate answer: The United States.
Follow up: Who is the director of Casino Royale?
Intermediate answer: The director of Casino Royale is Martin Campbell.
Follow up: Where is Martin Campbell from?
Intermediate answer: New Zealand.
So the final answer is: No

Question:

What is the zip code of the city where George Washington was born?

Are followup questions needed here: Yes.

Response:
Follow up: Where was George Washington born?
Value:
IntermediateState(s='Where was George Washington born?')
In [ ]:
    
In [7]:
def selfask(inp: str, openai: Backend, google: Backend) -> str:
    prompt1 = SelfAsk(openai)
    prompt2 = SimplePrompt(google)
    suffix = ""
    for i in range(3):
        r1 = prompt1(
            dict(input=inp, suffix=suffix, agent_scratchpad=True), name=f"Chat {i}"
        )
        if isinstance(r1.val, FinalState):
            break
        out = prompt2(r1.val.s, name=f"Google{i}")
        suffix += "\nIntermediate answer:" + out.echo
    return r1.val.s
In [8]:
with start_chain("selfask") as backend:
    result = selfask(
        "What is the zip code of the city where George Washington was born?",
        backend.Mock(
            [
                "Follow up: Where was George Washington born?",
                "Follow up: What is the zip code of Virginia?",
                "So the final answer is: 12312",
            ]
        ),
        # OpenAI("")
        backend.Mock(["Virginia", "12312"]),
    )
print(result)
12312
In [9]:
!eliot-tree -l 0 selfask.log | grep -v "succeeded" | grep -v "started"
    
    # Google("593a073fa4c730efe918e592a538b36e80841bc8f8dd4070c1566920f75ba140")))
7ddbd0c1-8097-4462-be99-111df5d2d298

64d9f8ad-084a-4216-987a-4c74e4162cb8
    │   ├── input: What is the zip code of Virginia?
    │   ├── prompt: What is the zip code of Virginia?
    │   ├── result: 12312
    │   │   └── returned: 12312

e9d4f97d-0b3f-4303-9c98-d904c1aa8f24
    │   ├── input: 
    │   │   ├── agent_scratchpad: True
    │   │   ├── input: What is the zip code of the city where George Washington was born?
    │   │   └── suffix: ⏎
    │   │       Intermediate answer:Where was George Washington born?Virginia⏎
    │   │       Intermediate answer:What is the zip code of Virginia?12312
    │   ├── prompt: Question: Who lived longer, Muhammad Ali or Alan Turing?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: How old was Muhammad Ali when he died?⏎
    │   │   Intermediate answer: Muhammad Ali was 74 years old when he died.⏎
    │   │   Follow up: How old was Alan Turing when he died?⏎
    │   │   Intermediate answer: Alan Turing was 41 years old when he died.⏎
    │   │   So the final answer is: Muhammad Ali⏎
    │   │   ⏎
    │   │   Question: When was the founder of craigslist born?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who was the founder of craigslist?⏎
    │   │   Intermediate answer: Craigslist was founded by Craig Newmark.⏎
    │   │   Follow up: When was Craig Newmark born?⏎
    │   │   Intermediate answer: Craig Newmark was born on December 6, 1952.⏎
    │   │   So the final answer is: December 6, 1952⏎
    │   │   ⏎
    │   │   Question: Who was the maternal grandfather of George Washington?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who was the mother of George Washington?⏎
    │   │   Intermediate answer: The mother of George Washington was Mary Ball Washington.⏎
    │   │   Follow up: Who was the father of Mary Ball Washington?⏎
    │   │   Intermediate answer: The father of Mary Ball Washington was Joseph Ball.⏎
    │   │   So the final answer is: Joseph Ball⏎
    │   │   ⏎
    │   │   Question: Are both the directors of Jaws and Casino Royale from the same country?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who is the director of Jaws?⏎
    │   │   Intermediate answer: The director of Jaws is Steven Spielberg.⏎
    │   │   Follow up: Where is Steven Spielberg from?⏎
    │   │   Intermediate answer: The United States.⏎
    │   │   Follow up: Who is the director of Casino Royale?⏎
    │   │   Intermediate answer: The director of Casino Royale is Martin Campbell.⏎
    │   │   Follow up: Where is Martin Campbell from?⏎
    │   │   Intermediate answer: New Zealand.⏎
    │   │   So the final answer is: No⏎
    │   │   ⏎
    │   │   Question: What is the zip code of the city where George Washington was born?⏎
    │   │   Are followup questions needed here: Yes.⏎
    │   │   ⏎
    │   │   Intermediate answer:Where was George Washington born?Virginia⏎
    │   │   Intermediate answer:What is the zip code of Virginia?12312
    │   ├── result: So the final answer is: 12312
    │   ├── eliot:destination_failure/4/3 2023-02-23 22:29:49Z
    │   │   ├── exception: builtins.TypeError
    │   │   └── reason: Object of type FinalState is not JSON serializable

4a574952-6198-446a-a637-7697eede2043
    │   ├── input: 
    │   │   ├── agent_scratchpad: True
    │   │   ├── input: What is the zip code of the city where George Washington was born?
    │   │   └── suffix: 
    │   ├── prompt: Question: Who lived longer, Muhammad Ali or Alan Turing?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: How old was Muhammad Ali when he died?⏎
    │   │   Intermediate answer: Muhammad Ali was 74 years old when he died.⏎
    │   │   Follow up: How old was Alan Turing when he died?⏎
    │   │   Intermediate answer: Alan Turing was 41 years old when he died.⏎
    │   │   So the final answer is: Muhammad Ali⏎
    │   │   ⏎
    │   │   Question: When was the founder of craigslist born?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who was the founder of craigslist?⏎
    │   │   Intermediate answer: Craigslist was founded by Craig Newmark.⏎
    │   │   Follow up: When was Craig Newmark born?⏎
    │   │   Intermediate answer: Craig Newmark was born on December 6, 1952.⏎
    │   │   So the final answer is: December 6, 1952⏎
    │   │   ⏎
    │   │   Question: Who was the maternal grandfather of George Washington?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who was the mother of George Washington?⏎
    │   │   Intermediate answer: The mother of George Washington was Mary Ball Washington.⏎
    │   │   Follow up: Who was the father of Mary Ball Washington?⏎
    │   │   Intermediate answer: The father of Mary Ball Washington was Joseph Ball.⏎
    │   │   So the final answer is: Joseph Ball⏎
    │   │   ⏎
    │   │   Question: Are both the directors of Jaws and Casino Royale from the same country?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who is the director of Jaws?⏎
    │   │   Intermediate answer: The director of Jaws is Steven Spielberg.⏎
    │   │   Follow up: Where is Steven Spielberg from?⏎
    │   │   Intermediate answer: The United States.⏎
    │   │   Follow up: Who is the director of Casino Royale?⏎
    │   │   Intermediate answer: The director of Casino Royale is Martin Campbell.⏎
    │   │   Follow up: Where is Martin Campbell from?⏎
    │   │   Intermediate answer: New Zealand.⏎
    │   │   So the final answer is: No⏎
    │   │   ⏎
    │   │   Question: What is the zip code of the city where George Washington was born?⏎
    │   │   Are followup questions needed here: Yes.⏎
    │   │   
    │   ├── result: Follow up: Where was George Washington born?
    │   ├── eliot:destination_failure/4/3 2023-02-23 22:29:49Z
    │   │   ├── exception: builtins.TypeError
    │   │   └── reason: Object of type IntermediateState is not JSON serializable

d364fca6-fe3e-40df-8996-b5b24ca32c75
    │   ├── input: 
    │   │   ├── agent_scratchpad: True
    │   │   ├── input: What is the zip code of the city where George Washington was born?
    │   │   └── suffix: ⏎
    │   │       Intermediate answer:Where was George Washington born?Virginia
    │   ├── prompt: Question: Who lived longer, Muhammad Ali or Alan Turing?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: How old was Muhammad Ali when he died?⏎
    │   │   Intermediate answer: Muhammad Ali was 74 years old when he died.⏎
    │   │   Follow up: How old was Alan Turing when he died?⏎
    │   │   Intermediate answer: Alan Turing was 41 years old when he died.⏎
    │   │   So the final answer is: Muhammad Ali⏎
    │   │   ⏎
    │   │   Question: When was the founder of craigslist born?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who was the founder of craigslist?⏎
    │   │   Intermediate answer: Craigslist was founded by Craig Newmark.⏎
    │   │   Follow up: When was Craig Newmark born?⏎
    │   │   Intermediate answer: Craig Newmark was born on December 6, 1952.⏎
    │   │   So the final answer is: December 6, 1952⏎
    │   │   ⏎
    │   │   Question: Who was the maternal grandfather of George Washington?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who was the mother of George Washington?⏎
    │   │   Intermediate answer: The mother of George Washington was Mary Ball Washington.⏎
    │   │   Follow up: Who was the father of Mary Ball Washington?⏎
    │   │   Intermediate answer: The father of Mary Ball Washington was Joseph Ball.⏎
    │   │   So the final answer is: Joseph Ball⏎
    │   │   ⏎
    │   │   Question: Are both the directors of Jaws and Casino Royale from the same country?⏎
    │   │   Are follow up questions needed here: Yes.⏎
    │   │   Follow up: Who is the director of Jaws?⏎
    │   │   Intermediate answer: The director of Jaws is Steven Spielberg.⏎
    │   │   Follow up: Where is Steven Spielberg from?⏎
    │   │   Intermediate answer: The United States.⏎
    │   │   Follow up: Who is the director of Casino Royale?⏎
    │   │   Intermediate answer: The director of Casino Royale is Martin Campbell.⏎
    │   │   Follow up: Where is Martin Campbell from?⏎
    │   │   Intermediate answer: New Zealand.⏎
    │   │   So the final answer is: No⏎
    │   │   ⏎
    │   │   Question: What is the zip code of the city where George Washington was born?⏎
    │   │   Are followup questions needed here: Yes.⏎
    │   │   ⏎
    │   │   Intermediate answer:Where was George Washington born?Virginia
    │   ├── result: Follow up: What is the zip code of Virginia?
    │   ├── eliot:destination_failure/4/3 2023-02-23 22:29:49Z
    │   │   ├── exception: builtins.TypeError
    │   │   └── reason: Object of type IntermediateState is not JSON serializable

09dba4ca-0839-4cd8-a9da-f5815e126bb7
    │   ├── input: Where was George Washington born?
    │   ├── prompt: Where was George Washington born?
    │   ├── result: Virginia
    │   │   └── returned: Virginia