Need a quick way to add a conversational AI experience to a .NET application? With Aspose.LLM, you can build a simple chat without explicitly creating or managing session IDs. SendMessageAsync automatically uses the current session, allowing successive messages to share conversation context.
This pattern is useful for one conversation at a time, including CLI tools, prototypes, scripts, and other simple applications.
When Simple Chat Is the Right Choice
The implicit-session pattern is a good fit for:
- A single question and answer.
- A short back-and-forth conversation.
- CLI applications and scripts.
- Prototypes.
- Applications where only one conversation needs to be active at a time.
If your application needs multiple independent conversations—for example, a chatbot serving multiple users—you should use Aspose.LLM’s explicit session APIs instead.
Send a Message Without Managing a Session
The core messaging pattern is deliberately small:
string reply = await api.SendMessageAsync("What is 2 + 2?");
Console.WriteLine(reply);
When you make subsequent calls on the same AsposeLLMApi instance, they belong to the same current conversation:
await api.SendMessageAsync("My name is Alice.");
string reply = await api.SendMessageAsync("What is my name?");
Console.WriteLine(reply);
You don’t create a session ID, pass one between calls, or explicitly append conversation history. The same AsposeLLMApi instance maintains the current conversation for you. The existing source documents this behavior as an implicit session maintained for the lifetime of the API instance. fileciteturn0file0L29-L34
The complete setup still requires a licensed API instance and a preset. The source example uses Aspose.LLM.License, Qwen25Preset, and AsposeLLMApi.Create(preset). fileciteturn0file0L87-L122
Build an Interactive Console Chat
With the basic messaging pattern understood, let’s turn it into a small interactive console application.
The important part is to create the API once, then reuse it for every message:
using Aspose.LLM;
using Aspose.LLM.Abstractions.Parameters.Presets;
internal class SimpleChatDemo
{
public static async Task Main()
{
var license = new Aspose.LLM.License();
license.SetLicense("Aspose.LLM.lic");
var preset = new Qwen25Preset();
using var api = AsposeLLMApi.Create(preset);
Console.WriteLine("Ask me anything. Type 'quit' to exit.");
while (true)
{
Console.Write("> ");
string? line = Console.ReadLine();
if (line == null || line.Equals("quit", StringComparison.OrdinalIgnoreCase))
break;
try
{
string reply = await api.SendMessageAsync(line);
Console.WriteLine(reply);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
}
This example keeps the chat simple:
- The API instance is created once.
- Every
SendMessageAsynccall uses the same implicit session. - Previous messages remain available to the model.
- The loop provides a natural back-and-forth conversation.
- Exceptions are handled per message so one failed request does not terminate the application.
The source example uses the same structure and notes that the single API instance preserves context across iterations. fileciteturn0file0L192-L245
To run it, create a .NET console project, replace the generated Program.cs, and run:
dotnet new console
dotnet run
Aspose.LLM provides a high‑level AsposeLLMApi class that abstracts model loading, tokenisation, and inference. To get started, install the NuGet package:
Install-Package Aspose.LLM
Customize a Single Message with a Different Preset
What if most of your conversation should use the default behavior, but one particular question needs a more deterministic response?
You can pass a different preset to SendMessageAsync for that call:
var strictPreset = new Qwen25Preset();
strictPreset.SamplerParameters.Temperature = 0.1f;
string reply = await api.SendMessageAsync(
"What is the square root of 144?",
preset: strictPreset);
Console.WriteLine(reply);
The override applies only to that call. The API’s default preset remains unchanged, and the underlying session continues to provide conversation context. fileciteturn0file0L126-L159
A low temperature such as 0.1f makes sampling more deterministic, which can be useful for questions where you want less variation in the generated response. fileciteturn0file0L155-L159
Protect Long-Running Chat Requests with Cancellation
Local model inference can take longer for large prompts or longer responses. You can prevent an individual request from running indefinitely by passing a CancellationToken.
For example, set a 30-second timeout:
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
try
{
string reply = await api.SendMessageAsync(
"Write a 500-word essay about migration patterns of the Arctic tern.",
cancellationToken: cts.Token);
Console.WriteLine(reply);
}
catch (OperationCanceledException)
{
Console.WriteLine("Generation timed out.");
}
The source documents the same pattern: create a CancellationTokenSource with a timeout, pass its token to SendMessageAsync, and catch OperationCanceledException when the request is canceled. fileciteturn0file0L161-L190
In an ASP.NET Core application, you can also pass the request’s cancellation token so generation can stop when the client disconnects.
Common Issues
| Problem | What to check |
|---|---|
Not licensed for this method | Make sure the license is loaded before calling SendMessageAsync. |
Multiple AsposeLLMApi instances | Reuse a single instance and dispose it correctly. |
| Slow first response | The initial model/engine download can take several minutes on a fresh machine. |
The source specifically documents the licensing error, the single-active-instance behavior, and the slower first model load. fileciteturn0file0L249-L255
Simple Chat vs. Explicit Sessions
The implicit-session approach is intentionally limited: it keeps the implementation small by maintaining one current conversation on an AsposeLLMApi instance.
| If you need… | Use |
|---|---|
| One conversation | SendMessageAsync |
| A short interactive chat | SendMessageAsync |
| A CLI or prototype | SendMessageAsync |
| Multiple independent conversations | Explicit sessions |
| Multiple users or concurrent chats | Explicit sessions |
| Persistent conversation state | Session persistence |
Use implicit messaging when one conversation is enough. Move to explicit session APIs when you need independent conversations, concurrent users, persistent state, or more control over session identity. The existing source recommends explicit sessions for concurrent conversations and persistent state. fileciteturn0file0L266-L274
For the explicit-session approach, see the Aspose.LLM documentation and multi-turn/session APIs.
Get a Free License
Aspose offers temporary licenses for evaluation. Grab one from the Aspose temporary license page and place the Aspose.LLM.lic file where your application can load it.
Free Additional Resources
Conclusion
Building a simple AI chat with Aspose.LLM does not require explicit session management. By creating one AsposeLLMApi instance and calling SendMessageAsync, you can build a conversational experience where the API maintains the current session automatically.
For simple applications, prototypes, scripts, and single-user chat experiences, this keeps the implementation minimal. When your application grows to support multiple conversations or requires explicit session control, you can move to the session-based APIs.
FAQs
Do I need to manage chat sessions manually with Aspose.LLM? No. The implicit‑session
SendMessageAsyncmethod automatically tracks conversation state for the lifetime of theAsposeLLMApiinstance.Can I change the model behavior for just one question? Yes. Supply a different preset (e.g., with lower temperature) as the
presetargument toSendMessageAsync. Only that call uses the new settings.How do I stop a long‑running generation? Create a
CancellationTokenSourcewith a timeout and pass its token toSendMessageAsync. CatchOperationCanceledExceptionto handle the timeout.What common errors should I watch for? Missing license, creating multiple API instances simultaneously, and a slow first model load are the most frequent issues. Follow the mitigation steps described above.
Is the sample code ready for production? The snippets are reproduced from Aspose’s official documentation and have not been sandbox‑tested. Test them in your own environment and add robust error handling before deploying.
