Преглед изворни кода

feat(template): add llama.cpp template

Thomas Zhang пре 2 месеци
родитељ
комит
8053290aa1

+ 2 - 0
internal/controller/component_controller.go

@@ -169,6 +169,8 @@ func (r *ComponentReconciler) getComponentReconciler(comp *v1alpha1.Component, s
 		return reconciler.NewPostgreSQLReconciler(r.Client, r.Scheme, stack, comp)
 	case "phoenix":
 		return reconciler.NewPhoenixReconciler(r.Client, r.Scheme, stack, comp)
+	case "llama.cpp":
+		return reconciler.NewLlamaCPPReconciler(r.Client, r.Scheme, stack, comp)
 	default:
 		return reconciler.NewDefaultComponentReconciler(r.Client, r.Scheme, stack, comp)
 	}

+ 41 - 0
internal/reconciler/llama_cpp.go

@@ -0,0 +1,41 @@
+/*
+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 reconciler
+
+import (
+	"github.com/LocoStack/loco-operator/api/v1alpha1"
+	"k8s.io/apimachinery/pkg/runtime"
+	"sigs.k8s.io/controller-runtime/pkg/client"
+)
+
+type LlamaCPPReconciler struct {
+	*DefaultComponentReconciler
+	client    client.Client
+	scheme    *runtime.Scheme
+	stack     *v1alpha1.Stack
+	component *v1alpha1.Component
+}
+
+func NewLlamaCPPReconciler(client client.Client, scheme *runtime.Scheme, stack *v1alpha1.Stack, component *v1alpha1.Component) *LlamaCPPReconciler {
+	return &LlamaCPPReconciler{
+		DefaultComponentReconciler: NewDefaultComponentReconciler(client, scheme, stack, component),
+		client:                     client,
+		scheme:                     scheme,
+		stack:                      stack,
+		component:                  component,
+	}
+}

+ 141 - 0
pkg/templates/llama_cpp/template.go

@@ -0,0 +1,141 @@
+/*
+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 llamacpp
+
+import (
+	"fmt"
+
+	"github.com/LocoStack/loco-operator/api/v1alpha1"
+	corev1 "k8s.io/api/core/v1"
+	"k8s.io/utils/ptr"
+)
+
+const (
+	LLAMA_CPP_MODEL_PATH    = "/models"
+	LLAMA_CPP_TEMPLATE_PATH = "/templates"
+)
+
+var LlamaCPPTemplate = v1alpha1.Template{
+	Name: "llama.cpp",
+	Spec: v1alpha1.TemplateSpec{
+		Runtime: v1alpha1.RuntimeSpec{
+			Name:  "llama.cpp",
+			Image: "ghcr.io/ggml-org/llama.cpp:server",
+			Args: []string{
+				"--model", fmt.Sprintf("%s/$(spec.modelName)", LLAMA_CPP_MODEL_PATH),
+				"--host", "0.0.0.0",
+				"--port", "$(spec.runtime.port)",
+				"--jinja",
+			},
+			ConditionalArgs: []v1alpha1.ConditionalArgs{
+				{
+					When: "$(spec.runtimeInferenceParameters.chatTemplateFilePath)",
+					Args: []string{
+						"--chat-template-file",
+						"$(spec.runtimeInferenceParameters.chatTemplateFilePath)",
+					},
+				},
+				{
+					When: "$(spec.runtimeInferenceParameters.contextWindow)",
+					Args: []string{
+						"--ctx-size",
+						"$(spec.runtimeInferenceParameters.contextWindow)",
+					},
+				},
+				{
+					When: "$(spec.runtimeInferenceParameters.temperature)",
+					Args: []string{
+						"--temperature",
+						"$(spec.runtimeInferenceParameters.temperature)",
+					},
+				},
+				{
+					When: "$(spec.runtimeInferenceParameters.topK)",
+					Args: []string{
+						"--top-k",
+						"$(spec.runtimeInferenceParameters.topK)",
+					},
+				},
+				{
+					When: "$(spec.runtimeInferenceParameters.topP)",
+					Args: []string{
+						"--top-p",
+						"$(spec.runtimeInferenceParameters.topP)",
+					},
+				},
+			},
+			VolumeMounts: []corev1.VolumeMount{
+				{Name: "model", MountPath: LLAMA_CPP_MODEL_PATH},
+				{Name: "chat-template", MountPath: LLAMA_CPP_TEMPLATE_PATH},
+			},
+			Port: 8080,
+		},
+		Volumes: []corev1.Volume{
+			{
+				Name: "model",
+				VolumeSource: corev1.VolumeSource{
+					PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
+						ClaimName: "$(spec.pvcName)",
+					},
+				},
+			},
+			{
+				Name: "chat-template",
+				VolumeSource: corev1.VolumeSource{
+					ConfigMap: &corev1.ConfigMapVolumeSource{
+						LocalObjectReference: corev1.LocalObjectReference{
+							Name: "$(spec.chatTemplateConfigMapName)",
+						},
+						Optional: ptr.To(true),
+					},
+				},
+			},
+		},
+	},
+}
+
+var LlamaCPPEmbeddingRuntime = v1alpha1.RuntimeSpec{
+	Name:  "llama.cpp-embedding",
+	Image: "ghcr.io/ggml-org/llama.cpp:server",
+	Args: []string{
+		"--model", fmt.Sprintf("%s/$(spec.modelName)", LLAMA_CPP_MODEL_PATH),
+		"--host", "0.0.0.0",
+		"--port", "$(spec.runtime.port)",
+		"--ctx-size", "$(spec.runtimeInferenceParameters.contextWindow)",
+		"--embedding",
+	},
+	VolumeMounts: []corev1.VolumeMount{
+		{Name: "model", MountPath: LLAMA_CPP_MODEL_PATH},
+	},
+	Port: 8080,
+}
+
+var LlamaCPPRerankerRuntime = v1alpha1.RuntimeSpec{
+	Name:  "llama.cpp-reranker",
+	Image: "ghcr.io/ggml-org/llama.cpp:server",
+	Args: []string{
+		"--model", fmt.Sprintf("%s/$(spec.modelName)", LLAMA_CPP_MODEL_PATH),
+		"--host", "0.0.0.0",
+		"--port", "$(spec.runtime.port)",
+		"--ctx-size", "$(spec.runtimeInferenceParameters.contextWindow)",
+		"--reranking",
+	},
+	VolumeMounts: []corev1.VolumeMount{
+		{Name: "model", MountPath: LLAMA_CPP_MODEL_PATH},
+	},
+	Port: 8080,
+}

+ 5 - 0
pkg/templates/templates.go

@@ -23,6 +23,7 @@ import (
 
 	"github.com/LocoStack/loco-operator/api/v1alpha1"
 	"github.com/LocoStack/loco-operator/pkg/templates/litellm"
+	llamacpp "github.com/LocoStack/loco-operator/pkg/templates/llama_cpp"
 	"github.com/LocoStack/loco-operator/pkg/templates/neo4j"
 	"github.com/LocoStack/loco-operator/pkg/templates/phoenix"
 	"github.com/LocoStack/loco-operator/pkg/templates/postgresql"
@@ -252,4 +253,8 @@ func init() {
 	Manager.RegisterRuntime(postgresql.PostgreSQLTemplate.Spec.Runtime)
 	Manager.RegisterTemplate(phoenix.PhoenixTemplate)
 	Manager.RegisterRuntime(phoenix.PhoenixTemplate.Spec.Runtime)
+	Manager.RegisterTemplate(llamacpp.LlamaCPPTemplate)
+	Manager.RegisterRuntime(llamacpp.LlamaCPPTemplate.Spec.Runtime)
+	Manager.RegisterRuntime(llamacpp.LlamaCPPEmbeddingRuntime)
+	Manager.RegisterRuntime(llamacpp.LlamaCPPRerankerRuntime)
 }