AI-Studio/app/MindWork AI Studio/Tools/TokenizerFingerprint.cs
Thorsten Sommer e42277beba
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Verify (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Ask before local data sources are indexed again (#983)
2026-09-19 10:12:06 +02:00

46 lines
2.1 KiB
C#

using System.Security.Cryptography;
namespace AIStudio.Tools;
/// <summary>
/// Identifies a tokenizer by what is inside its file, not by where the file lies.
/// </summary>
/// <remarks>
/// The embedding signature asks this to decide whether stored vectors still belong to the current
/// configuration, and the path cannot answer it. A tokenizer is stored below the data directory under
/// the model it belongs to, keeping the name it came with -- and the usual name for one is
/// tokenizer.json. Picking a different tokenizer with that name lands on the identical path, so the
/// index would be kept although the chunk boundaries moved. The other way round, moving the data
/// directory changes every path without changing a single tokenizer.
/// </remarks>
public static class TokenizerFingerprint
{
/// <summary>
/// Reads a tokenizer file and returns a fingerprint of its content.
/// </summary>
/// <param name="tokenizerPath">The tokenizer file to read. May be empty when no tokenizer is set.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>The fingerprint, or an empty string when there is no readable file.</returns>
public static async Task<string> ForFileAsync(string tokenizerPath, CancellationToken token = default)
{
if (string.IsNullOrWhiteSpace(tokenizerPath))
return string.Empty;
try
{
await using var stream = File.OpenRead(tokenizerPath);
return Convert.ToHexString(await SHA256.HashDataAsync(stream, token));
}
catch
{
//
// An unreadable tokenizer is not this method's problem to report: the dialog validates the
// file before it ever gets here, and an indexing run says so again when it cannot tokenize
// anything. Whoever stores a provider has to decide what an empty answer means for them,
// because writing it into the settings would look like another tokenizer and throw the
// stored vectors away.
//
return string.Empty;
}
}
}