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
最后,我们可以对格式化的提示调用聊天模型了!
相关推荐
NEXT0610 小时前
AI 应用工程化实战:使用 LangChain.js 编排 DeepSeek 复杂工作流
前端·javascript·langchain
孤舟晓月12 小时前
Langchain 1.0后astream_events事件类型及生命周期简析
langchain·大模型·langgraph
玄同76513 小时前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
学习是生活的调味剂14 小时前
大模型应用之使用LangChain实现RAG(一)
langchain·rag
小天呐15 小时前
08—langchain Retrieval
langchain
Dontla19 小时前
黑马大模型RAG与Agent智能体实战教程LangChain提示词——6、提示词工程(提示词优化、few-shot、金融文本信息抽取案例、金融文本匹配案例)
redis·金融·langchain
JaydenAI19 小时前
[LangChain之链]LangChain的Chain——由Runnable构建的管道
python·langchain
草帽lufei19 小时前
LangChain 框架核心向量存储
langchain
猫头虎19 小时前
如何使用Docker部署OpenClaw汉化中文版?
运维·人工智能·docker·容器·langchain·开源·aigc
qq_54702617920 小时前
LangChain 1.0 核心概念
运维·服务器·langchain