Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): parse composite llm_id right-anchored on provider (#19752)

coderdailyone1d9173072857
diff --git a/internal/agent/component/categorize_test.go b/internal/agent/component/categorize_test.go
index b227fc8d5f..65f7836dd2 100644
--- a/internal/agent/component/categorize_test.go
+++ b/internal/agent/component/categorize_test.go
@@ -359,6 +359,11 @@ func TestSplitCompositeLLMID(t *testing.T) {
{"gpt-4o", "gpt-4o", "", false},
{"gpt-4o@OpenAI", "gpt-4o", "OpenAI", true},
{"Qwen/Qwen3-8B@default@SILICONFLOW", "Qwen/Qwen3-8B", "SILICONFLOW", true},
+ // Model names may themselves contain '@' (e.g. LM Studio quant
+ // suffixes): the provider is the LAST segment and the model name
+ // keeps its embedded '@'. Mirrors Python's rsplit("@", 2).
+ {"model@q8_0@lmstudio@LM-Studio", "model@q8_0", "LM-Studio", true},
+ {"a@b@c@d@e", "a@b@c", "e", true},
}
for _, tc := range cases {
gotModel, gotDrv, gotOK := splitCompositeLLMID(tc.in)
@@ -413,6 +418,61 @@ func TestCategorize_ResolvesTenantModelInstanceCredentials(t *testing.T) {
}
}
+// TestCategorize_ResolvesInstanceCredentialsModelNameWithAt covers a
+// composite llm_id whose model name itself contains '@' — e.g. LM Studio's
+// "model@q8_0" ids produce "model@q8_0@instance@provider". The provider must
+// be taken from the LAST segment (Python split_model_name uses rsplit); a
+// left-anchored split resolves provider "q8_0" and no credentials are found.
+func TestCategorize_ResolvesInstanceCredentialsModelNameWithAt(t *testing.T) {
+ db := setupComponentTestDB(t)
+ pushComponentDB(t, db)
+ if err := db.Create(&entity.TenantModelProvider{
+ ID: "provider-1",
+ TenantID: "tenant-1",
+ ProviderName: "LM-Studio",
+ }).Error; err != nil {
+ t.Fatalf("create provider: %v", err)
+ }
+ if err := db.Create(&entity.TenantModelInstance{
+ ID: "instance-1",
+ ProviderID: "provider-1",
+ InstanceName: "lmstudio",
+ APIKey: "lm-studio-key",
+ Status: "active",
+ Extra: `{"base_url":"http://localhost:1234/v1"}`,
+ }).Error; err != nil {
+ t.Fatalf("create instance: %v", err)
+ }
+
+ stub := &stubInvoker{resp: &ChatInvokeResponse{Content: "support", Model: "stub"}}
+ withStubInvoker(t, stub)
+
+ c := NewCategorizeComponent(CategorizeParam{
+ ModelID: "text-embedding-nomic-embed-text-v1.5@q8_0@lmstudio@LM-Studio",
+ Categories: []string{"sales", "support"},
+ DefaultCategory: "support",
+ })
+ _, err := c.Invoke(stateWithTenant("tenant-1"), db, map[string]any{})
+ if err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+ if stub.captured == nil {
+ t.Fatal("invoker not called")
+ }
+ if stub.captured.Driver != "LM-Studio" {
+ t.Fatalf("Driver=%q, want %q", stub.captured.Driver, "LM-Studio")
+ }
+ if stub.captured.ModelName != "text-embedding-nomic-embed-text-v1.5@q8_0" {
+ t.Fatalf("ModelName=%q, want %q", stub.captured.ModelName, "text-embedding-nomic-embed-text-v1.5@q8_0")
+ }
+ if stub.captured.APIKey != "lm-studio-key" {
+ t.Fatalf("APIKey=%q, want %q", stub.captured.APIKey, "lm-studio-key")
+ }
+ if stub.captured.BaseURL != "http://localhost:1234/v1" {
+ t.Fatalf("BaseURL=%q, want %q", stub.captured.BaseURL, "http://localhost:1234/v1")
+ }
+}
+
func TestCategorize_ResolvesTenantModelID(t *testing.T) {
db := setupComponentTestDB(t)
pushComponentDB(t, db)
diff --git a/internal/agent/component/llm_credentials.go b/internal/agent/component/llm_credentials.go
index c1cc5d3419..e2d032f752 100644
--- a/internal/agent/component/llm_credentials.go
+++ b/internal/agent/component/llm_credentials.go
@@ -355,21 +355,22 @@ func isBareTenantModelID(s string) bool {
}
// parseLLMIDParts splits a composite llm_id into model, instance, and
-// provider segments.
+// provider segments. The split is right-anchored — provider is the last
+// segment, instance the second-to-last, and everything left of them is the
+// model name, which may itself contain '@' (e.g. LM Studio quant-suffixed
+// ids). Mirrors Python's split_model_name (rsplit("@", 2)).
//
// "model@provider" -> ("model", "default", "provider")
// "model@instance@provider" -> ("model", "instance", "provider")
-// 4+ parts -> ("parts[0]", "parts[1]", "parts[2]")
+// "model@tag@inst@provider" -> ("model@tag", "inst", "provider")
func parseLLMIDParts(s string) (modelName, instanceName, providerName string) {
parts := strings.Split(strings.TrimSpace(s), "@")
switch len(parts) {
case 2:
return parts[0], "default", parts[1]
- case 3:
- return parts[0], parts[1], parts[2]
default:
- if len(parts) >= 4 {
- return parts[0], parts[1], parts[2]
+ if len(parts) >= 3 {
+ return strings.Join(parts[:len(parts)-2], "@"), parts[len(parts)-2], parts[len(parts)-1]
}
return s, "", ""
}
diff --git a/internal/agent/component/llm_id.go b/internal/agent/component/llm_id.go
index 58b7495a93..9e2eb6986a 100644
--- a/internal/agent/component/llm_id.go
+++ b/internal/agent/component/llm_id.go
@@ -7,14 +7,22 @@ import "strings"
// - "model@provider" -> ("model", "provider", true)
// - "model@instance@provider" -> ("model", "provider", true)
// - bare "model" -> ("model", "", false)
+//
+// The split is right-anchored: the provider is the last segment, the instance
+// (dropped here) the second-to-last, and everything further left is the model
+// name — which may itself contain '@' (e.g. LM Studio quant-suffixed ids such
+// as "text-embedding-nomic-embed-text-v1.5@q8_0"). Mirrors Python's
+// split_model_name (rsplit("@", 2)) and parseModelName in
+// internal/service/model_service.go.
func splitCompositeLLMID(s string) (modelName, driver string, hasDriver bool) {
parts := strings.Split(strings.TrimSpace(s), "@")
switch len(parts) {
case 2:
return parts[0], parts[1], true
- case 3:
- return parts[0], parts[2], true
default:
+ if len(parts) >= 3 {
+ return strings.Join(parts[:len(parts)-2], "@"), parts[len(parts)-1], true
+ }
return s, "", false
}
}
diff --git a/internal/agent/component/llm_id_test.go b/internal/agent/component/llm_id_test.go
new file mode 100644
index 0000000000..cb28dc820b
--- /dev/null
+++ b/internal/agent/component/llm_id_test.go
@@ -0,0 +1,48 @@
+// Package component — composite llm_id parsing tests.
+//
+// The canvas llm_id is right-anchored on the provider (Python
+// split_model_name uses rsplit("@", 2)): the last '@'-separated segment is
+// the provider, the second-to-last the instance, and everything to the left
+// is the model name — which may itself contain '@' (e.g. LM Studio
+// quant-suffixed ids like "text-embedding-nomic-embed-text-v1.5@q8_0",
+// producing composites like "...@q8_0@lmstudio@LM-Studio").
+package component
+
+import "testing"
+
+func TestParseLLMIDPartsRightAnchored(t *testing.T) {
+ cases := []struct {
+ in string
+ wantModel string
+ wantInstance string
+ wantProvider string
+ }{
+ {"gpt-4o", "gpt-4o", "", ""},
+ {"gpt-4o@OpenAI", "gpt-4o", "default", "OpenAI"},
+ {"Qwen/Qwen3-8B@default@SILICONFLOW", "Qwen/Qwen3-8B", "default", "SILICONFLOW"},
+ // Model name containing '@' — the real-world shape Python's
+ // split_model_name is written for.
+ {"text-embedding-nomic-embed-text-v1.5@q8_0@lmstudio@LM-Studio",
+ "text-embedding-nomic-embed-text-v1.5@q8_0", "lmstudio", "LM-Studio"},
+ {"a@b@c@d@e", "a@b@c", "d", "e"},
+ }
+ for _, tc := range cases {
+ gotModel, gotInstance, gotProvider := parseLLMIDParts(tc.in)
+ if gotModel != tc.wantModel || gotInstance != tc.wantInstance || gotProvider != tc.wantProvider {
+ t.Errorf("parseLLMIDParts(%q) = (%q, %q, %q), want (%q, %q, %q)",
+ tc.in, gotModel, gotInstance, gotProvider, tc.wantModel, tc.wantInstance, tc.wantProvider)
+ }
+ }
+}
+
+func TestSplitCompositeLLMIDEmbeddedAt(t *testing.T) {
+ // With a '@' inside the model name the provider must still be the last
+ // segment — the old left-anchored split returned hasDriver=false here, so
+ // no driver was resolved and the whole composite was passed upstream as
+ // the model name.
+ model, driver, ok := splitCompositeLLMID("text-embedding-nomic-embed-text-v1.5@q8_0@lmstudio@LM-Studio")
+ if !ok || model != "text-embedding-nomic-embed-text-v1.5@q8_0" || driver != "LM-Studio" {
+ t.Fatalf("splitCompositeLLMID = (%q, %q, %v), want (%q, %q, true)",
+ model, driver, ok, "text-embedding-nomic-embed-text-v1.5@q8_0", "LM-Studio")
+ }
+}