并行化步骤
RunnableParallel(又名 RunnableMap)可以轻松并行执行多个 Runnable,并将这些 Runnable 的输出作为映射返回。
RunnableParallel 对于并行运行独立进程也很有用,因为映射中的每个 Runnable 都是并行执行的。
# 加载模型
from langchain_openai import ChatOpenAI, OpenAI
openai_api_key = "EMPTY"
openai_api_base = "http://127.0.0.1:1234/v1"
model = ChatOpenAI(
openai_api_key=openai_api_key,
openai_api_base=openai_api_base,
temperature=0.3,
)
导入相关的包,并且将两个并行处理的内容;这里先列举了传统的处理方法;
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableParallel
outlinePromptTemplate = '''主题:{theme}
如果要根据主题写一篇文章,请列出文章的大纲。'''
outlinePrompt = ChatPromptTemplate.from_template(outlinePromptTemplate)
tipsPromptTemplate = '''主题:{theme}
如果要根据主题写一篇文章,应该需要注意哪些方面,才能把这篇文章写好。
'''
tipsPrompt = ChatPromptTemplate.from_template(tipsPromptTemplate)<