createProviderFromConfig creates a provider from a ModelConfig using the runtime's configuration.
(ctx context.Context, cfg *latest.ModelConfig)
| 858 | |
| 859 | // createProviderFromConfig creates a provider from a ModelConfig using the runtime's configuration. |
| 860 | func (r *LocalRuntime) createProviderFromConfig(ctx context.Context, cfg *latest.ModelConfig) (provider.Provider, error) { |
| 861 | opts := []options.Opt{ |
| 862 | options.WithGateway(r.modelSwitcherCfg.ModelsGateway), |
| 863 | options.WithProviders(r.modelSwitcherCfg.Providers), |
| 864 | } |
| 865 | |
| 866 | // Use max_tokens from config if specified, otherwise look up from models.dev |
| 867 | if cfg.MaxTokens != nil { |
| 868 | opts = append(opts, options.WithMaxTokens(*cfg.MaxTokens)) |
| 869 | } else if r.modelsStore != nil { |
| 870 | m, err := r.modelsStore.GetModel(ctx, modelsdev.NewID(cfg.Provider, cfg.Model)) |
| 871 | if err == nil && m != nil { |
| 872 | opts = append(opts, options.WithMaxTokens(m.Limit.Output)) |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if store, ok := r.modelsStore.(*modelsdev.Store); ok && store != nil { |
| 877 | opts = append(opts, options.WithModelsDevStore(store)) |
| 878 | } |
| 879 | |
| 880 | registry := r.modelSwitcherCfg.ProviderRegistry |
| 881 | if registry == nil { |
| 882 | registry = r.providerRegistry |
| 883 | } |
| 884 | if registry == nil { |
| 885 | registry = provider.DefaultRegistry() |
| 886 | } |
| 887 | return registry.NewWithModels(ctx, |
| 888 | cfg, |
| 889 | r.modelSwitcherCfg.Models, |
| 890 | r.modelSwitcherCfg.EnvProvider, |
| 891 | opts..., |
| 892 | ) |
| 893 | } |
| 894 | |
| 895 | // WithModelSwitcherConfig sets the model switcher configuration for the runtime. |
| 896 | func WithModelSwitcherConfig(cfg *ModelSwitcherConfig) Opt { |
no test coverage detected