🔥🔥🔥一文搞懂 Langchain Models (三)

接上文《🔥🔥🔥一文搞懂 Langchain Models (二)》

Chat Models

聊天模型使用LLM(大型语言模型)进行操作,但具有不同的接口,使用"消息"而不是原始文本输入/输出。LangChain 提供了与这些模型轻松交互的功能。

在聊天模型中,支持三种类型的消息:

  • SystemMessage(系统消息) - 这设置了LLM的行为和目标。您可以在这里给出具体的指示,如:"扮演市场经理。" 或 "只返回JSON响应,不返回说明文本"
  • HumanMessage(人类消息) - 这是您将用户提示输入并发送给LLM的地方
  • AIMessage(AI消息) - 当将聊天记录传回LLM以供将来请求时,您可以在这里存储来自LLM的响应。

还有一个通用的 ChatMessage,它接受一个任意的"角色"输入,可以在需要除系统/人类/AI之外的其他内容的情况下使用。但通常情况下,您会使用上面提到的三种类型。

要使用它,您需要导入所使用集成的聊天模型。

python 复制代码
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

然后,您需要初始化聊天代理。这个示例使用了 OpenAI 的聊天模型。

python 复制代码
chat = ChatOpenAI(temperature=0)

与LLM模型一样,这也有多个可以调整的设置,例如:

  • model(模型) - 默认为 "gpt-3.5-turbo"
  • temperature(温度) - 参见上面的解释
  • max_tokens(最大令牌数) - 设置LLM在响应中生成的令牌数量的限制 然后,您需要将一系列消息传递给聊天代理以生成响应。在关于LangChain的"Memory"(记忆)的未来文章中,我们将讨论用于存储和应用聊天记录的ChatMessageHistory类,它可以使代理记住聊天的上下文并在未来的响应中使用它,从而产生"对话"的好处。
python 复制代码
messages = [
	SystemMessage(content="Return only a JSON object as a response with no explanation text"),
	HumanMessage(content="Generate a JSON response object containing a brief description and release year for the movie 'Inception'")
]

chat(messages)

# AIMessage(content='{\\n  "title": "Inception",\\n  "description": "A skilled thief is given a final chance at redemption which involves executing his toughest job yet: Inception. The idea of planting an idea into someone\\'s mind is deemed impossible by most, but Cobb and his team of specialists must accomplish this task to save their lives.",\\n  "release_year": 2010\\n}', additional_kwargs={})

聊天模型(Chat Model)和LLM模型一样,也有一个生成函数,您可以在其中传入多个消息集合。与前述类似,它还包括有用的信息,如令牌使用情况。

python 复制代码
batch_messages = [
    [
        SystemMessage(content="Return only a JSON object as a response with no explanation text"),
        HumanMessage(content="Generate a JSON response object containing a brief description and release year for the movie 'Inception'")
    ],
    [
        SystemMessage(content="Return only a JSON object as a response with no explanation text"),
        HumanMessage(content="Generate a JSON response object containing a brief description and release year for the movie 'Avatar'")
    ]
]
result = chat.generate(batch_messages)

print(result.generations[1])
# ChatGeneration(text='{\\n  "title": "Avatar",\\n  "description": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",\\n  "release_year": 2009\\n}', generation_info=None, message=AIMessage(content='{\\n  "title": "Avatar",\\n  "description": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",\\n  "release_year": 2009\\n}', additional_kwargs={}))

print(result.llm_outout)
# {'token_usage': {'prompt_tokens': 91, 'completion_tokens': 151, 'total_tokens': 242}, 'model_name': 'gpt-3.5-turbo'}
相关推荐
后端小肥肠26 分钟前
18条作品狂揽390万赞?我用Coze破解了“情绪放大镜”的流量密码
人工智能·aigc·coze
Electrolux3 小时前
[wllama]纯前端实现大语言模型调用:在浏览器里跑 AI 是什么体验。以调用腾讯 HY-MT1.5 混元翻译模型为例
前端·aigc·ai编程
新智元3 小时前
全球第二易主,谷歌逆袭登顶!OpenAI 500 亿股票池曝光,Ilya 躺赚 40 亿
人工智能·openai
新智元4 小时前
1 人顶 1 个 Infra 团队!OpenAI 前 CTO 新招,让大模型训练跌成白菜价
人工智能·openai
147AI5 小时前
LLM 应用评测闭环:eval.jsonl + LLM-as-judge + 线上指标(含 Python 最小实现)
aigc·ai编程
_清欢l6 小时前
Dify+test2data实现自然语言查询数据库
数据库·人工智能·openai
哥只是传说中的小白6 小时前
Nano Banana Pro高并发接入Grsai Api实战!0.09/张无限批量生成(附接入实战+开源工具)
开发语言·数据库·ai作画·开源·aigc·php·api
向量引擎6 小时前
【万字硬核】解密GPT-5.2-Pro与Sora2底层架构:从Transformer到世界模型,手撸一个高并发AI中台(附Python源码+压测报告)
人工智能·gpt·ai·aigc·ai编程·ai写作·api调用
Esun_R8 小时前
当 LLM 开始连接真实世界:MCP 的原理、通信与工程落地
node.js·openai·mcp
DisonTangor8 小时前
UltraShape 1.0: 高保真三维形状生成:基于可扩展几何优化
人工智能·3d·开源·aigc