refactor: consolidate OpenAI-compatible provider chat into Nous.Provider macro#65
Merged
Merged
Conversation
…der macro
Six providers (vLLM, SGLang, LM Studio, Mistral, OpenAICompatible, Custom)
carried near-identical chat/2 + chat_stream/2 implementations — the local
trio (vLLM/SGLang/LM Studio) were byte-for-byte structurally identical
except for an id, base URL, env prefix, and display name.
Extend `use Nous.Provider` with an opt-in `chat:` config that injects the
shared chat/2, chat_stream/2, base-URL resolver, and header helpers. The
duplication reduced to three variation axes:
* base_url: :plain | :local | :required
* headers: :bearer | :bearer_org
* timeout: / stream_timeout: (replaces duplicated module-attr constants)
Each provider collapses to a moduledoc + a single `use` block.
Behavior-preserving:
* Error message strings reproduced exactly (generated from id/env where
they were provider-specific).
* Local trio now forwards finch_name in chat_stream, matching the
convention already used by the other 7 providers (they were the
outliers that dropped it).
Verified: provider tests 197 passed, full suite 1896 passed,
credo --strict 0 issues, compile --warnings-as-errors clean, format clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Six providers — vLLM, SGLang, LM Studio, Mistral, OpenAICompatible, Custom —
carried near-identical
chat/2+chat_stream/2implementations. The local trio(vLLM/SGLang/LM Studio) were byte-for-byte structurally identical except for an
id, base URL, env prefix, and display name (a clear three-strikes duplication).This extends
use Nous.Providerwith an opt-inchat:config that injects theshared
chat/2,chat_stream/2, base-URL resolver, and header helpers. Theduplication reduces to three variation axes:
base_url::plain·:local·:requiredheaders::bearer·:bearer_orgopenai-organizationtimeout:/stream_timeout:@default_timeout/@streaming_timeoutEach provider collapses to a moduledoc + a single
useblock. Adding a newOpenAI-compatible provider is now just the
chat:option.Changes
Nous.Providermacro: newchat_ast/1injects overridablechat/2,chat_stream/2,chat_resolve_base_url/1, andchat_build_headers/2.duplicated bodies, replaced with
chat:config.7 files changed, 233 insertions(+), 329 deletions(-)— ~250 lines ofcopy-pasted logic replaced by ~190 lines of reusable macro.
Behavior preservation
This is a behavior-preserving refactor:
id/env wherethey were provider-specific — e.g. Custom's SSRF and "requires a base_url"
messages).
finch_nameinchat_stream, matching the convention already used by the 7 other providers.Previously they silently dropped it — they were the outliers.
Verification
mix credo --strict: 0 issuesmix compile --warnings-as-errors: cleanmix format --check-formatted: clean