Merge pull request #95 from saddam213/logging

Add native logging output
This commit is contained in:
Martin Evans 2023-08-10 17:05:58 +01:00 committed by GitHub
commit 9b4d0e3bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 9 deletions

View File

@ -1,4 +1,5 @@
using System;
using LLama.Native;
using System;
using System.Diagnostics;
using System.IO;
using static LLama.Common.ILLamaLogger;
@ -9,13 +10,13 @@ public interface ILLamaLogger
{
public enum LogLevel
{
Info,
Debug,
Warning,
Error
Debug = 1,
Error = 2,
Warning = 3,
Info = 4
}
/// <summary>
/// Write the log in cosutomized way
/// Write the log in customized way
/// </summary>
/// <param name="source">The source of the log. It may be a method name or class name.</param>
/// <param name="message">The message.</param>
@ -25,7 +26,7 @@ public interface ILLamaLogger
/// <summary>
/// The default logger of LLamaSharp. On default it write to console. User methods of `LLamaLogger.Default` to change the behavior.
/// It's more recommended to inherit `ILLamaLogger` to cosutomize the behavior.
/// It's more recommended to inherit `ILLamaLogger` to customize the behavior.
/// </summary>
public sealed class LLamaDefaultLogger : ILLamaLogger
{
@ -44,6 +45,16 @@ public sealed class LLamaDefaultLogger : ILLamaLogger
}
/// <summary>
/// Enable logging output from llama.cpp
/// </summary>
/// <returns></returns>
public LLamaDefaultLogger EnableNative()
{
EnableNativeLogCallback();
return this;
}
public LLamaDefaultLogger EnableConsole()
{
_toConsole = true;
@ -157,4 +168,31 @@ public sealed class LLamaDefaultLogger : ILLamaLogger
string formattedDate = now.ToString("yyyy.MM.dd HH:mm:ss");
return $"[{formattedDate}][{level}]: {message}";
}
/// <summary>
/// Register native logging callback
/// </summary>
private void EnableNativeLogCallback()
{
// TODO: Move to a more appropriate place once we have a intitialize method
NativeApi.llama_log_set(NativeLogCallback);
}
/// <summary>
/// Callback for native logging function
/// </summary>
/// <param name="level">The log level</param>
/// <param name="message">The log message</param>
private void NativeLogCallback(LogLevel level, string message)
{
if (string.IsNullOrEmpty(message))
return;
// Note that text includes the new line character at the end for most events.
// If your logging mechanism cannot handle that, check if the last character is '\n' and strip it
// if it exists.
// It might not exist for progress report where '.' is output repeatedly.
Log(default!, message.TrimEnd('\n'), level);
}
}

View File

@ -1,12 +1,16 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using LLama.Common;
using LLama.Exceptions;
namespace LLama.Native
{
using llama_token = Int32;
public unsafe partial class NativeApi
public delegate void LLamaLogCallback(ILLamaLogger.LogLevel level, string message);
public unsafe partial class NativeApi
{
public static readonly int LLAMA_MAX_DEVICES = 1;
static NativeApi()
@ -331,5 +335,8 @@ namespace LLama.Native
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_tokenize_with_model(SafeLlamaModelHandle model, byte* text, int* tokens, int n_max_tokens, bool add_bos);
}
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern void llama_log_set(LLamaLogCallback logCallback);
}
}