LangChain核心模块 Model I/O

Model I/O

任何语言模型应用程序的核心元素都是模型。LangChain 为您提供了与任何语言模型交互的构建块。

Models

llm chat_model 都是表示特定模型配置的对象。

LLM 对象将字符串作为输入和输出字符串。ChatModel 对象将消息列表作为输入并输出消息。

LLM 返回一个字符串,而 ChatModel 返回一条消息。

Prompt Templates

通常会将用户输入 添加到较大的文本中,称为提示模板,该文本提供有关当前特定任务的附加上下文。PromptTemplates捆绑了从用户输入到完全格式化的提示的所有逻辑。

PromptTemplates也可用于生成消息列表。在这种情况下,提示不仅包含有关内容的消息,还包含每条消息(其角色、在列表中的位置等)。最常用的是 ChatPromptTemplate 是 ChatMessageTemplates 的列表。每个 ChatMessageTemplate 都包含有关如何格式化该 ChatMessage 的说明 - 它的角色,以及它的内容。

python 复制代码
from langchain.prompts.chat import ChatPromptTemplate

template = "You are a helpful assistant that translates {input_language} to {output_language}."
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", template),
    ("human", human_template),
])

chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")

Output parsers

OutputParser将语言模型的原始输出转换为可以在下游使用的格式。OutputParsers主要有以下几种类型:

  • 将文本从LLM转换为结构化信息(例如 JSON)
  • ChatMessage 转换为字符串
  • 将除消息之外的调用返回的额外信息(如 OpenAI 函数调用)转换为字符串。

Composing with LCEL

将上面的各个模块组合成一条链,该链将获取输入变量,将这些变量传递给提示模板以创建提示,将提示传递给语言模型,然后通过(可选)输出解析器传递输出。

使用|语法将这些组件链接在一起,由LCEL提供支持,并依赖Runnable所有这些对象实现的通用接口。

相关推荐
hrhcode3 分钟前
【LangGraph】二.State 和 Node 的设计细节
python·ai·langchain·langgraph·ai框架
Trouvaille ~29 分钟前
零基础入门 LangChain 与 LangGraph(八):真正让 Agent“活起来”——持久化、记忆、人机交互与时间旅行
langchain·人机交互·agent·python3.11·持久化机制·langgraph·ai应用开发
abigale031 小时前
LangChain:自定义模型・RAG 检索・Agent 原理笔记
langchain·llm·prompt·agent·rag·lcel
tangweiguo030519872 小时前
AI图生图完整实战:基于阿里云百炼通义万相
人工智能·langchain
LucaJu3 小时前
DeepAgents 人工介入实战|LangGraph 实现 Agent 高危工具人工审批
python·langchain·agent·langgraph·deepagents
tangweiguo030519873 小时前
AI文生图完整实战:基于阿里云百炼通义万相
人工智能·langchain
Flittly4 小时前
【LangGraph新手村系列】(2)自定义状态与归约器:让 LangGraph 记住更多东西
python·langchain·aigc
hrhcode4 小时前
【LangChain】一.LangChain v1.0-快速上手(核心组件、工具、中间件)
python·ai·langchain·agent
yuyuyui5 小时前
LangChain框架-数据检索
langchain