大模型开发中LCEL与LLMChain响应度的对比

管道连接

python 复制代码
import time

from langchain_community.chat_models import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate

t1 = time.time()
llm = ChatOpenAI(
)


resp_prompt_path = 'response_prompt.md'
prompt = PromptTemplate.from_file(resp_prompt_path,encoding='utf-8')
prompt = prompt.partial(
            query="现在客运量是多少?",
            result="### rt_schema:['客运量'], rt_result:[888461]",
            reply_nodata="昨日数据未完成结算, 请12点以后查看。",
            today="2024-11-27")

chain = prompt | llm | StrOutputParser()

print(chain.invoke({"query":"现在客运量是多少?","result":"### rt_schema:['客运量'], rt_result:[888461]","today":"2024-11-27"}))

print(time.time()-t1)

其中,要求prompt类型为PromptTemplate类型。

LLMChain

python 复制代码
import time

from langchain.chains.llm import LLMChain
from langchain.memory import ConversationBufferMemory
from langchain_community.chat_models import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, \
    SystemMessagePromptTemplate, MessagesPlaceholder

resp_prompt_path = 'response_prompt.md'
prompt = PromptTemplate.from_file(resp_prompt_path,encoding='utf-8')
prompt = prompt.partial(
            query="现在客运量是多少?",
            result="### rt_schema:['客运量'], rt_result:[888461]",
            reply_nodata="昨日数据未完成结算, 请12点以后查看。",
            today="2024-11-27")

prompt_ = prompt.format()


t2 = time.time()
llm = ChatOpenAI(
)
prompt = ChatPromptTemplate(
    messages=[
        SystemMessagePromptTemplate.from_template(
            "You are a nice chatbot having a conversation with a human."
        ),
        MessagesPlaceholder(variable_name="chat_history"),
        HumanMessagePromptTemplate.from_template(prompt_)
    ]
)

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
conversation = LLMChain(
    llm=llm,
    prompt=prompt,
    verbose=True,
    memory=memory
)

resp = conversation.invoke({"data":str({"query":"现在客运量是多少?","result":"### rt_schema:['客运量'], rt_result:[888461]","today":"2024-11-27"})})
resp_str = StrOutputParser.parse(self='',text=resp.get('text'))
print(resp_str)

print(time.time()-t2)

其中,HumanMessagePromptTemplate.from_template()要求参数是str类型,需要将prompt通过prompt.format()转成str,进入LLMChain后prompt要求是ChatPromptTemplate类型的。另外,该模型只能接收一个参数,如果出现多个参数,需要转换。

目前在大模型开发中,遇到响应度体验的问题,本想通过拆掉pipeline提升速度,但是最终发现效果不明显。就保留langchain的LCEL模式。

相关推荐
梨子串桃子_8 分钟前
推荐系统学习笔记 | PyTorch学习笔记
pytorch·笔记·python·学习·算法
文言一心1 小时前
LINUX离线升级 Python 至 3.11.9 操作手册
linux·运维·python
诗词在线2 小时前
中国古代诗词名句按主题分类有哪些?(爱国 / 思乡 / 送别)
人工智能·python·分类·数据挖掘
高锰酸钾_2 小时前
机器学习-L1正则化和L2正则化解决过拟合问题
人工智能·python·机器学习
天天睡大觉2 小时前
Python学习11
网络·python·学习
智航GIS2 小时前
11.11 Pandas性能革命:向量化操作与内存优化实战指南
python·pandas
laplace01232 小时前
# 第六章 agent框架开发实践 - 学习笔记
人工智能·笔记·学习·语言模型·agent
空中楼阁,梦幻泡影2 小时前
LoRA 详细解析,使用LoRA 方式对模型进行微调详细操作指南
运维·服务器·人工智能·机器学习·语言模型
写代码的【黑咖啡】3 小时前
Python中的Selenium:强大的浏览器自动化工具
python·selenium·自动化