AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


LangChain 与 ollama 携手python 环境演示 Hello World 构建属于自己的大模型应用
发布日期:2024-04-12 06:20:52 浏览次数: 2298


1.LangChain简介

LangChain是一个开源框架,用于构建大型语言模型LLM应用程序,LangChain旨在帮助开发应用,基于文档数据的问答,聊天机器人,代理等。LangChain 提供各种工具和抽象,以提高模型生成的信息的定制性、准确性和相关性。例如,开发人员可以使用 LangChain组件来构建新的提示链或自定义现有模板。LangChain 还包括一些组件,可让 LLM 无需重新训练即可访问新的数据集。

2.python和ollama安装

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:4bllama2qwen:4b等。运行python代码前,后台开启服务,后台有小骆驼图标,或者有命令行运行ollama serve

3.Hello World

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是什么。这是最简单的LangChainollama的演示,几行代码,就可以在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""金庸有哪些小说?"})

回答:

金庸先生的代表作包括《射雕英雄传》、《神雕侠侣》、《倚天屠龙记》、《笑傲江湖》、《鹿鼎记》等等。这些作品不仅在中国有着广泛的影响,而且在全球范围内都有着广泛的读者群和影响力。

4.OpenAI Hello world

上面例子是使用本地模型,也可以通过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+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询