Visitar URL original
Ollama integration by alexthotse · Pull Request #346 · opencode-ai/opencode · GitHub
Skip to content
This repository was archived by the owner on Sep 18, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: Implement several new features
This commit introduces several new features and improvements to the codebase.

- Reordered the provider priority to prioritize Ollama, OpenRouter, and Gemini.
- Added a new `config` command to the CLI that allows you to set your API keys and other settings.
- Added support for Hugging Face, Replicate, and Cohere as new LLM providers.
- Added a testing framework for the providers, including a mock provider.
- Improved the error handling in the application by creating a new `errors.go` file with custom error types.
  • Loading branch information
google-labs-jules[bot] committed Jul 27, 2025
commit 61d098b70ad82d47a8605ce81e476c63063b70f0
34 changes: 34 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"fmt"

"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/llm/models"
"github.com/spf13/cobra"
)

var configCmd = &cobra.Command{
Use: "config",
Short: "Configure opencode CLI",
RunE: func(cmd *cobra.Command, args []string) error {
return configure(cmd, args)
},
}

func configure(cmd *cobra.Command, args []string) error {
key, _ := cmd.Flags().GetString("key")
value, _ := cmd.Flags().GetString("value")

if key == "" || value == "" {
return fmt.Errorf("key and value are required")
}

return config.UpdateProviderAPIKey(models.ModelProvider(key), value)
}

func init() {
configCmd.Flags().StringP("key", "k", "", "Configuration key")
configCmd.Flags().StringP("value", "v", "", "Configuration value")
rootCmd.AddCommand(configCmd)
}
21 changes: 21 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"testing"

"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/llm/models"
"github.com/stretchr/testify/require"
)

func TestConfigCmd(t *testing.T) {
_, err := config.Load(".", false)
require.NoError(t, err)

rootCmd.SetArgs([]string{"config", "--key", "openai", "--value", "test-key"})
err = rootCmd.Execute()
require.NoError(t, err)

cfg := config.Get()
require.Equal(t, "test-key", cfg.Providers[models.ProviderOpenAI].APIKey)
}
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/opencode-ai/opencode/internal/app"
"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/db"
customerrors "github.com/opencode-ai/opencode/internal/errors"
"github.com/opencode-ai/opencode/internal/format"
"github.com/opencode-ai/opencode/internal/llm/agent"
"github.com/opencode-ai/opencode/internal/logging"
Expand Down Expand Up @@ -284,6 +285,13 @@ func setupSubscriptions(app *app.App, parentCtx context.Context) (chan tea.Msg,
func Execute() {
err := rootCmd.Execute()
if err != nil {
// If the error is a configuration error, we want to print a more helpful message
if e, ok := err.(*customerrors.Error); ok {
if e.Code == customerrors.ErrNotFound {
fmt.Println("No valid provider available. Please configure a provider using 'opencode config'")
os.Exit(1)
}
}
os.Exit(1)
}
}
Expand Down
36 changes: 33 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"strings"

customerrors "github.com/opencode-ai/opencode/internal/errors"
"github.com/opencode-ai/opencode/internal/llm/models"
"github.com/opencode-ai/opencode/internal/logging"
"github.com/spf13/viper"
Expand Down Expand Up @@ -488,7 +489,7 @@ func validateAgent(cfg *Config, name AgentName, agent Agent) error {
if setDefaultModelForAgent(name) {
logging.Info("set default model for agent", "agent", name, "model", cfg.Agents[name].Model)
} else {
return fmt.Errorf("no valid provider available for agent %s", name)
return customerrors.Newf(customerrors.ErrNotFound, "no valid provider available for agent %s", name)
}
return nil
}
Expand All @@ -510,7 +511,7 @@ func validateAgent(cfg *Config, name AgentName, agent Agent) error {
if setDefaultModelForAgent(name) {
logging.Info("set default model for agent", "agent", name, "model", cfg.Agents[name].Model)
} else {
return fmt.Errorf("no valid provider available for agent %s", name)
return customerrors.Newf(customerrors.ErrNotFound, "no valid provider available for agent %s", name)
}
} else {
// Add provider with API key from environment
Expand All @@ -530,7 +531,7 @@ func validateAgent(cfg *Config, name AgentName, agent Agent) error {
if setDefaultModelForAgent(name) {
logging.Info("set default model for agent", "agent", name, "model", cfg.Agents[name].Model)
} else {
return fmt.Errorf("no valid provider available for agent %s", name)
return customerrors.Newf(customerrors.ErrNotFound, "no valid provider available for agent %s", name)
}
}

Expand Down Expand Up @@ -930,6 +931,35 @@ func UpdateTheme(themeName string) error {
})
}

