Query a knowledge base
Query a Knowledge Base for matching results.
Use the retrieve API to query a Knowledge Base for information matching keywords or phrases.
When you query a knowledge base, you must submit a POST request:
Identify the Knowledge Base to query using its code in the URL of the request.
Specify the text to search the Knowledge Base for using the
textparameter.Specify filters to restrict the documents that are searched.
The API will return matching items (portions of legislation, pages of judgment text) that match your query, including a score, the text of the portion, and metadata.
Filters
It is useful to apply filters to limit results, particularly when doing legislation searches. It is recommended you apply these filters for legislation:
Principal works only (ie. exclude amending and commencement notices):
principal: trueExclude repealed works:
repealed: false
In JSON format, use:
"filters": {"principal": true, "repealed": false}}Example
This searches the legislation-za knowledge base for the text "national anthem". The filters are recommended and ensure that only un-repealed, principal Acts are returned.
POST https://api.laws.africa/ai/v1/knowledge-bases/legislation-za/retrieve
Authorization: Token abc-123
Content-Type: application/json
{
"text": "national anthem",
"top_k": 5,
"filters": {"principal": true, "repealed": "false"}
}The results look like this:
{
"results": [
{
"content": {
"text": "\n\nThe national flag of the Republic is black, gold, green, white, red and blue, as described and sketched in Schedule 1 ."
},
"metadata": {
"work_frbr_uri": "/akn/za/act/1996/constitution",
"frbr_place": "za",
"frbr_country": "za",
"frbr_doctype": "act",
"frbr_subtype": null,
"title": "Constitution of the Republic of South Africa, 1996",
"expression_date": "2013-08-23",
"expression_frbr_uri": "/akn/za/act/1996/constitution/eng@2013-08-23",
"portion_type": "provision",
"portion_id": "chp_1__sec_5",
"repealed": false,
"commenced": true,
"principal": true
},
"score": 0.17306449999999995
}
]
}Result formats
Legislation results
The Retrieve API for legislation Knowledge Bases returns the text of the matched provision, the score of the match (a lower score is better), and metadata for the portion. This provides very precise details on the portion of legislation that matched the query, allowing you to provide concrete citation information to your user.
The metadata includes a combination of:
Details of the legislative work, including FRBR URI information.
Table of Contents information for the portion of that work.
For example:
{
"metadata": {
"work_frbr_uri": "/akn/za/act/1996/constitution",
"frbr_place": "za",
"frbr_country": "za",
"frbr_doctype": "act",
"frbr_subtype": null,
"title": "Constitution of the Republic of South Africa, 1996",
"expression_date": "2013-08-23",
"expression_frbr_uri": "/akn/za/act/1996/constitution/eng@2013-08-23",
"portion_type": "provision",
"portion_id": "chp_1__sec_5",
"repealed": false,
"commenced": true,
"principal": true
}
}Judgment results
The Retrieve API for judgment Knowledge Bases returns the text of the matched chunk, the score of the match (a lower score is better), and metadata for the chunk.
Up to three chunks will be returned for the same judgment.
The metadata for each chunk includes a combination of:
Details of the legislative work, including FRBR URI information.
The 1-based page number, where available.
For example:
{
"metadata": {
"work_frbr_uri": "/akn/za/judgment/zalcjhb/2014/139",
"frbr_place": "za",
"frbr_country": "za",
"title": "NEHAWU obo Manyana and Another v Masege N.O. and Others (JR 363/2012) [2014] ZALCJHB 139 (8 April 2014)",
"expression_date": "2014-04-08",
"expression_frbr_uri": "/akn/za/judgment/zalcjhb/2014/139/eng@2014-04-08",
"portion_type": "page",
"portion_id": 30
}
}Retrieve items from the knowledge base that match your query.
Unique code identifying this knowledge base
Details to retrieve items from a knowledge base.
The text to find matching items for
Number of results to return
10POST /ai/v1/knowledge-bases/{code}/retrieve HTTP/1.1
Host:
Content-Type: application/json
Accept: */*
Content-Length: 350
{
"text": "text",
"top_k": 10,
"filters": {
"work_frbr_uri": "text",
"work_frbr_uri__in": [
"text"
],
"expression_frbr_uri": "text",
"expression_frbr_uri__in": [
"text"
],
"frbr_place": "text",
"frbr_place__in": [
"text"
],
"frbr_doctype": "text",
"frbr_doctype__in": [
"text"
],
"frbr_subtype": "text",
"frbr_subtype__in": [
"text"
],
"repealed": true,
"commenced": true,
"principal": true
}
}{
"results": [
{
"content": {
"text": "text"
},
"metadata": {
"ANY_ADDITIONAL_PROPERTY": "anything"
},
"score": 1
}
]
}Last updated