POST
https\:
/
/
api.ai21.com
/
studio
/
v1
/
library
/
answer
client = AI21Client(api_key=<<AI21_API_KEY>>)

# For this example, limit search to files with label "hr"
def query_library(query, labels=None, path=None):
    response = client.library.answer.create(
        question=query,
        path=path,
        labels=labels
    )
    if response.answer_in_context:
      print(response.answer)
      print(response.sources)
    else:
      print("No answer found")

query_library("How many days can I work from home in the US?",labels=["hr"])
{
   "question": "During the past decade, what amount did the bank remunerate the Federal Deposit Insurance Corporation?",
   "labels": ["financial_reports", "banking"]
}

Overview

The RAG contextual answer model answers questions based on specified files in your RAG Engine document library. If the answer is not within the library, the model will say so. Optionally filter results to documents with matching labels, paths, or IDs. Note that if you specify multiple filtering fields (both labels and paths), only files matching both a specified label and path will be queried. Specifying multiple labels, though, matches when any one of the specified labels matches a given document.

You can manage your library either through the library management endpoints or through the playground in the browser .

Read more about the RAG Engine.

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

# For this example, limit search to files with label "hr"
def query_library(query, labels=None, path=None):
    response = client.library.answer.create(
        question=query,
        path=path,
        labels=labels
    )
    if response.answer_in_context:
      print(response.answer)
      print(response.sources)
    else:
      print("No answer found")

query_library("How many days can I work from home in the US?",labels=["hr"])
{
   "question": "During the past decade, what amount did the bank remunerate the Federal Deposit Insurance Corporation?",
   "labels": ["financial_reports", "banking"]
}

Body Params

question
string
required

The question to be answered based on the content in your document library.
Limit: 150 characters

path
string

Specify a path to restrict answers to documents that match these path, either as a complete or prefix match.
That is, specifying /pets/ would match documents labeled /pets/ or /pets/dogs/.
Use to focus the question on specific path in your library.

labels
array of strings

Specify labels to restrict answers to documents with any of these labels.
Labels are exact, case-sensitive matches, no substring matches.
If you specify both a path and labels, then only files with both a path and a label match will be included.

fileIds
array of strings

Specify which files should be included in the results.

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.

id
string
required

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
boolean
required

True if an answer could be found, False if an answer could not be found.

answer
string or null
required

The answer to the question based on the provided context, or null/None if the answer cannot be found.

sources
array of objects
required

An array of objects, where each object represents a source document in which the answer was found.
Each source object contains the following:

  • 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).
POST
https\:
/
/
api.ai21.com
/
studio
/
v1
/
library
/
answer
client = AI21Client(api_key=<<AI21_API_KEY>>)

# For this example, limit search to files with label "hr"
def query_library(query, labels=None, path=None):
    response = client.library.answer.create(
        question=query,
        path=path,
        labels=labels
    )
    if response.answer_in_context:
      print(response.answer)
      print(response.sources)
    else:
      print("No answer found")

query_library("How many days can I work from home in the US?",labels=["hr"])
{
   "question": "During the past decade, what amount did the bank remunerate the Federal Deposit Insurance Corporation?",
   "labels": ["financial_reports", "banking"]
}

Overview

The RAG contextual answer model answers questions based on specified files in your RAG Engine document library. If the answer is not within the library, the model will say so. Optionally filter results to documents with matching labels, paths, or IDs. Note that if you specify multiple filtering fields (both labels and paths), only files matching both a specified label and path will be queried. Specifying multiple labels, though, matches when any one of the specified labels matches a given document.

You can manage your library either through the library management endpoints or through the playground in the browser .

Read more about the RAG Engine.

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

# For this example, limit search to files with label "hr"
def query_library(query, labels=None, path=None):
    response = client.library.answer.create(
        question=query,
        path=path,
        labels=labels
    )
    if response.answer_in_context:
      print(response.answer)
      print(response.sources)
    else:
      print("No answer found")

query_library("How many days can I work from home in the US?",labels=["hr"])
{
   "question": "During the past decade, what amount did the bank remunerate the Federal Deposit Insurance Corporation?",
   "labels": ["financial_reports", "banking"]
}

Body Params

question
string
required

The question to be answered based on the content in your document library.
Limit: 150 characters

path
string

Specify a path to restrict answers to documents that match these path, either as a complete or prefix match.
That is, specifying /pets/ would match documents labeled /pets/ or /pets/dogs/.
Use to focus the question on specific path in your library.

labels
array of strings

Specify labels to restrict answers to documents with any of these labels.
Labels are exact, case-sensitive matches, no substring matches.
If you specify both a path and labels, then only files with both a path and a label match will be included.

fileIds
array of strings

Specify which files should be included in the results.

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.

id
string
required

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
boolean
required

True if an answer could be found, False if an answer could not be found.

answer
string or null
required

The answer to the question based on the provided context, or null/None if the answer cannot be found.

sources
array of objects
required

An array of objects, where each object represents a source document in which the answer was found.
Each source object contains the following:

  • 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).