Explaining Concepts with QA Examples¶
In this section, we will explain the core concepts of APPL using question-answering examples. These examples demonstrate how to use APPL to manage prompts, request and retrieve LM responses, and embed LM interactions in Python workflows.
Extract Name from Quotation¶
Let's begin with a simple task used in this cookbook that involves extracting the author's name from a quotation.
The @ppl
decorator marks the get_author_name
function as an APPL function. Within the function, the string literal "Extract the name of the author from the quotation below."
and variable quotation
are added to the prompt. The gen()
function is then called to generate responses using the current prompt. Finally, the generated response is returned as the answer to the question. This APPL function can be called like a normal Python function, as shown in the complete code snippet below.
In this code, the call to the get_author_name
function is equivalent to querying the LM with the prompt:
Role | Message |
---|---|
User | Extract the name of the author from the quotation below. "Simplicity is the ultimate sophistication." -- Leonardo da Vinci |
The output may looks like:
Specify Prefix for Response¶
Further, you may want to guide the response following a specific prefix so that the generated response is just the author's name like Leonardo da Vinci
in the previous example.
To achieve this, you can specify the prefix in the prompt as shown below:
Notably, the f-string is processed part by part, so the gen
function inside the f-string intuitively uses the contents before that. The warlus operator :=
is used to store the generated response in the answer
variable and return it as the answer to the question.
The prompt to the LM includes the prefix The name of the author is
as an assistant message, and the generated response (in bold) is also added to the conversation as part of the f-string:
Role | Message |
---|---|
User | Extract the name of the author from the quotation below. "Simplicity is the ultimate sophistication." -- Leonardo da Vinci |
Assistant | The name of the author is |
Role | Message |
---|---|
User | Extract the name of the author from the quotation below. "Simplicity is the ultimate sophistication." -- Leonardo da Vinci |
Assistant | The name of the author is Leonardo da Vinci |
Using AIRole
context manager
The AIRole
context manager is used to specify the message role of the prompts within its scope. That saying, the prompts within the AIRole
block is treated as assistant messages. Similarly, you can use SystemRole
to specify system messages.
You can use from appl import AIRole, SystemRole
to import these context managers.
Answer Follow-up Questions¶
Let's extend the previous example to answer multiple independent follow-up questions about the author. The following code demonstrates how to answer questions about the author's era based on the extracted name.
Note the @ppl(ctx="copy")
decorator in the get_answer
function, which specify the method to obtain the context from its caller is by copying (more options explained in context passing). This ensures that the context of the caller is not affected by the prompts in the get_answer
function. The resulting conversation (convo) for the first and second question would look like (generated responses are in bold):
Role | Message |
---|---|
User | Extract the name of the author from the quotation below and answer questions. "Simplicity is the ultimate sophistication." -- Leonardo da Vinci |
Assistant | The name of the author is Leonardo da Vinci. |
User | In what era did the author live? |
Assistant | Leonardo da Vinci lived during the Renaissance era. |
Role | Message |
---|---|
User | Extract the name of the author from the quotation below and answer questions. "Simplicity is the ultimate sophistication." -- Leonardo da Vinci |
Assistant | The name of the author is Leonardo da Vinci. |
User | What is the most famous painting of the author? |
Assistant | The most famous painting of Leonardo da Vinci is the Mona Lisa. |
Role | Message |
---|---|
User | Extract the name of the author from the quotation below and answer questions. "Simplicity is the ultimate sophistication." -- Leonardo da Vinci |
Assistant | The name of the author is Leonardo da Vinci. |
The output of the code snippet would look like:
Leonardo da Vinci lived during the Renaissance era.
The most famous painting of Leonardo da Vinci is the Mona Lisa.
Automatic Parallelization¶
Thanks to the asynchronous design of APPL, the indenpendent gen
function calls in the answer_questions
function can be executed in parallel, without introducing extra code for parallelization. See more details in concurrent execution.
Sequential Question Answering¶
In some cases, you may want to answer questions sequentially, where the answer to a question depends on the previous answers. The following code demonstrates how to answer questions sequentially by iterating over the questions and generating responses one by one.
The resulting conversation would look like (generated responses are in bold):
Role | Message |
---|---|
User | Extract the name of the author from the quotation below and answer questions. "Simplicity is the ultimate sophistication." -- Leonardo da Vinci |
Assistant | The name of the author is Leonardo da Vinci. |
User | In what era did the author live? |
Assistant | Leonardo da Vinci lived during the Renaissance era. |
User | What is the most famous painting of the author? |
Assistant | The most famous painting of Leonardo da Vinci is the Mona Lisa. |