The Contextual Answers API is a tool for providing accurate and reliable question answering based on specific document context. The API has two modes of operation:

  1. Contextual Answers [Single Document]: This feature receives document text and a question and returns an answer based solely on the provided context. This method requires the context to be sent along with the question in every request to the API.

    • Try Contextual Answers [Single Document] here
  2. Contextual Answers [RAG Engine]: A new feature that allows users to upload multiple documents to the RAG Engine in advance, and then retrieve answers based on these stored documents. This method only requires the question to be sent in the API request, and it returns an answer found in the organization's RAG Engine, along with information about which document(s) the answer was found in.

    • Try Contextual Answers [Document Library] here

Users also have a set of APIs to manage their Document Library. It includes operations to upload a document, get a list of documents, get a document by id, update a document, and delete a document.

Request Parameters

The Contextual Answers API requires different parameters depending on the method used:

For Contextual Answers [Single Document]:

  • context: A string containing the document context for which the question will be answered. Limit: 50,000 characters.
  • question: A string containing the question to be answered based on the provided context. Limit: 160 characters.

For Contextual Answers [RAG Engine]:

  • question: A string containing the question to be answered based on the documents in the RAG Engine. Limit: 150 characters.
  • path: A string specifying the document's path in the RAG Engine. Optional
  • labels: An array of strings representing the labels of the documents to be considered when answering the question. Optional
  • documentIds: An array of document IDs to specify which documents to consider when answering the question. Optional

Response Parameters

For Contextual Answers [Single Document]:

The Contextual Answers API returns the following parameter in its response:

  • answer: A string containing the answer to the question based on the provided context. The response will be "Answer not in document" if the model cannot find an answer in the provided context.
  • 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.

For Contextual Answers [RAG Engine]:

The Contextual Answers API returns the following parameters in its response:

  • answer: A string containing the answer to the question based on the contents in the RAG Engine. The response will be "Answer not in document" if the model cannot find an answer anywhere in the RAG Engine.
  • 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.
  • answerInContext: A boolean indicating if the answer was found within the RAG Engine.
  • sources: An array of objects, where each object represents a source document in which the answer was found. Each source object contains:
    • document_id: A unique identifier of the source document.
    • name: The name of the source document.
    • highlights: An array of strings containing the sections of the source document that are relevant to the answer.
    • publicUrl: A URL pointing to the source document (if available).

Example: Contextual Answers [Single Document]

{
   "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. Fortunately, during these two years, vaccines for COVID-19 were also rapidly developed and distributed.In today's economy, the consumer is in excellent financial shape (on average), with leverage among the lowest on record, excellent mortgage underwriting (even though we've had home price appreciation), plentiful jobs with wage increases and more than $2 trillion in excess savings, mostly due to government stimulus. Most consumers and companies (and states) are still flush with the money generated in 2020 and 2021, with consumer spending over the last several months 12% above pre-COVID-19 levels. (But we must recognize that the account balances in lower-income households, smaller to begin with, are going down faster and that income for those households is not keeping pace with rising inflation.) Today's economic landscape is completely different from the 2008 financial crisis when the consumer was extraordinarily overleveraged, as was the financial system as a whole — from banks and investment banks to shadow banks, hedge funds, private equity, Fannie Mae and many other entities. In addition, home price appreciation, fed by bad underwriting and leverage in the mortgage system, led to excessive speculation, which was missed by virtually everyone — eventually leading to nearly $1 trillion in actual losses.",
   "question": "Did the economy shrink after the Omicron variant arrived?"
}

The following is a possible response from the Contextual Answers [Single Document] API to the example request above:

{
  "id": "36d2ec63-ef15-2296-af51-ef34206fc655",
  "answer": "The Delta and Omicron variants and the global supply chain shortages did not slow down economic growth."
}

Example: Contextual Answers [RAG Engine]

{
   "question": "During the past decade, what amount did the bank remunerate the Federal Deposit Insurance Corporation?",
   "labels": ["financial_reports", "banking"]
}

The following is a possible response from the Contextual Answers [RAG Engine] API to the example request above:

{
   "id":"28a6f1a8-31f3-cca4-d71a-6bca2e62d124",
   "answerInContext":true,
   "answer":"JPMorgan Chase paid the Federal Deposit Insurance Corporation $11 billion over the past decade.",
   "sources":[
      {
         "document_id":"f8527829-c036-4d3a-9a6e-3169f4db1765",
         "name":"examples/banking/jpmorgan-annual-report-2021.txt",
         "highlights":[
            "I do just want to note that in our case, the silent partner is not so silent.\n\nJPMorgan Chase is a healthy and thriving company, and we always want to give\r\nback and pay our fair share.\n\nWe do — and we want it to be spent well and have\r\nthe greatest impact.\n\nTo give you an idea of where our taxes and fees go: In the\r\nlast 10 years, we paid $42 billion in federal, state and local taxes in the United\r\nStates and $17 billion in taxes outside of the United States.\n\nWe also paid the\r\nFederal Deposit Insurance Corporation $11 billion so that it has the resources\r\nto cover the failure of any major American bank.",
            "31 Traditional assets include Equity, Fixed Income, Multi-Asset and Liquidity assets under\r\n2 Federal Deposit Insurance Corporation (“FDIC”)\n\n2021 Summary of Deposits survey per S&P                 management; Brokerage, Administration and Custody assets under supervision.",
            "In addition to banks’ shrinking global role, you\r\n  U.S. debt and equity markets, down from 11%         can see that the number of public companies,\r\n  in 2010.\n\nwhich should have grown substantially over the\r\n• Conversely, U.S. banks’ liquid assets are up        past decade, is remarkably reduced.\n\nInstead, U.S.\r\n  more than 300% to $8.6 trillion, most of which      public companies peaked in 1996 at 7,300 and\r\n  is needed to meet liquidity requirements.\n\nnow total 4,800.\n\nConversely, the number of pri-\r\n                                                      vate U.S. companies backed by private equity\r\n• Banks’ share of mortgage originations has           companies has grown from 1,600 to 10,100 — a\r\n  gone from 91% to 32%.\n\nremarkable increase.",
            "Before we give an update on the\r\n                                                        million customer accounts — refunding more\r\nstructural shifts taking place, it would be good to\r\n                                                        than $250 million for nearly 2 million consumer\r\naddress the question: How did banks perform\r\n                                                        deposit and lending accounts and offering\r\nduring the recent COVID-19 crisis?",
            "Although I begin this annual letter to shareholders in a challenging landscape,\r\nI remain proud of what our company and our hundreds of thousands of employ-\r\nees around the world have achieved, collectively and individually.\n\nAs you know,\r\nwe have long championed the essential role of banking in a community — its\r\npotential for bringing people together, for enabling companies and individuals to\r\nreach for their dreams, and for being a source of strength in difficult times.\n\nThroughout these past two challenging years, we never stopped doing all the\r\nthings we should be doing to serve our clients and our communities.\n\nLooking back on the last year and the past two decades — starting from my time\r\nas CEO of Bank One in 2000 — it is clear that our financial discipline, constant\r\ninvestment in innovation and ongoing development of our people are what\r\nenabled us to persevere in our steadfast dedication to help clients, communities\r\nand countries throughout the world."
         ],
         "publicUrl":"https://www.jpmorganchase.com/ir/annual-report"
      }
   ]
}