Skip to content

feat(opencode): dynamic model discovery for local providers (LM Studio, llama.cpp, etc.)#17670

Open
dmitryryabkov wants to merge 2 commits intoanomalyco:devfrom
dmitryryabkov:dynamic-model-discovery
Open

feat(opencode): dynamic model discovery for local providers (LM Studio, llama.cpp, etc.)#17670
dmitryryabkov wants to merge 2 commits intoanomalyco:devfrom
dmitryryabkov:dynamic-model-discovery

Conversation

@dmitryryabkov
Copy link

Issue for this PR

Closes #6231

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds an option for dynamic model list population for OpenAI-compatible providers supporting /model API (as opposed to manually typing them in opencode.json):

  • There's a new option which can be added to a "provider" in opencode.json: "dynamicModelList": true
  • If the option is not set, the old behavior takes precedence (backward compatibility)
  • When set to true issues an API request and retrieves the list of the models supported by the provider
  • If the list is returned successfully, populates the models from the response
  • In the explicit list of models is provided, it takes precedence even if the "dynamicModelList": true flag is set

This change makes working with local AI inference engines much nicer, because it eliminates the need to update opencode.json every time a new model is added, or if the engine is started with the specific model as a parameter. The model list is now completely driven by the inference engine itself.

Example configuration (opencode.json):

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "llama.cpp": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "llama.cpp (local)",
      "options": {
        "baseURL": "http://127.0.0.1:8080/v1"
      },
      "dynamicModelList": true
    },
    "lm_studio": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "LM Studio (local)",
      "options": {
        "baseURL": "http://127.0.0.1:1234/v1",
      },
      "dynamicModelList": true
    }
  }
}

will result in the following model list in OpenCode, for LM Studio with "Just-in-Time Model Loading" enabled:

image

or llama.cpp started without specifying a model:

image

The selected model will be loaded by the inference engine once the first /chat/completion request is received.

If, however, the inference engines only serving specific models, only those models will be returned and available in OpenCode model selector. For LM Studio with "Just-in-Time Model Loading" disabled:

image

or llama.cpp started with a specific model:

image

I'm aware that there's a number of opened PRs on this issue already (i.e. #15732, #13234), however I believe this is the most robust implementation because:

  • it doesn't hard-code any specific provider name
  • it maintains the existing behavior by default (if the new flag is not used)
  • it works with any provider which supports /model endpoint (not even just a local one, i.e. Ollama Cloud also works just fine)
  • adds a bunch of tests

🤖 Developed with some help from OpenCode running against a local model hosted in llama.cpp!

How did you verify your code works?

Run OpenCode with models hosted in LM Studio and llama.cpp. Tried various scenarios:

  • with a single model loaded vs dynamic model loading (see the screenshots above)
  • with/without auth
  • with the inference engine down

Also tested it agains Ollama Cloud

Screenshots / recordings

See above

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate PRs Found

  1. PR feat(opencode): add dynamic configuration and context discovery for LM Studio #15732: "feat(opencode): add dynamic configuration and context discovery for LM Studio"

  2. PR feat(opencode): add LiteLLM provider with auto model discovery #14468: "feat(opencode): add LiteLLM provider with auto model discovery"

  3. PR feat(opencode): add auto loading models for litellm providers #13896: "feat(opencode): add auto loading models for litellm providers"

The current PR (#17670) explicitly acknowledges PRs #15732 and #13234 as prior attempts but argues that this implementation is more robust because it's provider-agnostic, maintains backward compatibility, and works with any OpenAI-compatible provider supporting the /models endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-discover models from OpenAI-compatible provider endpoints

1 participant