Add an ollama provider for local models

Thin constructor over the existing OpenAI-compatible path, same shape
as litellm/openrouter: NewOllamaProvider(baseURL) defaults to Ollama's
local endpoint (http://localhost:11434/v1) and needs no API key, since
Ollama doesn't authenticate requests. Also covers other local servers
that share Ollama's OpenAI-compatible endpoint shape (LM Studio,
llama.cpp's server, vLLM).
This commit is contained in:
sausheong committed 2026-09-07 11:57:56 +08:00
1 parent 897643db18
commit 3b5aadb31f
4 files changed
+84 -12

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
// Package ollama provides an llm.LLMProvider for a local Ollama server
// (or any other local server sharing Ollama's OpenAI-compatible endpoint
// shape — LM Studio, llama.cpp's server, vLLM). Like providers/litellm
// and providers/openrouter, this adds no new request/stream-translation
// code of its own — it's a thin constructor around harness's existing
// OpenAI-compatible support.
package ollama
import "github.com/sausheong/harness/providers/openai"
// DefaultBaseURL is Ollama's local OpenAI-compatible endpoint, used when
// NewOllamaProvider is called with an empty baseURL.
const DefaultBaseURL = "http://localhost:11434/v1"
// NewOllamaProvider returns an OpenAI-compatible provider pointed at
// baseURL, or DefaultBaseURL when baseURL is empty. No API key is
// needed — Ollama doesn't authenticate requests. kind="local" suppresses
// reasoning_effort, since locally-served models don't uniformly support
// it, and keeps retryable-error classification from treating a down
// local server the way it would a hosted API outage (see llm/retry.go).
func NewOllamaProvider(baseURL string) *openai.OpenAIProvider {
if baseURL == "" {
baseURL = DefaultBaseURL
}
return openai.NewOpenAIProviderWithKind("", baseURL, "local")
}
+46
View File
@@ -0,0 +1,46 @@
package ollama
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"github.com/sausheong/harness/llm"
)
func TestDefaultBaseURL(t *testing.T) {
require.Equal(t, "http://localhost:11434/v1", DefaultBaseURL)
}
// TestNewOllamaProvider_HitsGivenBaseURL confirms a custom baseURL
// overrides the default and is actually what the provider talks to.
func TestNewOllamaProvider_HitsGivenBaseURL(t *testing.T) {
var hit bool
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hit = true
w.Header().Set("Content-Type", "text/event-stream")
_, _ = w.Write([]byte("data: [DONE]\n\n"))
}))
t.Cleanup(srv.Close)
p := NewOllamaProvider(srv.URL + "/v1")
stream, err := p.ChatStream(context.Background(), llm.ChatRequest{SystemPrompt: "hi"})
require.NoError(t, err)
for range stream {
}
require.True(t, hit, "provider did not hit the configured baseURL")
}
// TestNewOllamaProvider_EmptyBaseURLDoesNotPanic confirms the empty ->
// DefaultBaseURL fallback path constructs a usable provider (the actual
// outbound host isn't inspectable from outside the openai package —
// OpenAIProvider's client config is unexported — so this checks the
// fallback doesn't panic or return nil, and DefaultBaseURL's own value
// is asserted separately above).
func TestNewOllamaProvider_EmptyBaseURLDoesNotPanic(t *testing.T) {
p := NewOllamaProvider("")
require.NotNil(t, p)
}