How to use all-MiniLM-L6-v2 in .net android?

mc 7,341 Reputation points
2026-09-08T14:12:58.5733333+00:00

I am using .net android (android application) and I want to ask that how to use all-MiniLM-L6-V2 in it?

I want to feed a txt to it and ask questions about the txt.

Developer technologies | .NET | .NET Multi-platform App UI
0 comments No comments

3 answers

Sort by: Most helpful
  1. Nguyen Dam (WICLOUD CORPORATION) 0 Reputation points Microsoft External Staff Moderator
    2026-09-09T06:43:16.7+00:00

    Hello, and thank you for your question.

    The all-MiniLM-L6-v2 model is an embedding model rather than a chat model. It is designed to understand and compare text by converting it into a format that applications can search, match, or retrieve information from. It cannot generate responses on its own.

    If your goal is to create a .NET MAUI Android app that can work with text files, you can build a simple workflow:

    1. Install ONNX Runtime and add the ONNX model (you should have downloaded it somewhere) to your MAUI project
    dotnet add package Microsoft.ML.OnnxRuntime 
    
    1. Import ONNX and load by its directory

    Option 1: Load the model from MAUI Resources/Raw (recommended)

    using Microsoft.ML.OnnxRuntime; 
    var stream = await FileSystem.OpenAppPackageFileAsync("all-MiniLM-L6-v2.onnx"); 
    var tempPath = Path.Combine(FileSystem.CacheDirectory, "all-MiniLM-L6-v2.onnx"); 
    using (var file = File.Create(tempPath)) 
    { 
    	await stream.CopyToAsync(file); 
    } 
    using var session = new InferenceSession(tempPath); 
    

    Option 2: Load the model directly from an absolute path

    using Microsoft.ML.OnnxRuntime; 
    string modelPath = Path.Combine( 
    FileSystem.AppDataDirectory, 
    "all-MiniLM-L6-v2.onnx"); 
    using var session = new InferenceSession(modelPath); 
     
    
    1. Make your application loads the text file by choice. Here is an example for the code-behind:
    private async void OnPickFileClicked(object sender, EventArgs e) 
    { 
    try 
    { 
    	var result = await FilePicker.Default.PickAsync(new PickOptions 
    	{ 
    		PickerTitle = "Select a text file" 
    	}); 
    	if (result == null) 
    		return; 
    	string text = await File.ReadAllTextAsync(result.FullPath); 
    	FileNameLabel.Text = result.FileName; 
    	ContentEditor.Text = text; 
    	// Generate embeddings using the ONNX model. 
    	// Tokenization and tensor creation are omitted 
    	// from this example for brevity. 
    	float[] embedding = await GenerateEmbeddingAsync(text); 
    	// Use the embedding as input to the chat workflow. 
    	string response = await QueryChatModelAsync(text, embedding); 
    	ResultEditor.Text = response; 
    } 
    catch (Exception ex) 
    { 
    	await DisplayAlert("Error", ex.Message, "OK"); 
    } 
    } 
    

    (You may want to add some equivalent UI features to the XAML file like this:)

    <VerticalStackLayout Padding="20"> 
    <Button 
    	Text="Pick Text File" 
    	Clicked="OnPickFileClicked"/> 
    <Label 
    	x:Name="FileNameLabel" 
    	Text="No file selected" /> 
    <Editor 
    	x:Name="ContentEditor" 
    	AutoSize="TextChanges" 
    	HeightRequest="400" /> 
    </VerticalStackLayout> 
    
    1. (Optional) Since you are using an embedding model, you may want to consider adding a helper method to convert document text into embeddings.
    private async Task<float[]> GenerateEmbeddingAsync(string text) 
    { 
    	// Placeholder for embedding generation. 
    	// The implementation depends on the 
    	// model and libraries being used. 
    	await Task.CompletedTask; 
    	return Array.Empty<float>(); 
    } 
     
    
    1. As said above, because the embedding model can't generate a response on its own, you will need to send the data to a chat model.
    private async Task<string> QueryChatModelAsync( 
    string text, 
    float[] embedding) 
    { 
    	string prompt = 
    	$""" 
    	Please answer questions about the following document. 
    	Document: 
    	{text} 
    	"""; 
    	// Replace with your preferred LLM API or SDK. 
    	string response = 
    	await YourLlmService.GenerateAsync(prompt); 
    	return response; 		
    } 
    

    For more information on how to call an LLM API from a .NET or .NET MAUI application, refer to the following Microsoft documentation: build-chat-app

    Tip: The examples above use a hardcoded prompt for simplicity. If you would like users to enter their own questions, consider adding a text input field to the UI and passing its contents to QueryChatModelAsync() instead.

    If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation. Thank you. 

    Was this answer helpful?

    1 person found this answer helpful.

  2. Vignesh vicky 0 Reputation points
    2026-09-09T04:54:07.3+00:00

    Convert/load all-MiniLM-L6-v2 as an ONNX model.

    Tokenize the TXT content.

    Generate embeddings using the MiniLM model.

    Store/search those embeddings to find the most relevant parts of the text.

    Use an LLM/API to generate the final answer from the retrieved text.

    Note that all-MiniLM-L6-v2 is an embedding model, not a question-answering/chat model. It can help find relevant text, but it does not itself generate natural-language answers.

    In a .NET MAUI Android app, ONNX Runtime is therefore a more suitable option than trying to use ML.NET directly.

    Was this answer helpful?


  3. Bruce (SqlWork.com) 85,276 Reputation points
    2026-09-08T14:36:25.8+00:00

    Maui AI does not currently support Android. You will need to pick an Android library that supports your model, and create Maui bindings for the library.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.