大模型开发中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模式。

相关推荐
sg_knight几秒前
设计模式实战:责任链模式(Chain of Responsibility)
python·设计模式·责任链模式
2301_803875617 分钟前
如何通过phpMyAdmin给WordPress所有用户发送全站通知_系统表插入
jvm·数据库·python
学弟39 分钟前
【内涵】深度学习中的三种变量及pytorch中对应的三种tensor
人工智能·pytorch·python
2301_7775993743 分钟前
mysql如何进行数据库容量规划_评估磁盘空间增长趋势
jvm·数据库·python
aq55356001 小时前
PHP vs Python:30秒看懂核心区别
开发语言·python·php
m0_377618231 小时前
Redis怎样应对大规模集群的重启风暴_分批次重启节点并等待集群状态恢复绿灯后再继续操作
jvm·数据库·python
心态与习惯2 小时前
Julia 初探,及与 C++,Java,Python 的比较
java·c++·python·julia·比较
ZC跨境爬虫2 小时前
3D 地球卫星轨道可视化平台开发 Day8(分步渲染200颗卫星+ 前端分页控制)
前端·python·3d·重构·html
zopple2 小时前
ThinkPHP5.x与3.x核心差异解析
java·python·php
2401_835956812 小时前
Golang怎么写基准测试benchmark_Golang基准测试教程【完整】
jvm·数据库·python