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)
相关推荐
Csvn2 天前
🌟 LangChain 30 天保姆级教程 · Day 13|OutputParser 进阶!让 AI 输出自动转为结构化对象,并支持自动重试!
python·langchain
InKomorebi2 天前
LangChain Tools:BaseTool/Callable/Runnable 核心类型 | 三种工具定义方式 | 串行与并行调用 | 错误处理与重试降级
langchain
怕浪猫2 天前
第10章 RAG(检索增强生成)系统构建(LangChain实战)
langchain·aigc·ai编程
阿捞22 天前
python-langchain框架(3-20-智能问答ZeroShot_ReAct Agent 从零搭建)
python·react.js·langchain
qyhua2 天前
开源推荐 | ModelX RAG:基于 LangChain + Ollama 的企业级知识库系统
python·langchain·开源
斯外戈的小白2 天前
【Agent】LangChain 1.0架构
架构·langchain
tkevinjd3 天前
基于LangChain的简易智能旅游助手Agent
langchain·agent
liu****3 天前
LangChain-AI应用开发框架(七)
人工智能·python·langchain·大模型应用·本地部署大模型
秦jh_3 天前
【LangChain】大模型介绍
langchain
InKomorebi3 天前
LangChain Agent 中间件完全指南:六种钩子函数从入门到生产(附完整教学代码)
langchain