Skip to content

Commit c269ba6

Browse files
authored
feat: support domain level auto system prompt injection (#257)
* feat: support domain level auto system prompt injection Signed-off-by: bitliu <[email protected]> * ut2 Signed-off-by: bitliu <[email protected]> --------- Signed-off-by: bitliu <[email protected]>
1 parent 2db9fd1 commit c269ba6

File tree

8 files changed

+380
-1
lines changed

8 files changed

+380
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Benchmarking will be conducted to determine the best implementation.
4444

4545
Select the tools to use based on the prompt, avoiding the use of tools that are not relevant to the prompt so as to reduce the number of prompt tokens and improve tool selection accuracy by the LLM.
4646

47+
#### Category-Specific System Prompts
48+
49+
Automatically inject specialized system prompts based on query classification, ensuring optimal model behavior for different domains (math, coding, business, etc.) without manual prompt engineering.
50+
4751
### Enterprise Security 🔒
4852

4953
#### PII detection

config/config.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,71 +63,85 @@ classifier:
6363
# Categories with new use_reasoning field structure
6464
categories:
6565
- name: business
66+
system_prompt: "You are a senior business consultant and strategic advisor with expertise in corporate strategy, operations management, financial analysis, marketing, and organizational development. Provide practical, actionable business advice backed by proven methodologies and industry best practices. Consider market dynamics, competitive landscape, and stakeholder interests in your recommendations."
6667
model_scores:
6768
- model: openai/gpt-oss-20b
6869
score: 0.7
6970
use_reasoning: false # Business performs better without reasoning
7071
- name: law
72+
system_prompt: "You are a knowledgeable legal expert with comprehensive understanding of legal principles, case law, statutory interpretation, and legal procedures across multiple jurisdictions. Provide accurate legal information and analysis while clearly stating that your responses are for informational purposes only and do not constitute legal advice. Always recommend consulting with qualified legal professionals for specific legal matters."
7173
model_scores:
7274
- model: openai/gpt-oss-20b
7375
score: 0.4
7476
use_reasoning: false
7577
- name: psychology
78+
system_prompt: "You are a psychology expert with deep knowledge of cognitive processes, behavioral patterns, mental health, developmental psychology, social psychology, and therapeutic approaches. Provide evidence-based insights grounded in psychological research and theory. When discussing mental health topics, emphasize the importance of professional consultation and avoid providing diagnostic or therapeutic advice."
7679
model_scores:
7780
- model: openai/gpt-oss-20b
7881
score: 0.6
7982
use_reasoning: false
8083
- name: biology
84+
system_prompt: "You are a biology expert with comprehensive knowledge spanning molecular biology, genetics, cell biology, ecology, evolution, anatomy, physiology, and biotechnology. Explain biological concepts with scientific accuracy, use appropriate terminology, and provide examples from current research. Connect biological principles to real-world applications and emphasize the interconnectedness of biological systems."
8185
model_scores:
8286
- model: openai/gpt-oss-20b
8387
score: 0.9
8488
use_reasoning: false
8589
- name: chemistry
90+
system_prompt: "You are a chemistry expert specializing in chemical reactions, molecular structures, and laboratory techniques. Provide detailed, step-by-step explanations."
8691
model_scores:
8792
- model: openai/gpt-oss-20b
8893
score: 0.6
8994
use_reasoning: true # Enable reasoning for complex chemistry
9095
- name: history
96+
system_prompt: "You are a historian with expertise across different time periods and cultures. Provide accurate historical context and analysis."
9197
model_scores:
9298
- model: openai/gpt-oss-20b
9399
score: 0.7
94100
use_reasoning: false
95101
- name: other
102+
system_prompt: "You are a helpful and knowledgeable assistant. Provide accurate, helpful responses across a wide range of topics."
96103
model_scores:
97104
- model: openai/gpt-oss-20b
98105
score: 0.7
99106
use_reasoning: false
100107
- name: health
108+
system_prompt: "You are a health and medical information expert with knowledge of anatomy, physiology, diseases, treatments, preventive care, nutrition, and wellness. Provide accurate, evidence-based health information while emphasizing that your responses are for educational purposes only and should never replace professional medical advice, diagnosis, or treatment. Always encourage users to consult healthcare professionals for medical concerns and emergencies."
101109
model_scores:
102110
- model: openai/gpt-oss-20b
103111
score: 0.5
104112
use_reasoning: false
105113
- name: economics
114+
system_prompt: "You are an economics expert with deep understanding of microeconomics, macroeconomics, econometrics, financial markets, monetary policy, fiscal policy, international trade, and economic theory. Analyze economic phenomena using established economic principles, provide data-driven insights, and explain complex economic concepts in accessible terms. Consider both theoretical frameworks and real-world applications in your responses."
106115
model_scores:
107116
- model: openai/gpt-oss-20b
108117
score: 1.0
109118
use_reasoning: false
110119
- name: math
120+
system_prompt: "You are a mathematics expert. Provide step-by-step solutions, show your work clearly, and explain mathematical concepts in an understandable way."
111121
model_scores:
112122
- model: openai/gpt-oss-20b
113123
score: 1.0
114124
use_reasoning: true # Enable reasoning for complex math
115125
- name: physics
126+
system_prompt: "You are a physics expert with deep understanding of physical laws and phenomena. Provide clear explanations with mathematical derivations when appropriate."
116127
model_scores:
117128
- model: openai/gpt-oss-20b
118129
score: 0.7
119130
use_reasoning: true # Enable reasoning for physics
120131
- name: computer science
132+
system_prompt: "You are a computer science expert with knowledge of algorithms, data structures, programming languages, and software engineering. Provide clear, practical solutions with code examples when helpful."
121133
model_scores:
122134
- model: openai/gpt-oss-20b
123135
score: 0.6
124136
use_reasoning: false
125137
- name: philosophy
138+
system_prompt: "You are a philosophy expert with comprehensive knowledge of philosophical traditions, ethical theories, logic, metaphysics, epistemology, political philosophy, and the history of philosophical thought. Engage with complex philosophical questions by presenting multiple perspectives, analyzing arguments rigorously, and encouraging critical thinking. Draw connections between philosophical concepts and contemporary issues while maintaining intellectual honesty about the complexity and ongoing nature of philosophical debates."
126139
model_scores:
127140
- model: openai/gpt-oss-20b
128141
score: 0.5
129142
use_reasoning: false
130143
- name: engineering
144+
system_prompt: "You are an engineering expert with knowledge across multiple engineering disciplines including mechanical, electrical, civil, chemical, software, and systems engineering. Apply engineering principles, design methodologies, and problem-solving approaches to provide practical solutions. Consider safety, efficiency, sustainability, and cost-effectiveness in your recommendations. Use technical precision while explaining concepts clearly, and emphasize the importance of proper engineering practices and standards."
131145
model_scores:
132146
- model: openai/gpt-oss-20b
133147
score: 0.7
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# System Prompt Configuration Example
2+
# This example demonstrates how to configure category-specific system prompts
3+
# that will be automatically injected into requests based on query classification
4+
5+
# Basic configuration
6+
classifier:
7+
category_model:
8+
model_id: "sentence-transformers/all-MiniLM-L6-v2"
9+
threshold: 0.7
10+
use_cpu: false
11+
use_modernbert: true
12+
category_mapping_path: "models/category_classifier_modernbert-base_model/category_mapping.json"
13+
14+
# Categories with system prompts for different domains
15+
categories:
16+
- name: math
17+
description: "Mathematical queries, calculations, and problem solving"
18+
system_prompt: "You are a mathematics expert. Always provide step-by-step solutions, show your work clearly, and explain mathematical concepts in an understandable way. When solving equations, break down each step and explain the reasoning behind it."
19+
model_scores:
20+
- model: openai/gpt-oss-20b
21+
score: 0.9
22+
use_reasoning: true
23+
24+
- name: computer science
25+
description: "Programming, algorithms, software engineering, and technical topics"
26+
system_prompt: "You are a computer science expert with deep knowledge of algorithms, data structures, programming languages, and software engineering best practices. Provide clear, practical solutions with well-commented code examples when helpful. Always consider performance, readability, and maintainability."
27+
model_scores:
28+
- model: openai/gpt-oss-20b
29+
score: 0.8
30+
use_reasoning: true
31+
32+
- name: creative writing
33+
description: "Creative writing, storytelling, poetry, and literary analysis"
34+
system_prompt: "You are a creative writing expert with a passion for storytelling, poetry, and literature. Help users craft engaging narratives, develop compelling characters, and improve their writing style. Provide constructive feedback and creative suggestions."
35+
model_scores:
36+
- model: openai/gpt-oss-20b
37+
score: 0.7
38+
use_reasoning: false
39+
40+
- name: business
41+
description: "Business strategy, management, finance, and professional advice"
42+
system_prompt: "You are a professional business consultant with expertise in strategy, operations, management, and finance. Provide practical, actionable advice backed by business best practices. Consider both short-term and long-term implications of your recommendations."
43+
model_scores:
44+
- model: openai/gpt-oss-20b
45+
score: 0.8
46+
use_reasoning: false
47+
48+
- name: science
49+
description: "General science questions, research, and scientific concepts"
50+
system_prompt: "You are a scientist with broad knowledge across multiple scientific disciplines. Provide accurate, evidence-based explanations of scientific concepts. When discussing theories or research, cite the scientific method and encourage critical thinking."
51+
model_scores:
52+
- model: openai/gpt-oss-20b
53+
score: 0.8
54+
use_reasoning: true
55+
56+
- name: health
57+
description: "Health, wellness, medical information, and fitness"
58+
system_prompt: "You are a knowledgeable health and wellness expert. Provide accurate health information while always emphasizing that your responses are for educational purposes only and not a substitute for professional medical advice. Encourage users to consult healthcare professionals for medical concerns."
59+
model_scores:
60+
- model: openai/gpt-oss-20b
61+
score: 0.7
62+
use_reasoning: false
63+
64+
- name: education
65+
description: "Teaching, learning, educational methods, and academic topics"
66+
system_prompt: "You are an experienced educator with expertise in pedagogy and learning theory. Help users understand complex topics by breaking them down into manageable parts. Use examples, analogies, and interactive questioning to enhance learning."
67+
model_scores:
68+
- model: openai/gpt-oss-20b
69+
score: 0.8
70+
use_reasoning: false
71+
72+
- name: other
73+
description: "General queries that don't fit into specific categories"
74+
system_prompt: "You are a helpful and knowledgeable assistant. Provide accurate, helpful responses across a wide range of topics. When you're uncertain about something, acknowledge the limitation and suggest where users might find more authoritative information."
75+
model_scores:
76+
- model: openai/gpt-oss-20b
77+
score: 0.6
78+
use_reasoning: false
79+
80+
# Default model for fallback
81+
default_model: openai/gpt-oss-20b
82+
83+
# Model configuration
84+
model_config:
85+
"openai/gpt-oss-20b":
86+
reasoning_family: "gpt-oss"
87+
preferred_endpoints: ["mock"]
88+
pii_policy:
89+
allow_by_default: true
90+
91+
# Reasoning family configurations
92+
reasoning_families:
93+
gpt-oss:
94+
type: "reasoning_effort"
95+
parameter: "reasoning_effort"
96+
97+
# Global default reasoning effort level
98+
default_reasoning_effort: medium
99+
100+
# vLLM endpoints configuration
101+
vllm_endpoints:
102+
- name: "mock"
103+
address: "http://127.0.0.1:8000"
104+
models:
105+
- "openai/gpt-oss-20b"
106+
107+
# Usage Notes:
108+
# 1. System prompts are automatically injected based on query classification
109+
# 2. If a request already has a system message, it will be replaced with the category-specific one
110+
# 3. If no system_prompt is configured for a category, no system message is added
111+
# 4. System prompts work with both "auto" model selection and specific model requests
112+
# 5. The system prompt is added before reasoning mode processing

src/semantic-router/pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ type Category struct {
273273
// used by the classifier model. When provided, classifier outputs will be translated
274274
// from these MMLU categories to this generic category name.
275275
MMLUCategories []string `yaml:"mmlu_categories,omitempty"`
276+
// SystemPrompt is an optional category-specific system prompt automatically injected into requests
277+
SystemPrompt string `yaml:"system_prompt,omitempty"`
276278
}
277279

278280
// Legacy types - can be removed once migration is complete

src/semantic-router/pkg/extproc/request_handler.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,62 @@ func serializeOpenAIRequest(req *openai.ChatCompletionNewParams) ([]byte, error)
3232
return json.Marshal(req)
3333
}
3434

