微信扫码
添加专属顾问
我要投稿
LangChain是一个开源框架,用于构建大型语言模型LLM应用程序,LangChain旨在帮助开发应用,基于文档数据的问答,聊天机器人,代理等。LangChain 提供各种工具和抽象,以提高模型生成的信息的定制性、准确性和相关性。例如,开发人员可以使用 LangChain组件来构建新的提示链或自定义现有模板。LangChain 还包括一些组件,可让 LLM 无需重新训练即可访问新的数据集。
python版本要3.8以上,我使用的是3.11.8,另外后面要用的都可以一起安装上,以下是我虚拟环境下的LangChain的版本:
langchain 0.1.11
langchain-community 0.0.27
langchain-core 0.1.35
langchain-openai 0.1.1
langchain-text-splitters 0.0.1
langchainhub 0.1.15
langserve 0.0.51
langsmith 0.1.23
使用pip安装即可。
ollama安装非常简单,我前面几篇文章有过介绍,这里不做介绍,默认安装好ollama,安装好几个模型,比如gemma:4b,llama2,qwen:4b等。运行python代码前,后台开启服务,后台有小骆驼图标,或者有命令行运行ollama serve。
from langchain_community.llms import Ollama
llm = Ollama(model="llama2")
llm.invoke("What is ollama?")
输出结果:
I believe you meant "what is llama?"
Llama is a South American camelid species known for their soft, woolly coats and distinctive long neck and ears. Llamas are native to the Andean region of South America and have been domesticated for thousands of years for their meat, wool, and milk. They are known for their gentle nature and ability to carry heavy loads, making them popular as pack animals in many parts of the world.
这个模型不知道ollama是什么。这是最简单的LangChain与ollama的演示,几行代码,就可以在Python环境中调用ollama,使用大模型。
再稍微修改一下,使用LangChain的模板进行问答:
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
llm = Ollama(model="qwen:4b")
output_parser = StrOutputParser()
prompt = ChatPromptTemplate.from_messages([
("system", "You are world class technical documentation writer."),
("user", "{input}")
])
chain = prompt | llm | output_parser
chain.invoke({"input": "金庸有哪些小说?"})
回答:
金庸先生的代表作包括《射雕英雄传》、《神雕侠侣》、《倚天屠龙记》、《笑傲江湖》、《鹿鼎记》等等。这些作品不仅在中国有着广泛的影响,而且在全球范围内都有着广泛的读者群和影响力。
上面例子是使用本地模型,也可以通过api访问线上的模型。使用openai,需要有openai api key,把API KEY设置到环境变量中。
from langchain_openai import ChatOpenAI
from langchain import hub
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
import os
os.environ['OPENAI_API_KEY'] = '您的有效OpenAI API Key'
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0,openai_api_base="https://xxxxxxx/v1")
output_parser = StrOutputParser()
prompt = ChatPromptTemplate.from_messages([
("system", "You are world class technical documentation writer."),
("user", "{input}")
])
chain = prompt | llm | output_parser
chain.invoke({"input": "金庸有哪些小说?"})
使用gpt-3.5-turbo模型,temperature为0,使用三方接口,回答结果:
金庸(原名查良镛)是中国著名作家,以其武侠小说闻名于世。他创作了许多经典的武侠小说,其中一些最著名的作品包括:
1. 《射雕英雄传》
2. 《神雕侠侣》
3. 《倚天屠龙记》
4. 《笑傲江湖》
5. 《天龙八部》
6. 《鹿鼎记》
7. 《碧血剑》
8. 《鸳鸯刀》
9. 《连城诀》
10. 《越女剑》
这些小说被广泛认为是中国武侠文学的经典之作,深受读者喜爱。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-29
为什么我们选择 LangGraph 作为智能体系统的技术底座?
2025-10-27
Langchain 、 Manus 组了一个研讨会:Agent越智能,死得越快!
2025-10-23
LangChain V1.0 深度解析:手把手带你跑通全新智能体架构
2025-10-23
LangChain 与 LangGraph 双双发布 1.0:AI 智能体框架迎来里程碑时刻!
2025-10-19
AI 不再“乱跑”:LangChain × LangGraph 打造可控多阶段智能流程
2025-10-15
LangChain对话Manus创始人:顶级AI智能体上下文工程的“满分作业”首次公开
2025-10-09
Langchain回应OpenAI:为什么我们不做拖拉拽工作流
2025-09-21
告别无效检索:我用LangExtract + Milvus升级 RAG 管道的实战复盘
2025-09-13
2025-09-21
2025-10-19
2025-08-19
2025-08-17
2025-09-19
2025-09-12
2025-09-06
2025-08-03
2025-08-29
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21
2025-05-19