Skip to content

add LOCAL_API_KEY environment variable to support api like ChatAnywhere #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ You can configure OpenCode using environment variables:
| `AZURE_OPENAI_API_KEY` | For Azure OpenAI models (optional when using Entra ID) |
| `AZURE_OPENAI_API_VERSION` | For Azure OpenAI models |
| `LOCAL_ENDPOINT` | For self-hosted models |
| `LOCAL_API_KEY` | For self-hosted models |
| `SHELL` | Default shell to use (if not specified in config) |

### Shell Configuration
Expand Down Expand Up @@ -628,9 +629,11 @@ This is useful for developers who want to experiment with custom models.

You can use a self-hosted model by setting the `LOCAL_ENDPOINT` environment variable.
This will cause OpenCode to load and use the models from the specified endpoint.
`LOCAL_API_KEY` can be empty, or set to your api key.

```bash
LOCAL_ENDPOINT=http://localhost:1235/v1
LOCAL_API_KEY=YOUR_API_KEY
```

### Configuring a self-hosted model
Expand All @@ -639,12 +642,11 @@ You can also configure a self-hosted model in the configuration file under the `

```json
{
"agents": {
"coder": {
"model": "local.granite-3.3-2b-instruct@q8_0",
"task": {
"model": "local.o3",
"maxTokens": 100000,
"reasoningEffort": "high"
}
}
}
```

Expand Down
26 changes: 22 additions & 4 deletions internal/llm/models/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const (
)

func init() {
if endpoint := os.Getenv("LOCAL_ENDPOINT"); endpoint != "" {
endpoint := os.Getenv("LOCAL_ENDPOINT")
apiKey := os.Getenv("LOCAL_API_KEY")

if endpoint != "" {
localEndpoint, err := url.Parse(endpoint)
if err != nil {
logging.Debug("Failed to parse local endpoint",
Expand All @@ -34,7 +37,7 @@ func init() {

load := func(url *url.URL, path string) []localModel {
url.Path = path
return listLocalModels(url.String())
return listLocalModels(url.String(), apiKey)
}

models := load(localEndpoint, lmStudioBetaModelsPath)
Expand Down Expand Up @@ -74,8 +77,23 @@ type localModel struct {
LoadedContextLength int64 `json:"loaded_context_length"`
}

func listLocalModels(modelsEndpoint string) []localModel {
res, err := http.Get(modelsEndpoint)

func listLocalModels(modelsEndpoint string, apiKey string) []localModel {
client := &http.Client{}
req, err := http.NewRequest("GET", modelsEndpoint, nil)
if err != nil {
logging.Debug("Failed to create request for local models",
"error", err,
"endpoint", modelsEndpoint,
)
return []localModel{}
}

if apiKey != "" {
req.Header.Add("Authorization", "Bearer "+apiKey)
}

res, err := client.Do(req)
if err != nil {
logging.Debug("Failed to list local models",
"error", err,
Expand Down