workgraph-llm

    Runtime-independent typed LLM nodes for workgraph

    agent
    graph
    llm
    moonbit
    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    3 hours ago
    Downloads
    51

    #moonbit: import: - path: totto2727/llm@0.3.3 alias: llm - path: moonbitlang/async@0.22.9 alias: async - path: totto2727/workgraph-core@0.1.4 alias: core - path: totto2727/workgraph-llm@0.1.4 alias: workgraph_llm backend: native

    #workgraph-llm

    workgraph-llm adapts messages, tools, streamed events, and collected results from totto2727/llm to typed Workgraph nodes without selecting a provider. For shared installation and graph construction, see the root Setup and Usage.

    The node awaits the provider on JavaScript, native, and Wasm1, and cancellation propagates to the provider before response decoding. Use a Wasm1 host with MoonBit's async socket support for HTTP providers. Browser HTTP providers require a CORS-compatible endpoint, and local subprocess providers cannot run in a browser.

    #Usage

    Run an LLM node with a deterministic provider and observe its response in graph state:

    ///|
    async test "workgraph-llm response usage" {
    let entry = @core.NodeId::NodeId("llm")
    let provider = @llm.MockProvider::new([
    [
    @llm.MessageStart,
    @llm.TextDelta("Use a typed node"),
    @llm.MessageEnd(finish_reason=@llm.Stop, usage=None),
    ],
    ]).boxed()
    let node = @workgraph_llm.llm_node(
    entry,
    @core.NodeMetadata::NodeMetadata(
    name="LLM",
    description=None,
    kind=@core.Llm,
    tags=[],
    ),
    @workgraph_llm.LlmNodeSpec::LlmNodeSpec(
    provider,
    fn(_context, prompt : String) {
    @workgraph_llm.LlmRequest::LlmRequest([@llm.Message::user(prompt)])
    },
    fn(_state, response) {
    @core.NodeOutput::NodeOutput(Some(response.text), None)
    },
    ),
    )
    let definition = @core.GraphDefinition::GraphDefinition(
    @core.Reducer::Reducer(fn(_state : String, patch : String) { patch }),
    )
    definition.add_node(node)
    definition.set_router(
    entry,
    @core.router([], fn(_state, _completion) { @core.End }),
    )
    definition.set_entry(entry)
    let result = @core.GraphRuntime::GraphRuntime(definition.compile()).invoke(
    "Plan the next step",
    )
    inspect(result.final_state, content="Use a typed node")
    inspect(result.steps, content="1")
    }

    The checked LLM example also reports collected token usage.

    #API

    This README was generated from the share-artifact skill and README template.

    LlmNodeError

    pub suberror LlmNodeError {
    ProviderFailed(String)
    } derive(
    Debug
    )

    Raised when an LLM provider reports a stream failure.

    LlmNodeSpec

    LlmNodeSpec::LlmNodeSpec

    Creates a provider-backed LLM graph-node specification.

    LlmRequest

    pub(all) struct LlmRequest {
    messages : Array[
    Message
    ]
    tools : Array[
    ToolDef
    ]
    }

    A provider-neutral request built from totto2727/llm message and tool types.

    LlmRequest::LlmRequest

    Creates one LLM invocation request.

    llm_node

    Creates a graph node that builds a request, invokes its provider, and decodes the collected response.

    Source Files