diff --git a/README.md b/README.md index 2b02c5a..af08759 100644 --- a/README.md +++ b/README.md @@ -87,9 +87,8 @@ a lot. The package is optimized for the use cases above, not these. hardened in 1.25. Older toolchains will fail at `go build`. - **An LLM API key**, depending on which provider(s) you wire in: `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, Google ADC for Gemini, or a - LiteLLM/OpenRouter key for those aggregators. For local models, point - `openai.NewOpenAIProviderWithKind` at an Ollama endpoint — no key - required. + LiteLLM/OpenRouter key for those aggregators. For local models, + `ollama.NewOllamaProvider` needs no key at all. - **Optional: an MCP server** for `tools/mcp` integrations (any binary that speaks the Model Context Protocol — `npx @modelcontextprotocol/server-filesystem`, etc.). @@ -123,7 +122,8 @@ github.com/sausheong/harness/ │ ├── openai/ # OpenAI / OpenAI-compatible / local Ollama │ ├── gemini/ # Google Gemini via google.golang.org/genai │ ├── litellm/ # Self-hosted LiteLLM proxy (OpenAI-compatible) -│ └── openrouter/ # OpenRouter aggregator (OpenAI-compatible) +│ ├── openrouter/ # OpenRouter aggregator (OpenAI-compatible) +│ └── ollama/ # Local Ollama server (OpenAI-compatible, no API key) └── tools/ # Batteries-included concrete tools (each importable separately) ├── file/ # read_file (with vision), write_file, edit_file ├── bash/ # bash (with ExecPolicy: deny | allowlist | full) @@ -315,12 +315,11 @@ Deliberately not in scope (numbers from the same conceptual list): `clientcredentials.Config.Client(ctx)`) as `HTTPClient` — auto-refresh comes for free. Call `Runtime.Close()` to release sessions. -- **Five providers, one interface.** Anthropic, OpenAI (and - OpenAI-compatible / local Ollama), Gemini, LiteLLM, and OpenRouter ship - built-in — LiteLLM and OpenRouter cost none of that ~300 LOC each, - since both are thin constructors over the existing OpenAI-compatible - path. Adding a genuinely new wire format is implementing - `llm.Provider` (~300 LOC). +- **Six providers, one interface.** Anthropic, OpenAI, Gemini, LiteLLM, + OpenRouter, and Ollama ship built-in — LiteLLM, OpenRouter, and Ollama + cost none of that ~300 LOC each, since all three are thin constructors + over the existing OpenAI-compatible path. Adding a genuinely new wire + format is implementing `llm.Provider` (~300 LOC). See [`developer.md`](./developer.md) for a step-by-step guide to building agents on top of Harness, including diagrams of the diff --git a/developer.md b/developer.md index 1db2c3f..445f1da 100644 --- a/developer.md +++ b/developer.md @@ -59,8 +59,8 @@ required pieces are inside `inputs`; everything in `deps` is optional; The four mandatory pieces: * **`llm.LLMProvider`** (in `RuntimeInputs.Provider`) — a streaming - chat client (Anthropic / OpenAI / Gemini / LiteLLM / OpenRouter, or - your own). + chat client (Anthropic / OpenAI / Gemini / LiteLLM / OpenRouter / + Ollama, or your own). * **`tool.Registry`** (in `RuntimeInputs.Tools`) — the tools the LLM may invoke. Each tool implements `tool.Tool`. * **`session.Session`** (in `RuntimeInputs.Session`) — append-only @@ -392,6 +392,7 @@ Each provider has its own constructor. All return an `llm.LLMProvider`. | Gemini | `gemini.NewGeminiProvider(ctx, apiKey)` | takes `context.Context` | | LiteLLM | `litellm.NewLiteLLMProvider(apiKey, baseURL)` | `baseURL` required (self-hosted) | | OpenRouter | `openrouter.NewOpenRouterProvider(apiKey, baseURL)` | `baseURL == ""` → OpenRouter's public endpoint | +| Ollama | `ollama.NewOllamaProvider(baseURL)` | `baseURL == ""` → `http://localhost:11434/v1`; no API key | The provider you pass on `RuntimeInputs.Provider` only needs to match the provider prefix in `AgentSpec.Model` — `runtime.BuildRuntime` parses diff --git a/providers/ollama/ollama.go b/providers/ollama/ollama.go new file mode 100644 index 0000000..122a987 --- /dev/null +++ b/providers/ollama/ollama.go @@ -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") +} diff --git a/providers/ollama/ollama_test.go b/providers/ollama/ollama_test.go new file mode 100644 index 0000000..0f48486 --- /dev/null +++ b/providers/ollama/ollama_test.go @@ -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) +}