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.chatsession.md 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. # ChatSession
  2. Namespace: LLama
  3. The main chat session class.
  4. ```csharp
  5. public class ChatSession
  6. ```
  7. Inheritance [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object) → [ChatSession](./llama.chatsession.md)
  8. ## Fields
  9. ### **OutputTransform**
  10. The output transform used in this session.
  11. ```csharp
  12. public ITextStreamTransform OutputTransform;
  13. ```
  14. ### **MODEL_STATE_FILENAME**
  15. The filename for the serialized model state (KV cache, etc).
  16. ```csharp
  17. public static string MODEL_STATE_FILENAME;
  18. ```
  19. ### **EXECUTOR_STATE_FILENAME**
  20. The filename for the serialized executor state.
  21. ```csharp
  22. public static string EXECUTOR_STATE_FILENAME;
  23. ```
  24. ### **HISTORY_STATE_FILENAME**
  25. The filename for the serialized chat history.
  26. ```csharp
  27. public static string HISTORY_STATE_FILENAME;
  28. ```
  29. ### **INPUT_TRANSFORM_FILENAME**
  30. The filename for the serialized input transform pipeline.
  31. ```csharp
  32. public static string INPUT_TRANSFORM_FILENAME;
  33. ```
  34. ### **OUTPUT_TRANSFORM_FILENAME**
  35. The filename for the serialized output transform.
  36. ```csharp
  37. public static string OUTPUT_TRANSFORM_FILENAME;
  38. ```
  39. ### **HISTORY_TRANSFORM_FILENAME**
  40. The filename for the serialized history transform.
  41. ```csharp
  42. public static string HISTORY_TRANSFORM_FILENAME;
  43. ```
  44. ## Properties
  45. ### **Executor**
  46. The executor for this session.
  47. ```csharp
  48. public ILLamaExecutor Executor { get; private set; }
  49. ```
  50. #### Property Value
  51. [ILLamaExecutor](./llama.abstractions.illamaexecutor.md)<br>
  52. ### **History**
  53. The chat history for this session.
  54. ```csharp
  55. public ChatHistory History { get; private set; }
  56. ```
  57. #### Property Value
  58. [ChatHistory](./llama.common.chathistory.md)<br>
  59. ### **HistoryTransform**
  60. The history transform used in this session.
  61. ```csharp
  62. public IHistoryTransform HistoryTransform { get; set; }
  63. ```
  64. #### Property Value
  65. [IHistoryTransform](./llama.abstractions.ihistorytransform.md)<br>
  66. ### **InputTransformPipeline**
  67. The input transform pipeline used in this session.
  68. ```csharp
  69. public List<ITextTransform> InputTransformPipeline { get; set; }
  70. ```
  71. #### Property Value
  72. [List&lt;ITextTransform&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1)<br>
  73. ## Constructors
  74. ### **ChatSession(ILLamaExecutor)**
  75. Create a new chat session.
  76. ```csharp
  77. public ChatSession(ILLamaExecutor executor)
  78. ```
  79. #### Parameters
  80. `executor` [ILLamaExecutor](./llama.abstractions.illamaexecutor.md)<br>
  81. The executor for this session
  82. ### **ChatSession(ILLamaExecutor, ChatHistory)**
  83. Create a new chat session with a custom history.
  84. ```csharp
  85. public ChatSession(ILLamaExecutor executor, ChatHistory history)
  86. ```
  87. #### Parameters
  88. `executor` [ILLamaExecutor](./llama.abstractions.illamaexecutor.md)<br>
  89. `history` [ChatHistory](./llama.common.chathistory.md)<br>
  90. ## Methods
  91. ### **InitializeSessionFromHistoryAsync(ILLamaExecutor, ChatHistory)**
  92. Create a new chat session and preprocess history.
  93. ```csharp
  94. public static Task<ChatSession> InitializeSessionFromHistoryAsync(ILLamaExecutor executor, ChatHistory history)
  95. ```
  96. #### Parameters
  97. `executor` [ILLamaExecutor](./llama.abstractions.illamaexecutor.md)<br>
  98. The executor for this session
  99. `history` [ChatHistory](./llama.common.chathistory.md)<br>
  100. History for this session
  101. #### Returns
  102. [Task&lt;ChatSession&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1)<br>
  103. ### **WithHistoryTransform(IHistoryTransform)**
  104. Use a custom history transform.
  105. ```csharp
  106. public ChatSession WithHistoryTransform(IHistoryTransform transform)
  107. ```
  108. #### Parameters
  109. `transform` [IHistoryTransform](./llama.abstractions.ihistorytransform.md)<br>
  110. #### Returns
  111. [ChatSession](./llama.chatsession.md)<br>
  112. ### **AddInputTransform(ITextTransform)**
  113. Add a text transform to the input transform pipeline.
  114. ```csharp
  115. public ChatSession AddInputTransform(ITextTransform transform)
  116. ```
  117. #### Parameters
  118. `transform` [ITextTransform](./llama.abstractions.itexttransform.md)<br>
  119. #### Returns
  120. [ChatSession](./llama.chatsession.md)<br>
  121. ### **WithOutputTransform(ITextStreamTransform)**
  122. Use a custom output transform.
  123. ```csharp
  124. public ChatSession WithOutputTransform(ITextStreamTransform transform)
  125. ```
  126. #### Parameters
  127. `transform` [ITextStreamTransform](./llama.abstractions.itextstreamtransform.md)<br>
  128. #### Returns
  129. [ChatSession](./llama.chatsession.md)<br>
  130. ### **SaveSession(String)**
  131. Save a session from a directory.
  132. ```csharp
  133. public void SaveSession(string path)
  134. ```
  135. #### Parameters
  136. `path` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  137. #### Exceptions
  138. [ArgumentException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentexception)<br>
  139. ### **GetSessionState()**
  140. Get the session state.
  141. ```csharp
  142. public SessionState GetSessionState()
  143. ```
  144. #### Returns
  145. [SessionState](./llama.sessionstate.md)<br>
  146. SessionState object representing session state in-memory
  147. ### **LoadSession(SessionState, Boolean)**
  148. Load a session from a session state.
  149. ```csharp
  150. public void LoadSession(SessionState state, bool loadTransforms)
  151. ```
  152. #### Parameters
  153. `state` [SessionState](./llama.sessionstate.md)<br>
  154. `loadTransforms` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  155. If true loads transforms saved in the session state.
  156. #### Exceptions
  157. [ArgumentException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentexception)<br>
  158. ### **LoadSession(String, Boolean)**
  159. Load a session from a directory.
  160. ```csharp
  161. public void LoadSession(string path, bool loadTransforms)
  162. ```
  163. #### Parameters
  164. `path` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  165. `loadTransforms` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  166. If true loads transforms saved in the session state.
  167. #### Exceptions
  168. [ArgumentException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentexception)<br>
  169. ### **AddMessage(Message)**
  170. Add a message to the chat history.
  171. ```csharp
  172. public ChatSession AddMessage(Message message)
  173. ```
  174. #### Parameters
  175. `message` [Message](./llama.common.chathistory.message.md)<br>
  176. #### Returns
  177. [ChatSession](./llama.chatsession.md)<br>
  178. ### **AddSystemMessage(String)**
  179. Add a system message to the chat history.
  180. ```csharp
  181. public ChatSession AddSystemMessage(string content)
  182. ```
  183. #### Parameters
  184. `content` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  185. #### Returns
  186. [ChatSession](./llama.chatsession.md)<br>
  187. ### **AddAssistantMessage(String)**
  188. Add an assistant message to the chat history.
  189. ```csharp
  190. public ChatSession AddAssistantMessage(string content)
  191. ```
  192. #### Parameters
  193. `content` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  194. #### Returns
  195. [ChatSession](./llama.chatsession.md)<br>
  196. ### **AddUserMessage(String)**
  197. Add a user message to the chat history.
  198. ```csharp
  199. public ChatSession AddUserMessage(string content)
  200. ```
  201. #### Parameters
  202. `content` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  203. #### Returns
  204. [ChatSession](./llama.chatsession.md)<br>
  205. ### **RemoveLastMessage()**
  206. Remove the last message from the chat history.
  207. ```csharp
  208. public ChatSession RemoveLastMessage()
  209. ```
  210. #### Returns
  211. [ChatSession](./llama.chatsession.md)<br>
  212. ### **AddAndProcessMessage(Message)**
  213. Compute KV cache for the message and add it to the chat history.
  214. ```csharp
  215. public Task<ChatSession> AddAndProcessMessage(Message message)
  216. ```
  217. #### Parameters
  218. `message` [Message](./llama.common.chathistory.message.md)<br>
  219. #### Returns
  220. [Task&lt;ChatSession&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1)<br>
  221. ### **AddAndProcessSystemMessage(String)**
  222. Compute KV cache for the system message and add it to the chat history.
  223. ```csharp
  224. public Task<ChatSession> AddAndProcessSystemMessage(string content)
  225. ```
  226. #### Parameters
  227. `content` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  228. #### Returns
  229. [Task&lt;ChatSession&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1)<br>
  230. ### **AddAndProcessUserMessage(String)**
  231. Compute KV cache for the user message and add it to the chat history.
  232. ```csharp
  233. public Task<ChatSession> AddAndProcessUserMessage(string content)
  234. ```
  235. #### Parameters
  236. `content` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  237. #### Returns
  238. [Task&lt;ChatSession&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1)<br>
  239. ### **AddAndProcessAssistantMessage(String)**
  240. Compute KV cache for the assistant message and add it to the chat history.
  241. ```csharp
  242. public Task<ChatSession> AddAndProcessAssistantMessage(string content)
  243. ```
  244. #### Parameters
  245. `content` [String](https://docs.microsoft.com/en-us/dotnet/api/system.string)<br>
  246. #### Returns
  247. [Task&lt;ChatSession&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1)<br>
  248. ### **ReplaceUserMessage(Message, Message)**
  249. Replace a user message with a new message and remove all messages after the new message.
  250. This is useful when the user wants to edit a message. And regenerate the response.
  251. ```csharp
  252. public ChatSession ReplaceUserMessage(Message oldMessage, Message newMessage)
  253. ```
  254. #### Parameters
  255. `oldMessage` [Message](./llama.common.chathistory.message.md)<br>
  256. `newMessage` [Message](./llama.common.chathistory.message.md)<br>
  257. #### Returns
  258. [ChatSession](./llama.chatsession.md)<br>
  259. ### **ChatAsync(Message, Boolean, IInferenceParams, CancellationToken)**
  260. Chat with the model.
  261. ```csharp
  262. public IAsyncEnumerable<string> ChatAsync(Message message, bool applyInputTransformPipeline, IInferenceParams inferenceParams, CancellationToken cancellationToken)
  263. ```
  264. #### Parameters
  265. `message` [Message](./llama.common.chathistory.message.md)<br>
  266. `applyInputTransformPipeline` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  267. `inferenceParams` [IInferenceParams](./llama.abstractions.iinferenceparams.md)<br>
  268. `cancellationToken` [CancellationToken](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken)<br>
  269. #### Returns
  270. [IAsyncEnumerable&lt;String&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1)<br>
  271. #### Exceptions
  272. [ArgumentException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentexception)<br>
  273. ### **ChatAsync(Message, IInferenceParams, CancellationToken)**
  274. Chat with the model.
  275. ```csharp
  276. public IAsyncEnumerable<string> ChatAsync(Message message, IInferenceParams inferenceParams, CancellationToken cancellationToken)
  277. ```
  278. #### Parameters
  279. `message` [Message](./llama.common.chathistory.message.md)<br>
  280. `inferenceParams` [IInferenceParams](./llama.abstractions.iinferenceparams.md)<br>
  281. `cancellationToken` [CancellationToken](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken)<br>
  282. #### Returns
  283. [IAsyncEnumerable&lt;String&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1)<br>
  284. ### **ChatAsync(ChatHistory, Boolean, IInferenceParams, CancellationToken)**
  285. Chat with the model.
  286. ```csharp
  287. public IAsyncEnumerable<string> ChatAsync(ChatHistory history, bool applyInputTransformPipeline, IInferenceParams inferenceParams, CancellationToken cancellationToken)
  288. ```
  289. #### Parameters
  290. `history` [ChatHistory](./llama.common.chathistory.md)<br>
  291. `applyInputTransformPipeline` [Boolean](https://docs.microsoft.com/en-us/dotnet/api/system.boolean)<br>
  292. `inferenceParams` [IInferenceParams](./llama.abstractions.iinferenceparams.md)<br>
  293. `cancellationToken` [CancellationToken](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken)<br>
  294. #### Returns
  295. [IAsyncEnumerable&lt;String&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1)<br>
  296. #### Exceptions
  297. [ArgumentException](https://docs.microsoft.com/en-us/dotnet/api/system.argumentexception)<br>
  298. ### **ChatAsync(ChatHistory, IInferenceParams, CancellationToken)**
  299. Chat with the model.
  300. ```csharp
  301. public IAsyncEnumerable<string> ChatAsync(ChatHistory history, IInferenceParams inferenceParams, CancellationToken cancellationToken)
  302. ```
  303. #### Parameters
  304. `history` [ChatHistory](./llama.common.chathistory.md)<br>
  305. `inferenceParams` [IInferenceParams](./llama.abstractions.iinferenceparams.md)<br>
  306. `cancellationToken` [CancellationToken](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken)<br>
  307. #### Returns
  308. [IAsyncEnumerable&lt;String&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1)<br>
  309. ### **RegenerateAssistantMessageAsync(InferenceParams, CancellationToken)**
  310. Regenerate the last assistant message.
  311. ```csharp
  312. public IAsyncEnumerable<string> RegenerateAssistantMessageAsync(InferenceParams inferenceParams, CancellationToken cancellationToken)
  313. ```
  314. #### Parameters
  315. `inferenceParams` [InferenceParams](./llama.common.inferenceparams.md)<br>
  316. `cancellationToken` [CancellationToken](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken)<br>
  317. #### Returns
  318. [IAsyncEnumerable&lt;String&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1)<br>
  319. #### Exceptions
  320. [InvalidOperationException](https://docs.microsoft.com/en-us/dotnet/api/system.invalidoperationexception)<br>