langChain

  • 提示词优化的相关功能API
  • 调用各类模型的功能API
  • 会话记忆的相关功能API
  • 各类文档管理分析的功能API
  • 构建Agent智能体的相关功能API
  • 各类功能链式执行的能力

pip install langchain langchain-community langchain-ollama dashscope chromadb

  • langchain:核心包
  • Langchain-community:社区支持包提供了更多的第三方模型调用(我们用的阿里云千问模型就需要这个包)
  • Langchain-ollama:0llama支持包,支持调用0llama托管部署的本地模型
  • dashscope:阿里云通义千问的Python SDK
  • chromadb:轻量向量数据库(后续使用)

LLMs大语言模型调用

在线模型调用

python 复制代码
# 阿里云大模型调用
from langchain_community.llms.tongyi import Tongyi

# 1 实例化模型
model = Tongyi(model="qwen-plus")  # 注意:环境变量中要有 DASHSCOPE_API_KEY / OPENAI_API_KEY
# 2 模型推理
#res= model.invoke("你是谁?")
#print(res)
# 流式输出
res = model.stream("你是谁?")
for chunk in res:
    print(chunk, end=" ", flush=True)

本地ollama大模型调用

python 复制代码
from langchain_ollama import OllamaLLM

# 1 获取模型对象
model = OllamaLLM(model="qwen3:0.6b")
# 2 模型推理
#res = model.invoke("你是谁?")
#print(res)
# 流式输出
res = model.stream("你是谁?")
for chunk in res:
    print(chunk, end=" ", flush=True)

Chat聊天模型调用

  • AIMessage:就是AI 输出的消息,可以是针对问题的回答。(0penAI库中的assistant角色)
  • HumanMessage:人类消息就是用户信息,由给出的信息发送给LLMs的提示信息,比如"实现一个快速排序方法".(0penAI库中的user角色)
  • SystemMessage:可以用于指定模型具体所的环境和背景,如角色扮演等。你可以在这里给个代码专家",或者"返回json格式"(0penAI库中的system角色)

在线模型调用

python 复制代码
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
# 1 获取模型
chat = ChatTongyi(model="qwen3-plus")
# 2 消息
# message = [
#     SystemMessage(content="你是一位唐代的诗人"),
#     AIMessage(content="锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。"),
#     HumanMessage(content="根据你上一首诗的格式,再写一首诗")
# ]
# 消息的另一种写法 (角色,内容) 角色:system/ai/human
message = [
    ("system", "你是一位唐代的诗人"),
    ("ai", "锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。"),
    ("human", "根据你上一首诗的格式,再写一首诗"),
]
res = chat.stream(message)
for chunk in res:
    print(chunk.content, end="", flush=True)

本地ollama大模型调用

python 复制代码
from langchain_ollama import ChatOllama
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
# 1 获取模型
chat = ChatOllama(model="qwen3:0.6b")
# 2 消息
# message = [
#     SystemMessage(content="你是一位唐代的诗人"),
#     AIMessage(content="锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。"),
#     HumanMessage(content="根据你上一首诗的格式,再写一首诗")
# ]
# 消息的另一种写法
message = [
    ("system", "你是一位唐代的诗人"),
    ("ai", "锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。"),
    ("human", "根据你上一首诗的格式,再写一首诗"),
]
res = chat.stream(message)
for chunk in res:
    print(chunk.content, end="", flush=True)
相关推荐
梦在远山后9 小时前
从手写 Loop 到可恢复 Runtime:用 LangGraph、PostgreSQL Checkpoint 与 AG-UI 跑通中断恢复
python·langchain·agent
前端精髓15 小时前
langchain TS 基本用法入门
langchain
小白勇闯网安圈19 小时前
第5章 流式输出与人工审核
python·langchain
小白勇闯网安圈19 小时前
第2章 状态 State 详解
python·langchain
半个落月1 天前
LangChain.js Agent Memory 实战(下):用 Milvus 构建可检索的长期记忆
javascript·langchain
半个落月1 天前
LangChain.js Agent Memory 实战(上):从内存对话、文件持久化到截断与摘要
javascript·langchain
梦在远山后1 天前
Electron 与 FastAPI 如何完成流式 Agent 对话
python·langchain·agent
猿人谷1 天前
Jev:当 AI 不再生成 Token,而是直接做决策
后端·langchain·aigc
逆风飞翔的小叔2 天前
【AI智能体】Langchain 主流大模型调用与对话API使用详解
langchain·langchain 对话api·langchain 对话·langchain 对话使用·langchain 对话详解·langchain api使用
王国强20092 天前
如何通过 Deep Agents、LangChain 与 LangGraph 构建生产级智能体:从 Deep Agents Code 源码看 Agent 工程实
langchain