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

相关推荐
前端付豪40 分钟前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户03321266636743 分钟前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫2 小时前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派2 小时前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风4 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽14 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户83562907805118 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon20 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly20 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程20 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python