支付宝网站登录入口,个人微信公众号如何推广,一件代发50个货源网站,好网站建设公司哪个好呀Model I/O
可以把对模型的使用过程拆解成三块#xff0c;分别是输入提示#xff08;对应图中的Format#xff09;、调用模型#xff08;对应图中的Predict#xff09;和输出解析#xff08;对应图中的Parse#xff09;。这三块形成了一个整体#xff0c;因此在LangCha…
Model I/O
可以把对模型的使用过程拆解成三块分别是输入提示对应图中的Format、调用模型对应图中的Predict和输出解析对应图中的Parse。这三块形成了一个整体因此在LangChain中这个过程被统称为 Model I/O。
提示模板 提示工程Prompt Engineering吴恩达老师在他的提示工程课程中所说的
给予模型清晰明确的指示让模型慢慢地思考
# 导入LangChain中的提示模板
from langchain import PromptTemplate
# 创建原始模板
template
您是一位专业的鲜花店文案撰写员。\n
对于售价为 {price} 元的 {flower_name} 您能提供一个吸引人的简短描述吗# 根据原始模板创建LangChain提示模板
prompt PromptTemplate.from_template(template)
# 打印LangChain提示模板的内容
print(prompt)提示模板的具体内容如下
input_variables[flower_name, price]
output_parserNone partial_variables{}
template/\n您是一位专业的鲜花店文案撰写员。
\n对于售价为 {price} 元的 {flower_name} 您能提供一个吸引人的简短描述吗\n
template_formatf-string
validate_templateTrueLangChain 提供了多个类和函数也为各种应用场景设计了很多内置模板使构建和使用提示变得容易。
语言模型
LangChain中支持的模型有三大类。
大语言模型LLM 也叫Text Model这些模型将文本字符串作为输入并返回文本字符串作为输出。Open AI的text-davinci-003、Facebook的LLaMA、ANTHROPIC的Claude都是典型的LLM。聊天模型Chat Model主要代表Open AI的ChatGPT系列模型。这些模型通常由语言模型支持但它们的 API 更加结构化。具体来说这些模型将聊天消息列表作为输入并返回聊天消息。文本嵌入模型Embedding Model这些模型将文本作为输入并返回浮点数列表也就是Embedding。
接上面的代码
# 导入LangChain中的OpenAI模型接口
from langchain import OpenAI
# 创建模型实例
model OpenAI(model_nametext-davinci-003)
# 输入提示
input prompt.format(flower_name[玫瑰], price50)
# 得到模型的输出
output model(input)
# 打印输出内容
print(output) 具体的提示“您是一位专业的鲜花店文案撰写员。对于售价为 50 元的玫瑰您能提供一个吸引人的简短描述吗” 模型可以自由选择、自主训练而调用模型的框架往往是有章法、而且可复用的。 输出解析
在开发具体应用的过程中很明显我们不仅仅需要文字更多情况下我们需要的是程序能够直接处理的、结构化的数据。在这个文案中如果你希望模型返回两个字段
description鲜花的说明文本reason解释一下为何要这样写上面的文案
A“文案是让你心动50元就可以拥有这支充满浪漫气息的玫瑰花束让TA感受你的真心爱意。为什么这样说呢因为爱情是无价的50元对应热恋中的情侣也会觉得值得。”B{description: “让你心动50元就可以拥有这支充满浪漫气息的玫瑰花束让TA感受你的真心爱意。” ; reason: “因为爱情是无价的50元对应热恋中的情侣也会觉得值得。”}像b这种数据结构langchain中的输出解析器可以帮助我们实现
# 通过LangChain调用模型
from langchain import PromptTemplate, OpenAI# 导入OpenAI Key
import os
os.environ[OPENAI_API_KEY] 你的OpenAI API Key# 创建原始提示模板
prompt_template 您是一位专业的鲜花店文案撰写员。
对于售价为 {price} 元的 {flower_name} 您能提供一个吸引人的简短描述吗
{format_instructions}# 创建模型实例
model OpenAI(model_nametext-davinci-003)# 导入结构化输出解析器和ResponseSchema
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
# 定义我们想要接收的响应模式
response_schemas [ResponseSchema(namedescription, description鲜花的描述文案),ResponseSchema(namereason, description问什么要这样写这个文案)
]
# 创建输出解析器
output_parser StructuredOutputParser.from_response_schemas(response_schemas)# 获取格式指示
format_instructions output_parser.get_format_instructions()
# 根据原始模板创建提示同时在提示中加入输出解析器的说明
prompt PromptTemplate.from_template(prompt_template, partial_variables{format_instructions: format_instructions}) # 数据准备
flowers [玫瑰, 百合, 康乃馨]
prices [50, 30, 20]# 创建一个空的DataFrame用于存储结果
import pandas as pd
df pd.DataFrame(columns[flower, price, description, reason]) # 先声明列名for flower, price in zip(flowers, prices):# 根据提示准备模型的输入input prompt.format(flower_nameflower, priceprice)# 获取模型的输出output model(input)# 解析模型的输出这是一个字典结构parsed_output output_parser.parse(output)# 在解析后的输出中添加“flower”和“price”parsed_output[flower] flowerparsed_output[price] price# 将解析后的输出添加到DataFrame中df.loc[len(df)] parsed_output # 打印字典
print(df.to_dict(orientrecords))# 保存DataFrame到CSV文件
df.to_csv(flowers_with_descriptions.csv, indexFalse)输出
[{flower: 玫瑰, price: 50, description: Luxuriate in the beauty of this 50 yuan rose, with its deep red petals and delicate aroma., reason: This description emphasizes the elegance and beauty of the rose, which will be sure to draw attention.},
{flower: 百合, price: 30, description: 30元的百合象征着坚定的爱情带给你的是温暖而持久的情感, reason: 百合是象征爱情的花写出这样的描述能让顾客更容易感受到百合所带来的爱意。},
{flower: 康乃馨, price: 20, description: This beautiful carnation is the perfect way to show your love and appreciation. Its vibrant pink color is sure to brighten up any room!, reason: The description is short, clear and appealing, emphasizing the beauty and color of the carnation while also invoking a sense of love and appreciation.}]LangChain框架的好处模板管理、变量提取和检查、模型切换、输出解析