langchain 入门(一)

导读

环境:OpenEuler、Windows 11、WSL 2、Python 3.12.3 langchain 0.3.15

背景:前期忙碌的开发阶段结束,需要沉淀自己的应用知识,过一遍LangChain

时间:20250124

说明:使用langchain,实现基本的翻译应用,分别为:普通模式、流式输出、多语言模式

官方文档:简单LLM应用

1、安装模块

python 复制代码
pip install langchain langchain_openai

当前这个时间会自动安装共计44个packages,真多

2、模型的apikey

推荐免费的阿里云百炼:阿里云百炼 - 通义千问 - QwQ-32B-Preview

具体模型自己选择,如果不知道如何选择,可使用我给的链接,注册即有免费的额度

请注意红框内的信息,此处说明可以免费使用,可按照图示获取apikey

接口调用方法文档,如下:

阿里云百炼 - 通义千问 - QwQ-32B-Preview-OpenAI接口

3、创建应用

该应用实现翻译的功能,自英文翻译为中文(官方案例为英文到意大利文)

python 复制代码
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

client = ChatOpenAI(
    # 根据自己的需求配置,可以是环境变量,也可以是文本内容
    api_key="sk-005dc4fxxxxxxxxxxxxxxxxxxxxxxx5415ca", 
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="qwen-plus"
)

messages = [
    # 系统提示词:将英文翻译为中文
    SystemMessage("Translate the following from English into chinese"),
    HumanMessage("hi!"),
]

aimessages = client.invoke(messages)
print(aimessages.content)

结果如下:

python 复制代码
(venv) [jack@Laptop-L14-gen4 langtest]$ /home/jack/langtest/venv/bin/python /home/jack/langtest/chain_test/simple_LLM_application.py
你好!

4、流式输出

现在主流都是(异步)流式输出,langchain也支持该功能

python 复制代码
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage

client = ChatOpenAI(
    # 根据自己的需求配置,可以是环境变量,也可以是文本内容
    api_key="sk-005dc4fxxxxxxxxxxxxxxxxxxxxxxx5415ca", 
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="qwen-plus"
)

messages = [
    # 系统提示词:将英文翻译为中文
    SystemMessage("Translate the following from English into chinese"),
    HumanMessage("Translate the following from English into chinese!"),
]

for token in client.stream(messages):
    print(token.content, end="|")

解决如下:

python 复制代码
[jack@Laptop-L14-gen4 langtest]$ /home/jack/langtest/venv/bin/python /home/jack/langtest/chain_test/simple_LLM_application.py
|请|将|以下|内容从英语翻译|成中文!||

5、灵活控制提示词

通过控制提示词,实现多语言额翻译功能

python 复制代码
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

system_template = "Translate the following from English into {language}"

prompt_template = ChatPromptTemplate.from_messages(
    [("system", system_template), ("user", "{text}")]
)
client = ChatOpenAI(
    # 根据自己的需求配置,可以是环境变量,也可以是文本内容
    api_key="sk-005dc4fxxxxxxxxxxxxxxxxxxxxxxx5415ca", 
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="qwen-plus"
)

prompt = prompt_template.invoke({"language": "Chinese", "text": "Finally, we can invoke the chat model on the formatted prompt!"})

prompt.to_messages()
response = client.invoke(prompt)
print(response.content)

结果:

python 复制代码
(venv) [jack@Laptop-L14-gen4 langtest]$ /home/jack/langtest/venv/bin/python /home/jack/langtest/chain_test/simple_LLM_application.py
最后,我们可以对格式化的提示调用聊天模型了!
相关推荐
哥本哈士奇14 小时前
LangChain Deepagent 版本0.6.1中间件一个bug
中间件·langchain·bug
秦jh_16 小时前
【LangChain】流式传输
langchain
前端小超人rui16 小时前
AI Agent对比 / Coze Dify LangChain LangGraph的区别
langchain·a i agent
Restart-AHTCM18 小时前
LangChain学习之环境搭建与基础概念(1/8)
学习·langchain
米小虾18 小时前
从 ReAct 到 Multi-Agent:AI Agent 架构设计的演进与实践
langchain·jetbrains
小趴菜不能喝20 小时前
LangChain 开发Agent基础
langchain
云姜.20 小时前
如何快速使用Langchain上手编程
python·langchain
情绪总是阴雨天~20 小时前
深度解析:LangChain、Agent、RAG、FC、ReAct、LangGraph、A2A、MCP — 区别、联系与全景图
python·langchain·agent·rag·langgraph·mcp·a2a
赢乐21 小时前
AI大模型学习笔记:LangChain核心组件-工具(Tools)
langchain·大模型·agent·function_call·工具(tools)·tool装饰器·定义工具
秦歌66621 小时前
LangChain-9-中间件middleware
langchain