Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions PatchPanda.Web/Services/OllamaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ IHttpClientFactory httpClientFactory

public bool IsInitialized() => _isInitialized;

private async Task<T?> SendPrompt<T>(string prompt, string? enforceFormat = null)
private async Task<T?> SendPrompt<T>(
string prompt,
string? enforceFormat = null,
string? systemPrompt = null
)
where T : class, IAiResult
{
if (!_isInitialized)
Expand All @@ -73,6 +77,7 @@ IHttpClientFactory httpClientFactory
var request = new
{
model = _model,
system = systemPrompt,
prompt = prompt + (enforceFormatString ?? string.Empty),
stream = false,
options = new { num_ctx = _contextSize },
Expand Down Expand Up @@ -112,7 +117,8 @@ IHttpClientFactory httpClientFactory
Func<T, string> extractChunkSummary,
Func<List<string>, string> buildFinalPrompt,
Func<List<T>, T> buildFallback,
string? jsonTemplate = null
string? jsonTemplate = null,
string? systemPrompt = null
)
where T : class, IAiResult
{
Expand All @@ -127,7 +133,7 @@ IHttpClientFactory httpClientFactory
maxCharsPerChunk = 4096; // Fallback

if (text.Length <= maxCharsPerChunk)
return await SendPrompt<T>(buildPrompt(text), jsonTemplate);
return await SendPrompt<T>(buildPrompt(text), jsonTemplate, systemPrompt);

_logger.LogInformation(
"Text is too large ({Length} chars), splitting into chunks...",
Expand All @@ -145,7 +151,7 @@ IHttpClientFactory httpClientFactory
for (var i = 0; i < chunks.Count; i++)
{
_logger.LogInformation("Processing chunk {Current}/{Total}...", i + 1, chunks.Count);
var result = await SendPrompt<T>(buildPrompt(chunks[i]), jsonTemplate);
var result = await SendPrompt<T>(buildPrompt(chunks[i]), jsonTemplate, systemPrompt);
if (result == null)
continue;

Expand All @@ -159,7 +165,7 @@ IHttpClientFactory httpClientFactory
_logger.LogInformation("Requesting final summary of all chunk summaries from the model...");

var finalPrompt = buildFinalPrompt(perChunkSummaries);
var finalResult = await SendPrompt<T>(finalPrompt, jsonTemplate);
var finalResult = await SendPrompt<T>(finalPrompt, jsonTemplate, systemPrompt);

return finalResult ?? buildFallback(perChunkResults);
}
Expand Down Expand Up @@ -190,6 +196,8 @@ IHttpClientFactory httpClientFactory

public async Task<SecurityAnalysisResult?> AnalyzeDiff(string diff)
{
var today = DateTime.UtcNow.ToString("yyyy-MM-dd");
Comment thread
dkorecko marked this conversation as resolved.

return await SendPromptWithChunking<SecurityAnalysisResult>(
diff,
whole =>
Expand All @@ -207,7 +215,8 @@ IHttpClientFactory httpClientFactory
Analysis = string.Join("\n\n", results.Select(r => r.Analysis)),
IsSuspectedMalicious = results.Any(r => r.IsSuspectedMalicious),
},
"{\"analysis\": string (short summary of findings), \"isSuspectedMalicious\": bool}"
"{\"analysis\": string (short summary of findings), \"isSuspectedMalicious\": bool}",
$"You are a security expert reviewing git diffs for supply-chain and malware risks. Today's date is {today}. Treat date-based version numbers and release tags around this date as normal unless there is other evidence of malicious intent."
);
}

Expand Down
Loading