arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2602.21257cs.CLcs.DBcs.PL
收藏

结构化提示语言:面向大语言模型的声明式上下文管理

Structured Prompt Language: Declarative Context Management for LLMs

Wen G. Gong

AI总结:

结构化提示语言SPL通过声明式框架管理大语言模型的上下文资源,实现高效提示生成和多模型协同,减少65%的模板开销,提升成本透明度和执行效率。

摘要:

我们介绍了SPL(结构化提示语言),一种受SQL启发的声明式语言,将大语言模型视为生成知识库,将其上下文窗口视为受限资源。SPL提供显式的WITH BUDGET/LIMIT令牌管理,自动查询优化器,类似于SQL的EXPLAIN ANALYZE的EXPLAIN透明性,并在单一声明式框架中原生集成检索增强生成(RAG)和持久内存。SPL-flow将SPL扩展为具有三级提供者回退策略的健壮代理流水线(Ollama -> OpenRouter -> 自愈重试),完全透明于.spl脚本。五个扩展展示了该范式的广度:(1)Text2SPL(多语言NL->SPL翻译);(2)混合模型(MoM)路由,将每个PROMPT动态分配到领域专家模型;(3)逻辑分块,一种用于超过单个上下文窗口的文档的智能策略——通过SPL现有的CTE语法自然表达,无需新构造,将大型查询分解为Map-Reduce流水线,将注意力成本从O(N^2)降低到O(N^2/k),并在云(并行)或本地硬件(顺序)上运行相同;(4)SPL-flow,一种声明式代理编排层,具有健壮的三级提供者回退;(5)BENCHMARK用于并行多模型比较,具有自动胜者持久化。我们提供了一个形式化的EBNF语法,两个可安装的Python包(spl-llm,spl-flow),以及与Prompty、DSPy和LMQL的比较。SPL平均减少了65%的提示模板,揭示了模型层级间的68倍成本差异作为预执行信号,并在OpenRouter上以$0.002或本地Ollama实例上以零边际成本运行相同的.spl脚本——无需修改。

英文摘要:

We present SPL (Structured Prompt Language), a declarative SQL-inspired language that treats large language models as generative knowledge bases and their context windows as constrained resources. SPL provides explicit WITH BUDGET/LIMIT token management, an automatic query optimizer, EXPLAIN transparency analogous to SQL's EXPLAIN ANALYZE, and native integration of retrieval-augmented generation (RAG) and persistent memory in a single declarative framework. SPL-flow extends SPL into resilient agentic pipelines with a three-tier provider fallback strategy (Ollama -> OpenRouter -> self-healing retry) fully transparent to the .spl script. Five extensions demonstrate the paradigm's breadth: (1) Text2SPL (multilingual NL->SPL translation); (2) Mixture-of-Models (MoM) routing that dispatches each PROMPT to a domain-specialist model at runtime; (3) Logical Chunking, an intelligent strategy for documents exceeding a single context window--expressed naturally through SPL's existing CTE syntax with no new constructs, decomposing a large query into a Map-Reduce pipeline that reduces attention cost from O(N^2) to O(N^2/k) and runs identically on cloud (parallel) or local hardware (sequential); (4) SPL-flow, a declarative agentic orchestration layer with resilient three-tier provider fallback; and (5) BENCHMARK for parallel multi-model comparison with automatic winner persistence. We provide a formal EBNF grammar, two pip-installable Python packages (spl-llm, spl-flow), and comparison against Prompty, DSPy, and LMQL. SPL reduces prompt boilerplate by 65% on average, surfaces a 68x cost spread across model tiers as a pre-execution signal, and runs the identical .spl script at $0.002 on OpenRouter or at zero marginal cost on a local Ollama instance--without modification.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