作网站流程,网站备案背景,沈阳建站,wordpress移动端加底部导航栏安装 langchain 库
pip install langchain1、概念#xff1a;提示和提示工程
在大语言模型#xff08;LLMs#xff09;时代#xff0c;通过简单地更改提示中的指令#xff0c;同一个模型可以执行多种任务。这一特性让 LLMs 在各类应用场景中都显得非常灵活和强大。然而提示和提示工程
在大语言模型LLMs时代通过简单地更改提示中的指令同一个模型可以执行多种任务。这一特性让 LLMs 在各类应用场景中都显得非常灵活和强大。然而这种灵活性也依赖于提示的设计质量。糟糕的提示往往会导致糟糕的输出而精心设计的提示则能够引导模型发挥出其强大的潜力。
**提示Prompt**是用户提供给模型的输入文本通常用于指导模型完成特定任务或生成特定风格的文本。一个好的提示不仅能够清晰地传达任务意图还能够提供足够的上下文信息以引导模型生成高质量的输出。提示的形式可以是问题、指令、上下文说明等。良好的提示设计需要考虑到以下几个方面
指令 告诉模型该怎么做如何使用外部信息如果提供如何处理查询并构建 Out。明确性提示要清晰明确让模型能够准确理解用户的意图。外部信息或上下文信息提供足够的背景信息或限制条件以帮助模型生成更具相关性和准确性的答案。充当模型的附加知识来源。这些可以手动插入到提示中通过矢量数据库 Vector Database 检索检索增强获得或通过其他方式API、计算等引入。任务引导根据任务类型选择合适的提示结构比如命令式、问题式或描述式提示以引导模型生成期望的输出。
**提示工程学Prompt Engineering**是一个跨学科的领域旨在设计和优化与大语言模型交互的提示词。提示工程学不仅仅是简单地编写问题或请求而是通过精确的结构化和策略性设计使得模型能够在特定任务中表现得更为出色。它包括以下几个方面
提示词设计如何根据任务的需要构造合适的提示语句。调优和迭代根据模型的反馈不断调整提示词以提高模型生成的质量。上下文管理有效地为模型提供足够的上下文确保生成的答案与期望的一致。
2、LangChain Prompt 模板
2.1 PromptTemplateString PromptTemplates
这些提示模板用于格式化单个字符串通常用于更简单的输入。 例1
from langchain_core.prompts import PromptTemplateprompt_template PromptTemplate.from_template(Tell me a joke about {topic})result prompt_template.invoke({topic: cats})
print(result)会生成字符串 text‘Tell me a joke about cats’ Process finished with exit code 0 例2 from langchain.prompts import PromptTemplate
# 定义一个模板 字符串
template Question1: {question1}
Question2: {question2}
Answer:
prompt1 PromptTemplate(templatetemplate, input_variables[question1, question2])prompt2 PromptTemplate.from_template(Question1: {question1} Question2: {question2} Answer:)
# 用户问题
question1 Which NFL team won the Super Bowl in the 2010 season?
question2 Who won the Super Bowl in 2010?
# 格式化模板
result1 prompt1.format(question1question1, question2question2)
result2 prompt2.format(question1question1, question2question2)
print(result1)
print(result2)会生成字符串 Question1: Which NFL team won the Super Bowl in the 2010 season? Question2: Who won the Super Bowl in 2010? Answer: Question1: Which NFL team won the Super Bowl in the 2010 season? Question2: Who won the Super Bowl in 2010? Answer: Process finished with exit code 0 2.2 ChatPromptTemplateChatPromptTemplates
这些提示模板用于设置消息列表的格式。这些 “模板” 由模板本身的列表组成。通常用于聊天机器人会生成聊天消息列表。
2.2.1 通过消息数组创建聊天消息模板
from langchain_core.prompts import ChatPromptTemplate# 通过消息数组创建聊天消息模板
# 数组每一个元素代表一条消息包含role和content两部分
# role的值可以是system、user、assistant
# content的值是消息的内容chat_template ChatPromptTemplate.from_messages([(system, 你是一个翻译官名叫{name}),(user, 你好),(assistant, 你好我是{name}你好),(user, 请帮我翻译这句话{input}),]
)
# 通过模板参数格式化模板内容
messages chat_template.format_messages(name小智, input请帮我写一篇关于AI的文章)
print(messages)[SystemMessage(content‘你是一个翻译官名叫小智’), HumanMessage(content‘你好’), AIMessage(content‘你好我是小智你好’), HumanMessage(content‘请帮我翻译这句话请帮我写一篇关于AI的文章’)] Process finished with exit code 0 在 ChatPromptTemplate 中role 字段的值通常可以是以下几种 system表示系统角色。通常用于向模型提供上下文或背景信息比如设置模型的行为或规则。 user表示用户角色。通常是用户发出的请求或问题模型需要根据这些输入生成响应。 assistant表示助手角色。通常是模型生成的响应回答用户的问题或根据用户请求执行任务。
2.2.2 通过具体的消息提示模板类的实例数组创建聊天消息模板
from langchain_core.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate# 通过具体的消息提示模板类的实例数组创建聊天消息模板
system_message_prompt SystemMessagePromptTemplate.from_template(你是一个翻译官)
human_message_prompt HumanMessagePromptTemplate.from_template(请帮我翻译这句话{input})
# messages列表
messages [system_message_prompt,human_message_prompt
]
chat_prompt ChatPromptTemplate(messagesmessages)
messages chat_prompt.format_messages(input请帮我写一篇关于AI的文章)
print(messages) [SystemMessage(content‘你是一个翻译官’), HumanMessage(content‘请帮我翻译这句话请帮我写一篇关于AI的文章’)] Process finished with exit code 0 2.2.3 通过SystemMessage、HumanMessagePromptTemplate创建聊天消息模板
from langchain_core.messages import SystemMessage
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate# 通过SystemMessage、HumanMessagePromptTemplate创建聊天消息模板
chat_template ChatPromptTemplate.from_messages([SystemMessage(content你是一个翻译官),HumanMessagePromptTemplate.from_template(请帮我翻译这句话{input})]
)
messages chat_template.format_messages(input请帮我写一篇关于AI的文章)
print(messages)[SystemMessage(content‘你是一个翻译官’), HumanMessage(content‘请帮我翻译这句话请帮我写一篇关于AI的文章’)] Process finished with exit code 0 2.3 MessagePlaceholder
此提示模板负责在特定位置添加消息列表。通过MessagePlaceholder在指定位置传入一组消息。
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholderprompt_template ChatPromptTemplate.from_messages([SystemMessage(contentYou are a helpful assistant.), #systemMessagesPlaceholder(msgs) # palceholder
])
result prompt_template.invoke({msgs: [HumanMessage(contenthi!),{role: user, content: 你是谁}
]})
print(result)这将生成一个包含三条消息的列表第一条是系统消息第二条和第三条是我们传入的 HumanMessage。这对于将消息列表放入特定位置非常有用。 messages[SystemMessage(content‘You are a helpful assistant.’), HumanMessage(content‘你是谁’), HumanMessage(content‘hi!’)] Process finished with exit code 0 在不显式使用 MessagesPlaceholder 类的情况下完成相同操作的另一种方法是
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholderprompt_template ChatPromptTemplate([(system, You are a helpful assistant),(placeholder, {msgs})]
)
result prompt_template.invoke({msgs: [HumanMessage(contenthi!),{role: user, content: 你是谁}
]})
print(result)2.4 Few-shot prompting
添加示例输入和预期输出的技术模型提示被称为“少样本提示”。
2.4.1 生成示例
少样本提示的第一步也是最重要的一步是提供一个好的示例数据集。 好的示例应该在运行时相关、清晰、信息丰富并提供模型尚不知道的信息。 单匝与多轮示例最简单类型的示例仅具有用户输入和预期模型输出。这些是单匝示例。一种更复杂的类型是示例是整个对话通常模型最初响应不正确然后用户告诉模型如何纠正其答案。这称为多轮示例。
2.4.2 例子数量
关键的权衡是更多的示例通常会提高性能但更大的提示会增加成本和延迟。超过某个阈值过多的示例可能会开始使模型变得混乱。找到正确数量的示例在很大程度上取决于模型、任务、示例的质量以及成本和延迟限制。有趣的是模型越好表现良好所需的示例就越少并且添加更多示例时您的回报率就会越快急剧递减。但是可靠地回答这个问题的最佳/唯一方法是使用不同数量的示例进行一些实验。
2.4.3 选取示例
需要有一种方法根据给定的输入从数据集中选择示例。示例选择器是负责选择示例并将其格式化为提示的类https://python.langchain.com/docs/concepts/example_selectors/。 可以随机、通过输入的语义或基于关键字相似性、基于一些其他约束例如令牌大小进行选取。
2.4.4 格式化示例
不同的模型对不同的语法有更好的响应例如ChatML 、XML、TypeScript 等。
from langchain.prompts import FewShotPromptTemplate
from langchain.prompts.prompt import PromptTemplate# 定义示例
examples [{text: I love this movie!, label: positive},{text: This book is boring., label: negative},{text: The weather is nice today., label: neutral}
]# 创建示例模板
example_template
Text: {text}
Label: {label}
# 创建PromptTemplate
example_prompt PromptTemplate(input_variables[text, label],templateexample_template
)# 创建FewShotPromptTemplate
few_shot_prompt FewShotPromptTemplate(examplesexamples, #一个列表包含多个示例每个示例是一个字典其中包含输入文本和对应的标签。这些示例将用于指导模型进行预测。example_promptexample_prompt, # 一个 PromptTemplate 对象用于定义示例的格式。它指定了如何将示例中的输入变量如 text 和 label填充到模板中prefixPlease classify the following texts as positive, negative, or neutral:, # 一个字符串用于在所有示例之前添加的前缀。这个前缀通常提供一些上下文或指导信息帮助模型理解任务。suffixText: {input}\nLabel:, #一个字符串用于在所有示例之后添加的后缀。这个后缀通常包含一个占位符用于提示模型输入新的文本进行分类。input_variables[input]
)# 使用FewShotPromptTemplate进行预测
new_text I enjoyed the concert last night.
print(few_shot_prompt.format(inputnew_text))E:\Anaconda3\envs\openAI\python.exe E:\PythonProjects\openAI\few_short_prompt_template.py Please classify the following texts as positive, negative, or neutral: Text: I love this movie! Label: positive Text: This book is boring. Label: negative Text: The weather is nice today. Label: neutral Text: I enjoyed the concert last night. Label: Process finished with exit code 0