config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2026 LocoStack.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package litellm
  14. import "github.com/LocoStack/loco-operator/api/v1alpha1"
  15. type LiteLLMConfig struct {
  16. ModelList []modelEntry `yaml:"model_list"`
  17. MCPServers map[string]mcpServerEntry `yaml:"mcp_servers,omitempty"`
  18. LiteLLMSettings *liteLLMSettings `yaml:"litellm_settings,omitempty"`
  19. GeneralSettings *liteLLMGeneralSettings `yaml:"general_settings,omitempty"`
  20. EnvVars map[string]string `yaml:"environment_variables,omitempty"`
  21. }
  22. type modelEntry struct {
  23. ModelName string `yaml:"model_name"`
  24. LiteLLMParams map[string]any `yaml:"litellm_params"`
  25. }
  26. type mcpServerEntry struct {
  27. // url is the HTTP/SSE endpoint for sse and http transports.
  28. URL string `yaml:"url,omitempty"`
  29. // transport selects the MCP transport: sse (default), http, or stdio.
  30. Transport string `yaml:"transport,omitempty"`
  31. // --- authentication ---
  32. // authType selects the auth method: api_key, bearer_token, basic,
  33. // authorization, oauth2, or aws_sigv4.
  34. AuthType string `yaml:"auth_type,omitempty"`
  35. // authValue is the credential for api_key, bearer_token, basic, and
  36. // authorization auth types. May reference an env var via os.environ/VAR.
  37. AuthValue string `yaml:"auth_value,omitempty"`
  38. // --- headers ---
  39. // staticHeaders are key-value pairs sent with every request to this server.
  40. StaticHeaders map[string]string `yaml:"static_headers,omitempty"`
  41. // extraHeaders lists client request header names that LiteLLM should
  42. // forward to this MCP server.
  43. ExtraHeaders []string `yaml:"extra_headers,omitempty"`
  44. // allowAllKeys grants every LiteLLM API key access to this server when true.
  45. AllowAllKeys bool `yaml:"allow_all_keys,omitempty"`
  46. }
  47. type liteLLMSettings struct {
  48. SuccessCallback []string `yaml:"success_callback,omitempty"`
  49. FailureCallback []string `yaml:"failure_callback,omitempty"`
  50. JSONLogs bool `yaml:"json_logs,omitempty"`
  51. }
  52. type liteLLMGeneralSettings struct {
  53. MasterKey string `yaml:"master_key,omitempty"`
  54. }
  55. type LiteLLMConfigBuilder struct {
  56. MasterKeyEnvName string
  57. Stack *v1alpha1.Stack
  58. ObservabilityComponent *v1alpha1.Component
  59. }
  60. func (l *LiteLLMConfigBuilder) BuildLiteLLMConfig() (LiteLLMConfig, error) {
  61. phoenixEnabled := phoenixEnabled(l.Stack)
  62. successCB := []string{}
  63. failureCB := []string{}
  64. if phoenixEnabled {
  65. successCB = append(successCB, "arize_phoenix")
  66. failureCB = append(failureCB, "arize_phoenix")
  67. }
  68. cfg := LiteLLMConfig{
  69. ModelList: []modelEntry{},
  70. MCPServers: map[string]mcpServerEntry{},
  71. LiteLLMSettings: &liteLLMSettings{
  72. SuccessCallback: successCB,
  73. FailureCallback: failureCB,
  74. JSONLogs: true,
  75. },
  76. GeneralSettings: &liteLLMGeneralSettings{
  77. MasterKey: "os.environ/" + l.MasterKeyEnvName,
  78. },
  79. }
  80. if phoenixEnabled {
  81. cfg.EnvVars = map[string]string{
  82. "PHOENIX_PROJECT_NAME": "locostack",
  83. "PHOENIX_COLLECTOR_HTTP_ENDPOINT": l.ObservabilityComponent.Status.Endpoint + "/v1/traces",
  84. }
  85. }
  86. return cfg, nil
  87. }
  88. func phoenixEnabled(stack *v1alpha1.Stack) bool {
  89. if stack == nil {
  90. return false
  91. }
  92. if stack.Spec.Observability == nil {
  93. return false
  94. }
  95. if !stack.Spec.Observability.Enabled {
  96. return false
  97. }
  98. if stack.Spec.Observability.Template != nil && stack.Spec.Observability.Template.Name != "phoenix" {
  99. return false
  100. }
  101. return true
  102. }