当前位置: 首页 > news >正文

python购物网站开发流程权威发布的意思是什么

python购物网站开发流程,权威发布的意思是什么,wordpress 图片圆边,企业 cms安装 langchain 库 pip install langchain1、概念:提示和提示工程 在大语言模型(LLMs)时代,通过简单地更改提示中的指令,同一个模型可以执行多种任务。这一特性让 LLMs 在各类应用场景中都显得非常灵活和强大。然而&…

安装 langchain 库

pip install langchain

1、概念:提示和提示工程

在大语言模型(LLMs)时代,通过简单地更改提示中的指令,同一个模型可以执行多种任务。这一特性让 LLMs 在各类应用场景中都显得非常灵活和强大。然而,这种灵活性也依赖于提示的设计质量。糟糕的提示往往会导致糟糕的输出,而精心设计的提示则能够引导模型发挥出其强大的潜力。

**提示(Prompt)**是用户提供给模型的输入文本,通常用于指导模型完成特定任务或生成特定风格的文本。一个好的提示不仅能够清晰地传达任务意图,还能够提供足够的上下文信息,以引导模型生成高质量的输出。提示的形式可以是问题、指令、上下文说明等。良好的提示设计需要考虑到以下几个方面:

  • 指令 :告诉模型该怎么做,如何使用外部信息(如果提供),如何处理查询并构建 Out。
  • 明确性:提示要清晰明确,让模型能够准确理解用户的意图。
  • 外部信息或上下文信息:提供足够的背景信息或限制条件,以帮助模型生成更具相关性和准确性的答案。充当模型的附加知识来源。这些可以手动插入到提示中,通过矢量数据库 (Vector Database) 检索(检索增强)获得,或通过其他方式(API、计算等)引入。
  • 任务引导:根据任务类型选择合适的提示结构,比如命令式、问题式或描述式提示,以引导模型生成期望的输出。

**提示工程学(Prompt Engineering)**是一个跨学科的领域,旨在设计和优化与大语言模型交互的提示词。提示工程学不仅仅是简单地编写问题或请求,而是通过精确的结构化和策略性设计,使得模型能够在特定任务中表现得更为出色。它包括以下几个方面:

  • 提示词设计:如何根据任务的需要,构造合适的提示语句。
  • 调优和迭代:根据模型的反馈,不断调整提示词,以提高模型生成的质量。
  • 上下文管理:有效地为模型提供足够的上下文,确保生成的答案与期望的一致。

2、LangChain Prompt 模板

2.1 PromptTemplate(String 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(template=template, 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(question1=question1, question2=question2)
result2 = prompt2.format(question1=question1, question2=question2)
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 ChatPromptTemplate(ChatPromptTemplates)

这些提示模板用于设置消息列表的格式。这些 “模板” 由模板本身的列表组成。通常用于聊天机器人,会生成聊天消息列表。

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(messages=messages)
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(content="You are a helpful assistant."),        #systemMessagesPlaceholder("msgs")         # palceholder
])
result = prompt_template.invoke({"msgs": [HumanMessage(content="hi!"),{"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(content="hi!"),{"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"],template=example_template
)# 创建FewShotPromptTemplate
few_shot_prompt = FewShotPromptTemplate(examples=examples, #一个列表,包含多个示例,每个示例是一个字典,其中包含输入文本和对应的标签。这些示例将用于指导模型进行预测。example_prompt=example_prompt, #  一个 PromptTemplate 对象,用于定义示例的格式。它指定了如何将示例中的输入变量(如 text 和 label)填充到模板中prefix="Please classify the following texts as positive, negative, or neutral:",  # 一个字符串,用于在所有示例之前添加的前缀。这个前缀通常提供一些上下文或指导信息,帮助模型理解任务。suffix="Text: {input}\nLabel:",   #一个字符串,用于在所有示例之后添加的后缀。这个后缀通常包含一个占位符,用于提示模型输入新的文本进行分类。input_variables=["input"]
)# 使用FewShotPromptTemplate进行预测
new_text = "I enjoyed the concert last night."
print(few_shot_prompt.format(input=new_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

http://www.yayakq.cn/news/293401/

相关文章:

  • 网站建设业务员话术合肥网站建设公司
  • 如何创建网站内容企业网站的功能列表
  • 集美网站建设网络公司经营范围可以加婚介吗
  • 做什么网站开发好服务周到的上海网站建设公
  • 做网站如何寻找客源婚纱影楼网站
  • 扁平化配色方案网站扬州做网站多少钱
  • 网站建设研究课题怎么开网站详细步骤
  • 网站开发好了 怎么发布wordpress 内容可以是表格吗
  • 建站软件安卓wordpress编辑主页
  • 自己做的网站怎么在移动端访问设计封面
  • 我们公司想做网络推广福州seo公司技术
  • p2p视频网站建设波密网站建设
  • 批量网站访问检测建设企业网站e路护航
  • 深圳送花网站哪个好制作花灯
  • 医院网站怎么做运营长沙网站托管seo优化公司
  • 微信网站怎么开发网站建设如何做账
  • 北京网站建设公司降龙冯提莫斗鱼前在哪个网站做直播
  • 深圳在建高铁站宿迁百度
  • 浙江住房和建设网站首页世界上最大的在线设计平台
  • 上饶市住房城乡建设局网站wordpress固定字段
  • 网站报价单网站模版怎么修改
  • 外贸网站运营是做什么的咨询公司网站建设
  • 网站备案信息保护湖南省郴州市天气
  • 专业建站工作室开源cms框架
  • 青岛制作企业网站的公司wordpress 图片模板修改
  • 拱墅区建设局网站.网站开发工具dw
  • php网站运行很慢无锡网站建设要多少钱
  • 建设个人网站第一步这么做均安网站建设
  • 查看网站开通时间网站开发费用属无形资产吗
  • 网站开发的后期维护怎么做购物网站到