Contextual Answers (In-prompt)

Contextual answers based on information passed into the prompt

Overview

Given a prompt containing information, ask a question about that information. The response will be limited to what the model can learn based on the provided information; information gained during model training outside the prompt will not be used in the answer.

Examples

from ai21 import AI21Client

client = AI21Client(api_key=<<AI21_API_KEY>>)

def in_prompt_contextual_answer():
    CONTEXT = """
In 2020 and 2021, enormous QE — approximately $4.4 trillion, or 18%, of 2021 gross
domestic product (GDP) — and enormous fiscal stimulus (which has been and
always will be inflationary) — approximately $5 trillion, or 21%, of 2021 GDP
— stabilized markets and allowed companies to raise enormous amounts of
capital. In addition, this infusion of capital saved many small businesses and
put more than $2.5 trillion in the hands of consumers and almost $1 trillion into
state and local coffers. These actions led to a rapid decline in unemployment, 
dropping from 15% to under 4% in 20 months — the magnitude and speed of which were both
unprecedented. Additionally, the economy grew 7% in 2021 despite the arrival of
the Delta and Omicron variants and the global supply chain shortages, which were
largely fueled by the dramatic upswing in consumer spending and the shift in
that spend from services to goods.
"""
    response = client.answer.create(
        context=CONTEXT,
        question="Did the economy shrink after the Omicron variant arrived?",
    )
    print(response)

## Response
AnswerResponse(id='c3b99578-b705-c245-ce54-5601c9b77cdb',
               answer_in_context=True, answer='No, the economy grew 7% in 2021 despite the arrival of the Omicron variant.')
import requests
ROOT_URL = "https://api.ai21.com/studio/v1/"

def tokenize():
   url = ROOT_URL + "tokenize"
   response = requests.post(
      url,
      headers={"Authorization": f"Bearer {AI21_API_KEY}"}, 
      json={"text": "I want to break free."}
   )
   print(response.json())
    
    
# Response
import requests
ROOT_URL = "https://api.ai21.com/studio/v1/"

def in_prompt_contextual_answer():
  url = ROOT_URL + "answer"
  CONTEXT = """
In 2020 and 2021, enormous QE — approximately $4.4 trillion, or 18%, of 2021 gross
domestic product (GDP) — and enormous fiscal stimulus (which has been and
always will be inflationary) — approximately $5 trillion, or 21%, of 2021 GDP
— stabilized markets and allowed companies to raise enormous amounts of
capital. In addition, this infusion of capital saved many small businesses and
put more than $2.5 trillion in the hands of consumers and almost $1 trillion into
state and local coffers. These actions led to a rapid decline in unemployment, 
dropping from 15% to under 4% in 20 months — the magnitude and speed of which were both
unprecedented. Additionally, the economy grew 7% in 2021 despite the arrival of
the Delta and Omicron variants and the global supply chain shortages, which were
largely fueled by the dramatic upswing in consumer spending and the shift in
that spend from services to goods.
"""
  response = requests.post(
      url,
      headers={"Authorization": f"Bearer {AI21_API_KEY}"}, 
      json={
         "context": CONTEXT,
         "question": "Did the economy shrink after the Omicron variant arrived?"
      }   
  )
  print(response.json())

# Response
{
  'id': '6e5ef1b9-1714-9e2a-2f16-02f60bf6d9aa',
  'answerInContext': True, 
  'answer': 'No, the economy grew 7% in 2021 despite the arrival of the Omicron variant.'
}

Response

The casing and styling depends on whether you are using the SDK or looking at the JSON returned by a REST call. For the SDK, convert the fields below to camel case.

  • answer: The answer to the question based on the provided context, or null if the answer cannot be found.
  • answerInContext: Boolean, True if an answer could be found, False if an answer could not be found.
  • id: A unique identifier assigned by the API to identify the specific request that generated the response. This parameter can be useful for tracking and logging purposes, particularly if multiple requests are being made to the API.
Language
Credentials
Header