使用LangChain框架的核心目标是连接多种大语言模型(如ChatGP、LLaMA等)和外部资源(如Google、Wikipedia、Notion及Wolffram等),提供抽象组件和工具以在文本输入和输出之间进行接口处理。
一、langChain安装
pip install langchain
pip install langchain_community
pip install -U langchain-openai
二、简单使用
import langchain
langchain.__version__
from langchain import PromptTemplate
template = """
You are a naming consultant for new companies.
What is a good name for a company that makes {product}?
"""
prompt = PromptTemplate.from_template(template)
print(prompt)
print(prompt.format(product="colorful socks"))
# input_variables=['product'] template='\nYou are a naming consultant for new companies.\nWhat is a good name for a company that makes {product}?\n'
# You are a naming consultant for new companies.
# What is a good name for a company that makes colorful socks?
三、LangChain框架核心模块
1. 模型输入/输出(Model I/O) :与大语言模型交互的接口
- LLMs:大语言模型
- Chat Models:一般基于 LLMs,但按对话结构重新封装
- PromptTemple:提示词模板
- OutputParser:解析输出
2. 数据连接(Data connection):与特定应用程序的数据进行交互的接口
- Document Loaders:各种格式文件的加载器
- Document Transformers:对文档的常用操作,如:split, filter, translate, extract metadata, etc
- Text Embedding Models:文本向量化表示,用于检索等操作(啥意思?别急,后面详细讲)
- Verctorstores: (面向检索的)向量的存储
- Retrievers: 向量的检索
3. 记忆(Memory):用于在链的多次运行之间持久化应用程序状态
- Memory:这里不是物理内存,从文本的角度,可以理解为“上文”、“历史记录”或者说“记忆力”的管理
4. 架构
- Chain:实现一个功能或者一系列顺序功能组合
- Agent:根据用户输入,自动规划执行步骤,自动选择每步需要的工具,最终完成用户指定的功能
- Tools:调用外部功能的函数,例如:调 google 搜索、文件 I/O、Linux Shell 等等
- Toolkits:操作某软件的一组工具集,例如:操作 DB、操作 Gmail 等等