You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

NativeApi.Grammar.cs 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace LLama.Native
  4. {
  5. using llama_token = Int32;
  6. public unsafe partial class NativeApi
  7. {
  8. /// <summary>
  9. /// Create a new grammar from the given set of grammar rules
  10. /// </summary>
  11. /// <param name="rules"></param>
  12. /// <param name="n_rules"></param>
  13. /// <param name="start_rule_index"></param>
  14. /// <returns></returns>
  15. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  16. public static extern IntPtr llama_grammar_init(LLamaGrammarElement** rules, ulong n_rules, ulong start_rule_index);
  17. /// <summary>
  18. /// Free all memory from the given SafeLLamaGrammarHandle
  19. /// </summary>
  20. /// <param name="grammar"></param>
  21. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  22. public static extern void llama_grammar_free(IntPtr grammar);
  23. /// <summary>
  24. /// Create a copy of an existing grammar instance
  25. /// </summary>
  26. /// <param name="grammar"></param>
  27. /// <returns></returns>
  28. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  29. public static extern IntPtr llama_grammar_copy(SafeLLamaGrammarHandle grammar);
  30. /// <summary>
  31. /// Apply constraints from grammar
  32. /// </summary>
  33. /// <param name="ctx"></param>
  34. /// <param name="candidates"></param>
  35. /// <param name="grammar"></param>
  36. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  37. public static extern void llama_sample_grammar(SafeLLamaContextHandle ctx, ref LLamaTokenDataArrayNative candidates, SafeLLamaGrammarHandle grammar);
  38. /// <summary>
  39. /// Accepts the sampled token into the grammar
  40. /// </summary>
  41. /// <param name="ctx"></param>
  42. /// <param name="grammar"></param>
  43. /// <param name="token"></param>
  44. [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
  45. public static extern void llama_grammar_accept_token(SafeLLamaContextHandle ctx, SafeLLamaGrammarHandle grammar, llama_token token);
  46. }
  47. }