Skip to content

Commit 3e58aa1

Browse files
ooplesclaude
andcommitted
feat(prompt-engineering): add comprehensive prompt engineering features
Implements Issue #420 with 7 phases of prompt engineering enhancements: Phase 1: Core infrastructure - PredictionModelResultOptions for configurable prompt features - Updated PredictionModelResult and PredictionModelBuilder Phase 2: Advanced example selectors - SemanticSimilarityExampleSelector (embedding-based similarity) - DiversityExampleSelector (maximizes example diversity) - MMRExampleSelector (maximal marginal relevance) - ClusterBasedExampleSelector (k-means clustering) Phase 3: Advanced chains - RouterChain (routes to different chains based on classification) - MapReduceChain (parallel processing with reduce) - LoopChain (iterative processing with termination) - ParallelChain (concurrent execution) - ConditionalChain (branching logic) Phase 4: Token compression - StopWordRemovalCompressor, SentenceCompressor - RedundancyRemovalCompressor, LLMSummarizationCompressor - CachingCompressor, CompositeCompressor Phase 5: Prompt analysis and metrics - PromptMetrics, PromptAnalyzerBase - ComplexityAnalyzer, TokenCountAnalyzer - PatternDetectionAnalyzer, PromptValidator Phase 6: Advanced optimizers - GeneticOptimizer (evolutionary algorithm) - BeamSearchOptimizer (multiple candidates) - SimulatedAnnealingOptimizer (escapes local optima) - EnsembleOptimizer (combines strategies) - GreedyHillClimbingOptimizer Phase 7: Advanced templates - ChainOfThoughtTemplate (step-by-step reasoning) - StructuredOutputTemplate (JSON/XML/CSV/YAML) - RolePlayingTemplate (persona-based prompts) - InstructionFollowingTemplate (structured instructions) - CompositePromptTemplate, ConditionalPromptTemplate All code compiles for net8.0 and net471 with zero warnings. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d942a17 commit 3e58aa1

40 files changed

Lines changed: 11452 additions & 0 deletions

