Create, update, and delete KS via API
The examples below use curl and shell scripts. You can change it. Use StackSpot AI to adapt it to the language you want.
Requirements
-
Due to authentication restrictions, uploading directly via API is only available for Enterprise accounts.
-
Authenticate to get to use the script.
Follow the steps to authenticate on the Services Credential page refer to the StackSpot EDP documentation.
Activate the following permissions:
- ai_dev and ai_admin.
Contact your account administrator if you cannot create a Service Credential.
- Copy the 'client id', 'client key', and 'realm' fields to use as environment variables (the first two should be secrets) or the curl example at the bottom of the page in the 'how to use it' section. You won't be able to see them again.
If you lose them, revoke the credential and create a new one.
- The following examples consider the access token to be set as the
JWT
environment variable. You can extract it from the authenticationcurl
with thejq
command to get the value for theaccess_token
key from the returned JSON, then save it as an environment variable using theexport
command, such as:
export JWT=$(curl -s "https://idm.stackspot.com/$REALM/oidc/oauth/token" -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=client_credentials' -d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_KEY" | jq -r '.access_token')
Create Knowledge Source via API
Follow the steps below:
Step 1. Run to create the KS:
curl -s -X POST "https://genai-code-buddy-api.stackspot.com/v1/knowledge-sources" -H "Authorization: Bearer $JWT" -H 'Content-Type: application/json' -d '{"slug": "<KS identifier in API>", "name": "<display name>", "description": "<more details about KS>", "type": "<one of API, SNIPPET or CUSTOM>"}'
Step 2. Go to the StackSpot AI Portal.
- Access the ‘Knowledge Sources’ section on the left-side menu and search for the one you just created.
Upload files to an existing Knowledge Source
When adding new files to a Knowledge Source, the limit is 10 MiB.
You need to have already created a Knowledge Source here. If you still need to make it, please create it now.
You can upload one or more files to an existing Knowledge Source. You can also choose to upload a zip file. Follow the steps below to do it.
Step 1. Run the following script to start the uploading process:
upload_data=$(curl -X POST 'https://genai-code-buddy-api.stackspot.com/v1/file-upload/form' \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $JWT" \
-d '{"file_name": "<desired file's name>", "target_id": "<KS slug>", "target_type": "KNOWLEDGE_SOURCE", "expiration": 600 }')
echo -n 'file upload id = '
echo "$upload_data" | jq -r '.id'
curl -X POST "$(echo "$upload_data" | jq -r '.url')" \
-F "key=$(echo "$upload_data" | jq -r '.form.key')" \
-F "x-amz-algorithm=$(echo "$upload_data" | jq -r '.form["x-amz-algorithm"]')" \
-F "x-amz-credential=$(echo "$upload_data" | jq -r '.form["x-amz-credential"]')" \
-F "x-amz-date=$(echo "$upload_data" | jq -r '.form["x-amz-date"]')" \
-F "x-amz-security-token=$(echo "$upload_data" | jq -r '.form["x-amz-security-token"]')" \
-F "policy=$(echo "$upload_data" | jq -r '.form.policy')" \
-F "x-amz-signature=$(echo "$upload_data" | jq -r '.form["x-amz-signature"]')" \
-F "file=@'<absolute path to desired file>'"
Step 2. To check the upload status, run the command below:
curl --location 'https://genai-code-buddy-api.stackspot.com/v1/file-upload/@<file upload id>' \
--header 'Authorization: Bearer $JWT'
Step 3. After the upload is done, access the StackSpot AI Portal;
Step 4. On the left side menu, click ‘Knowledge Sources’ and then check if your Knowledge Source has new objects.
Upload standalone content to an existing Knowledge Source
You must have already created a Knowledge Source here. If you still need to create it, please do so now.
Only the KS types mentioned support this.
Snippet
curl -L 'https://genai-code-buddy-api.stackspot.com/v1/knowledge-sources/<KS slug>/snippets' \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $JWT" \
-d '{
"use_case": "<description>",
"code": "<content>",
"language": "<one of sql, csp, freemarker2, systemverilog, sb, scheme, groovy, postiats, sol, mips, twig, php, aes, coffeescript, json, ocaml, sparql, xml, powershell, haskell, rust, scala, plaintext, octave, abap, lua, shell, less, r, yaml, fortran, bat, cobol, hcl, msdax, graphql, qsharp, objective-c, bicep, st, cpp, css, redis, javascript, julia, apex, dart, pug, markdown, flow9, sbcl, pascal, kotlin, c, csharp, pascaligo, scss, go, swift, proto, typescript, liquid, m3, razor, html, lexon, java, azcli, tcl, fsharp, ruby, vb, python, cameligo, ini, clojure, ada, elixir, restructuredtext, dockerfile, handlebars, racket, ecl, pla, perl, erlang, powerquery, verilog, d>"
}'
Custom
curl -L 'https://genai-code-buddy-api.stackspot.com/v1/knowledge-sources/<KS slug>/custom' \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $JWT" \
-d '{"content": "<content>"}'
Delete Knowledge Sources Objects
All files
If you want to delete all the Knowledge Source Objects, run on your terminal:
curl -s -X DELETE "https://genai-code-buddy-api.stackspot.com/v1/knowledge-sources/<KS slug>/objects" -H "Authorization: Bearer $JWT"
Standalone
If you want to delete only the standalone, you’ve manually added:
curl -s -X DELETE "https://genai-code-buddy-api.stackspot.com/v1/knowledge-sources/<KS slug>/objects?standalone=true" -H "Authorization: Bearer $JWT"
Only uploaded
To delete the zip files, run the following:
curl -s -X DELETE "https://genai-code-buddy-api.stackspot.com/v1/knowledge-sources/<KS slug>/objects?standalone=false" -H "Authorization: Bearer $JWT"