本文概览
一、配置OLLaMA1.1 安装OLLaMA1.2 下载OLLaMA模型二、OLLaMA+LangChain搭建本地大模型2.1 代码2.2 模型耗时对比
一、配置OLLaMA
考虑到大家不是那么方便科学上网,笔者推荐使用OLLaMA框架。
1.1 安装OLLaMA
安装地址 https://ollama.ai/download
支持的平台包括:Mac、Linux、Windows,并提供了docker 镜像。
- Mac / windows:直接下载安装包,无脑安装即可
- Linux:
curl https://ollama.ai/install.sh | sh
1.2 下载OLLaMA模型
以通义千问模型为例:
# 示例:ollama run 模型名
ollama run qwen:7b
第一次下载时间长点,后面再运行就不用下载了,直接进入对话环境。
# 我下载了 以下4个 模型
llama2
openhermes
solar
qwen:7b
Ollama 提供了多个模型,每个都有其特点和适用场景:
- Llama 2 :这是一个预训练的大型语言模型,具有7B、13B和70B三种不同规模的模型。Llama 2增加了预训练语料,上下文长度从2048提升到4096,使得模型能够理解和生成更长的文本。
- OpenHermes :这个模型专注于代码生成和编程任务,适合用于软件开发和脚本编写等场景。
- Solar :这是一个基于Llama 2的微调版本,专为对话场景优化。Solar在安全性和有用性方面进行了人工评估和改进,旨在成为封闭源模型的有效替代品。
- Qwen:7B :这是一个中文微调过的模型,特别适合处理中文文本。它需要至少8GB的内存进行推理,推荐配备16GB以流畅运行。
综上所述,这些模型各有侧重点,用户可以根据自己的需求选择合适的模型进行使用。
二、OLLaMA+LangChain搭建本地大模型
2.1 代码
实现目标:创建LLM链。假设我们想要创建一个公司名字
英文版
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import Ollama
prompt_template = "What is a good name for a company that makes {product}?"
ollama_llm = Ollama(model="qwen:7b")
llm_chain = LLMChain(
llm = ollama_llm,
prompt = PromptTemplate.from_template(prompt_template)
)
print(llm_chain("colorful socks"))
中文版
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import Ollama
prompt_template = "请给制作 {product} 的公司起个名字,只回答公司名即可"
ollama_llm = Ollama(model="qwen:7b")
llm_chain = LLMChain(
llm = ollama_llm,
prompt = PromptTemplate.from_template(prompt_template)
)
print(llm_chain("袜子"))
# print(llm\_chain.run("袜子")) # 加个.run也可
输出:{'product': '袜子', 'text': '"袜界精品"'}
print(llm_chain.predict("袜子"))
输出:袜梦工坊
run和 predict的区别是
- llm_chain.
run:结合 输入{product} 和 大模型输出内容一起输出 - llm_chain.
predict:只给出大模型输出内容
2.2 模型耗时对比
来看下其他3个模型的回答,没有GPU,运行泰慢啦!
- llama2 (20min)
prompt_template = "请给制作 {product} 的公司起个名字,只回答公司名即可"
ollama_llm = Ollama(model="llama2")
llm_chain = LLMChain(
llm = ollama_llm,
prompt = PromptTemplate.from_template(prompt_template)
)
llm_chain("袜子")
{'product': '袜子',
'text': 'Certainly! Here are some name suggestions for a company that manufactures socks:1. SoleMates - A play on the phrase "soul mates," suggesting that the socks are perfect companions for your feet.\n2. Footloose & Co. - A nod to the classic song and dance, and also highlighting the company\'s focus on providing loose-fitting socks.\n3. HipHuggers - A playful name that evokes the idea of the socks hugging your hips and providing comfort.\n4. ToeTastic - A fun, catchy name that suggests the socks are amazing and worth getting excited about.\n5. SoxAppeal - A clever name that combines "socks" and "appeal," implying that the company\'s products are appealing and desirable.\n6. AnkleAdorers - A name that suggests the socks are adorable and lovable, and also highlights their focus on providing ankle-friendly fits.\n7. FootFlair - A playful name that suggests the socks offer a touch of flair and personality to your outfit.\n8. SockSational - A cheesy but fun name that suggests the socks are remarkable and exceptional.\n9. HeelHappiness - A name that implies the socks will bring happiness to your heels and overall foot health.\n10. ToeTasticToes - A playful name that combines "toes" and "fantastic," suggesting that the socks are fantastic for your toes and overall foot comfort.'}
- openhermes (13min)
{'product': '袜子',
'text': '凯戴缝袜公司 (Kaida Gao Nu Gong Si)'}
- solar (48min)
{'product': '袜子',
'text': '一格袜子 (Yi Ge Wu Zi) could be a company name for producing gloves. The characters used are in simplified Chinese and translate to "One Glove" in English.'}
