Skip to main content

Text Simplification

In this use-case example, we show you how to rewrite complex texts with our Luminous models. More precisely, we demonstrate how to simplify texts. In doing so, the content and structure of a text are changed in such a way that it is easier to understand, while retaining the original meaning.

Specifically, we go through the process of building and optimizing a prompt to rewrite texts.

First, we import the necessary packages.

from aleph_alpha_client import Prompt, Client, CompletionRequest
import os

Normally, this is where we would load the training data and explain which model is best suited to solve this task. However, as our Luminous models are pre-trained large language models, there is no need to train a new model. We simply have to define a prompt and test it on some data.

This prompt is from our Playground and can serve as a basis for your language simplification experiments with our Luminous models.

prompt_text = """My sixth grader asked me to explain each of the following texts to him:
###
Text: The natural phenomenon of gravity asserts that all objects with mass or energy are attracted to (or gravitate toward) one another It is the weakest of the four fundamental interactions of physics but the most impactful one at a macroscopic scale.
Simplified: Gravity describes how heavy objects move towards one another.
###
Text: A loan describes the lending of money by some entity (or multiple entities) to another. Such entities may be individuals or organizations. The borrower incurs a debt to the lender and is usually obligated to pay interest on that debt until the loan is fully repaid.
Simplified: A loan is when one person gives money to another in hopes of getting more money back later.
###
Text: {}
Simplified:"""

Next, we define some test data to see how well our prompt works.

test_examples = [
"Insurance is a contract in which one party agrees to provide financial protection to another party in the event of a specified loss. The insurer agrees to pay the policyholder a predetermined amount in the event of a covered loss, such as the death of an insured individual or the destruction of property. In exchange, the policyholder agrees to pay premiums to the insurer on a regular basis.",
"Existentialism is a form of philosophical inquiry that addresses the problem of human existence and focuses on the experience of thinking, feeling and acting. Existentialists assume that the starting point of the individual is a state of 'existential angst' - a sense of fear in the face of the apparent meaninglessness of life. Without inherent meaning, individuals must create a purpose for themselves to give meaning to their lives.",
"A basketball game consists of two teams of five players each who compete against each other on a rectangular court. The objective of the game is to score points by shooting a ball through a hoop, called the basket, that is mounted on a backboard at each end of the court. Points are scored when a player successfully throws the ball into the basket, with different shot values awarded based on the distance from which the shot was taken."
]

Finally, let's initialize the client and iterate over the test examples to generate a completion for each. 

client = Client(token=os.getenv("AA_TOKEN"))
for example in test_examples:
params = {
"prompt": Prompt.from_text(prompt_text.format(example)),
"maximum_tokens": 150,
"stop_sequences": ["###", "\n"],
}
request = CompletionRequest(**params)
response = client.complete(request=request, model="luminous-supreme")
completion = response.completions[0].completion
print(f"Simplified Text: {completion.strip()}""")

Example Output:

Insurance is a contract between two people. One person pays the other person money in case something bad happens.
Existentialism is a philosophy that says that we are all alone in the world and we have to make our own meaning.
A basketball game is a game where two teams of five people each try to score points by throwing a ball into a basket.

Using Luminous-supreme-control

Luminous-supreme-control is our steerable model that is optimized to work well with zero-shot tasks. In the following prompt, we use a similar text to the one in the few-shot example but leave out the actual examples. This saves us a lot of input tokens with results of similar quality.

control_prompt_text = """My sixth grader asked me to explain the following texts to him.
Text: {}
Output:"""

for example in test_examples:
params = {
"prompt": Prompt.from_text(control_prompt_text.format(example)),
"maximum_tokens": 150,
}
request = CompletionRequest(**params)
response = client.complete(request=request, model="luminous-supreme-control")
completion = response.completions[0].completion
print(f"Simplified Text: {completion.strip()}""")

Example Output:

Insurance is a contract in which one party agrees to provide financial protection to another party in the event of a specified loss.
Existentialism is a philosophy that focuses on the experience of thinking, feeling and acting.
Basketball is a sport played by two teams of five players each. The objective of the game is to score points by shooting a ball through a hoop, called the basket, that is mounted on a backboard at each end of the court.