first add
This commit is contained in:
34
src/fgai/llm.py
Normal file
34
src/fgai/llm.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from urllib import request
|
||||
|
||||
from .models import BlockCandidate, Finding
|
||||
|
||||
|
||||
def ollama_summary(findings: list[Finding], candidates: list[BlockCandidate], model: str | None = None) -> str:
|
||||
host = os.getenv("OLLAMA_HOST", "http://127.0.0.1:11434").rstrip("/")
|
||||
selected_model = model or os.getenv("OLLAMA_MODEL", "llama3.3")
|
||||
prompt = {
|
||||
"findings": [finding.__dict__ for finding in findings],
|
||||
"block_candidates": [
|
||||
{"src_ip": candidate.src_ip, "score": candidate.score, "reasons": candidate.reasons}
|
||||
for candidate in candidates
|
||||
],
|
||||
}
|
||||
body = json.dumps(
|
||||
{
|
||||
"model": selected_model,
|
||||
"stream": False,
|
||||
"prompt": (
|
||||
"You are a local FortiGate security analyst. Summarize these policy findings "
|
||||
"and UTM block candidates. Be concise, include risk, likely cause, and next action. "
|
||||
f"Data: {json.dumps(prompt)}"
|
||||
),
|
||||
}
|
||||
).encode("utf-8")
|
||||
req = request.Request(f"{host}/api/generate", data=body, method="POST", headers={"Content-Type": "application/json"})
|
||||
with request.urlopen(req, timeout=60) as response:
|
||||
data = json.loads(response.read().decode("utf-8"))
|
||||
return str(data.get("response", "")).strip()
|
||||
Reference in New Issue
Block a user