-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolyllm.go
128 lines (111 loc) · 3.02 KB
/
polyllm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package polyllm
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"log/slog"
"os"
mcpclient "github.com/mark3labs/mcp-go/client"
"github.com/recally-io/polyllm/llms"
"github.com/recally-io/polyllm/mcps"
)
//go:embed builtin-llm-providers.json
var builtInLLMProvidersBytes []byte
var builtInLLMProviders []llms.Provider
type PolyLLM struct {
Config
llms []LLM
modelLLMMappings map[string]LLM
mcpClientMappings map[string]mcpclient.MCPClient
}
type Config struct {
LLMProvides []llms.Provider `json:"llms"`
MCPProviders map[string]mcps.Provider `json:"mcps"`
}
func init() {
if err := json.Unmarshal(builtInLLMProvidersBytes, &builtInLLMProviders); err != nil {
slog.Error("failed to unmarshal built-in llms", "err", err)
}
}
func LoadConfig(configPath string) (Config, error) {
var cfg Config
configFile, err := os.ReadFile(configPath)
if err != nil {
return Config{}, fmt.Errorf("failed to read config file: %w", err)
}
if err := json.Unmarshal(configFile, &cfg); err != nil {
return Config{}, fmt.Errorf("failed to load config file: %w", err)
}
return cfg, nil
}
type Option func(*Config)
func WithMCPProviders(mcpServers map[string]mcps.Provider) Option {
return func(c *Config) {
c.MCPProviders = mcpServers
}
}
func WithLLMProviders(providers ...llms.Provider) Option {
return func(c *Config) {
c.LLMProvides = append(c.LLMProvides, providers...)
}
}
func NewFromConfig(cfg Config) *PolyLLM {
cfg.LLMProvides = append(builtInLLMProviders, cfg.LLMProvides...)
p := &PolyLLM{
llms: make([]LLM, 0),
modelLLMMappings: make(map[string]LLM),
mcpClientMappings: make(map[string]mcpclient.MCPClient),
Config: cfg,
}
// add llm providers
p.addLLMProviders(p.Config.LLMProvides...)
// add mcp providers
p.addMCPProviders(p.Config.MCPProviders)
return p
}
func New(opts ...Option) *PolyLLM {
cfg := Config{}
for _, opt := range opts {
opt(&cfg)
}
return NewFromConfig(cfg)
}
func (p *PolyLLM) addLLMProviders(providers ...llms.Provider) {
for _, provider := range providers {
provider.Load()
if provider.APIKey != "" {
llm, err := NewLLM(&provider)
if err != nil {
slog.Error("failed to create llm client", "provider", provider.Name, "err", err)
continue
}
p.llms = append(p.llms, llm)
models, err := p.loadProviderModelsWithCache(context.Background(), llm)
if err != nil {
slog.Error("failed to load llm models", "provider", provider.Name, "err", err)
continue
}
for _, model := range models {
p.modelLLMMappings[model.ID] = llm
}
}
}
}
func (p *PolyLLM) addMCPProviders(providers map[string]mcps.Provider) {
// start MCP clients
if len(providers) > 0 {
mcpClients, err := mcps.CreateMCPClients(providers)
if err != nil {
slog.Error("failed to create MCP clients", "err", err)
}
p.mcpClientMappings = mcpClients
}
}
func (p *PolyLLM) GetLLMByModel(model string) (LLM, error) {
llm, ok := p.modelLLMMappings[model]
if !ok {
return nil, fmt.Errorf("model %s not found", model)
}
return llm, nil
}