我正在参加「豆包MarsCode AI练中学体验活动」
1.LLMChain链
LLMChain围绕着语言模型推理功能又添加了一些功能,整合了PromptTemplate、语言模型(LLM或聊天模型)和 Output Parser,相当于把Model I/O放在一个链中整体操作。它使用提示模板格式化输入,将格式化的字符串传递给 LLM,并返回 LLM 输出。
接下来我们看看不使用链的默认写法:
api_key = ''
llm_model = 'ep-20241104131149-csxf9'
base_url = "https://ark.cn-beijing.volces.com/api/v3"
import os
# ----第一步 创建提示
# 导入LangChain中的提示模板
from langchain import PromptTemplate
# 原始字符串模板
template = "{flower}的花语是?"
# 创建LangChain模板
prompt_temp = PromptTemplate.from_template(template)
# 根据模板创建提示
prompt = prompt_temp.format(flower="玫瑰")
# 打印提示的内容
print(prompt)
# ----第二步 创建并调用模型
# 导入LangChain中的OpenAI模型接口
from langchain_openai import ChatOpenAI
# 创建模型实例
model = ChatOpenAI(
temperature=0, model=llm_model, base_url=base_url, api_key=api_key)
# 传入提示,调用模型,返回结果
result = model.predict(prompt)
print(result)
使用Chain链的写法:
api_key = ''
llm_model = 'ep-20241104131149-csxf9'
base_url = "https://ark.cn-beijing.volces.com/api/v3"
# 设置OpenAI API密钥
import os
# 导入所需的库
from langchain import PromptTemplate, LLMChain
from langchain_openai import ChatOpenAI
# 原始字符串模板
template = "{flower}的花语是?"
# 创建模型实例
llm = ChatOpenAI(temperature=0, model=llm_model, base_url=base_url, api_key=api_key)
# 创建LLMChain
llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(template))
# 调用LLMChain,返回结果
result = llm_chain("玫瑰")
print(result)
优点:更加简洁。
2.链的调用方式
2.1直接调用
- 如果提示模板中包含多个变量,使用字典进行输入:
prompt = PromptTemplate(
input_variables=["flower", "season"],
template="{flower}在{season}的花语是?",
)
llm_chain = LLMChain(llm=llm, prompt=prompt)
print(llm_chain({
'flower': "玫瑰",
'season': "夏季" }))
2.2通过run方法
llm_chain("玫瑰")
等价于
llm_chain.run("玫瑰")
2.3通过predict方法
predict方法类似于run,只是输入键被指定为关键字参数而不是 Python 字典。
result = llm_chain.predict(flower="玫瑰")
print(result)
2.4通过apply方法
apply方法允许我们针对输入列表运行链,一次处理多个输入。
# apply允许您针对输入列表运行链
input_list = [
{"flower": "玫瑰",'season': "夏季"},
{"flower": "百合",'season': "春季"},
{"flower": "郁金香",'season': "秋季"}
]
result = llm_chain.apply(input_list)
print(result)
2.5通过generate方法
generate方法类似于apply,只不过它返回一个LLMResult对象,而不是字符串。LLMResult通常包含模型生成文本过程中的一些相关信息,例如令牌数量、模型名称等。
result = llm_chain.generate(input_list)
print(result)
2.6总体代码
api_key = ''
llm_model = 'ep-20241104131149-csxf9'
base_url = "https://ark.cn-beijing.volces.com/api/v3"
# 设置OpenAI API密钥
import os
# 导入所需库
from langchain import PromptTemplate, LLMChain
from langchain_openai import ChatOpenAI
# 设置提示模板
prompt = PromptTemplate(
input_variables=["flower", "season"], template="{flower}在{season}的花语是?"
)
# 初始化大模型
llm = ChatOpenAI(model=llm_model, temperature=0, base_url=base_url, api_key=api_key)
# 初始化链
llm_chain = LLMChain(llm=llm, prompt=prompt)
# 调用链
response = llm_chain({"flower": "玫瑰", "season": "夏季"})
print(response)
# run方法
llm_chain.run({"flower": "玫瑰", "season": "夏季"})
# predict方法
result = llm_chain.predict(flower="玫瑰", season="夏季")
print(result)
# apply方法允许您针对输入列表运行链
input_list = [
{"flower": "玫瑰", "season": "夏季"},
{"flower": "百合", "season": "春季"},
{"flower": "郁金香", "season": "秋季"},
]
result = llm_chain.apply(input_list)
print(result)
# generate方法
result = llm_chain.generate(input_list)
print(result)
3.Sequential Chain顺序链
这个示例中,我们的目标是这样的:
- 第一步,我们假设大模型是一个植物学家,让他给出某种特定鲜花的知识和介绍。
- 第二步,我们假设大模型是一个鲜花评论者,让他参考上面植物学家的文字输出,对鲜花进行评论。
- 第三步,我们假设大模型是易速鲜花的社交媒体运营经理,让他参考上面植物学家和鲜花评论者的文字输出,来写一篇鲜花运营文案。
api_key = ''
llm_model = 'ep-20241104131149-csxf9'
base_url = "https://ark.cn-beijing.volces.com/api/v3"
# 设置OpenAI API密钥
import os
# 导入所需要的库
from langchain.chains import LLMChain, SequentialChain
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
# 第一个LLMChain:生成鲜花的介绍
llm = ChatOpenAI(
base_url=base_url,
api_key=api_key,
temperature=0.7,
model=llm_model,
)
template = """
你是一个植物学家。给定花的名称和类型,你需要为这种花写一个200字左右的介绍。
花名: {name}
颜色: {color}
植物学家: 这是关于上述花的介绍:"""
prompt_template = PromptTemplate(input_variables=["name", "color"], template=template)
introduction_chain = LLMChain(
llm=llm, prompt=prompt_template, output_key="introduction"
)
# 第二个LLMChain:根据鲜花的介绍写出鲜花的评论
template = """
你是一位鲜花评论家。给定一种花的介绍,你需要为这种花写一篇200字左右的评论。
鲜花介绍:
{introduction}
花评人对上述花的评论:"""
prompt_template = PromptTemplate(input_variables=["introduction"], template=template)
review_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="review")
# 第三个LLMChain:根据鲜花的介绍和评论写出一篇自媒体的文案
template = """
你是一家花店的社交媒体经理。给定一种花的介绍和评论,你需要为这种花写一篇社交媒体的帖子,300字左右。
鲜花介绍:
{introduction}
花评人对上述花的评论:
{review}
社交媒体帖子:
"""
prompt_template = PromptTemplate(
input_variables=["introduction", "review"], template=template
)
social_post_chain = LLMChain(
llm=llm, prompt=prompt_template, output_key="social_post_text"
)
# 总的链:按顺序运行三个链
overall_chain = SequentialChain(
chains=[introduction_chain, review_chain, social_post_chain],
input_variables=["name", "color"],
output_variables=["introduction", "review", "social_post_text"],
verbose=True,
)
# 运行链并打印结果
result = overall_chain({"name": "玫瑰", "color": "黑色"})
print(result)