35+
// addSystemPromptToRequestBody adds a system prompt to the beginning of the messages array in the JSON request body
36+
func addSystemPromptToRequestBody(requestBody []byte, systemPrompt string) ([]byte, error) {
37+
if systemPrompt == "" {
38+
return requestBody, nil
39+
}
40+
41+
// Parse the JSON request body
42+
var requestMap map[string]interface{}
43+
if err := json.Unmarshal(requestBody, &requestMap); err != nil {
44+
return nil, err
45+
}
46+
47+
// Get the messages array
48+
messagesInterface, ok := requestMap["messages"]
49+
if !ok {
50+
return requestBody, nil // No messages array, return original
51+
}
52+
53+
messages, ok := messagesInterface.([]interface{})
54+
if !ok {
55+
return requestBody, nil // Messages is not an array, return original
56+
}
57+
58+
// Create a new system message
59+
systemMessage := map[string]interface{}{
60+
"role": "system",
61+
"content": systemPrompt,
62+
}
63+
64+
// Check if there's already a system message at the beginning
65+
hasSystemMessage := false
66+
if len(messages) > 0 {
67+
if firstMsg, ok := messages[0].(map[string]interface{}); ok {
68+
if role, ok := firstMsg["role"].(string); ok && role == "system" {
69+
hasSystemMessage = true
70+
}
71+
}
72+
}
73+
74+
if hasSystemMessage {
75+
// Replace the existing system message
76+
messages[0] = systemMessage
77+
observability.Infof("Replaced existing system message with category-specific system prompt")
78+
} else {
79+
// Prepend the system message to the beginning of the messages array
80+
messages = append([]interface{}{systemMessage}, messages...)
81+
observability.Infof("Added category-specific system prompt to the beginning of messages")
82+
}
83+
84+
// Update the messages in the request map
85+
requestMap["messages"] = messages
86+
87+
// Marshal back to JSON
88+
return json.Marshal(requestMap)
89+
}
90+
3591
// extractUserAndNonUserContent extracts content from request messages
3692
func extractUserAndNonUserContent(req *openai.ChatCompletionNewParams) (string, []string) {
3793
var userContent string
@@ -416,6 +472,20 @@ func (r *OpenAIRouter) handleModelRouting(openAIRequest *openai.ChatCompletionNe
416472
return nil, status.Errorf(codes.Internal, "error setting reasoning mode: %v", err)
417473
}
418474

475+
// Add category-specific system prompt if configured
476+
if categoryName != "" {
477+
category := r.Classifier.GetCategoryByName(categoryName)
478+
if category != nil && category.SystemPrompt != "" {
479+
modifiedBody, err = addSystemPromptToRequestBody(modifiedBody, category.SystemPrompt)
480+
if err != nil {
481+
observability.Errorf("Error adding system prompt to request: %v", err)
482+
metrics.RecordRequestError(actualModel, "serialization_error")
483+
return nil, status.Errorf(codes.Internal, "error adding system prompt: %v", err)
484+
}
485+
observability.Infof("Added category-specific system prompt for category: %s", categoryName)
486+
}
487+
}
488+
419489
// Create body mutation with the modified body
420490
bodyMutation := &ext_proc.BodyMutation{
421491
Mutation: &ext_proc.BodyMutation_Body{

0 commit comments

Comments
 (0)