Replace Qwen provider with LiteLLM and OpenRouter aggregators
Adding one bespoke provider package per vendor doesn't scale. LiteLLM proxy and OpenRouter both speak the OpenAI chat-completions wire format and each front dozens-to-hundreds of underlying models, so supporting them once gets that whole catalog instead of one more one-off integration — providers/qwen turns out to have been exactly that pattern hand-rolled a second time (its own copy of the OpenAI-compatible translation, just pointed at DashScope's base URL). providers/litellm and providers/openrouter are both thin constructors over the OpenAI provider's existing NewOpenAIProviderWithKind(apiKey, baseURL, "openai-compatible") — no new request/stream-translation code. OpenRouter defaults to its public endpoint when baseURL is empty; LiteLLM has none (self-hosted), so callers must supply one. Context-window detection (tokens.ContextWindow) already keys off the model ID's family rather than the provider prefix, specifically so a model exposed under a custom/proxy provider name still gets the right window — so litellm/openrouter models need zero changes there.
This commit is contained in:
1 parent
0ba1b4dbcc
commit
f624e4c7bd
11 files changed
+143
-431
No files matched your search
@@ -0,0 +1,23 @@
|
||||
// Package openrouter provides an llm.LLMProvider for OpenRouter, an
|
||||
// aggregator that fronts hundreds of underlying models behind one
|
||||
// OpenAI-compatible endpoint. Like providers/litellm, this adds no new
|
||||
// request/stream-translation code of its own — it's a thin constructor
|
||||
// around harness's existing OpenAI-compatible support.
|
||||
package openrouter
|
||||
|
||||
import "github.com/sausheong/harness/providers/openai"
|
||||
|
||||
// DefaultBaseURL is OpenRouter's public API endpoint, used when
|
||||
// NewOpenRouterProvider is called with an empty baseURL.
|
||||
const DefaultBaseURL = "https://openrouter.ai/api/v1"
|
||||
|
||||
// NewOpenRouterProvider returns an OpenAI-compatible provider pointed at
|
||||
// baseURL, or DefaultBaseURL when baseURL is empty. kind="openai-compatible"
|
||||
// suppresses reasoning_effort, since OpenRouter's underlying models don't
|
||||
// uniformly support it.
|
||||
func NewOpenRouterProvider(apiKey, baseURL string) *openai.OpenAIProvider {
|
||||
if baseURL == "" {
|
||||
baseURL = DefaultBaseURL
|
||||
}
|
||||
return openai.NewOpenAIProviderWithKind(apiKey, baseURL, "openai-compatible")
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package openrouter
|
||||
|
||||
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, "https://openrouter.ai/api/v1", DefaultBaseURL)
|
||||
}
|
||||
|
||||
// TestNewOpenRouterProvider_HitsGivenBaseURL confirms a custom baseURL
|
||||
// overrides the default and is actually what the provider talks to.
|
||||
func TestNewOpenRouterProvider_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 := NewOpenRouterProvider("test-key", 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")
|
||||
}
|
||||
|
||||
// TestNewOpenRouterProvider_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 TestNewOpenRouterProvider_EmptyBaseURLDoesNotPanic(t *testing.T) {
|
||||
p := NewOpenRouterProvider("test-key", "")
|
||||
require.NotNil(t, p)
|
||||
}
|
||||
Reference in new issue
Block a user