
Instructor 高级提示工程实战指南58 种研究支持的 Prompting 技术及其结构化输出实现【免费下载链接】instructorstructured outputs for llms项目地址: https://gitcode.com/GitHub_Trending/in/instructor导读本指南系统梳理 Instructor 项目structured outputs for LLMs内置的 58 种研究支持的提示Prompting技术覆盖零样本、少样本、思维生成、集成、自我批判与问题分解六大类并给出每种技术对应到 Instructor 结构化输出管线的可运行实现。读完本文你将能够根据是否拥有示例、是否需要推理、问题是否复杂、是否需要验证、是否想要多视角五个决策维度快速选择最合适的提示策略并用 Pydantic 模型 Instructor 客户端把任意提示技术固化成可验证、可复用的生产级代码。本指南内容基于 docs/prompting/index.md 展开其技术分类框架源自《The Prompt Report: A Systematic Survey of Prompting Techniques》对 1,500 篇学术论文的系统分析每个分类下都有配套的深度子文档与对应源码示例可继续深入阅读。提示技术地图如何选择合适的技术对于给定的任务提示技术并非越多越好而是需要依据任务特征进行决策。docs/prompting/index.md中给出了一张决策流程图按五个关键问题逐层路由决策逻辑可以归纳为五个层次有没有示例可用没有则走 Zero-Shot 路线角色设定、情绪语言、风格定义等有则走 Few-Shot 路线示例排序、示例选择、示例生成。是否需要推理需要时引入 Thought Generation 家族Chain of Thought 及其零样本/少样本变体。问题是否复杂复杂问题应分解为子问题Decomposition 家族如 Plan and Solve、Tree of Thought。是否需要验证需要可靠性与事实校验时叠加 Self-Criticism 家族。是否想要多视角希望聚合多个输出时采用 Ensembling 家族Self-Consistency 等。按目标快速选型docs/prompting/index.md还给出了面向常见目标的推荐技术组合可直接对照使用目标推荐技术提升准确率Chain of Thought、Self-Verification、Self-Consistency处理复杂问题Decomposition、Tree of Thought、Least-to-Most生成创意内容Role Prompting、Emotional Language、Style Definition验证事实正确性Chain of Verification、Self-Calibration用少量示例优化KNN Example Selection、Active Prompting处理不确定性Uncertainty-Routed CoT、Self-ConsistencyZero-Shot 技术无示例直接提升表现Zero-Shot 家族详见 docs/prompting/zero_shot/在提示中不提供任何示例通过改造提示本身来激发模型能力技术说明适用场景Emotional Language在提示中加入情绪化语气创意写作、共情式回复Role Assignment赋予模型特定角色专家知识、专业视角Style Definition明确指定写作风格特定语气或格式的内容Prompt Refinement自动优化提示词结果的迭代改进Perspective Simulation让模型采纳特定视角多方利益相关者分析Ambiguity Clarification识别并澄清模糊点提升回复精确度Query Repetition让模型复述任务增强任务理解Follow-Up Generation生成澄清性问题主题的深度探索以Role Prompting角色提示为例docs/prompting/zero_shot/role_prompting.md角色可以是与查询相关的You are a talented writer. Write me a poem.也可以是通用/社交的You are a helpful AI assistant.。其 Instructor 实现非常简洁——把角色与查询拼进 system 消息即可返回结果自动约束为 Pydantic 模型import instructor from pydantic import BaseModel client instructor.from_provider(openai/gpt-5-nano) class Response(BaseModel): poem: str def role_prompting(query, role): return client.create( modelgpt-4o, response_modelResponse, messages[ { role: system, content: f{role} {query}, }, ], ) if __name__ __main__: query Write me a short poem about coffee. role You are a renowned poet. response role_prompting(query, role) print(response.poem)关于角色选择的进一步研究方向可参考子文档中提到的 RoleLLM、社交角色评估与多角色自我协作Multi-Persona Self-Collaboration等论文线索。Few-Shot 技术让示例发挥最大价值Few-Shot 家族详见 docs/prompting/few_shot/关注如何构建与排列示例技术说明适用场景Example Generation自动生成示例示例数据有限的领域Example Ordering优化示例排序改进模式识别KNN Example Selection选择与查询相似的示例领域特定准确率Vote-K Selection基于相似度的进阶选择复杂模式匹配其中Example Ordering的核心洞察是LLM 输出对 few-shot 示例的排列顺序高度敏感docs/prompting/few_shot/example_ordering.md选择示例有三种方法组合法Combinatorics人工枚举所有示例组合逐一尝试找到最优组合KATEk-Nearest Example Tuning按语义相似度为每个测试样本检索 K 个最近邻示例再选取在不同查询中出现最频繁的 K 个作为最优上下文示例无监督检索器用大模型为每个示例相对给定提示打一个相关性分数据此构造训练集并训练打分模型从而在用户查询时动态确定最相关与最不相关的 top-k 示例拼入最终提示。Thought Generation引导模型产生类人推理Thought Generation 家族详见 docs/prompting/thought_generation/按是否需要示例分为两组。零样本推理Zero-Shot Reasoning技术说明适用场景Analogical CoT借助类比生成推理复杂问题求解Step-Back Prompting先考虑更高层问题科学、抽象推理Thread of Thought鼓励逐步分析详细解释生成Tabular CoT以表格结构组织推理多因素分析Step-Back Prompting是其中最具代表性的两步式方法docs/prompting/thought_generation/chain_of_thought_zero_shot/step_back_prompting.md抽象Abstraction先让 LLM 回答一个更高层的通用问题step-back question推理Reasoning将抽象问题的答案作为上下文再回答原始问题abstracted-grounded reasoning。示例如下原始问题When temperature and volume of an ideal gas are increased, what happens to its pressure?Step-Back 问题What are the physics concepts associated with this question?推理提示{step-back response} {original question}值得注意step-back 问题本身也是通过一次 LLM 查询生成的。该方法在 PaLM-2L 与 GPT-4 上均被证明能提升推理基准分数。其 Instructor 实现分三步走先用Stepback模型生成抽象问题再用Iterable[Education]流式收集多条目结构化回答最后把抽象问题 答案作为上下文引导Response输出最终答案import instructor from pydantic import BaseModel from typing import Iterable, Literal client instructor.from_provider(openai/gpt-5-nano) class Stepback(BaseModel): original_question: str abstract_question: str class Education(BaseModel): degree: Literal[Bachelors, Masters, PhD] school: str topic: str year: int class Response(BaseModel): school: str def generate_stepback_question(): return client.create( modelgpt-4o, response_modelStepback, messages[ { role: user, content: You are an expert at world knowledge. Your task is to step back and paraphrase a question to a more generic step-back question, which is easier to answer. Here are a few examples: Original Question: Which position did Knox Cunningham hold from May 1955 to Apr 1956? Step-back Question: Which positions has Knox Cunningham held in his career? Original Question: Who was the spouse of Anna Karina from 1968 to 1974? Step-back Question: Who were the spouses of Anna Karina? Now, generate the step-back question for the following question: Estella Leopold went to which school between Aug 1954 and Nov 1954? , }, ], ) def ask_stepback_question(stepback): return client.create( modelgpt-4o, response_modelIterable[Education], messages[ {role: user, content: stepback.abstract_question}, ], ) def get_final_response(stepback, stepback_response): return client.create( modelgpt-4o, response_modelResponse, messages[ { role: user, content: f Q: {stepback.abstract_question}, A: {stepback_response} Q: {stepback.original_question} A: , }, ], ) if __name__ __main__: stepback generate_stepback_question() print(stepback.original_question) # Estella Leopold went to which school between Aug 1954 and Nov 1954? print(stepback.abstract_question) # Which schools did Estella Leopold attend in her life? stepback_response ask_stepback_question(stepback) for item in stepback_response: print(item) # degreeBachelors schoolUniversity of Wisconsin-Madison ... print(get_final_response(stepback, stepback_response)) # schoolYale University少样本推理Few-Shot Reasoning技术说明适用场景Active Prompting标注不确定的示例边缘情况准确率提升Auto-CoT选择多样化示例广泛领域覆盖Complexity-Based CoT使用复杂示例高难度问题类型Contrastive CoT同时包含正确与错误案例错误检测与规避Memory of Thought使用高置信度示例关键应用中的可靠性Uncertainty-Routed CoT选择最确定的推理路径不确定条件下的决策Prompt Mining生成模板化提示高效的提示工程Decomposition复杂问题的拆解之道Decomposition 家族详见 docs/prompting/decomposition/面向需要拆解的复杂任务技术说明适用场景Functional Decomposition将子问题实现为函数模块化问题求解Faithful CoT混合自然语言与符号语言数学推理Least-to-Most逐步求解越来越难的子问题教育类应用Plan and Solve先生成结构化计划项目规划Program of Thought用代码执行推理算法类问题Recursive Thought递归求解子问题层级结构问题Skeleton of Thought生成提纲结构写作、规划Tree of Thought搜索多条可能路径决策树、探索Plan and Solve是对零样本 CoT 提示的改进版本docs/prompting/decomposition/plan_and_solve.md在多个推理数据集上被证明能生成比标准 Zero-Shot CoT 更稳健的推理过程。其提示模板如下Lets first understand the problem, extract relevant variables and their corresponding numerals, and make a complete plan. Then, lets carry out the plan, calculate intermediate variables (pay attention to correct numerical calculation and commonsense), solve the problem step by step, and show the answer.—— 模型回复结尾Therefore the answer (arabic numerals) is该技术引导模型更关注计算与中间结果确保尽可能正确地执行。其完整流程分为两步生成推理Generate Reasoning用 Plan and Solve 提示让模型在生成中间推理前先明确制定求解计划提取答案Extract Answer把上一步的 chain-of-thought 拼入新提示再单独提取最终答案。Instructor 实现将两个阶段分别映射到Reasoning与Response两个 Pydantic 模型通过model_dump_json即可获得干净、可序列化的结构化中间产物import instructor from pydantic import BaseModel client instructor.from_provider(openai/gpt-5-nano) class Reasoning(BaseModel): chain_of_thought: str class Response(BaseModel): correct_answer: str PLAN_AND_SOLVE_PROMPT Lets first understand the problem, extract relevant variables and their corresponding numerals, and make a complete plan. Then, lets carry out the plan, calculate intermediate variables (pay attention to correct numerical calculation and commonsense), solve the problem step by step, and show the answer. def generate_reasoning(query: str): return client.create( messages[ { role: user, content: f user query {query} /user query {PLAN_AND_SOLVE_PROMPT} , }, ], response_modelReasoning, modelgpt-4o, ) def extract_answer(query: str, reasoning: Reasoning): return client.create( messages[ { role: user, content: f user query {query} /user query {PLAN_AND_SOLVE_PROMPT} reasoning {reasoning.chain_of_thought} /reasoning Therefore the answer (arabic numerals) is , } ], modelgpt-4o, response_modelResponse, ) if __name__ __main__: query ( In a dance class of 20 students, 20% enrolled in contemporary dance, 25% of the remaining enrolled in jazz dance and the rest enrolled in hip-hop dance. What percentage of the entire students enrolled in hip-hop dance? ) reasoning generate_reasoning(query) response extract_answer(query, reasoning) print(reasoning.model_dump_json(indent2)) print(response.model_dump_json(indent2)) # {correct_answer: 60}Ensembling聚合多视角输出Ensembling 家族详见 docs/prompting/ensembling/通过组合多个提示或响应来提升稳定性技术说明适用场景Consistent, Diverse Sets构建一致且多样的示例集稳定性能Batched In-Context Examples高效的示例批处理性能优化Step Verification验证每一步复杂工作流Maximizing Mutual Information信息论优化信息密集输出Meta-CoT融合多条推理链复杂问题求解Specialized Experts使用不同的专家提示多领域任务Self-Consistency选择最一致的推理逻辑准确性Universal Self-Consistency领域无关的一致性通用知识任务Task-Specific Selection按任务选择示例专业领域任务Prompt Paraphrasing使用同一提示的多个变体稳健输出Self-Consistency是最常用的聚合策略docs/prompting/ensembling/self_consistency.md并行采样多条候选响应对其中的最终答案做多数投票。它天然适合 Instructor 的异步客户端——先设置temperature0.5保证采样多样性再用asyncio.gather并发发起多个client.create调用最后用Counter统计众数import instructor from pydantic import BaseModel, Field import asyncio from collections import Counter from textwrap import dedent class SelfConsistencyResponse(BaseModel): chain_of_thought: str Field( descriptionreasoning behind the final correct answer ) correct_answer: int client instructor.from_provider(openai/gpt-5-nano, async_clientTrue) async def generate_self_consistent_response(prompt: str): return await client.create( modelgpt-4o, messages[ { role: system, content: You are an intelligent question answering AI system that excels at answering user queries. Make sure to generate a comprehensive explanation of your thought process before providing the final answer, }, {role: user, content: prompt}, ], response_modelSelfConsistencyResponse, temperature0.5, ) async def generate_self_consistent_responses(prompt: str, num_responses: int): coros [generate_self_consistent_response(prompt) for _ in range(num_responses)] responses await asyncio.gather(*coros) return responses if __name__ __main__: prompt dedent( Janets ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder for $2 per egg. How much does she make every day? ) responses asyncio.run(generate_self_consistent_responses(prompt, 5)) answer_counts Counter([response.correct_answer for response in responses]) most_common_answer, _ answer_counts.most_common(1)[0] print(most_common_answer) # 18Self-Criticism让模型自我校验与修正Self-Criticism 家族详见 docs/prompting/self_criticism/让模型验证或改进自身输出技术说明适用场景Chain of Verification生成验证问题事实核查、准确性Self-Calibration询问答案是否正确置信度估计Self-Refinement自动生成反馈并改进迭代改进Self-Verification为多个解打分质量评估Reverse CoT重建问题复杂推理验证Cumulative Reasoning生成所有可能步骤全面分析Self-Verification框架docs/prompting/self_criticism/self_verification.md先批量生成候选回答再用 LLM 验证候选分两个阶段阶段一正向推理Forward Reasoning——利用 CoT 生成多个候选解。阶段二反向验证Backward Verification——包含三个步骤改写为陈述句Rewrite As Declarative把原始问题与候选解改写为陈述句。例如原题Jackie has 10 apples. Adam has 8 apples. How many more apples does Jackie have than Adam?候选解Jackie has 10 apples. so Jackie has 10-82 more apples than Adam, and the answer is 2.改写后Jackie has 10 apples. Adam has 8 apples. Jackie has 2 more apples than Adam.构造新问题Construct New Question两种验证方法——TFVTrue-False Item Verification让 LLM 判断改写陈述是否正确Jackie has 10 apples. Adam has 8 apples. Jackie has 2 more apples than Adam. Is this correct?CMVCondition Mask Verification遮住原题中的条件让 LLM 预测被遮住的值Jackie has X apples. Adam has 8 apples. Jackie has 2 more apples than Adam. What is X?。计算验证分数Compute Verification Score对每个候选用新问题查询 k 次TFV 下分数为输出 True 的次数CMV 下分数为掩码值与真实值匹配的次数。分数最高的候选即为最终答案。Instructor 实现通过Literal[True, False]把验证结果严格约束为布尔语义保证计分逻辑的确定性import instructor from pydantic import BaseModel from typing import Literal client instructor.from_provider(openai/gpt-5-nano) n 3 # Number of candidates to generate k 5 # Number of times to verify class Candidate(BaseModel): reasoning_steps: list[str] month: str class Rewritten(BaseModel): declarative: str class Verification(BaseModel): correct: Literal[True, False] def query_llm(query, model): return client.create( modelgpt-4o, response_modelmodel, messages[{role: user, content: fThink step by step: {query}}], ) def rewrite(query, candidate): return client.create( modelgpt-4o, response_modelRewritten, messages[ { role: user, content: f Please change the questions and answers into complete declarative sentences {query} The answer is {candidate.month}. , } ], ) def verify(question): return client.create( modelgpt-4o, response_modelVerification, messages[{role: user, content: question}], ) if __name__ __main__: query What month is it now if it has been 3 weeks, 10 days, and 2 hours since May 1, 2024 6pm? # Step 1: Forward Reasoning candidates [query_llm(query, Candidate) for _ in range(n)] # Step 2: Backwards Verification for candidate in candidates: rewritten rewrite(query, candidate) question f{rewritten.declarative} Do it is correct (True or False)? scores [verify(question).correct for _ in range(k)] verification_score sum(1 for s in scores if s True) print(fCandidate: {candidate.month}, Verification Score: {verification_score}) # Candidate: May, Verification Score: 0 # Candidate: June, Verification Score: 2统一实现框架Instructor 如何承载这些技术docs/prompting/index.md明确指出以上所有提示技术都可以通过 Instructor 实现统一遵循三步方法论定义合适的 Pydantic 模型来捕获期望的输出结构把提示技术融入模型 docstring 或字段描述如Field(description...)使用打补丁后的 LLM 客户端配合响应模型发起请求。最简单的示范是用字段实现 Chain of Thoughtimport instructor from pydantic import BaseModel, Field class ReasonedAnswer(BaseModel): Answer the following question with detailed reasoning. chain_of_thought: str Field( descriptionStep-by-step reasoning process to solve the problem ) final_answer: str Field( descriptionThe final conclusion after reasoning ) client instructor.from_provider(openai/gpt-5-nano) response client.create( modelgpt-5.4-mini, response_modelReasonedAnswer, messages[ {role: user, content: What is the cube root of 27?} ] ) print(fReasoning: {response.chain_of_thought}) print(fAnswer: {response.final_answer})从源码理解from_provider的统一客户端上述所有示例都依赖instructor.from_provider这一统一入口。从源码instructor/v2/auto_client.py可以看到其工作机制它接受provider/model-name格式的模型字符串例如openai/gpt-5-nano、anthropic/claude-3-sonnet、google/gemini-pro若字符串缺少/或任一部分为空会抛出ConfigurationError可选参数async_clientTrue返回AsyncInstructorSelf-Consistency 等并发场景使用默认返回同步Instructor可选参数cache可注入BaseCache适配器如AutoCache、RedisCache实现透明响应缓存并自动透传到各 Provider 实现可选参数mode可覆盖各 Provider 的默认模式从 instructor/init.py 与 instructor/v2/auto_client.py 的各个_build_*构建器可见v2 下 OpenAI/Anthropic/Cohere 等默认走Mode.TOOLSGemini 默认走Mode.MD_JSON底层通过_PROVIDER_BUILDERS分发到各 Provider 构建器如_build_openai、_build_anthropic并由instructor/__init__.py中的懒加载机制按需导入对应 SDK。与此同时响应侧的 Pydantic 校验由 instructor/v2/core/function_calls.py 中的ResponseSchema与_validate_model_from_json完成它会把模型 JSON 解析后经model_validate或TypeAdapter严格校验不合法即触发重试或异常处理若出现finish_reason lengthOpenAI 格式或stop_reason max_tokensAnthropic 格式等截断信号还会抛出IncompleteOutputException保证下游拿到的永远是完整且符合 schema 的结构化数据。结语把提示技术沉淀为结构化资产提示技术决定模型怎么想结构化输出决定结果怎么用。Instructor 的价值在于把两者无缝衔接无论你选用 Step-Back Prompting 做抽象推理、Plan and Solve 做问题拆解、Self-Consistency 做多数投票还是 Self-Verification 做反向校验最终产出的都是经过 Pydantic 校验的干净对象可直接进入业务逻辑、存储或下游管线。建议的落地路径是先用本文的技术地图判断任务类型 → 参考对应的子文档与示例docs/prompting/ 目录下的 58 份技术文档均有完整可运行代码→ 用from_provider统一客户端 响应模型固化流程。若需进一步了解客户端打补丁、模式选择Mode与响应模型的高级用法可继续阅读 docs/concepts/usage.md 与 docs/modes-comparison.md文中出现的 Role Prompting、Step-Back、Plan and Solve、Self-Verification、Self-Consistency 等完整示例脚本也可以在本仓库的 examples/ 目录与 docs/prompting/ 各子文档中找到对应实现。【免费下载链接】instructorstructured outputs for llms项目地址: https://gitcode.com/GitHub_Trending/in/instructor创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考