微信扫码
与创始人交个朋友
我要投稿
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+中大型企业
2024-12-26
开发者的选择越来越多了,又一个AI智能体框架玩家:PydanticAI
2024-12-25
2025年,拥抱AI Agent!
2024-12-25
基于LangChain构建安全Agent应用实践(含代码)
2024-12-22
ANTHROPIC:高端的食材往往需要最朴素的烹饪方法: prompt, workflow, agent
2024-12-21
用LangChain教AI模仿你的写作风格:详细教程
2024-12-18
一站式 LLM 工程观测平台:Langfuse,让所有操作可观测
2024-12-17
LLMs开发者必看!Pydantic AI代理框架震撼登场!
2024-12-16
用LangChain实现一个Agent
2024-04-08
2024-08-18
2024-06-03
2024-10-10
2024-04-08
2024-04-17
2024-06-24
2024-07-13
2024-09-04
2024-04-11
2024-12-02
2024-11-25
2024-10-30
2024-10-11
2024-08-18
2024-08-16
2024-08-04
2024-07-29