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)
相关推荐
孤舟晓月2 小时前
Langchain 1.0后astream_events事件类型及生命周期简析
langchain·大模型·langgraph
玄同7652 小时前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
学习是生活的调味剂4 小时前
大模型应用之使用LangChain实现RAG(一)
langchain·rag
小天呐5 小时前
08—langchain Retrieval
langchain
Dontla8 小时前
黑马大模型RAG与Agent智能体实战教程LangChain提示词——6、提示词工程(提示词优化、few-shot、金融文本信息抽取案例、金融文本匹配案例)
redis·金融·langchain
JaydenAI9 小时前
[LangChain之链]LangChain的Chain——由Runnable构建的管道
python·langchain
草帽lufei9 小时前
LangChain 框架核心向量存储
langchain
猫头虎9 小时前
如何使用Docker部署OpenClaw汉化中文版?
运维·人工智能·docker·容器·langchain·开源·aigc
qq_54702617910 小时前
LangChain 1.0 核心概念
运维·服务器·langchain
uXrvbWJGleS10 小时前
三相两电平整流器Simulink仿真探究
langchain