Skip to content

Ollama

The Ollama provider uses provider key ollama and the HPD-Agent.Providers.Ollama package. It does not require an API key. ModelName is a local Ollama model tag, such as llama3.2 or llama3:8b.

Start Ollama and make sure the model is available locally:

bash
ollama pull llama3.2

Use fluent setup:

csharp
using HPD.Agent;
using HPD.Agent.Providers.Ollama;

var agent = await new AgentBuilder()
    .WithOllama(model: "llama3.2")
    .BuildAsync();

var result = await agent.RunAsync("Write one sentence about local model setup.");
Console.WriteLine(result.Text);

Provider construction options stay small. Configure the Ollama server connection on the provider, and put model behavior on chat defaults or per-run chat config:

csharp
using HPD.Agent;
using HPD.Agent.Providers.Ollama;

var agent = await new AgentBuilder()
    .WithOllama(
        model: "qwen3:latest",
        endpoint: "http://localhost:11434",
        configure: ollama =>
        {
            ollama.TimeoutMs = 120_000;
        })
    .WithChatDefaults(chat =>
    {
        chat.Temperature = 0.2;
        chat.MaxOutputTokens = 1024;
        chat.Reasoning = new ReasoningOptions
        {
            Effort = ReasoningEffort.Medium
        };
    })
    .WithOllamaChatRequestOptions(ollama =>
    {
        ollama.KeepAlive = "10m";
        ollama.NumCtx = 8192;
        ollama.NumGpu = 99;
        ollama.UseMlock = true;
    })
    .BuildAsync();

The equivalent Clients.Chat shape is:

json
{
  "Clients": {
    "Chat": {
      "ProviderKey": "ollama",
      "ModelName": "llama3.2",
      "Endpoint": "http://localhost:11434",
      "ChatDefaults": {
        "Temperature": 0.2,
        "MaxOutputTokens": 1024,
        "Reasoning": {
          "Effort": "Medium"
        },
        "AdditionalProperties": {
          "keep_alive": "10m",
          "num_ctx": 8192,
          "num_gpu": 99,
          "use_mlock": true
        }
      },
      "ProviderOptions": {
        "TimeoutMs": 120000
      }
    }
  }
}

Endpoint resolution in fluent setup checks:

  1. the explicit endpoint argument
  2. OLLAMA_ENDPOINT
  3. OLLAMA_HOST
  4. http://localhost:11434

Runtime Options

Use ChatDefaults or AgentRunConfig.Chat for generic model behavior:

  • ModelId
  • Temperature
  • TopP
  • TopK
  • MaxOutputTokens
  • FrequencyPenalty
  • PresencePenalty
  • Seed
  • StopSequences
  • ResponseFormat
  • Reasoning

Use OllamaChatRequestOptions for Ollama-only request properties:

OptionOllama keyMeaning
KeepAlivekeep_aliveHow long Ollama keeps the model loaded after the request, such as 10m, 1h, or -1.
TemplatetemplatePrompt template override for this request. This overrides the template defined in the model file.
NumCtxnum_ctxContext window size used by the model.
NumGqanum_gqaGrouped-query-attention group count required by some model architectures.
NumGpunum_gpuNumber of layers to offload to GPU.
MainGpumain_gpuMain GPU index for small tensors when multiple GPUs are available.
NumBatchnum_batchMaximum batch size for prompt processing.
NumThreadnum_threadNumber of CPU threads used for generation. Ollama auto-detects when unset.
NumKeepnum_keepNumber of tokens to keep from the initial prompt. Ollama supports -1 to keep all.
MiroStatmirostatEnables Mirostat sampling: 0 disabled, 1 Mirostat, 2 Mirostat 2.0.
MiroStatEtamirostat_etaMirostat learning rate. Higher values react more quickly to feedback.
MiroStatTaumirostat_tauMirostat target entropy. Lower values are more focused; higher values add variety.
RepeatLastNrepeat_last_nNumber of prior tokens considered for repetition penalties. Ollama supports 0 to disable and -1 for the full context.
RepeatPenaltyrepeat_penaltyRepetition penalty strength. Higher values penalize repetition more strongly.
MinPmin_pMinimum probability threshold relative to the most likely token.
TypicalPtypical_pLocally typical sampling value. Lower values make output more conservative.
TfsZtfs_zTail-free sampling value. A value near 1 disables the setting.
PenalizeNewlinepenalize_newlineWhether newline tokens are penalized during repetition control.
UseMmapuse_mmapWhether model weights are memory-mapped.
UseMlockuse_mlockWhether to lock model memory to avoid swapping.
LowVramlow_vramEnables low-VRAM mode.
F16kvf16_kvEnables f16 key/value cache.
LogitsAlllogits_allReturns logits for all tokens, not only the last token.
VocabOnlyvocab_onlyLoads only the vocabulary, not model weights.
NumanumaEnables NUMA support.

Sampling values that already exist on ChatRunConfig, such as Temperature, TopP, TopK, MaxOutputTokens, Seed, FrequencyPenalty, PresencePenalty, and StopSequences, stay on ChatDefaults or AgentRunConfig.Chat.

Caveats

Validation checks endpoint URI shape. A successful build still requires a reachable Ollama server and a pulled model.

OLLAMA_ENDPOINT and OLLAMA_HOST are used by fluent .WithOllama(...). JSON/config setup should include Endpoint when the server is not at http://localhost:11434.

Built for production .NET agent applications.