Melange
LLM Inference

Retrieval-Augmented Generation

Use retrieval and local context with ZeticMLange LLMs.

Retrieval-augmented generation (RAG) lets your app retrieve relevant text chunks and pass them into an on-device LLM as grounded context.

This page reflects ZeticMLange 1.11.0 on Android and iOS, and zetic_mlange 1.11.1 on Flutter. RAG APIs are available on Android, iOS, and Flutter through a composition-based RagPipeline shape.

Concepts

ConceptPurpose
RetrieverApp-provided component that returns relevant chunks for a query.
RAG profileA value that selects one of the SDK's built-in prompt families.
Retrieved chunkText plus optional score, source, and metadata.
RAG documentText and source values indexed by a local RAG pipeline.
Local RAG configChunking, embedding, and retrieval settings for a local RAG pipeline.
RAG pipelineCombines retrieval, prompt assembly, and token streaming.
Local RAG pipelineFlutter, Android, and iOS helper for on-device chunking, embedding, and retrieval.

Android

Android exposes RagPipeline, RagProfile, Retriever, RetrievedChunk, LocalRagPipeline, RagDocument, and LocalRagConfig.

class MyRetriever : Retriever {
    override suspend fun retrieve(query: String, topK: Int): List<RetrievedChunk> {
        return listOf(
            RetrievedChunk(
                text = "ZeticMLange runs optimized models on-device.",
                score = 0.94f,
                source = "docs",
            ),
        )
    }
}

val pipeline = RagPipeline(
    retriever = MyRetriever(),
    llm = model,
    profile = RagProfile.qwen25(),
)

pipeline.respond("What does ZeticMLange do?").collect { token ->
    append(token)
}

For fully local retrieval, pass the same RagProfile to LocalRagPipeline and index named RagDocument values:

val localProfile = RagProfile.qwen25()
val localRag = LocalRagPipeline.create(
    embedderGgufPath = embedderPath,
    profile = localProfile,
    config = LocalRagConfig(),
)

localRag.indexDocs(
    listOf(
        RagDocument(
            text = "ZeticMLange runs optimized models on-device.",
            source = "docs",
        ),
    ),
)

val rag = RagPipeline(
    retriever = localRag,
    llm = model,
    profile = localProfile,
)

iOS

iOS exposes RagPipeline, RagProfile, Retriever, RetrievedChunk, LocalRagPipeline, RagDocument, and LocalRagConfig.

final class MyRetriever: Retriever {
  func retrieve(query: String, topK: Int) async throws -> [RetrievedChunk] {
    [
      RetrievedChunk(
        text: "ZeticMLange runs optimized models on-device.",
        score: 0.94,
        source: "docs"
      )
    ]
  }
}

let pipeline = RagPipeline(
  retriever: MyRetriever(),
  llm: model,
  profile: RagProfile.qwen25()
)

for try await token in pipeline.respond(query: "What does ZeticMLange do?") {
  append(token)
}

For fully local retrieval:

let localProfile = RagProfile.qwen25()
let localRag = try LocalRagPipeline.create(
  embedderGgufPath: embedderPath,
  profile: localProfile,
  config: LocalRagConfig()
)

try await localRag.indexDocs([
  RagDocument(
    text: "ZeticMLange runs optimized models on-device.",
    source: "docs"
  )
])

Flutter

Flutter exposes RagPipeline, RagRetriever, RetrievedChunk, LocalRagPipeline, RagDocument, and LocalRagConfig.

final retriever = MyRetriever();

final rag = RagPipeline(
  retriever: retriever,
  llm: model,
  profile: const RagProfile.qwen25(),
);

await for (final token in rag.respond(query: 'What does ZeticMLange do?')) {
  append(token);
}

MyRetriever can be any app-owned class that implements RagRetriever:

final class MyRetriever implements RagRetriever {
  @override
  Future<List<RetrievedChunk>> retrieve(String query, {required int topK}) async {
    return const [
      RetrievedChunk(
        text: 'ZeticMLange runs optimized models on-device.',
        score: 0.94,
        source: 'docs',
      ),
    ];
  }
}

For a local retrieval pipeline:

final localRag = await LocalRagPipeline.create(
  profile: const RagProfile.qwen25(),
  embedderGgufPath: embedderPath,
  config: const LocalRagConfig(),
);

await localRag.indexDocs([
  const RagDocument(
    text: 'ZeticMLange runs optimized models on-device.',
    source: 'docs',
  ),
]);

final rag = RagPipeline(
  retriever: localRag,
  llm: model,
  profile: const RagProfile.qwen25(),
);

await for (final token in rag.respond(query: 'What does ZeticMLange do?')) {
  append(token);
}

API Reference

On this page