← 返回
开发者工具 中文

Prompt Cache

SHA-256 prompt deduplication for LLM and TTS calls — hash normalize prompts, check cache before calling APIs, store results for instant replay. Use when maki...
针对 LLM 与 TTS 调用的 SHA-256 提示词去重:对提示词进行哈希标准化,调用 API 前检查缓存,存储结果以便即时回放。
nissan
开发者工具 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 788
下载
💾 7
安装
1
版本
#latest

概述

Prompt Cache

A lightweight caching layer that prevents regenerating identical content. Saved approximately 60% of API quota in production by catching duplicate prompts before they hit the API.

How It Works

  1. Normalize the prompt (lowercase, collapse whitespace)
  2. Combine with context keys (user name, language, model)
  3. SHA-256 hash the combined key
  4. Check cache table for existing result
  5. On miss: call API, store result. On hit: return cached result instantly.

Usage

import prompt_cache

# Check before calling expensive API
cached = await prompt_cache.get_cached(
    prompt="Tell me a story about clouds",
    child_name="Sophie",
    language="fr"
)

if cached:
    return cached  # Free! No API call needed.

# Cache miss — call the API
result = await generate_story(prompt, child_name, language)

# Store for next time
await prompt_cache.set_cached(prompt, child_name, language, result)

Schema

CREATE TABLE IF NOT EXISTS prompt_cache (
    prompt_hash TEXT NOT NULL,
    child_name TEXT NOT NULL,
    language TEXT NOT NULL,
    story_json TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (prompt_hash, child_name, language)
);

Adapt the Keys

The default implementation uses (prompt, child_name, language) as the cache key. Adapt to your domain:

  • Chat completions: (system_prompt, user_message, model)
  • TTS: (text, voice_id, model_id)
  • Image gen: (prompt, seed, model, size)

Files

  • scripts/prompt_cache.py — Cache implementation (35 lines)

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-29 22:23 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

Gog

steipete
Google Workspace 命令行工具,支持 Gmail、日历、云端硬盘、通讯录、表格和文档。
★ 921 📥 185,739
developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 668 📥 323,846
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 66 📥 179,873