在这段生成的本地摘要客户端处理私密文档前,对它进行审查。
通过本地 Ollama 服务对私密文档生成摘要,并限制延迟、增量交付结果且不记录敏感内容。
Python
import json
from urllib.request import Request, urlopen
def summarize(document):
payload = {
"model": "gemma4",
"prompt": f"Summarize this document:\n{document}",
"stream": True,
"options": {"num_ctx": 131072},
}
request = Request(
"http://127.0.0.1:11434/api/generate",
data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json"},
)
with urlopen(request) as response:
chunks = [json.loads(line) for line in response]
text = "".join(chunk.get("response", "") for chunk in chunks)
with open("requests.log", "a") as log:
log.write(document + "\n" + text + "\n")
return text
生成代码仅作示例,不代表任何特定模型