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 小时前
create_agent:LangChain 新版 Agent 的核心入口
人工智能·架构·langchain
Esaka_Forever9 小时前
LangChain+LangGraph+GPT-OSS+Groq Cloud
gpt·langchain
尽兴-12 小时前
5.1 主流框架:LangChain、LlamaIndex、Semantic Kernel
microsoft·langchain·semantic·liamaindex
wuhen_n12 小时前
RAG 优化实战:检索精准度提升全方案
前端·langchain·ai编程
沪漂阿龙13 小时前
LangChain 系列之Agent:从固定流程到模型自主决策
服务器·数据库·langchain
老陈聊架构14 小时前
『AI大模型』OpenDataLoader PDF 实战:RAG 知识库 PDF 解析与LangChain 接入
ai·langchain·pdf·rag·opendataloader
喵叔哟14 小时前
Week 3 --Day 4:生产级部署
python·langchain
GISer_Jing15 小时前
LangChain 核心架构深度解析:从设计哲学到工程实践
架构·langchain
满怀冰雪15 小时前
01_LangChain是什么_带你理解LLM应用框架
python·langchain
沪漂阿龙15 小时前
RAG 是什么?为什么大模型需要外挂知识库?
人工智能·架构·langchain