Compose GenAI into a Graph
| Field | Value |
|---|---|
| Category | GenAI |
| Difficulty | Advanced |
| Estimated Read Time | 20-25 minutes |
| Labels | genai, graph, composition, streaming, advanced |
Most GenAI applications should start with direct model APIs. Graph composition becomes useful when GenAI needs to sit beside other Neat stages, named inputs, named outputs, routing, or application-level orchestration.
Walkthrough
Create a GenAI graph fragment
Create a task-specific model handle, configure graph-fragment options, and build a public Graph fragment.
The vision-language fragment exposes prompt, image, and use_cached_image inputs plus tokens, done, encoded, and error outputs. The speech transcriber fragment exposes audio and audio_path inputs plus tokens, done, and error outputs.
SpeechTranscriberOptions defaults to automatic language detection and
transcription. Set task to ASRTask::Translate in C++ or
ASRTask.Translate in Python to translate speech into English. Its done
bundle reports the detected source language and, when available,
no_speech_prob and avg_logprob.
auto model = std::make_shared<genai::VisionLanguageModel>(args.model);
genai::VisionLanguageOptions options;
options.system_prompt = "You are concise.";
options.max_new_tokens = 96;
options.streaming = true;
options.encode_images_on_input = false;
simaai::neat::Graph genai_fragment =
genai::graphs::VisionLanguage(model, options, "genai_stage");
Add the fragment to an app graph
Add the fragment to a larger application graph. The fragment keeps its public endpoint names, so application code can push and pull by name.
simaai::neat::Graph app("genai_app");
app.add(genai_fragment);
std::cout << app.describe() << "\n";
Build and push graph inputs
Build the graph into a Run, push an image sample to the image input, then push a text sample to the prompt input and let the GenAI stage produce tokens.
simaai::neat::Run run = app.build();
if (!run.push("image", make_image_sample(args.image))) {
throw std::runtime_error("push(image) failed: " + run.last_error());
}
if (!run.push("prompt", make_text_sample("prompt", "Describe this image in one sentence."))) {
throw std::runtime_error("push(prompt) failed: " + run.last_error());
}
Pull tokens and completion metadata
Pull from tokens until a done sample arrives. The done sample is a bundle with fields such as generated token count and finish reason.
std::cout << "assistant: ";
for (int i = 0; i < 256; ++i) {
if (auto token = run.pull("tokens", 250)) {
std::cout << sample_text(*token) << std::flush;
continue;
}
if (auto done = run.pull("done", 10)) {
(void)done;
break;
}
if (auto error = run.pull("error", 10)) {
throw std::runtime_error(sample_text(*error));
}
}
std::cout << "\n";
run.close();
Run
On the Modalix DevKit, download the LFM2-VL 1.6B VLM from Hugging Face using the LLiMa CLI:
llima pull LFM2-VL-1.6B-a16w4
Run the tutorial on Modalix with the DevKit-local model directory and a local image:
C++ (prebuilt):
./lib/sima-neat/tutorials/tutorial_022_compose_genai_into_graph \
--model /media/nvme/llima/models/LFM2-VL-1.6B-a16w4 \
--image share/sima-neat/tutorials/assets/fronalpstock_1330.jpg
C++ (build from source):
./build.sh --target tutorial_022_compose_genai_into_graph
./build/tutorials-standalone/tutorial_022_compose_genai_into_graph \
--model /media/nvme/llima/models/LFM2-VL-1.6B-a16w4 \
--image share/sima-neat/tutorials/assets/fronalpstock_1330.jpg
Expected output prints the graph description and a streamed answer pulled from the tokens output.
In Practice
Use this pattern when GenAI is part of a larger application graph. Keep direct GenAIModel, VisionLanguageModel, and ASRModel calls for simple request/response application code.