Simple .NET 9 chat client/api example that demonstrates how to use the Ollama
Chat API.
- Set up persistent volume local folders and update
docker-compose.yml
with folder locations first. docker-compose up -d
to pull and startOllama
andRedis
docker containers.docker exec -it ollama ollama pull llama3
to pull thellama3
model into theOllama
container (this will take a while).- If using VS Code
RUN AND DEBUG > Launch Debug > Run
- Switching between emulator and Ollama AI Chat Client is done in
appSettings.development.json
"ChatSettings": {
"UseEmulator": true,
"EmulatorResponseTime": 10000 // milliseconds
}
- I am using
Redis
to store chat history, and this has been 'abused' to manage the chat history. - The
ChatService
should ideally be implemented as a scalable background microservice. I am usingDotNet Channel
for background process management because this is a monolithic example. - Throttling/request scaling has not been implemented as I am using a non-distributed Chat API (
Ollama
in this case), so if you throw multiple requests atOllama
while it is still trying to figure things out, you will run into issues. Redis
has been configured (in code) to invalidate conversations after 15 mins.- Plenty more that can be done. This is not production code, but a simple example to demonstrate how to use the
Ollama
Chat API. ChatClientEmulator
is a simpleIChatClient
replacement so we can bypass requests toOllama
. It is there to ease development.
docker exec -it ollamma bash
to enter the container.`- You are not confined to
llama3
. You can pull other models as well, such as DeepSeek-R1, Phi-4, Mistral, Gemma 3, and more. Visithttps://ollama.ai/models
for more information.