【LangChain系列 14】语言模型概述

本文速读

  • LLMs

  • 对话模型

LangChain集成了两种语言模型:

  • LLM:输入文本,返回文本

  • 对话模型:基于LLM,输入Message列表,返回一条Message

LLM和对话模型之间有着细微且重要的不同。在LangChain中,LLMs指的是纯文本补全模型,它的API封装是:将字符串prompt作为输入,然后返回一个字符串。GPT-3是作为一个LLM实现的,然后在它的基础上进行调整使其具有对话能力。GPT-4和Claude都是作为对话模型实现的。

在LangChain中,为了将LLMs和对话模型可以互换使用,它们都继承了 基础语言模型 接口,它包含一些公共方法,比如predict, predict messages。

01 LLMs


LLMs(Large Language Models )是LangChain在核心组件,LangChain并不服务于它自己的LLMs,而是提供标准接口来与不同的LLMs交互。

下面我们从OpenAI LLM开始吧。

  1. 安装openai
bash 复制代码
pip install openai
  1. 准备API key

注册好OpenAI的帐号后,可以在后台生成key。有两种方式使用API key,一种是通过环境变量的方式:

bash 复制代码
export OPENAI_API_KEY="..."

然后在代码中就不需要指定key了:

python 复制代码
from langchain.llms import OpenAI

llm = OpenAI()

另一种方式是直接在代码中指定:

python 复制代码
from langchain.llms import OpenAI

llm = OpenAI(openai_api_key="...")
  1. 使用LLM

最简单的方式是直接调用:

复制代码
llm("Tell me a joke")

'Why did the chicken cross the road?\n\nTo get to the other side.'

同时也支持批量调用:

复制代码
llm_result = llm.generate(["Tell me a joke", "Tell me a poem"]*15)

len(llm_result.generations)

30

查看结果:

python 复制代码
llm_result.generations[0]
python 复制代码
[Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'),
   Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side.')]
python 复制代码
llm_result.llm_output
bash 复制代码
  {'token_usage': {'completion_tokens': 3903,
    'total_tokens': 4023,
    'prompt_tokens': 120}
  }

02 对话模型


对话模型的底层是LLM,但是它的上层接口和LLM的还是有点不同。LLM的是"text in, text out"相关的API,对话模型接口的输入/输出是Message。

同样地,我们依然以OpenAI为例来介绍对话模型。

  1. 安装openai

    pip install openai

  2. 准备API key

和LLM的方法一致,也支持环境变量和代码中两种指定方式。

python 复制代码
from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI(openai_api_key="...")
  1. 使用对话模型

最简单的使用方式就是传入一条或多条Message,然后返回一条Message.

python 复制代码
messages = [
    SystemMessage(content="You are a helpful assistant that translates English to French."),
    HumanMessage(content="I love programming.")
]
chat(messages)
python 复制代码
AIMessage(content="J'aime programmer.", additional_kwargs={})

同样地,对话模型也支持批量:

python 复制代码
batch_messages = [
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="I love programming.")
    ],
    [
        SystemMessage(content="You are a helpful assistant that translates English to French."),
        HumanMessage(content="I love artificial intelligence.")
    ],
]
result = chat.generate(batch_messages)
result
python 复制代码
LLMResult(generations=[[ChatGeneration(text="J'aime programmer.", generation_info=None, message=AIMessage(content="J'aime programmer.", additional_kwargs={}))], [ChatGeneration(text="J'aime l'intelligence artificielle.", generation_info=None, message=AIMessage(content="J'aime l'intelligence artificielle.", additional_kwargs={}))]], llm_output={'token_usage': {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77}})

查看token信息:

python 复制代码
result.llm_output
bash 复制代码
{'token_usage': {'prompt_tokens': 57,
    'completion_tokens': 20,
    'total_tokens': 77}}

本文小结

LangChain集成了LLM和对话模型两类模型,两者上层接口的差别是:LLM是"text in, text out", 而对话模型是"message in, message out"。

公众号:大白爱爬山

相关推荐
BU摆烂会噶2 分钟前
【LangGraph】House_Agent 实战(五):持久化、流式输出与部署
人工智能·python·架构·langchain·人机交互
txg6666 分钟前
机器人领域简报(2026年5月15日—5月21日)
人工智能·机器人
码上滚雪球8 分钟前
Flink Agents 深度解读:当实时数据流遇上 AI 智能体
大数据·人工智能·flink·滚雪球
PNP Robotics9 分钟前
PNP机器人亮相南京学术论坛,分享具身智能多模态数据采集前沿成果
人工智能·深度学习·学习·机器学习·virtualenv
threelab9 分钟前
Three.js 银河星系效果 | 三维可视化 / AI 提示词
开发语言·javascript·人工智能
想你依然心痛13 分钟前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“译界智脑“——PC端AI智能体沉浸式智能翻译与跨语言协作工作台
人工智能·华为·ar·harmonyos
几司13 分钟前
OpenISP 模块拆解 · 第11讲:非局部均值降噪 (NLM)
人工智能·算法·均值算法·isp
海上彼尚13 分钟前
Nodejs也能写Agent - 7.基础篇 - MCP
前端·javascript·人工智能·node.js
灵途科技16 分钟前
具身智能时代,灵途科技重构机器人感知
人工智能·机器人
寻道码路17 分钟前
LangChain4j Java AI 应用开发实战(二):大模型参数调优实战:Temperature、TopP、MaxTokens 深度解析
java·开发语言·人工智能·aigc