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.

llama.llamacontext.md 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. # LLamaContext
  2. Namespace: LLama
  3. A llama_context, which holds all the context required to interact with a model
  4. ```csharp
  5. public sealed class LLamaContext : System.IDisposable
  6. ```
  7. Inheritance [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object) → [LLamaContext](./llama.llamacontext.md)<br>
  8. Implements [IDisposable](https://docs.microsoft.com/en-us/dotnet/api/system.idisposable)
  9. ## Properties
  10. ### **VocabCount**
  11. Total number of tokens in vocabulary of this model
  12. ```csharp
  13. public int VocabCount { get; }
  14. ```
  15. #### Property Value
  16. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  17. ### **ContextSize**
  18. Total number of tokens in the context
  19. ```csharp
  20. public int ContextSize { get; }
  21. ```
  22. #### Property Value
  23. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  24. ### **EmbeddingSize**
  25. Dimension of embedding vectors
  26. ```csharp
  27. public int EmbeddingSize { get; }
  28. ```
  29. #### Property Value
  30. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  31. ### **Params**
  32. The model params set for this model.
  33. ```csharp
  34. public IModelParams Params { get; set; }
  35. ```
  36. #### Property Value
  37. [IModelParams](./llama.abstractions.imodelparams.md)<br>
  38. ### **NativeHandle**
  39. The native handle, which is used to be passed to the native APIs
  40. ```csharp
  41. public SafeLLamaContextHandle NativeHandle { get; }
  42. ```
  43. #### Property Value
  44. [SafeLLamaContextHandle](./llama.native.safellamacontexthandle.md)<br>
  45. **Remarks:**
  46. Be careful how you use this!
  47. ### **Encoding**
  48. The encoding set for this model to deal with text input.
  49. ```csharp
  50. public Encoding Encoding { get; }
  51. ```
  52. #### Property Value
  53. [Encoding](https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding)<br>
  54. ### **EmbeddingLength**
  55. The embedding length of the model, also known as `n_embed`
  56. ```csharp
  57. public int EmbeddingLength { get; }
  58. ```
  59. #### Property Value
  60. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  61. ## Constructors
  62. ### **LLamaContext(IModelParams, ILLamaLogger)**
  63. #### Caution
  64. Use the LLamaWeights.CreateContext instead
  65. ---
  66. ```csharp
  67. public LLamaContext(IModelParams params, ILLamaLogger logger)
  68. ```
  69. #### Parameters
  70. `params` [IModelParams](./llama.abstractions.imodelparams.md)<br>
  71. Model params.
  72. `logger` [ILLamaLogger](./llama.common.illamalogger.md)<br>
  73. The logger.
  74. ### **LLamaContext(LLamaWeights, IModelParams, ILLamaLogger)**
  75. Create a new LLamaContext for the given LLamaWeights
  76. ```csharp
  77. public LLamaContext(LLamaWeights model, IModelParams params, ILLamaLogger logger)
  78. ```
  79. #### Parameters
  80. `model` [LLamaWeights](./llama.llamaweights.md)<br>
  81. `params` [IModelParams](./llama.abstractions.imodelparams.md)<br>
  82. `logger` [ILLamaLogger](./llama.common.illamalogger.md)<br>
  83. #### Exceptions
  84. [ObjectDisposedException](https://docs.microsoft.com/en-us/dotnet/api/system.objectdisposedexception)<br>
  85. ## Methods
  86. ### **Clone()**
  87. Create a copy of the current state of this context
  88. ```csharp
  89. public LLamaContext Clone()
  90. ```
  91. #### Returns
  92. [LLamaContext](./llama.llamacontext.md)<br>
  93. ### **Tokenize(String, Boolean)**
  94. Tokenize a string.
  95. ```csharp
  96. public Int32[] Tokenize(string text, bool addBos)
  97. ```
  98. #### Parameters
  99. `text` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  100. `addBos` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  101. Whether to add a bos to the text.
  102. #### Returns
  103. [Int32[]](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  104. ### **DeTokenize(IEnumerable&lt;Int32&gt;)**
  105. Detokenize the tokens to text.
  106. ```csharp
  107. public string DeTokenize(IEnumerable<int> tokens)
  108. ```
  109. #### Parameters
  110. `tokens` [IEnumerable&lt;Int32&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1)<br>
  111. #### Returns
  112. [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  113. ### **SaveState(String)**
  114. Save the state to specified path.
  115. ```csharp
  116. public void SaveState(string filename)
  117. ```
  118. #### Parameters
  119. `filename` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  120. ### **GetStateData()**
  121. #### Caution
  122. Use `GetState` instead, this supports larger states (over 2GB)
  123. ---
  124. Get the state data as a byte array.
  125. ```csharp
  126. public Byte[] GetStateData()
  127. ```
  128. #### Returns
  129. [Byte[]](https://docs.microsoft.com/en-us/dotnet/api/system.byte)<br>
  130. ### **GetState()**
  131. Get the state data as an opaque handle
  132. ```csharp
  133. public State GetState()
  134. ```
  135. #### Returns
  136. [State](./llama.llamacontext.state.md)<br>
  137. ### **LoadState(String)**
  138. Load the state from specified path.
  139. ```csharp
  140. public void LoadState(string filename)
  141. ```
  142. #### Parameters
  143. `filename` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  144. #### Exceptions
  145. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  146. ### **LoadState(Byte[])**
  147. Load the state from memory.
  148. ```csharp
  149. public void LoadState(Byte[] stateData)
  150. ```
  151. #### Parameters
  152. `stateData` [Byte[]](https://docs.microsoft.com/en-us/dotnet/api/system.byte)<br>
  153. #### Exceptions
  154. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  155. ### **LoadState(State)**
  156. Load the state from memory.
  157. ```csharp
  158. public void LoadState(State state)
  159. ```
  160. #### Parameters
  161. `state` [State](./llama.llamacontext.state.md)<br>
  162. #### Exceptions
  163. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  164. ### **Sample(LLamaTokenDataArray, Nullable`1&, Single, MirostatType, Single, Single, Int32, Single, Single, Single, SafeLLamaGrammarHandle)**
  165. Perform the sampling. Please don't use it unless you fully know what it does.
  166. ```csharp
  167. public int Sample(LLamaTokenDataArray candidates, Nullable`1& mirostat_mu, float temperature, MirostatType mirostat, float mirostatTau, float mirostatEta, int topK, float topP, float tfsZ, float typicalP, SafeLLamaGrammarHandle grammar)
  168. ```
  169. #### Parameters
  170. `candidates` [LLamaTokenDataArray](./llama.native.llamatokendataarray.md)<br>
  171. `mirostat_mu` [Nullable`1&](https://docs.microsoft.com/en-us/dotnet/api/system.nullable-1&)<br>
  172. `temperature` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  173. `mirostat` [MirostatType](./llama.common.mirostattype.md)<br>
  174. `mirostatTau` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  175. `mirostatEta` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  176. `topK` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  177. `topP` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  178. `tfsZ` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  179. `typicalP` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  180. `grammar` [SafeLLamaGrammarHandle](./llama.native.safellamagrammarhandle.md)<br>
  181. #### Returns
  182. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  183. ### **ApplyPenalty(IEnumerable&lt;Int32&gt;, Dictionary&lt;Int32, Single&gt;, Int32, Single, Single, Single, Boolean)**
  184. Apply the penalty for the tokens. Please don't use it unless you fully know what it does.
  185. ```csharp
  186. public LLamaTokenDataArray ApplyPenalty(IEnumerable<int> lastTokens, Dictionary<int, float> logitBias, int repeatLastTokensCount, float repeatPenalty, float alphaFrequency, float alphaPresence, bool penalizeNL)
  187. ```
  188. #### Parameters
  189. `lastTokens` [IEnumerable&lt;Int32&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1)<br>
  190. `logitBias` [Dictionary&lt;Int32, Single&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2)<br>
  191. `repeatLastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  192. `repeatPenalty` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  193. `alphaFrequency` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  194. `alphaPresence` [Single](https://docs.microsoft.com/en-us/dotnet/api/system.single)<br>
  195. `penalizeNL` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  196. #### Returns
  197. [LLamaTokenDataArray](./llama.native.llamatokendataarray.md)<br>
  198. ### **Eval(Int32[], Int32)**
  199. ```csharp
  200. public int Eval(Int32[] tokens, int pastTokensCount)
  201. ```
  202. #### Parameters
  203. `tokens` [Int32[]](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  204. `pastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  205. #### Returns
  206. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  207. The updated `pastTokensCount`.
  208. #### Exceptions
  209. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  210. ### **Eval(List&lt;Int32&gt;, Int32)**
  211. ```csharp
  212. public int Eval(List<int> tokens, int pastTokensCount)
  213. ```
  214. #### Parameters
  215. `tokens` [List&lt;Int32&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1)<br>
  216. `pastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  217. #### Returns
  218. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  219. The updated `pastTokensCount`.
  220. #### Exceptions
  221. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  222. ### **Eval(ReadOnlyMemory&lt;Int32&gt;, Int32)**
  223. ```csharp
  224. public int Eval(ReadOnlyMemory<int> tokens, int pastTokensCount)
  225. ```
  226. #### Parameters
  227. `tokens` [ReadOnlyMemory&lt;Int32&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.readonlymemory-1)<br>
  228. `pastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  229. #### Returns
  230. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  231. The updated `pastTokensCount`.
  232. #### Exceptions
  233. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  234. ### **Eval(ReadOnlySpan&lt;Int32&gt;, Int32)**
  235. ```csharp
  236. public int Eval(ReadOnlySpan<int> tokens, int pastTokensCount)
  237. ```
  238. #### Parameters
  239. `tokens` [ReadOnlySpan&lt;Int32&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.readonlyspan-1)<br>
  240. `pastTokensCount` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  241. #### Returns
  242. [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  243. The updated `pastTokensCount`.
  244. #### Exceptions
  245. [RuntimeError](./llama.exceptions.runtimeerror.md)<br>
  246. ### **GenerateResult(IEnumerable&lt;Int32&gt;)**
  247. ```csharp
  248. internal IEnumerable<string> GenerateResult(IEnumerable<int> ids)
  249. ```
  250. #### Parameters
  251. `ids` [IEnumerable&lt;Int32&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1)<br>
  252. #### Returns
  253. [IEnumerable&lt;String&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1)<br>
  254. ### **TokenToString(Int32)**
  255. Convert a token into a string
  256. ```csharp
  257. public string TokenToString(int token)
  258. ```
  259. #### Parameters
  260. `token` [Int32](https://docs.microsoft.com/en-us/dotnet/api/system.int32)<br>
  261. #### Returns
  262. [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  263. ### **Dispose()**
  264. ```csharp
  265. public void Dispose()
  266. ```