NativeLibraryConfig: WithLogs(LLamaLogLevel) (#529)

Adds a NativeLibraryConfig.WithLogs() overload to let the user indicate the log level (with "info" as the default)
This commit is contained in:
Scott W Harden 2024-02-21 18:51:09 -05:00 committed by GitHub
parent 06ffe3ac95
commit a6394001a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 15 deletions

View File

@ -19,7 +19,7 @@ Console.WriteLine(
NativeLibraryConfig
.Instance
.WithCuda()
.WithLogs();
.WithLogs(LLamaLogLevel.Warning);
NativeApi.llama_empty_call();

View File

@ -35,8 +35,12 @@ namespace LLama.Native
private static void Log(string message, LogLevel level)
{
if (!enableLogging) return;
Debug.Assert(level is LogLevel.Information or LogLevel.Error or LogLevel.Warning);
if (!enableLogging)
return;
if ((int)level < (int)logLevel)
return;
ConsoleColor color;
string levelPrefix;
if (level == LogLevel.Information)
@ -246,6 +250,7 @@ namespace LLama.Native
#if NET6_0_OR_GREATER
var configuration = NativeLibraryConfig.CheckAndGatherDescription();
enableLogging = configuration.Logging;
logLevel = configuration.LogLevel;
// We move the flag to avoid loading library when the variable is called else where.
NativeLibraryConfig.LibraryHasLoaded = true;
Log(configuration.ToString(), LogLevel.Information);
@ -336,5 +341,6 @@ namespace LLama.Native
private const string cudaVersionFile = "version.json";
private const string loggingPrefix = "[LLamaSharp Native]";
private static bool enableLogging = false;
private static LLamaLogLevel logLevel = LLamaLogLevel.Info;
}
}

View File

@ -29,6 +29,7 @@ namespace LLama.Native
private bool _allowFallback = true;
private bool _skipCheck = false;
private bool _logging = false;
private LLamaLogLevel _logLevel = LLamaLogLevel.Info;
/// <summary>
/// search directory -> priority level, 0 is the lowest.
@ -119,7 +120,7 @@ namespace LLama.Native
/// <param name="enable"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException">Thrown if `LibraryHasLoaded` is true.</exception>
public NativeLibraryConfig WithLogs(bool enable = true)
public NativeLibraryConfig WithLogs(bool enable)
{
ThrowIfLoaded();
@ -127,6 +128,21 @@ namespace LLama.Native
return this;
}
/// <summary>
/// Enable console logging with the specified log logLevel.
/// </summary>
/// <param name="logLevel"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException">Thrown if `LibraryHasLoaded` is true.</exception>
public NativeLibraryConfig WithLogs(LLamaLogLevel logLevel = LLamaLogLevel.Info)
{
ThrowIfLoaded();
_logging = true;
_logLevel = logLevel;
return this;
}
/// <summary>
/// Add self-defined search directories. Note that the file stucture of the added
/// directories must be the same as the default directory. Besides, the directory
@ -169,6 +185,7 @@ namespace LLama.Native
Instance._allowFallback,
Instance._skipCheck,
Instance._logging,
Instance._logLevel,
Instance._searchDirectories.Concat(new[] { "./" }).ToArray()
);
}
@ -250,7 +267,7 @@ namespace LLama.Native
Avx512,
}
internal record Description(string Path, bool UseCuda, AvxLevel AvxLevel, bool AllowFallback, bool SkipCheck, bool Logging, string[] SearchDirectories)
internal record Description(string Path, bool UseCuda, AvxLevel AvxLevel, bool AllowFallback, bool SkipCheck, bool Logging, LLamaLogLevel LogLevel, string[] SearchDirectories)
{
public override string ToString()
{
@ -272,9 +289,10 @@ namespace LLama.Native
$"- AllowFallback: {AllowFallback}\n" +
$"- SkipCheck: {SkipCheck}\n" +
$"- Logging: {Logging}\n" +
$"- LogLevel: {LogLevel}\n" +
$"- SearchDirectories and Priorities: {searchDirectoriesString}";
}
}
}
#endif
}
}