src/Interfaces/IPromptAnalyzer.cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using AiDotNet.PromptEngineering.Analysis;
2+
3+
namespace AiDotNet.Interfaces;
4+
5+
/// <summary>
6+
/// Defines the contract for analyzing prompts before sending them to language models.
7+
/// </summary>
8+
/// <remarks>
9+
/// <para>
10+
/// A prompt analyzer computes metrics and validates prompts to help developers understand
11+
/// and optimize their prompts before incurring API costs. Analysis includes token counting,
12+
/// cost estimation, complexity measurement, and issue detection.
13+
/// </para>
14+
/// <para><b>For Beginners:</b> A prompt analyzer is like a spell-checker and cost calculator for your prompts.
15+
///
16+
/// Before sending a prompt to an AI model (which costs money), the analyzer tells you:
17+
/// - How many tokens it uses (tokens = cost)
18+
/// - Estimated API cost in dollars
19+
/// - How complex the prompt is
20+
/// - Any potential problems (missing variables, too long, etc.)
21+
///
22+
/// Example workflow:
23+
/// <code>
24+
/// var analyzer = new TokenCountAnalyzer();
25+
/// var metrics = analyzer.Analyze("Translate this text...");
26+
///
27+
/// Console.WriteLine($"Tokens: {metrics.TokenCount}");
28+
/// Console.WriteLine($"Cost: ${metrics.EstimatedCost}");
29+
///
30+
/// if (metrics.TokenCount > 4000)
31+
/// {
32+
/// Console.WriteLine("Warning: Prompt is very long, consider compression");
33+
/// }
34+
/// </code>
35+
///
36+
/// Benefits:
37+
/// - Cost control: Know costs before making API calls
38+
/// - Optimization: Find prompts that are too long or complex
39+
/// - Debugging: Catch issues before they cause errors
40+
/// - Budgeting: Track and forecast API spending
41+
/// </para>
42+
/// </remarks>
43+
public interface IPromptAnalyzer
44+
{
45+
/// <summary>
46+
/// Analyzes a prompt and returns detailed metrics.
47+
/// </summary>
48+
/// <param name="prompt">The prompt string to analyze.</param>
49+
/// <returns>A PromptMetrics object containing analysis results.</returns>
50+
/// <remarks>
51+
/// <para>
52+
/// Performs comprehensive analysis of the prompt including token counting,
53+
/// cost estimation, complexity scoring, and pattern detection.
54+
/// </para>
55+
/// <para><b>For Beginners:</b> This examines your prompt and tells you everything about it.
56+
///
57+
/// Example:
58+
/// <code>
59+
/// var metrics = analyzer.Analyze("Summarize this article about climate change...");
60+
///
61+
/// // metrics.TokenCount: 150 (how many tokens)
62+
/// // metrics.EstimatedCost: $0.003 (cost at current rates)
63+
/// // metrics.ComplexityScore: 0.4 (0-1, higher = more complex)
64+
/// // metrics.VariableCount: 0 (number of {variables})
65+
/// // metrics.DetectedPatterns: ["summarization", "instruction"]
66+
/// </code>
67+
/// </para>
68+
/// </remarks>
69+
PromptMetrics Analyze(string prompt);
70+
71+
/// <summary>
72+
/// Analyzes a prompt asynchronously for use in async workflows.
73+
/// </summary>
74+
/// <param name="prompt">The prompt string to analyze.</param>
75+
/// <param name="cancellationToken">Token to cancel the operation.</param>
76+
/// <returns>A task that resolves to PromptMetrics.</returns>
77+
/// <remarks>
78+
/// <para>
79+
/// Async version of Analyze for non-blocking analysis, useful when analyzing
80+
/// many prompts or when analysis involves external calls (e.g., to a tokenizer API).
81+
/// </para>
82+
/// <para><b>For Beginners:</b> Same as Analyze, but doesn't block your program.
83+
/// Use this when analyzing many prompts at once or in web applications.
84+
/// </para>
85+
/// </remarks>
86+
Task<PromptMetrics> AnalyzeAsync(string prompt, CancellationToken cancellationToken = default);
87+
88+
/// <summary>
89+
/// Validates a prompt for potential issues.
90+
/// </summary>
91+
/// <param name="prompt">The prompt string to validate.</param>
92+
/// <param name="options">Options controlling validation behavior.</param>
93+
/// <returns>A collection of detected issues, empty if no issues found.</returns>
94+
/// <remarks>
95+
/// <para>
96+
/// Checks the prompt for common problems such as missing placeholders,
97+
/// excessive length, potential injection vulnerabilities, and format issues.
98+
/// </para>
99+
/// <para><b>For Beginners:</b> This looks for problems in your prompt.
100+
///
101+
/// Example issues it might find:
102+
/// - "Prompt exceeds maximum length for GPT-4 (8192 tokens)"
103+
/// - "Variable {user_input} contains potential prompt injection"
104+
/// - "Missing closing brace for variable {name"
105+
/// - "Prompt is empty or whitespace-only"
106+
///
107+
/// Use this before sending prompts to catch problems early:
108+
/// <code>
109+
/// var issues = analyzer.ValidatePrompt(prompt, ValidationOptions.Strict);
110+
/// if (issues.Any())
111+
/// {
112+
/// foreach (var issue in issues)
113+
/// {
114+
/// Console.WriteLine($"[{issue.Severity}] {issue.Message}");
115+
/// }
116+
/// }
117+
/// </code>
118+
/// </para>
119+
/// </remarks>
120+
IEnumerable<PromptIssue> ValidatePrompt(string prompt, ValidationOptions? options = null);
121+
122+
/// <summary>
123+
/// Gets the name of this analyzer implementation.
124+
/// </summary>
125+
/// <remarks>
126+
/// <para>
127+
/// A human-readable identifier for this analyzer, useful for logging
128+
/// and debugging which analyzer is being used.
129+
/// </para>
130+
/// <para><b>For Beginners:</b> The name of this specific analyzer.
131+
/// Examples: "TokenCountAnalyzer", "GPT4CostEstimator", "ClaudeAnalyzer"
132+
/// </para>
133+
/// </remarks>
134+
string Name { get; }
135+
}

0 commit comments

Comments
 (0)