Finding relevant paragraphs

Paragraph-wise classification with parallelism

Classifying individual paragraphs using classify

Let’s start by just classifying whether the first paragraph answers a question. To do this, we’ll use a new agent method, classify. It takes a prompt and a list of choices, and returns a choice, a choice probability, and for some agent implementations an explanation.

Our single-paragraph classifier looks like this:

paper_qa_class.py
from fvalues import F

from ice.paper import Paper
from ice.paper import Paragraph
from ice.recipe import recipe


def make_prompt(paragraph: Paragraph, question: str) -> str:
    return F(
        f"""
Here is a paragraph from a research paper: "{paragraph}"

Question: Does this paragraph answer the question '{question}'? Say Yes or No.
Answer:"""
    ).strip()


async def classify_paragraph(paragraph: Paragraph, question: str) -> float:
    choice_probs, _ = await recipe.agent().classify(
        prompt=make_prompt(paragraph, question),
        choices=(" Yes", " No"),
    )
    return choice_probs.get(" Yes", 0.0)


async def answer_for_paper(paper: Paper, question: str):
    paragraph = paper.paragraphs[0]
    return await classify_paragraph(paragraph, question)


recipe.main(answer_for_paper)

Save it and run it on a paper:

You should see a result like this:

The trace looks simple:

Execution trace (view online)

According to the model, the first paragraph is unlikely to answer the question.

Classifying all paragraphs in parallel with map_async

To find the most relevant paragraphs, we map the paragraph classifier over all paragraphs and get the most likely ones.

For mapping, we use the utility map_async which runs the language model calls in parallel:

You will now see a list of probabilities, one for each paragraph:

Now all we need to do is add a utility function for looking up the paragraphs with the highest probabilities:

Running the same command again…

…we indeed get paragraphs that answer the question what the study population was!

The trace:

Execution trace (view online)

Last updated