langchain qwen3 构建一个简单的对话系统

创建一个conda环境

bash 复制代码
conda create -n llm python=3.10

软件库安装,缺少的软件包,会报错提示安装即可

bash 复制代码
pip install langchain transformers

代码:

python 复制代码
from langchain_huggingface import ChatHuggingFace, HuggingFacePipeline
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
import torch

# 1. 初始化本地 LLM (保持你之前的配置)
llm = HuggingFacePipeline.from_model_id(
    model_id="/data2/wanghq/models/Qwen/Qwen3-8B",
    task="text-generation",
    pipeline_kwargs={
        "max_new_tokens": 1024,
        "temperature": 0.5,
        "do_sample": True,
        "device_map": "auto",
        "trust_remote_code": True,
        "dtype": torch.bfloat16, 
    },
)

model = ChatHuggingFace(llm=llm)

# 2. 建立内存中的存储字典,用于存放不同 session 的历史记录
store = {}

def get_session_history(session_id: str):
    if session_id not in store:
        store[session_id] = InMemoryChatMessageHistory()
    return store[session_id]

# 3. 封装成带记忆能力的 Chain
# 这里的 config 会根据 session_id 自动提取或存储对话历史
with_message_history = RunnableWithMessageHistory(
    model,
    get_session_history,
)

# 4. 实现循环对话
print("\n--- 已进入连续对话模式(输入 'exit' 退出) ---")
session_config = {"configurable": {"session_id": "robot_research_01"}}

# 可选:先存入一个系统提示词
get_session_history("robot_research_01").add_message(
    SystemMessage(content="你是一个机器人专家。直接回复,不要输出 <think> 标签。")
)

while True:
    user_input = input("\nUser: ")
    if user_input.lower() in ["exit", "quit", "退出"]:
        break

    print("\nQwen: ", end="", flush=True)
    
    # 5. 调用并流式输出
    # 注意:with_message_history 会自动处理历史记录的注入和更新
    for chunk in with_message_history.stream(
        [HumanMessage(content=user_input)],
        config=session_config
    ):
        print(chunk.content, end="", flush=True)
    print("\n")

执行结果

相关推荐
陳陈陳8 小时前
Workflow vs Agent:别再被“调包侠”忽悠了,一张图看懂AI工程的“骨架”与“大脑”
langchain·agent·workflow
大模型码小白10 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
麻雀飞吧10 小时前
最新量化学习路径,交易认知和技术实现要并行
人工智能·python
一只小bit11 小时前
LangGraph 记忆、人机交互、时间旅行和核心能力
机器学习·langchain·人机交互·langgraph
C^h14 小时前
python函数学习
人工智能·python·机器学习
早点睡啊Y14 小时前
深入学 LangChain 官方文档(十六)Built-in 与 Custom Middleware
langchain
Fanta丶14 小时前
4.Python set()集合、dict(字典、映射)、 数据容器的通用功能
python
决战灬14 小时前
langgraph之interrupt(理论篇)
人工智能·python·agent
兜客互动15 小时前
2026年AI关键词拓展挖掘软件,高效助力内容创作精准获流
人工智能·python
weixin_5386019715 小时前
智能体测开Day30pytest测试框架
python