Chat _chat;
OllamaApiClient? _ollamaApiClient;
uri = new Uri(txtOllamaUri.Text);
_ollamaApiClient = new OllamaApiClient(uri);
_ollamaApiClient.SelectedModel = txtModel.Text;
_chat = new Chat(_ollamaApiClient);
await foreach (var answerToken in _chat.SendAsync(txtMessage.Text))
Console.Write(answerToken);
Example download from here. How to Build a Chat Agent for Ollama Using C#
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 27
This example is based on an implementation taken from
this website.
To begin, I create a new WinForms project in C# targeting .NET 10, and then install the
OllamaSharp NuGet package.
In this example, txtOllamaUri, txtModel, and txtMessage are WinForms controls (for example, TextBox instances). Replace them with the corresponding controls or values from your own UI: