Semantic Search

Search library files for topics and keywords

Search RAG library files for a given query and return snippets that seem to be about the topic specified. This endpoint does not try to answer the question, only to find relevant snippets. If you want a question answering endpoint, use Contextual Answers [RAG Engine].

If you specify different filter parameters such as path, labels, or fileIds, only files that match ALL of the filters will be searched. So specifying path=pets/ and labels=feeding will only search files with both of those attributes. Specifying multiple labels or file IDs will search files with any matching values.

Examples

from ai21 import AI21Client

# Depend on API key being set in os.environment["AI21_API_KEY"]
client = AI21Client()
def search_library_file():
    response = client.library.search.create(
        query="How many days can I work from home?",
        labels=["hr"]
    )
    print(response.to_json())
import requests
ROOT_URL = "https://api.ai21.com/studio/v1/"

def search_library_files():
  url = ROOT_URL + "library/search"
  labels=["hr"]
  data = {
      "query": "How many days can I work from home?",
      "labels": labels
  }
  response = requests.post(
    url=url,
    headers={"Authorization": f"Bearer {AI21_API_KEY}"},
    json=data
  )
{
  "id":"8c085d66-b851-e6b0-6b57-bcb831c657bf",
  "results":[
    {
      "text":"US employees can work from home up to two days per week.",
      "fileId":"5143f17d-626d-4a1f-8e66-a4e7e129d238",
      "fileName":"hr/US/employee_handbook_us.txt",
      "score":0.862693787,
      "order":0,
      "publicUrl":null,
      "labels":[
        "hr",
        "policy"
      ]
    },
    {
      "text":"UK employees can work from home up to two days per week.",
      "fileId":"d195f8a9-a8fe-4f4e-96fb-342246d55f53",
      "fileName":"/hr/UK/employee_handbook_uk.txt",
      "score":0.858384848,
      "order":0,
      "publicUrl":null,
      "labels":[
        "hr"
      ]
    }
  ]
}
Language
Credentials
Header