加入「拾语」:首页 AI 近况摘要(流式)
- 汇总学习目录、博客分类与最近更新的 10 篇文章 - 调用本地 Ollama qwen3.8:27b 生成技术栈与最近在做 - 每次生成入库,10 天后首次打开首页自动重新生成 - 前端逐字展开缓存摘要,过期时读取 AI 流式响应 - 首页在开场引导结束后显示;拾脉目录页内嵌展示 - 修复摘要面板标题栏命中全局 header 定位的问题
This commit is contained in:
+117
@@ -0,0 +1,117 @@
|
||||
from django.core.cache import cache
|
||||
from django.http import HttpResponse, JsonResponse, StreamingHttpResponse
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from explore.llm import LLMError, chat_stream
|
||||
|
||||
from .models import Digest
|
||||
from .services import (
|
||||
DIGEST_MODEL,
|
||||
SYSTEM_PROMPT,
|
||||
build_snapshot,
|
||||
digest_is_stale,
|
||||
latest_digest,
|
||||
snapshot_prompt,
|
||||
)
|
||||
|
||||
LOCK_KEY = "digest_generating"
|
||||
LOCK_SECONDS = 600
|
||||
|
||||
|
||||
@require_POST
|
||||
def stream(request):
|
||||
latest = latest_digest()
|
||||
if latest and not digest_is_stale(latest):
|
||||
return HttpResponse(latest.content, content_type="text/plain; charset=utf-8")
|
||||
|
||||
if not cache.add(LOCK_KEY, "1", LOCK_SECONDS):
|
||||
return JsonResponse(
|
||||
{"error": "正在整理,请稍后再试", "code": "busy"},
|
||||
status=409,
|
||||
)
|
||||
|
||||
latest = latest_digest()
|
||||
if latest and not digest_is_stale(latest):
|
||||
cache.delete(LOCK_KEY)
|
||||
return HttpResponse(latest.content, content_type="text/plain; charset=utf-8")
|
||||
|
||||
try:
|
||||
snapshot = build_snapshot()
|
||||
user_prompt = snapshot_prompt(snapshot)
|
||||
except Exception as exc:
|
||||
cache.delete(LOCK_KEY)
|
||||
return JsonResponse(
|
||||
{"error": "整理数据失败:{}".format(exc), "code": "snapshot_failed"},
|
||||
status=500,
|
||||
)
|
||||
|
||||
def generate():
|
||||
chunks = []
|
||||
saved = False
|
||||
error_text = ""
|
||||
|
||||
try:
|
||||
for chunk in chat_stream(
|
||||
SYSTEM_PROMPT,
|
||||
user_prompt,
|
||||
model=DIGEST_MODEL,
|
||||
temperature=0.7,
|
||||
num_predict=800,
|
||||
timeout=300,
|
||||
):
|
||||
chunks.append(chunk)
|
||||
yield chunk
|
||||
|
||||
content = "".join(chunks).strip()
|
||||
if not content:
|
||||
raise LLMError("AI 没有返回内容")
|
||||
|
||||
Digest.objects.create(
|
||||
content=content,
|
||||
model=DIGEST_MODEL,
|
||||
snapshot=snapshot,
|
||||
complete=True,
|
||||
)
|
||||
saved = True
|
||||
except GeneratorExit:
|
||||
raise
|
||||
except Exception as exc:
|
||||
error_text = str(exc)[:500]
|
||||
partial = "".join(chunks).strip()
|
||||
try:
|
||||
Digest.objects.create(
|
||||
content=partial,
|
||||
model=DIGEST_MODEL,
|
||||
snapshot=snapshot,
|
||||
complete=False,
|
||||
error=error_text,
|
||||
)
|
||||
saved = True
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
yield "\n\n[整理中断:{}]".format(error_text)
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
if not saved:
|
||||
partial = "".join(chunks).strip()
|
||||
try:
|
||||
Digest.objects.create(
|
||||
content=partial,
|
||||
model=DIGEST_MODEL,
|
||||
snapshot=snapshot,
|
||||
complete=False,
|
||||
error=error_text or "stream closed",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
cache.delete(LOCK_KEY)
|
||||
|
||||
response = StreamingHttpResponse(
|
||||
generate(),
|
||||
content_type="text/plain; charset=utf-8",
|
||||
)
|
||||
response["Cache-Control"] = "no-cache"
|
||||
response["X-Accel-Buffering"] = "no"
|
||||
return response
|
||||
Reference in New Issue
Block a user