Intelligence Service
Intelligence Service is responsible for providing DataSapien's patented on-edge intelligence approach. Intelligence Service allows access to:
- Rules - these are the first level of intelligence and static in nature
- AI Models - Generative AI Models designed to work in a wide range of fields
All intelligence runs on the device: model files are downloaded once and inference happens locally, so user data never has to leave the device to be used with a model.
Use Cases
Running an AI Model On-Device
AI models are provisioned as Managed AI Models on the Orchestrator. The typical lifecycle in your host app is:
- Discover available models with
getManagedAIModels(orgetManagedAIModelfor a single one). These definitions are synchronized from the Orchestrator. Thenamereturned here is the model's programmatic name — you pass it to every model-file related call. - Check the model files with
isModelFilesDownloaded. Model definitions and model files are separate: the definition arrives with sync, the files are downloaded on demand. - Download if needed with
downloadModelFiles. It reports progress via a callback and the download keeps running while your app is in the background. - Load the model with
loadModel(name, key, modelParams). Thekeyis an alias you choose for this loaded instance — every later call (invokeModel,isModelLoaded,stopModelInference,unloadModel) refers to the model by this key.ModelParamstunes how the model is loaded:nCtx(context window, default 512),nBatchSize(default 64),nThreads(defaults to available CPU cores, minimum 2) andnGpuLayers(default 20). - Verify with
isModelLoaded(key)if needed. - Invoke with
invokeModel(key, prompts, inferenceParams). Prompts are an array ofrole/contentmessages, andInferenceParamscontrols generation:temperature(default 0.8),maxTokens(default 10.000),topP(default 0.95) and optionaltopK. Streaming output is delivered chunk by chunk through theonStreamcallback and the full answer arrives inonSuccess. - Clean up with
unloadModel(key)when you are done to release memory. To cancel an ongoing generation, callstopModelInference(key).
let intelligence = DataSapien.getIntelligenceService()
if !intelligence.isModelFilesDownloaded(name: "gemma-3n") {
intelligence.downloadModelFiles(
name: "gemma-3n",
onProgress: { progress in },
onSuccess: { }
)
}
intelligence.loadModel(name: "gemma-3n", key: "assistant", onSuccess: {
intelligence.invokeModel(
key: "assistant",
prompts: [Prompt(role: .user, content: "Suggest a savings plan")],
onStream: { chunk in },
onSuccess: { answer in
intelligence.unloadModel(key: "assistant")
}
)
})
To manage local storage: getDownloadedModelsList returns the names of all models whose files are on the device, and deleteModelFiles(name) removes a model's files.
Using Intelligence in Journeys
The same functions are available to Journey Script steps through the global IntelligenceService object, so a Journey can run a one-shot inference as part of its flow — for example generating a personalized suggestion between two screens:
await IntelligenceService.loadModel("gemma-3n", "journey-model");
const answer = await IntelligenceService.invokeModel("journey-model", [
{ role: "user", content: "Summarize the user's answers: " + context.answers }
]);
await IntelligenceService.unloadModel("journey-model");
The result can be written into the Journey Context and displayed on a following Screen step.
Rules & Triggers
Rules are designed on the Orchestrator and deployed to Mobile SDK instances. Their purpose is to do work that requires no UI, outside of Journeys — for example reading existing data to derive a new value, or changing the user's state.
- Rule definitions are synchronized during
DataSapien.setup; callsyncRulesto synchronize explicitly at any other time. - A rule can be flagged to run on SDK initialization; such rules are executed automatically during setup.
- To run rules yourself:
runRulesevaluates all of them, andrunRule(name)evaluates a specific rule.
Note that rule evaluation may be limited or completely impossible when your host app is in the background because of the limitations imposed by mobile operating systems.
Hallucinations & Fact Checking
AI models, because of their generative nature, are prone to hallucinations. DataSapien architecture allows you to fact check AI output by:
- Writing scripts to implement simple accept / reject algorithms
- Using an additional AI Model: feeding first AI output to an additional AI model
Intelligence Service Functions
To access IntelligenceService functions, get its instance from the DataSapien object: DataSapien.getIntelligenceService().
See the full function list per platform in the Intelligence Service API Reference.