> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inworld.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# LLM Node Demo

This demo showcases how to use the `LLMNode`.

## Run the Template

1. Go to `Assets/InworldRuntime/Scenes/Nodes` and play the `LLMNode` scene.
   <img src="https://mintcdn.com/inworldai/pDD5vvrZThONehMe/img/unity/framework/LLMNode00.png?fit=max&auto=format&n=pDD5vvrZThONehMe&q=85&s=8f6b9265198145f552ce748b51830476" alt="LLMNode00" width="1061" height="672" data-path="img/unity/framework/LLMNode00.png" />
2. Enter your request. The AI agent will respond.

<img src="https://mintcdn.com/inworldai/pDD5vvrZThONehMe/img/unity/framework/LLMNode.gif?s=cb2c0aed8b21bb4138d7f27bc5ccafb8" alt="LLMNode01" width="1920" height="1080" data-path="img/unity/framework/LLMNode.gif" />

## Understanding the Graph

You can find the graph on the `InworldGraphExecutor` of `LLMNodeCanvas`.

<img src="https://mintcdn.com/inworldai/pDD5vvrZThONehMe/img/unity/framework/LLMNode02.png?fit=max&auto=format&n=pDD5vvrZThONehMe&q=85&s=02a175b7080b84e10ee8265442505703" alt="LLMNode02" width="1365" height="708" data-path="img/unity/framework/LLMNode02.png" />

The graph is very simple. It contains a single node, `LLMNode`, with no edges.

`LLMNode` is both the `StartNode` and the `EndNode`.

<img src="https://mintcdn.com/inworldai/pDD5vvrZThONehMe/img/unity/framework/LLMNode03.png?fit=max&auto=format&n=pDD5vvrZThONehMe&q=85&s=05ea3c1a383faf66b1eb23f3ed76a88c" alt="LLMNode03" width="1014" height="804" data-path="img/unity/framework/LLMNode03.png" />

### InworldController

The `InworldController` is also simple; it contains only one primitive module: `LLM`.

<img src="https://mintcdn.com/inworldai/pDD5vvrZThONehMe/img/unity/framework/LLM05.png?fit=max&auto=format&n=pDD5vvrZThONehMe&q=85&s=911a11e32174478b4c9207eb184d6569" alt="LLMNode04" width="1362" height="651" data-path="img/unity/framework/LLM05.png" />

<Tip>
  For details about the primitive module, see the [LLM Primitive Demo](../primitives/llm).
</Tip>

## Workflow

1. When the game starts, `InworldController` initializes its only module, `LLMModule`, which creates the `LLMInterface`.
2. Next, `InworldGraphExecutor` initializes its graph asset by calling each component’s `CreateRuntime()`. In this case, only `LLMNode.CreateRuntime()` is called, using the created `LLMInterface` as input.
3. After initialization, the graph calls `Compile()` and returns the executor handle.
4. After compilation, the `OnGraphCompiled` event is invoked. In this demo, `LLMNodeTemplate` subscribes to it and enables the UI components. Users can then interact with the graph system.

```c# LLMNodeTemplate.cs theme={"system"}
protected override void OnGraphCompiled(InworldGraphAsset obj)
{
    foreach (InworldUIElement element in m_UIElements)
        element.Interactable = true;

}
```

5. When the user sends text, this demo wraps the message into an `LLMChatRequest` and sends it to the LLM as raw input.

```c# LLMNodeTemplate.cs theme={"system"}
public async void SendText()
{
    if (!InworldController.LLM)
    {
        Debug.LogError("Cannot find LLM Module!");
        return;
    }
    // Compose the input into `LLMChatRequest`
    InworldMessage message = PlayerSpeaks(m_InputField.text);
    m_InputField.text = "";
    LLMChatRequest llmChatRequest = new LLMChatRequest(m_Messages);
    await m_InworldGraphExecutor.ExecuteGraphAsync("LLM", llmChatRequest);
}
```

6. Calling `ExecuteGraphAsync()` eventually produces a result and invokes `OnGraphResult()`, which `LLMCanvas` subscribes to in order to receive the data.