func UpdateProviderAPIKey(provider models.ModelProvider, apiKey string) error {
if cfg == nil {
return fmt.Errorf("config not loaded")
}

if cfg.Providers == nil {
cfg.Providers = make(map[models.ModelProvider]Provider)
}

providerCfg, ok := cfg.Providers[provider]
if !ok {
providerCfg = Provider{}
}
providerCfg.APIKey = apiKey
cfg.Providers[provider] = providerCfg

return updateCfgFile(func(config *Config) {
if config.Providers == nil {
config.Providers = make(map[models.ModelProvider]Provider)
}
providerCfg, ok := config.Providers[provider]
if !ok {
providerCfg = Provider{}
}
providerCfg.APIKey = apiKey
config.Providers[provider] = providerCfg
})
}

// Tries to load Github token from all possible locations
func LoadGitHubToken() (string, error) {
// First check environment variable
Expand Down
46 changes: 46 additions & 0 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package errors

import "fmt"

// Error is a custom error type that contains a code and a message.
type Error struct {
Code int
Message string
}

// Error returns the error message.
func (e *Error) Error() string {
return e.Message
}

// New creates a new error.
func New(code int, message string) *Error {
return &Error{
Code: code,
Message: message,
}
}

// Newf creates a new error with a formatted message.
func Newf(code int, format string, a ...interface{}) *Error {
return &Error{
Code: code,
Message: fmt.Sprintf(format, a...),
}
}

// Error codes
const (
// ErrUnknown is an unknown error.
ErrUnknown = iota
// ErrNotFound is a not found error.
ErrNotFound
// ErrForbidden is a forbidden error.
ErrForbidden
// ErrBadRequest is a bad request error.
ErrBadRequest
// ErrUnauthorized is an unauthorized error.
ErrUnauthorized
// ErrInternal is an internal error.
ErrInternal
)
17 changes: 17 additions & 0 deletions internal/errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package errors

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestErrors(t *testing.T) {
err := New(ErrNotFound, "not found")
require.Equal(t, "not found", err.Error())
require.Equal(t, ErrNotFound, err.Code)

err = Newf(ErrBadRequest, "bad request %d", 400)
require.Equal(t, "bad request 400", err.Error())
require.Equal(t, ErrBadRequest, err.Code)
}
26 changes: 26 additions & 0 deletions internal/llm/models/cohere.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package models

const (
ProviderCohere ModelProvider = "cohere"
)

const (
CohereCommandRPlus ModelID = "cohere-command-r-plus"
)

var CohereModels = map[ModelID]Model{
CohereCommandRPlus: {
ID: CohereCommandRPlus,
Name: "Cohere: Command R+",
Provider: ProviderCohere,
APIModel: "command-r-plus",
CostPer1MIn: 0,
CostPer1MOut: 0,
CostPer1MInCached: 0,
CostPer1MOutCached: 0,
ContextWindow: 128000,
DefaultMaxTokens: 4096,
CanReason: true,
SupportsAttachments: true,
},
}
26 changes: 26 additions & 0 deletions internal/llm/models/huggingface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package models

const (
ProviderHuggingFace ModelProvider = "huggingface"
)

const (
HuggingFaceMistral7BInstruct ModelID = "huggingface-mistral-7b-instruct"
)

var HuggingFaceModels = map[ModelID]Model{
HuggingFaceMistral7BInstruct: {
ID: HuggingFaceMistral7BInstruct,
Name: "Hugging Face: Mistral 7B Instruct",
Provider: ProviderHuggingFace,
APIModel: "mistralai/Mistral-7B-Instruct-v0.1",
CostPer1MIn: 0,
CostPer1MOut: 0,
CostPer1MInCached: 0,
CostPer1MOutCached: 0,
ContextWindow: 4096,
DefaultMaxTokens: 2048,
CanReason: true,
SupportsAttachments: true,
},
}
26 changes: 16 additions & 10 deletions internal/llm/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ const (

// Providers in order of popularity
var ProviderPopularity = map[ModelProvider]int{
ProviderCopilot: 1,
ProviderAnthropic: 2,
ProviderOpenAI: 3,
ProviderGemini: 4,
ProviderGROQ: 5,
ProviderOpenRouter: 6,
ProviderBedrock: 7,
ProviderAzure: 8,
ProviderVertexAI: 9,
ProviderOllama: 10,
ProviderOllama: 1,
ProviderOpenRouter: 2,
ProviderGemini: 3,
ProviderCopilot: 4,
ProviderAnthropic: 5,
ProviderOpenAI: 6,
ProviderGROQ: 7,
ProviderBedrock: 8,
ProviderAzure: 9,
ProviderVertexAI: 10,
ProviderHuggingFace: 11,
ProviderReplicate: 12,
ProviderCohere: 13,
}

var SupportedModels = map[ModelID]Model{
Expand Down Expand Up @@ -97,4 +100,7 @@ func init() {
maps.Copy(SupportedModels, VertexAIGeminiModels)
maps.Copy(SupportedModels, CopilotModels)
maps.Copy(SupportedModels, OllamaModels)
maps.Copy(SupportedModels, HuggingFaceModels)
maps.Copy(SupportedModels, ReplicateModels)
maps.Copy(SupportedModels, CohereModels)
}
26 changes: 26 additions & 0 deletions internal/llm/models/replicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package models

const (
ProviderReplicate ModelProvider = "replicate"
)

const (
ReplicateLlama270BChat ModelID = "replicate-llama-2-70b-chat"
)

var ReplicateModels = map[ModelID]Model{
ReplicateLlama270BChat: {
ID: ReplicateLlama270BChat,
Name: "Replicate: Llama 2 70B Chat",
Provider: ProviderReplicate,
APIModel: "meta/llama-2-70b-chat:2796ee9483c3fd7aa2e171d38f4ca12251a30609463dcfd4cd76703f22e96e2e",
CostPer1MIn: 0,
CostPer1MOut: 0,
CostPer1MInCached: 0,
CostPer1MOutCached: 0,
ContextWindow: 4096,
DefaultMaxTokens: 2048,
CanReason: true,
SupportsAttachments: true,
},
}
18 changes: 18 additions & 0 deletions internal/llm/provider/cohere.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package provider

import ()

type CohereClient ProviderClient

func newCohereClient(opts providerClientOptions) OpenAIClient {
opts.openaiOptions = append(opts.openaiOptions,
WithOpenAIBaseURL("https://api.cohere.ai/v1"),
)
return newOpenAIClient(opts)
}

func WithCohereOptions(cohereOptions ...OpenAIOption) ProviderClientOption {
return func(options *providerClientOptions) {
options.openaiOptions = cohereOptions
}
}
26 changes: 26 additions & 0 deletions internal/llm/provider/cohere_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package provider

import (
"context"
"os"
"testing"

"github.com/opencode-ai/opencode/internal/llm/models"
"github.com/stretchr/testify/require"
)

func TestCohereProvider(t *testing.T) {
if os.Getenv("COHERE_API_KEY") == "" {
t.Skip("COHERE_API_KEY not set, skipping test")
}

provider, err := NewProvider(
models.ProviderCohere,
WithModel(models.SupportedModels[models.CohereCommandRPlus]),
WithAPIKey(os.Getenv("COHERE_API_KEY")),
)
require.NoError(t, err)

_, err = provider.SendMessages(context.Background(), nil, nil)
require.NoError(t, err)
}
18 changes: 18 additions & 0 deletions internal/llm/provider/huggingface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package provider

import ()

type HuggingFaceClient ProviderClient

func newHuggingFaceClient(opts providerClientOptions) OpenAIClient {
opts.openaiOptions = append(opts.openaiOptions,
WithOpenAIBaseURL("https://api-inference.huggingface.co/v1"),
)
return newOpenAIClient(opts)
}

func WithHuggingFaceOptions(huggingfaceOptions ...OpenAIOption) ProviderClientOption {
return func(options *providerClientOptions) {
options.openaiOptions = huggingfaceOptions
}
}
Loading