EN

定向刺激:完整指南与示例

掌握定向刺激——通过提示和线索引导大语言模型,实现精准输出

返回教程列表🌐 Read in English
进阶12 分钟

定向刺激:完整指南与示例

掌握定向刺激——通过提示和线索引导大语言模型,实现精准输出

定向刺激:完整指南 什么是定向刺激? 定向刺激是一种通过提示和线索引导大语言模型的提示技术,特别适用于精准输出。 何时使用定向刺激 使

定向刺激:完整指南

什么是定向刺激?

定向刺激是一种通过提示和线索引导大语言模型的提示技术。它特别适用于精准输出。

何时使用定向刺激

在以下情况下使用此技术:

  • 你需要精准输出
  • 标准提示结果不一致
  • 任务需要通过提示和线索引导大语言模型
  • 你希望获得更可靠、结构化的输出
  • 工作原理

    定向刺激的核心思路:

  • 设置:准备带有定向刺激结构的提示
  • 执行:使用适当参数发送给大语言模型
  • 解析:提取结构化响应
  • 验证:检查输出质量和格式
  • 基本示例

    python
    from openai import OpenAI

    client = OpenAI()

    def directional_stimulus_prompt(task: str, context: str = "") -> str: """应用定向刺激技术。""" # 定向刺激提示结构 system = """你是一位专家级AI助手。 对每个任务应用系统化推理。 做到精确、准确、结构清晰。""" # 定向刺激的核心提示 prompt = f"""任务:{task} {"上下文:" + context if context else ""}

    请通过提示和线索引导大语言模型,准确完成此任务。""" response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": system}, {"role": "user", "content": prompt} ], temperature=0.3, max_tokens=1500 ) return response.choices[0].message.content

    使用示例

    result = directional_stimulus_prompt( task="分析微服务架构的优缺点", context="针对一个拥有5名开发者和1000名用户的初创公司" ) print(result)

    高级实现

    python
    from pydantic import BaseModel
    from typing import Optional

    class PromptResult(BaseModel): output: str technique: str = "Directional Stimulus" confidence: Optional[float] = None reasoning: Optional[str] = None

    class DirectionalStimulusPrompter: """生产级定向刺激实现。""" def __init__(self, model: str = "gpt-4o"): self.client = OpenAI() self.model = model self.technique = "Directional Stimulus" def run(self, task: str, **kwargs) -> PromptResult: """执行定向刺激提示。""" response = self.client.chat.completions.create( model=self.model, messages=self._build_messages(task, **kwargs), temperature=kwargs.get("temperature", 0.3), max_tokens=kwargs.get("max_tokens", 2000) ) content = response.choices[0].message.content return PromptResult( output=content, technique=self.technique ) def _build_messages(self, task: str, **kwargs) -> list[dict]: """构建定向刺激专用消息。""" system = f"""你是一位使用{self.technique}解决任务的专家。 系统化应用{desc}。 格式:提供清晰、结构化的响应。""" return [ {"role": "system", "content": system}, {"role": "user", "content": self._build_prompt(task, **kwargs)} ] def _build_prompt(self, task: str, **kwargs) -> str: """构建定向刺激的专用提示。""" return f"""使用{self.technique}完成以下任务:

    任务:{task}

    应用{desc}以获得高质量答案。"""

    使用

    prompter = DirectionalStimulusPrompter() result = prompter.run("编写一个安全解析JSON的Python函数") print(result.output)

    真实用例

    用例1:精准输出

    python
    

    专用于精准输出

    prompter = DirectionalStimulusPrompter(model="gpt-4o")

    示例:精准输出任务

    result = prompter.run( f"解决这个精准输出问题:[你的具体问题]" ) print(f"解决方案:{result.output}")

    用例2:内容生成

    python
    

    应用于内容创作

    result = prompter.run( "写一篇关于AI代理的技术博客引言", temperature=0.7, # 创意任务使用较高温度 max_tokens=500 ) print(result.output)

    与其他技术的比较

    技术最适合Token数可靠性

    标准提示简单任务低中等 定向刺激精准输出中等高 思维链数学/逻辑高高 少样本提示格式任务高非常高

    常见错误

    效果评估

    python
    import json
    from statistics import mean

    def evaluate_prompt_quality( prompter: DirectionalStimulusPrompter, test_cases: list[dict], n_runs: int = 3 ) -> dict: """通过多次运行评估提示质量。""" scores = [] for test in test_cases: run_scores = [] for _ in range(n_runs): result = prompter.run(test["task"]) # 根据预期输出评分 score = 1.0 if test.get("expected") in result.output else 0.5 run_scores.append(score) scores.append(mean(run_scores)) return { "technique": "Directional Stimulus", "avg_score": mean(scores), "test_cases": len(test_cases) }

    资源

    相关工具

    openaianthropicpython