LangChain
Langchain(LangChain中文网 Concepts | 🦜️🔗 Langchain)是一个框架,它通过组合模块来扩展LLM的能力,帮助开发人员快速构建基于LLM的应用。
主要用于处理LLM的一些不足,包括:
a. Max token限制
b. 私有化模型
c. 没法查询数据库
d. 不能调用第三方API
e. 无法对接外部工具
f. 输出结果不稳定
e. 无法联网
g. 知识滞后
h. 只是提供底座模型和API,要在应用场景中产品化,仍然需要大量工作
本文介绍下LangChain里的一些重要元素以及常用的Chain
Model
(1)Concept
LangChain并不提供自己的LLMs,而是为与许多不同的LLMs(OpenAI、Cohere、Hugging Face等)进行交互提供了一个标准接口。
基类:BaseLanguageModel
其继承关系如下:
BaseLanguageModel --> BaseLLM --> <name>
有以下几个几个基础方法:
1.predict_messages(List[BaseMessage])->BaseMessage
2.predict(text)->str
3.generate_prompt(List[PromptValue])->LLMResult #LLMChain中采用这种方式
see more at https://api.python.langchain.com/en/latest/api_reference.html#module-langchain.llms
(2)Prompt Template
支持Prompt template,在runtime时填充参数,产生最终的prompt。通过format_prompt()方法实现渲染。
基础类关系如下
BasePromptTemplate --> StringPromptTemplate --> PromptTemplate BasePromptTemplate --> BaseChatPromptTemplate --> ChatPromptTemplate (可以包含SystemMessagePromptTemplate和HumanMessagePromptTemplate)
重要方法如下,必要时重写以下方法加入自己的逻辑
prompt.f