Browse Source

Some of the basics of the grammar API

tags/v0.5.1
Martin Evans 2 years ago
parent
commit
0294bb1303
3 changed files with 125 additions and 0 deletions
  1. +64
    -0
      LLama/Native/LLamaGrammarElement.cs
  2. +37
    -0
      LLama/Native/NativeApi.Grammar.cs
  3. +24
    -0
      LLama/Native/SafeLLamaGrammarHandle.cs

+ 64
- 0
LLama/Native/LLamaGrammarElement.cs View File

@@ -0,0 +1,64 @@
using System.Runtime.InteropServices;

namespace LLama.Native
{
/// <summary>
/// grammar element type
/// </summary>
public enum LLamaGrammarElementType
{
/// <summary>
/// end of rule definition
/// </summary>
END = 0,

/// <summary>
/// start of alternate definition for rule
/// </summary>
ALT = 1,

/// <summary>
/// non-terminal element: reference to rule
/// </summary>
RULE_REF = 2,

/// <summary>
/// terminal element: character (code point)
/// </summary>
CHAR = 3,

/// <summary>
/// inverse char(s) ([^a], [^a-b] [^abc])
/// </summary>
CHAR_NOT = 4,

/// <summary>
/// modifies a preceding LLAMA_GRETYPE_CHAR or LLAMA_GRETYPE_CHAR_ALT to
/// be an inclusive range ([a-z])
/// </summary>
CHAR_RNG_UPPER = 5,

/// <summary>
/// modifies a preceding LLAMA_GRETYPE_CHAR or
/// LLAMA_GRETYPE_CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
/// </summary>
CHAR_ALT = 6,
};

/// <summary>
/// An element of a grammar
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct LLamaGrammarElement
{
/// <summary>
/// The type of this element
/// </summary>
public LLamaGrammarElementType Type;

/// <summary>
/// Unicode code point or rule ID
/// </summary>
public uint Value;
}
}

+ 37
- 0
LLama/Native/NativeApi.Grammar.cs View File

@@ -0,0 +1,37 @@
using System;
using System.Runtime.InteropServices;

namespace LLama.Native
{
using llama_token = Int32;

public unsafe partial class NativeApi
{
//todo: LLAMA_API struct llama_grammar * llama_grammar_init(const llama_grammar_element** rules, size_t n_rules,size_t start_rule_index);

/// <summary>
/// Free all memory from the given SafeLLamaGrammarHandle
/// </summary>
/// <param name="grammar"></param>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern void llama_grammar_free(IntPtr grammar);

/// <summary>
/// Apply constraints from grammar
/// </summary>
/// <param name="ctx"></param>
/// <param name="candidates"></param>
/// <param name="grammar"></param>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern void llama_sample_grammar(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, SafeLLamaGrammarHandle grammar);

/// <summary>
/// Accepts the sampled token into the grammar
/// </summary>
/// <param name="ctx"></param>
/// <param name="grammar"></param>
/// <param name="token"></param>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern void llama_grammar_accept_token(SafeLLamaContextHandle ctx, SafeLLamaGrammarHandle grammar, llama_token token);
}
}

+ 24
- 0
LLama/Native/SafeLLamaGrammarHandle.cs View File

@@ -0,0 +1,24 @@
using System;

namespace LLama.Native
{
/// <summary>
/// A safe reference to a `llama_grammar`
/// </summary>
public class SafeLLamaGrammarHandle
: SafeLLamaHandleBase
{
internal SafeLLamaGrammarHandle(IntPtr handle)
: base(handle)
{
}

/// <inheritdoc />
protected override bool ReleaseHandle()
{
NativeApi.llama_grammar_free(handle);
SetHandle(IntPtr.Zero);
return true;
}
}
}

Loading…
Cancel
Save