Skip to content

Commit ccb5ebf

Browse files
committed
feat (AI): project-level prompt improvements (#8047)
* Add guardrail section * Improve comparison documentation * Add current date to prompt * (Temporary hack) Ensure we seed the project-level system message in the streaming context * Tweaks
1 parent eb2426f commit ccb5ebf

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

runtime/completion.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ func (r *Runtime) ensureConversation(ctx context.Context, instanceID, ownerID, c
196196
// processAppContext processes the app context and generates contextual system messages
197197
func (r *Runtime) processAppContext(ctx context.Context, instanceID string, appContext *runtimev1.AppContext, toolService ToolService) ([]*runtimev1.Message, error) {
198198
if appContext == nil {
199-
return nil, nil
199+
// If no app context, use project chat context
200+
return r.processProjectChatContext(ctx, instanceID)
200201
}
201202

202203
switch appContext.ContextType {
@@ -205,7 +206,8 @@ func (r *Runtime) processAppContext(ctx context.Context, instanceID string, appC
205206
case runtimev1.AppContextType_APP_CONTEXT_TYPE_EXPLORE_DASHBOARD:
206207
return r.processExploreDashboardContext(ctx, instanceID, appContext.ContextMetadata, toolService)
207208
default:
208-
return nil, nil // Unknown context type, no system message will be added
209+
// Unknown context type, use project chat context
210+
return r.processProjectChatContext(ctx, instanceID)
209211
}
210212
}
211213

@@ -720,8 +722,11 @@ func (r *Runtime) addMessage(ctx context.Context, instanceID, conversationID, ro
720722
// buildProjectChatSystemPrompt constructs the system prompt for the project chat context
721723
func buildProjectChatSystemPrompt(aiInstructions string) string {
722724
// TODO: call 'list_metrics_views' and seed the result in the system prompt
723-
basePrompt := `<role>
725+
currentTime := time.Now()
726+
basePrompt := fmt.Sprintf(`<role>
724727
You are a data analysis agent specialized in uncovering actionable business insights. You systematically explore data using available metrics tools, then apply analytical rigor to find surprising patterns and unexpected relationships that influence decision-making.
728+
729+
Today's date is %s (%s).
725730
</role>
726731
727732
<communication_style>
@@ -749,8 +754,8 @@ Execute a MINIMUM of 4-6 distinct analytical queries, building each query based
749754
750755
<analysis_guidelines>
751756
**Setup Phase (Steps 1-3)**:
757+
- Briefly explain your approach before starting
752758
- Complete each step fully before proceeding
753-
- Explain your approach briefly before starting
754759
- If any step fails, investigate and adapt
755760
756761
**Analysis Phase (Step 4)**:
@@ -772,6 +777,17 @@ Execute a MINIMUM of 4-6 distinct analytical queries, building each query based
772777
- Use only the exact numbers returned by the tools in your analysis
773778
</analysis_guidelines>
774779
780+
<guardrails>
781+
- If the user asks an unrelated question (e.g., general knowledge, politics, entertainment, trivia):
782+
1. Politely decline to answer the unrelated question
783+
2. Briefly explain that you specialize only in business data analysis
784+
3. Redirect the conversation back to datasets, metrics, or insights
785+
4. Do **NOT** attempt to answer the unrelated question
786+
787+
Example response style:
788+
*"I focus specifically on analyzing your business data and uncovering insights — so I won’t be the right source for general knowledge questions. Let’s pivot back to your project: would you like me to explore the available datasets so we can start surfacing insights?"*
789+
</guardrails>
790+
775791
<thinking>
776792
After each query in Phase 2, think through:
777793
- What patterns or anomalies did this reveal?
@@ -783,10 +799,8 @@ After each query in Phase 2, think through:
783799
784800
<output_format>
785801
Format your analysis as follows:
786-
` + "```markdown" + `
787-
[Brief acknowledgment and explanation of approach]
788-
789-
Based on my systematic analysis, here are the key insights:
802+
`+"```markdown"+`
803+
Based on the data analysis, here are the key insights:
790804
791805
1. ## [Headline with specific impact/number]
792806
[Finding with business context and implications]
@@ -797,9 +811,9 @@ Based on my systematic analysis, here are the key insights:
797811
3. ## [Headline with specific impact/number]
798812
[Finding with business context and implications]
799813
800-
[Offer specific follow-up analysis options]
801-
` + "```" + `
802-
</output_format>`
814+
[Optional: Offer specific follow-up analysis options]
815+
`+"```"+`
816+
</output_format>`, currentTime.Format("Monday, January 2, 2006"), currentTime.Format("2006-01-02"))
803817

804818
if aiInstructions != "" {
805819
return basePrompt + "\n\n## Additional Instructions (provided by the Rill project developer)\n" + aiInstructions

runtime/server/mcp_server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,14 @@ func (s *Server) mcpQueryMetricsViewTimeRange() (mcp.Tool, server.ToolHandlerFun
239239
func (s *Server) mcpQueryMetricsView() (mcp.Tool, server.ToolHandlerFunc) {
240240
description := `
241241
Perform an arbitrary aggregation on a metrics view.
242-
Tip: Use the 'sort' and 'limit' parameters for best results and to avoid large, unbounded result sets.
243-
Important note: The 'time_range' parameter is inclusive of the start time and exclusive of the end time.
244-
Note: 'time_dimension' is an optional parameter under "time_range" that can be used to specify the time dimension to use for the time range. If not provided, the default time column of the metrics view will be used.
242+
243+
Parameter notes:
244+
• The 'time_range' parameter is inclusive of the start time and exclusive of the end time
245+
• 'time_dimension' is optional under 'time_range' to specify which time column to use (defaults to the metrics view's default time column)
246+
247+
Best practices:
248+
• Use 'sort' and 'limit' parameters for best results and to avoid large, unbounded result sets
249+
• For comparison queries: ensure 'time_range' and 'comparison_time_range' are non-overlapping and similar in duration (~20% tolerance) to ensure valid period-over-period comparisons
245250
246251
Example: Get the total revenue by country and product category for 2024:
247252
{

0 commit comments

Comments
 (0)