Autocomplete
Sapling's autocomplete provides predictions of the next few characters or words given the current context in a particular editable. The predictions are based on the previous text.
Example: a possible completion for the context, Let us know if
might be you need anything else
.
Request Completion POST
Sample Code
- cURL
- JavaScript
- Python
curl -X POST https://api.sapling.ai/api/v1/complete \
-H "Content-Type: application/json" \
-d '{
"key":"<api-key>",
"query":"Hi how are",
"session_id": "test session",
}'
import axios from 'axios';
async function run(query) {
try {
const response = await axios.post(
'https://api.sapling.ai/api/v1/complete',
{
key: '<api-key>',
session_id: 'test session',
query,
},
);
const {status, data} = response;
console.log({status});
console.log(JSON.stringify(data, null, 4));
} catch (err) {
const { msg } = err.response.data;
console.log({err: msg});
}
}
run('Hi how are');
import requests
from pprint import pprint
key = '<api-key>'
url = 'https://api.sapling.ai/api/v1/complete'
data = {
'key': key,
'query': 'Hi how are',
'session_id': 'test session',
}
try:
response = requests.post(url, json=data)
resp_json = response.json()
if 200 <= response.status_code < 300:
edits = resp_json['edits']
pprint(edits)
else:
print('Error: ', resp_json)
except Exception as e:
print('Error: ', e)
Sample Response
{
"hash":"e3fffa5f-1f11-531e-8a8e-d9e758677e0c",
"output":" you",
"query":"Hi how are"
}
Request Parameters
https://api.sapling.ai/api/v1/complete
HTTP method: POST
JSON parameters:
key: String
32-character API key
session_id: String
Unique name or UUID of document or portion of text that is being checked.
query: String
Text that should be autocompleted.
Response Parameters
The Autocomplete endpoint returns JSON with parameters documented below:
output: String
Predicted completion. For example, for the query Hi, how are
, the predicted completion should be you
(with a leading space).
query: String
Actual query used to generate completion (may be a truncated version of request query).
hash: String (UUID)
Opaque ID of completion for use with accept event.
Accept Completion POST
Use this API endpoint to have Sapling adapt its system over time.
Each suggested completion has an completion UUID. You can pass this information back to Sapling to indicate the completion was used. For each unique completion UUID, use the accept endpoint up to once.
Request Parameters
https://api.sapling.ai/api/v1/complete/<completion_id>/accept
HTTP method: POST
<completion_id>: String (UUID)
ID of completion returned from a Completion POST.
Here's a sample request body:
{
"key": "<api-key>",
"context": {
"query": "Hi how are",
"completion": " you"
},
"session_id": "Test Document UUID"
}
Note that the query
and completion
arguments are required.
Response Parameters
Will return error or {"result": "success"}
.