当前位置:   article > 正文

langchain学习(四)_langchain invoke

langchain invoke

Interface官方文档

https://python.langchain.com/docs/expression_language/interfaceTo make it as easy as possible to create custom chains, we’vehttps://python.langchain.com/docs/expression_language/interface

1、langchain采用了一个叫“Runnable” 的标准协议/接口,给用户组合部件(component)形成chains,标准化的接口还包括:

stream:流式(实时)返回结果

invoke:单输入

batch:通过列表进行多输入

同时提供以上接口的异步方式

2、component的输入输出类型表

ComponentInput TypeOutput Type
PromptDictionaryPromptValue
ChatModelSingle string, list of chat messages or a PromptValueChatMessage
LLMSingle string, list of chat messages or a PromptValueString
OutputParserThe output of an LLM or ChatModelDepends on the parser
RetrieverSingle stringList of Documents
ToolSingle string or dictionary, depending on the toolDepends on the tool

3、并行请求

方式1通过RunnableParallel

  1. from langchain.prompts import ChatPromptTemplate
  2. from langchain_core.runnables import RunnableParallel
  3. from time import process_time
  4. process_time()
  5. chain1 = ChatPromptTemplate.from_template("tell me a joke about {topic}")
  6. chain2 = (
  7. ChatPromptTemplate.from_template("write a short (2 line) poem about {topic}")
  8. )
  9. combined = RunnableParallel(joke=chain1, poem=chain2)
  10. print(process_time())

方式2通过batch接口

combined.batch([{"topic": "bears"}, {"topic": "cats"}])

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/853648
推荐阅读
相关标签
  

闽ICP备14008679号