52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
|
|
package agent
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
|
||
|
|
"github.com/VictorVargas/rony-llm-agent/pkg/llm"
|
||
|
|
"github.com/VictorVargas/rony-llm-agent/pkg/llm/providers/anthropic"
|
||
|
|
"github.com/VictorVargas/rony-llm-agent/pkg/llm/providers/llamacpp"
|
||
|
|
"github.com/VictorVargas/rony-llm-agent/pkg/llm/providers/openai"
|
||
|
|
|
||
|
|
"github.com/VictorVargas/rony-chat-bot/internal/config"
|
||
|
|
)
|
||
|
|
|
||
|
|
// NewClient constructs the upstream LLMClient for a given provider config.
|
||
|
|
// "ollama" is handled via the openai-compat adapter: Ollama exposes
|
||
|
|
// /v1/chat/completions on its own port, so the provider list stays small.
|
||
|
|
func NewClient(p config.Provider) (llm.LLMClient, error) {
|
||
|
|
switch p.Type {
|
||
|
|
case "llamacpp":
|
||
|
|
return llamacpp.New(llamacpp.Config{
|
||
|
|
BaseURL: defaultIfEmpty(p.Endpoint, "http://localhost:8080/v1"),
|
||
|
|
Model: p.Model,
|
||
|
|
ContextWindow: p.ContextSize,
|
||
|
|
MaxTokens: p.MaxTokens,
|
||
|
|
Temperature: p.Temperature,
|
||
|
|
})
|
||
|
|
case "ollama", "openai":
|
||
|
|
return openai.New(openai.Config{
|
||
|
|
BaseURL: defaultIfEmpty(p.Endpoint, "http://localhost:11434/v1"),
|
||
|
|
Model: p.Model,
|
||
|
|
})
|
||
|
|
case "anthropic":
|
||
|
|
apiKey := ""
|
||
|
|
if p.APIKeyEnv != "" {
|
||
|
|
apiKey = os.Getenv(p.APIKeyEnv)
|
||
|
|
}
|
||
|
|
return anthropic.New(anthropic.Config{
|
||
|
|
APIKey: apiKey,
|
||
|
|
Model: p.Model,
|
||
|
|
})
|
||
|
|
default:
|
||
|
|
return nil, fmt.Errorf("unknown provider type %q (supported: llamacpp, ollama, openai, anthropic)", p.Type)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func defaultIfEmpty(s, def string) string {
|
||
|
|
if s == "" {
|
||
|
|
return def
|
||
|
|
}
|
||
|
|
return s
|
||
|
|
}
|