| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /*
- Copyright 2026 LocoStack.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package litellm
- import "github.com/LocoStack/loco-operator/api/v1alpha1"
- type LiteLLMConfig struct {
- ModelList []modelEntry `yaml:"model_list"`
- MCPServers map[string]mcpServerEntry `yaml:"mcp_servers,omitempty"`
- LiteLLMSettings *liteLLMSettings `yaml:"litellm_settings,omitempty"`
- GeneralSettings *liteLLMGeneralSettings `yaml:"general_settings,omitempty"`
- EnvVars map[string]string `yaml:"environment_variables,omitempty"`
- }
- type modelEntry struct {
- ModelName string `yaml:"model_name"`
- LiteLLMParams map[string]any `yaml:"litellm_params"`
- }
- type mcpServerEntry struct {
- // url is the HTTP/SSE endpoint for sse and http transports.
- URL string `yaml:"url,omitempty"`
- // transport selects the MCP transport: sse (default), http, or stdio.
- Transport string `yaml:"transport,omitempty"`
- // --- authentication ---
- // authType selects the auth method: api_key, bearer_token, basic,
- // authorization, oauth2, or aws_sigv4.
- AuthType string `yaml:"auth_type,omitempty"`
- // authValue is the credential for api_key, bearer_token, basic, and
- // authorization auth types. May reference an env var via os.environ/VAR.
- AuthValue string `yaml:"auth_value,omitempty"`
- // --- headers ---
- // staticHeaders are key-value pairs sent with every request to this server.
- StaticHeaders map[string]string `yaml:"static_headers,omitempty"`
- // extraHeaders lists client request header names that LiteLLM should
- // forward to this MCP server.
- ExtraHeaders []string `yaml:"extra_headers,omitempty"`
- // allowAllKeys grants every LiteLLM API key access to this server when true.
- AllowAllKeys bool `yaml:"allow_all_keys,omitempty"`
- }
- type liteLLMSettings struct {
- SuccessCallback []string `yaml:"success_callback,omitempty"`
- FailureCallback []string `yaml:"failure_callback,omitempty"`
- JSONLogs bool `yaml:"json_logs,omitempty"`
- }
- type liteLLMGeneralSettings struct {
- MasterKey string `yaml:"master_key,omitempty"`
- }
- type LiteLLMConfigBuilder struct {
- MasterKeyEnvName string
- Stack *v1alpha1.Stack
- ObservabilityComponent *v1alpha1.Component
- }
- func (l *LiteLLMConfigBuilder) BuildLiteLLMConfig() (LiteLLMConfig, error) {
- phoenixEnabled := phoenixEnabled(l.Stack)
- successCB := []string{}
- failureCB := []string{}
- if phoenixEnabled {
- successCB = append(successCB, "arize_phoenix")
- failureCB = append(failureCB, "arize_phoenix")
- }
- cfg := LiteLLMConfig{
- ModelList: []modelEntry{},
- MCPServers: map[string]mcpServerEntry{},
- LiteLLMSettings: &liteLLMSettings{
- SuccessCallback: successCB,
- FailureCallback: failureCB,
- JSONLogs: true,
- },
- GeneralSettings: &liteLLMGeneralSettings{
- MasterKey: "os.environ/" + l.MasterKeyEnvName,
- },
- }
- if phoenixEnabled {
- cfg.EnvVars = map[string]string{
- "PHOENIX_PROJECT_NAME": "locostack",
- "PHOENIX_COLLECTOR_HTTP_ENDPOINT": l.ObservabilityComponent.Status.Endpoint + "/v1/traces",
- }
- }
- return cfg, nil
- }
- func phoenixEnabled(stack *v1alpha1.Stack) bool {
- if stack == nil {
- return false
- }
- if stack.Spec.Observability == nil {
- return false
- }
- if !stack.Spec.Observability.Enabled {
- return false
- }
- if stack.Spec.Observability.Template != nil && stack.Spec.Observability.Template.Name != "phoenix" {
- return false
- }
- return true
- }
|