Skip to main content
The Knowledge template shows how to use the Knowledge primitive.

Run the Template

  1. Go to Assets/InworldRuntime/Scenes/Primitives and play the KnowledgeTemplate scene. Knowledge00
  2. After pressing Play, click Connect.
All currently available knowledge records will be displayed on the screen.
  1. Click Add Knowledge to add more records.
Enter the content you want to add and click Add. In this demo, all added records go to the knowledge/test collection.
  1. Click Query Knowledge to search the knowledge records.
Enter your query and click Query. Matching records will be displayed on the screen.
  1. Clicking the trash icon next to a knowledge record deletes all records in that knowledge collection for this game session.
Click the GIF to expand.
Knowledge

Understanding the Template

Structure

  • This demo has two prefabs under InworldController: TextEmbedder (contains TextEmbedderModule) and Knowledge (contains KnowledgeModule).
  • These modules create TextEmbedderFactory and KnowledgeFactory, and each factory creates a TextEmbedderInterface or KnowledgeInterface based on the current TextEmbedder/KnowledgeConfig.
Knowledge01

KnowledgeData

KnowledgeData is the asset this demo interacts with. After the text embedder initializes, whenever you submit a query, the AI module batches this KnowledgeData and tries to retrieve the most relevant pieces of knowledge. By default, it’s stored under Assets/InworldRuntime/Data/General. Knowledge01 Knowledge is stored in groups. Each group can contain multiple pieces of information. Each group ID should always start with knowledge/. See the example data in our Unity project. Knowledge04

Workflow

  • Initialization is sequenced because Knowledge depends on InworldController.
  • When InworldController initializes, it calls InitializeAsync() on TextEmbedderModule.
When that finishes, InworldController invokes OnProcessInitialized, which triggers KnowledgeModule initialization (see Primitives Overview).
  • Once KnowledgePanel is initialized, InworldController invokes OnFrameworkInitialized.
In this demo, the KnowledgeConfigPanel on KnowledgeCanvas > ConfigPanel is registered to this event.
  • Then KnowledgeConfigPanel compiles knowledge using the existing TextEmbedder module.
KnowledgeConfigPanel.cs
void OnFrameworkInitialized()
{
    if (m_ConnectButton)
        m_ConnectButton.Switch(true);
    foreach (InworldUIElement element in m_UIElements)
        element.Interactable = true;
    if (!InworldController.Knowledge || !m_KnowledgeData || !InworldController.TextEmbedder || !m_KnowledgeData) 
        return;
    m_KnowledgeData.CompileKnowledges();
}

Add Knowledge

  • When you press Add Knowledge and submit the input text, the data is added to the current KnowledgeData.
  • In this demo, this invokes KnowledgeInteractionPanel._AddKnowledge().
  • It tries to add a new knowledge group knowledge/test, then adds your entry to it.
  • It then automatically calls CompileKnowledges() to activate the new piece of knowledge.
KnowledgeInteractionPanel.cs
void _AddKnowledge()
{
    if (m_KnowledgeData)
        m_KnowledgeData.AddKnowledge("test", m_InputContent.text);
}
KnowledgeData.cs
public List<string> AddKnowledge(string knowledgeID, string content)
{
    if (string.IsNullOrEmpty(knowledgeID))
        knowledgeID = "knowledge/new";
    else if (!knowledgeID.StartsWith("knowledge/"))
        knowledgeID = $"knowledge/{knowledgeID}";
    Knowledges knowledge = knowledges.FirstOrDefault(k => k.title == knowledgeID);
    if (knowledge == null)
    {
        Knowledges newKnowledge = new Knowledges
        {
            title = knowledgeID,
            content = new List<string>{content}
        };
        knowledges.Add(newKnowledge);
    }
    else
        knowledge.content.Add(content);
    return CompileKnowledges();
}

Query Knowledge

  • As long as InworldController is connected and each module is initialized, you can click Query Knowledge.
  • Enter any question; if it matches the knowledge database above, related entries are listed.
  • More relevant entries appear higher in the list.
  • When you press Enter or click Query, it first triggers KnowledgeInteractionPanel._QueryKnowledge().
  • This calls InworldController.Knowledge.GetKnowledges(), which in turn calls KnowledgeInterface.GetKnowledge().
void _QueryKnowledge()
{
    InworldEvent inworldEvent = new InworldEvent();
    InworldAgentSpeech speech = inworldEvent.Speech;
    speech.AgentName = InworldFrameworkUtil.PlayerName;
    speech.Utterance = m_InputContent.text;
    if (InworldController.Knowledge)
        InworldController.Knowledge.GetKnowledges(m_KnowledgeData.IDs, new List<InworldEvent>{inworldEvent});
}
InworldController.cs
public List<string> GetKnowledges(List<string> knowledgeIDs, List<InworldEvent> eventHistory = null)
{
    if (!Initialized || !(m_Interface is KnowledgeInterface knowledgeInterface))
        return null;
    InworldVector<string> inputKnowledges = new InworldVector<string>();
    inputKnowledges.AddRange(knowledgeIDs);
    InworldVector<InworldEvent> inputEvents = new InworldVector<InworldEvent>();
    if (eventHistory != null && eventHistory.Count != 0)
        inputEvents.AddRange(eventHistory);
    InworldVector<string> output = knowledgeInterface.GetKnowledge(inputKnowledges, inputEvents);
    int nSize = output?.Size ?? -1;
    List<string> result = new List<string>();
    for (int i = 0; i < nSize; i++)
        result.Add(output?[i]);
    OnKnowledgeRespond?.Invoke(result);
    return result;
}