Skip to content

Commit ca68721

Browse files
authored
Merge pull request #1 from otapiero/copilot/update-prompt-key-configuration
Auto-apply configured Label from PromptConfiguration during prompt fetching
2 parents 15104f4 + dc4892a commit ca68721

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

Models/PromptConfiguration.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public sealed class PromptConfiguration
1414

1515
/// <summary>
1616
/// Optional label to use (e.g., "production", "staging", "latest")
17-
/// If neither version nor label is specified, defaults to "production"
17+
/// When configured, this label will be automatically used when fetching the prompt,
18+
/// unless explicitly overridden by passing a different label to GetPromptAsync/GetChatPromptAsync.
19+
/// If neither version nor label is specified, Langfuse will use its default behavior.
1820
/// </summary>
1921
public string? Label { get; set; }
2022
}

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ builder.Services.AddPromptProvider(
3333

3434
- `configureLangfuse`: (optional) Bind Langfuse API credentials and URL
3535
- `configurePrompts`: (optional) Bind local prompt defaults
36-
- `configurePromptKeys`: (optional) Map logical prompt names to Langfuse keys/labels
36+
- `configurePromptKeys`: (optional) Map logical prompt names to Langfuse keys/labels. When a label is configured here, it will be used automatically when fetching prompts unless explicitly overridden.
3737

3838
### 2. Example `appsettings.json`
3939

@@ -62,6 +62,11 @@ builder.Services.AddPromptProvider(
6262
}
6363
```
6464

65+
**Note on Labels**: When you configure a `Label` in `PromptKeys`, it will be automatically used when fetching that prompt. For example:
66+
- If `ChatTitle` is configured with `"Label": "staging"`, calling `GetPromptAsync("ChatTitle")` will fetch the "staging" label from Langfuse
67+
- You can still override this by explicitly passing a label: `GetPromptAsync("ChatTitle", label: "production")` will use the "production" label instead
68+
- If no label is configured and none is provided, Langfuse will use its default behavior (typically "production")
69+
6570
---
6671

6772
## Usage Example

Services/PromptService.cs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,18 @@ public async Task<ChatPromptResponse> CreateChatPromptAsync(CreateChatPromptRequ
163163

164164
var logicalKey = promptKey; // keep original for local fallback
165165

166-
// If the promptKey matches a configured logical key, use its mapping (string -> actual langfuse key)
167-
var promptKeys = _defaultPromptsProvider.GetPromptKeys();
168-
string actualKey;
169-
if (promptKeys != null && promptKeys.TryGetValue(logicalKey, out var mappedConfig) && mappedConfig is PromptConfiguration config && !string.IsNullOrWhiteSpace(config.Key))
170-
{
171-
actualKey = config.Key;
172-
}
173-
else
174-
{
175-
actualKey = logicalKey;
176-
}
166+
// Resolve the actual Langfuse key and effective label
167+
var (actualKey, effectiveLabel) = ResolvePromptKeyAndLabel(logicalKey, label);
177168

178169
// If Langfuse is configured, try to fetch from it first
179170
if (_langfuseOptions.Value.IsConfigured())
180171
{
181172
try
182173
{
183174
_logger.LogInformation("Fetching prompt '{LogicalKey}' -> '{ActualKey}' (version: {Version}, label: {Label}) from Langfuse",
184-
logicalKey, actualKey, version, label);
175+
logicalKey, actualKey, version, effectiveLabel);
185176

186-
var langfusePrompt = await _langfuseService.GetPromptAsync(actualKey, version, label, cancellationToken);
177+
var langfusePrompt = await _langfuseService.GetPromptAsync(actualKey, version, effectiveLabel, cancellationToken);
187178

188179
if (langfusePrompt != null)
189180
{
@@ -230,27 +221,18 @@ public async Task<ChatPromptResponse> CreateChatPromptAsync(CreateChatPromptRequ
230221

231222
var logicalKey = promptKey;
232223

233-
// If the promptKey matches a configured logical key, use its mapping
234-
var promptKeys = _defaultPromptsProvider.GetPromptKeys();
235-
string actualKey;
236-
if (promptKeys != null && promptKeys.TryGetValue(logicalKey, out var mappedConfig) && mappedConfig is PromptConfiguration config && !string.IsNullOrWhiteSpace(config.Key))
237-
{
238-
actualKey = config.Key;
239-
}
240-
else
241-
{
242-
actualKey = logicalKey;
243-
}
224+
// Resolve the actual Langfuse key and effective label
225+
var (actualKey, effectiveLabel) = ResolvePromptKeyAndLabel(logicalKey, label);
244226

245227
// If Langfuse is configured, try to fetch from it first
246228
if (_langfuseOptions.Value.IsConfigured())
247229
{
248230
try
249231
{
250232
_logger.LogInformation("Fetching chat prompt '{LogicalKey}' -> '{ActualKey}' (version: {Version}, label: {Label}) from Langfuse",
251-
logicalKey, actualKey, version, label);
233+
logicalKey, actualKey, version, effectiveLabel);
252234

253-
var langfusePrompt = await _langfuseService.GetChatPromptAsync(actualKey, version, label, cancellationToken);
235+
var langfusePrompt = await _langfuseService.GetChatPromptAsync(actualKey, version, effectiveLabel, cancellationToken);
254236

255237
if (langfusePrompt != null)
256238
{
@@ -398,6 +380,29 @@ public async Task<PromptResponse> UpdatePromptLabelsAsync(
398380
}
399381
}
400382

383+
private (string actualKey, string? effectiveLabel) ResolvePromptKeyAndLabel(string logicalKey, string? providedLabel)
384+
{
385+
var promptKeys = _defaultPromptsProvider.GetPromptKeys();
386+
string actualKey;
387+
string? effectiveLabel = providedLabel; // Start with the provided label
388+
389+
if (promptKeys != null && promptKeys.TryGetValue(logicalKey, out var mappedConfig) && mappedConfig is PromptConfiguration config && !string.IsNullOrWhiteSpace(config.Key))
390+
{
391+
actualKey = config.Key;
392+
// If no label was explicitly provided, use the configured label (if any)
393+
if (providedLabel == null && !string.IsNullOrWhiteSpace(config.Label))
394+
{
395+
effectiveLabel = config.Label;
396+
}
397+
}
398+
else
399+
{
400+
actualKey = logicalKey;
401+
}
402+
403+
return (actualKey, effectiveLabel);
404+
}
405+
401406
private PromptResponse? GetPromptFromDefaults(string promptKey)
402407
{
403408
var defaults = _defaultPromptsProvider.GetDefaults();

0 commit comments

Comments
 (0)