Skip to content

Sophia NLU Engine - Integration

Integrate Sophia into your applications with minimal effort.

Overview

Sophia Premium comes with a binary application that runs in the background, always available via CLI and localhost RPC requests.

Integrate with any programming language using the SDK available in many languages:

Map Word Clusters to Functions

Use the multi-hierarchial categorization system to easily map clusters of verbs to functions, as shown below.

Below shows an example of mapping the category verbs/action/travel/depart to a function. This category contains many words including "take off", so when the phrase is parsed it triggers the vacation() function, passing it the noun Thailand.

Python Example

from cicero_sdk import Sophia, Router
# Initialize Sophia and Router
nlu = Sophia()
router = Router()

# Define a callback function for vacation-related verbs
def vacation(verb_position: int, phrase_num: int, phrase: dict, output: dict) -> None:
    for noun in phrase["nouns"]:
        token = output["mwe"][noun["head"]]
        print(f"Looks like we're vacationing in {token['word']}")

# Route verb category to the vacation function
router.add("verbs/action/travel/depart", vacation)

# Trigger the function
nlu.route(router, "I want to take off to Thailand and have some fun")
JavaScript Example

import { Sophia, Router } from 'cicero-sdk';

async function main() {
  // Initialize Sophia and Router
  const nlu = new Sophia();
  const router = new Router();

  // Define a callback function for vacation-related verbs
  async function vacation(verbPosition, phraseNum, phrase, output) {
    for (const noun of phrase.nouns) {
      const token = output.mwe[noun.head];
      console.log(`Looks like we're vacationing in ${token.word}`);
    }
  }

  // Route verb category to the vacation function
  router.add('verbs/action/travel/depart', vacation);

  // Trigger the function
  await nlu.route(router, 'I want to take off to Thailand and have some fun');
PHP Example

<?php

use Aquila\SDK\Sophia\{Sophia, Router};

$nlu = new Sophia();
$router = new Router();

// Route verb category to the vacation() function
$router->add('verbs/action/travel/depart', "\\vacation");

// Trigger that function
$nlu->route($router, "I want to take off to Thailand and have some fun");

function vacation(int $verb_position, int $phrase_num, array $phrase, array $output) { 
    foreach ($phrase['nouns'] as $noun) {
        $token = $output['mwe'][$noun['head']];
            echo "Looks like we're vacationing in " . $token['word'] . "\n";
    }
}

Selector Phrase Matching

Sophia Premium comes with 64 pre-packaged selectors, and it's easy to define your own. For example, a selector may be named "request_demo" and contain the following potential answers: later, maybe_later, need_more_info, never, never. Within each potential answer will be a list of sample phrases with that meaning.

When running user input against a selector, it will try to match the correct answer with high confidence. If unable to, a LLM fallback mechanism is available that will ping Ollama or an API to request the answer, then cache the necessary details to self improve.

You can easily manually define your own selectors, or use the built-in option allowing the LLM to generate them for you. Below shows an example of running a selector against some user input.

Python Example

from cicero_sdk import Sophia

# Initialize Sophia
nlu = Sophia()

# Run selector
user_input = "Not now, but maybe in a little bit, ask me later"
res = nlu.run_selector("marketing/request_demo", user_input)

# Display response
print(f"Answer: {res['answer']}")   # maybe_later
print(f"Confidence: {res['confidence']}")
print(f"Intent: {res['intent']}")
print(f"Intent Confidence: {res['intent_score']}")
JavaScript Example

import { Sophia, Router } from 'cicero-sdk';

async function main() {
  // Initialize Sophia
  const nlu = new Sophia();

  // Run selector
  const user_input = "Not now, but maybe in a little bit, ask me later"
  const res = nlu.run_selector("marketing/request_demo", user_input);

  // Display res
  console.log(`Answer: ${res.answer}`);    // maybe_later
  console.log(`Confidence: ${res.confidence}`);
  console.log(`Intent: ${res.intent}`);
  console.log(`Intent Confidence: ${res.intent_score}`);
PHP Example

<?php

use Aquila\SDK\Sophia\Sophia;

$nlu = new Sophia();

// Run selector
$user_input = "Not now, but maybe in a little bit, ask me later"
$res = $nlu->runSelector('marketing/request_demo', $user_input);

// Display response
echo "Answer: $res[answer]\n";    // maybe_later
echo "Confidence: $res[confidence]\n";
echo "Intent: $res[intent]\n";
echo "Intent Confidence: $res[intent_score]\n";

RPC Commands

The following RPC commands are available via both CLI and RPC:

  • tokenize(input) - Tokenize the input, returning both individual tokens and MWEs (multi-word entities)
  • interpret(input) - Tokenize and interpret the input, returning the tokens and input broken into phrases and usable verb/noun phrases for actionable use within software
  • get_word(word) - Retrieve individual token of a given word
  • get_token(id) - Retrieve the token for the specific token ID number
  • get_category(category_path) - Retrieve category information including all words associated with the category
  • run_selector(selector, input) - Only available in Professional and higher plans, executes a selector against the input and determines the user's answer to the prompted question/selector

View the full schema of the objects returned by the SDK and RPC server by visiting href="/sophia/json_objects">JSON Objects page.

Along with the above commands, various CLI commands are available to easily import and manage custom entities, import and create selectors, read user submitted feedback, and view full usage statistics.

Get Setup Today!

Elevate your AI ecosystem with Sophia's powerful NLU engine - deploy in minutes, see results immediately.