【Basic】【response_synthesizers1】自定义提示词响应合成器案例

📅 发布时间:2026/7/15 8:30:31 👁️ 浏览次数:
【Basic】【response_synthesizers1】自定义提示词响应合成器案例
本案例演示如何使用LlamaIndex中的自定义提示词和响应合成器通过TreeSummarize和Refine策略实现不同风格的文本响应生成。1. 案例目标本案例的主要目标是演示如何创建自定义提示词模板用于控制AI响应的风格和格式展示如何使用TreeSummarize和Refine两种不同的响应合成策略实现基于Pydantic模型的结构化输出通过不同风格的提示词参数如莎士比亚风格、俳句风格等生成多样化的响应2. 技术栈与核心依赖LlamaIndex- 用于文档索引、查询和响应合成OpenAI API- 提供大语言模型能力Pydantic- 用于定义结构化数据模型核心依赖安装pip install llama-index3. 环境配置在开始之前需要设置OpenAI API密钥import os import openai os.environ[OPENAI_API_KEY] sk-... openai.api_key os.environ[OPENAI_API_KEY]4. 案例实现4.1 数据准备首先下载并加载Paul Graham的文章作为示例数据from llama_index.core import SimpleDirectoryReader # 创建目录并下载数据 !mkdir -p data/paul_graham/ !wget https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt -O data/paul_graham/paul_graham_essay.txt # 加载数据 reader SimpleDirectoryReader( input_files[./data/paul_graham/paul_graham_essay.txt] ) docs reader.load_data() text docs[0].text4.2 自定义提示词模板创建自定义的问答提示词和优化提示词模板from llama_index.core import PromptTemplate # 问答提示词模板添加了tone_name变量用于控制风格 qa_prompt_tmpl ( Context information is below.\n ---------------------\n {context_str}\n ---------------------\n Given the context information and not prior knowledge, answer the query.\n Please also write the answer in the style of {tone_name}.\n Query: {query_str}\n Answer: ) qa_prompt PromptTemplate(qa_prompt_tmpl) # 优化提示词模板用于改进已有答案 refine_prompt_tmpl ( The original query is as follows: {query_str}\n We have provided an existing answer: {existing_answer}\n We have the opportunity to refine the existing answer (only if needed) with some more context below.\n ------------\n {context_msg}\n ------------\n Given the new context, refine the original answer to better answer the query. Please also write the answer in the style of {tone_name}.\n If the context isnt useful, return the original answer.\n Refined Answer: ) refine_prompt PromptTemplate(refine_prompt_tmpl)4.3 使用TreeSummarize响应合成器使用TreeSummarize策略生成莎士比亚风格的响应from llama_index.core.response_synthesizers import TreeSummarize # 创建TreeSummarize实例使用自定义提示词 summarizer TreeSummarize(verboseTrue, summary_templateqa_prompt) # 生成响应 response summarizer.get_response( who is Paul Graham?, [text], tone_namea Shakespeare play ) print(str(response))4.4 使用Refine响应合成器使用Refine策略生成俳句风格的响应from llama_index.core.response_synthesizers import Refine # 创建Refine实例使用自定义提示词 summarizer Refine( verboseTrue, text_qa_templateqa_prompt, refine_templaterefine_prompt ) # 生成响应 response summarizer.get_response( who is Paul Graham?, [text], tone_namea haiku ) print(str(response))4.5 使用Pydantic模型的结构化输出定义Pydantic模型并生成结构化响应from llama_index.core.types import BaseModel from typing import List # 定义Pydantic模型 class Biography(BaseModel): Data model for a biography. name: str best_known_for: List[str] extra_info: str # 创建TreeSummarize实例指定输出模型 summarizer TreeSummarize( verboseTrue, summary_templateqa_prompt, output_clsBiography ) # 生成结构化响应 response summarizer.get_response( who is Paul Graham?, [text], tone_namea business memo ) print(str(response))5. 案例效果运行上述代码后我们可以看到以下效果5.1 莎士比亚风格响应Paul Graham, a noble and esteemed gentleman, is a man of many talents and accomplishments. He hath traversed the realms of art, entrepreneurship, and writing, leaving a lasting impact on each. With his brush, he hath brought life to canvases, capturing the essence of what he saw. In the realm of technology, he hath revolutionized the way we do business, founding Viaweb and bringing the power of the web to entrepreneurs and artists alike. His wisdom and guidance hath shaped the future of technology and entrepreneurship through his co-founding of Y Combinator. But above all, Paul Graham is a visionary, a trailblazer, and a true Renaissance man, whose intellectual curiosity and quest for lasting creation hath inspired generations to come.5.2 俳句风格响应Paul Graham, a web pioneer,Co-founded Y Combinator,But stepped down to ensure,Long-term success and more.5.3 结构化输出namePaul Graham best_known_for[Co-founder of Y Combinator, Writer, Investor] extra_infoPaul Graham is a renowned entrepreneur, writer, and investor. He is best known as the co-founder of Y Combinator, a highly successful startup accelerator. Graham has played a significant role in shaping the startup ecosystem and has been instrumental in the success of numerous startups. He is also a prolific writer, known for his insightful essays on a wide range of topics, including technology, startups, and entrepreneurship. Grahams writings have been widely read and have had a profound impact on the tech community. In addition to his work with Y Combinator and his writing, Graham is also an active investor, providing seed funding and mentorship to early-stage startups. His contributions to the startup world have earned him a reputation as one of the most influential figures in the industry.6. 案例实现思路本案例的核心实现思路包括6.1 提示词模板化通过将提示词模板化并在其中插入变量如{tone_name}实现了动态控制响应风格的能力。这种方法使得同一套代码可以生成不同风格的响应大大提高了代码的复用性。6.2 响应合成策略选择案例展示了两种主要的响应合成策略TreeSummarize通过构建树形结构来组织和总结信息适合处理大量文本数据Refine通过迭代优化已有答案来生成更准确的响应适合需要逐步完善答案的场景6.3 结构化输出通过使用Pydantic模型定义输出结构使AI能够生成符合特定格式的结构化数据这对于需要将AI输出集成到应用程序中的场景非常有用。7. 扩展建议多风格提示词库可以创建一个包含多种风格的提示词库用户可以通过参数选择不同的响应风格动态风格切换根据查询内容自动选择最适合的响应风格例如技术问题使用专业风格创意问题使用文学风格多语言支持扩展提示词模板以支持多语言输出满足不同语言用户的需求响应质量评估添加自动评估机制对不同风格生成的响应进行质量评分帮助用户选择最佳答案交互式风格调整开发交互式界面允许用户实时调整响应风格参数并即时查看效果上下文感知风格根据文档内容的类型自动调整响应风格例如对文学作品使用文学风格对技术文档使用技术风格8. 总结本案例展示了如何通过自定义提示词和响应合成器实现灵活多样的AI响应生成。通过模板化提示词、选择不同的响应合成策略以及使用结构化输出我们能够控制AI响应的风格、格式和内容使其更好地满足特定应用场景的需求。这种方法不仅提高了AI应用的灵活性也为构建更加智能和个性化的AI系统奠定了基础。