Token-Optimierung: Prompt Caching + Tool-Result-Truncation in History
This commit is contained in:
1 parent
ec5917ef63
commit
f74409dd89
12 files changed
+220
-59
No files matched your search
@@ -77,36 +77,7 @@ public static class ChatEndpoint
|
||||
res.Headers["Cache-Control"] = "no-cache";
|
||||
res.Headers["Connection"] = "keep-alive";
|
||||
|
||||
var promptParts = new List<string>();
|
||||
promptParts.Add($"Du bist {agent.Name}.");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(agent.Persona))
|
||||
{
|
||||
promptParts.Add($"## Persona\n{agent.Persona}");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(agent.Instructions))
|
||||
{
|
||||
promptParts.Add($"## Instructions\n{agent.Instructions}");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(session.Workspace))
|
||||
{
|
||||
promptParts.Add(@$"## Tools
|
||||
Du hast Tools zur Verfügung (read_file, write_file, edit_file, list_files, delete_file, execute). Benutze sie um Dateien zu lesen, schreiben und bearbeiten.
|
||||
|
||||
Der Workspace-Pfad ist: `{session.Workspace}`
|
||||
Alle Dateipfade in Tool-Aufrufen müssen relativ zu diesem Workspace sein oder absolute Pfade verwenden, die mit diesem Pfad beginnen.
|
||||
|
||||
Wenn der User auf eine bestimmte Zeile in einer Datei verweist (z.B. `[Datei: foo.txt:11]`), zähle Leerzeilen immer mit. Verwende `cat -n` oder ein ähnliches Tool um Zeilennummern exakt zu bestimmen - NIEMALS Leerzeilen beim Zählen überspringen.");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(session.Context))
|
||||
{
|
||||
promptParts.Add($"## Kontext\n{session.Context}");
|
||||
}
|
||||
|
||||
var systemPromptText = string.Join("\n\n", promptParts);
|
||||
var systemPromptText = SystemPromptBuilder.Build(agent, session);
|
||||
|
||||
var chainId = Guid.NewGuid().ToString("N");
|
||||
|
||||
@@ -171,9 +142,11 @@ Wenn der User auf eine bestimmte Zeile in einer Datei verweist (z.B. `[Datei: fo
|
||||
}, attachmentRecords);
|
||||
}
|
||||
|
||||
// cache_control markiert den System-Prompt als Cache-Breakpoint.
|
||||
// Anthropic cached alles bis einschließlich dieses Blocks für 5 Minuten → 90% Rabatt auf diese Tokens.
|
||||
var systemPrompt = new List<object>
|
||||
{
|
||||
new { type = "text", text = systemPromptText }
|
||||
new { type = "text", text = systemPromptText, cache_control = new { type = "ephemeral" } }
|
||||
};
|
||||
|
||||
var workspace = session.Workspace;
|
||||
|
||||
@@ -121,18 +121,7 @@ public static class SessionEndpoint
|
||||
return;
|
||||
}
|
||||
|
||||
var promptParts = new List<string>();
|
||||
promptParts.Add($"Du bist {agent.Name}.");
|
||||
if (!string.IsNullOrWhiteSpace(agent.Persona))
|
||||
promptParts.Add($"## Persona\n{agent.Persona}");
|
||||
if (!string.IsNullOrWhiteSpace(agent.Instructions))
|
||||
promptParts.Add($"## Instructions\n{agent.Instructions}");
|
||||
if (!string.IsNullOrWhiteSpace(session.Workspace))
|
||||
promptParts.Add("## Tools\nDu hast Tools zur Verfügung (read_file, write_file, edit_file, list_files, delete_file, execute). Benutze sie um Dateien zu lesen, schreiben und bearbeiten. Beschreibe NICHT was du tun würdest - tu es einfach über die Tools.");
|
||||
if (!string.IsNullOrWhiteSpace(session.Context))
|
||||
promptParts.Add($"## Kontext\n{session.Context}");
|
||||
|
||||
var systemPrompt = string.Join("\n\n", promptParts);
|
||||
var systemPrompt = SystemPromptBuilder.Build(agent, session);
|
||||
|
||||
ctx.Response.ContentType = "text/plain; charset=utf-8";
|
||||
await ctx.Response.WriteAsync(systemPrompt);
|
||||
|
||||
Reference in new issue
Block a user