Ollama 是一款開源的本地運行大型語言模型(LLM)的框架,它允許用戶在自己的設(shè)備上直接運行各種大型語言模型流济,包括 Llama奔则、Mistral、Qwen 等多種模型未蝌,無需依賴網(wǎng)絡(luò)連接驮吱。
Ollama 還提供跨平臺的支持,包括 macOS萧吠、Windows左冬、Linux 以及 Docker, 幾乎覆蓋了所有主流操作系統(tǒng)怎憋。
1. 配置OLLaMA
OLLaMA可以運行本地大語言模型又碌,模型名稱如下:
https://ollama.com/library
每個模型都有其特點和適用場景:
- Llama 2:這是一個預(yù)訓(xùn)練的大型語言模型,具有7B绊袋、13B和70B三種不同規(guī)模的模型毕匀。Llama 2增加了預(yù)訓(xùn)練語料,上下文長度從2048提升到4096癌别,使得模型能夠理解和生成更長的文本皂岔。
- OpenHermes:這個模型專注于代碼生成和編程任務(wù),適合用于軟件開發(fā)和腳本編寫等場景展姐。
- Solar:這是一個基于Llama 2的微調(diào)版本躁垛,專為對話場景優(yōu)化。Solar在安全性和有用性方面進(jìn)行了人工評估和改進(jìn)圾笨,旨在成為封閉源模型的有效替代品教馆。
- Qwen:7B:這是一個中文微調(diào)過的模型,特別適合處理中文文本擂达。它需要至少8GB的內(nèi)存進(jìn)行推理土铺,推薦配備16GB以流暢運行。
綜上所述,這些模型各有側(cè)重點悲敷,用戶可以根據(jù)自己的需求選擇合適的模型進(jìn)行使用究恤。
下載的模型列表,可以通過以下命令來查看:
ollama list
NAME ID SIZE MODIFIED
llama2:latest 78e26419b446 3.8 GB 38 hours ago
llama2-chinese:13b 990f930d55c5 7.4 GB 2 days ago
qwen:7b 2091ee8c8d8f 4.5 GB 7 days ago
qwen:latest d53d04290064 2.3 GB 2 days ago
1.1 安裝
ollama官網(wǎng) https://ollama.com/
1.2 下載模型
以通義千問模型為例:
ollama run 模型名
ollama run qwen:7b
第一次下載時間長點后德,后面再運行就不用下載了
2. langchain實現(xiàn)
2.1.LLMChain調(diào)用
LLMChain是一個簡單的鏈部宿,接受一個提示模板,使用用戶輸入格式化它并從LLM返回響應(yīng)瓢湃。
其中理张,prompt_template是一個非常關(guān)鍵的組件,可以讓你創(chuàng)建一個非常簡單的鏈箱季,它將接收用戶輸入涯穷,使用它格式化提示,然后將其發(fā)送到LLM藏雏。
實現(xiàn)目標(biāo):創(chuàng)建LLM鏈拷况。假設(shè)我們想要創(chuàng)建一個公司名字
英文版
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': '"棉語襪業(yè)公司"\n\n\n'}
print(llm_chain.predict(product="襪子"))
輸出:棉語襪業(yè)公司
run和 predict的區(qū)別是
- llm_chain.run:結(jié)合 輸入{product} 和 大模型輸出內(nèi)容一起輸出
- llm_chain.predict :只給出大模型輸出內(nèi)容
2.2.Model調(diào)用
直接調(diào)用llama2模型
from langchain_community.llms import Ollama
llm = Ollama(model="llama2")
response = llm.invoke("Who are you")
print(response)
運行輸出結(jié)果:
I'm LLaMA, an AI assistant developed by Meta AI that can understand and respond
to human input in a conversational manner. I'm here to help you with any questions
or topics you'd like to discuss!
Is there anything specific you'd like to talk about?
3.本地化LLM
前面講到,可以通過ollama run llama2 可以直接訪問大模型:
>>> hello
Hello! It's nice to meet you. Is there something I can help you
with or would you like to chat?
>>> tell me a joke
Sure, here's one:
Why don't scientists trust atoms?
Because they make up everything!
I hope that brought a smile to your face ??. Is there anything
else I can assist you with?
>>> Send a message (/? for help)
langchain集成
可以通過langchain本地代碼方式集成實現(xiàn)掘殴,實現(xiàn)方式如下:
ollama_host = "localhost"
ollama_port = 11434
ollama_model = "llama2"
from langchain_community.llms import Ollama
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
if __name__ == "__main__":
llm = Ollama(base_url = f"http://{ollama_host}:{ollama_port}",
model= ollama_model,
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]))
while True:
query = input("\n\n>>>Enter a query:")
llm(query)
運行后赚瘦,顯示效果如下:
>>>Enter a query:hello
Hello! It's nice to meet you. Is there something I can help you with or would you like to chat?
>>>Enter a query:tell me a joke
Sure! Here's one:
Why don't scientists trust atoms?
Because they make up everything!
I hope that made you smile! Do you want to hear another one?
>>>Enter a query:
4.定制化LLM
4.1.Modelfile
可以通過ModelFile的方式來對大模型進(jìn)行本地定制化:
1.Create a Modelfile:
FROM llama2
SYSTEM """
You are responsible for translating user's query to English. You should only respond
with the following content:
1. The translated content.
2. Introduction to some ket concepts or words in the translated content, to help
users understand the context.
"""
2.創(chuàng)建LLM:
ollama create llama-translator -f ./llama2-translator.Modelfile
創(chuàng)建完后,ollama list 可以發(fā)現(xiàn):
llama-translator:latest 40f41df44b0a 3.8 GB 53 minutes ago
3.運行LLM
ollama run llama-translator
運行結(jié)果如下:
>>> 今天心情不錯
Translation: "Today's mood is good."
Introduction to some key concepts or words in the translated content:
* 心情 (xīn jìng) - mood, state of mind
* 不錯 (bù hǎo) - good, fine, well
So, "今天心情不錯" means "Today's mood is good." It is a simple sentence that expresses a positive emotional state.
The word "心情" is a key term in Chinese that refers to one's emotions or mood, and the word "不錯"
is an adverb that can be translated as "good," "fine," or "well."
>>> 我愛你中國
Translation: "I love you China."
Introduction to some key concepts or words in the translated content:
* 愛 (ài) - love, loving
* 中國 (zhōng guó) - China, People's Republic of China
So, "我愛你中國" means "I love you China." It is a simple sentence that expresses affection
or fondness towards a country. The word "愛" is a key term in Chinese that refers to romantic
love, while the word "中國" is a geographical term that refers to the People's Republic of China.
>>> Send a message (/? for help)
4.2.自定義系統(tǒng)提示詞
根據(jù) ChatGPT 的使用經(jīng)驗奏寨,大家都知道系統(tǒng)提示詞的重要性起意。好的系統(tǒng)提示詞能有效地將大模型定制成自己需要的狀態(tài)。在 Ollama 中病瞳,有多種方法可以自定義系統(tǒng)提示詞揽咕。
首先,不少 Ollama 前端已提供系統(tǒng)提示詞的配置入口套菜,推薦直接利用其功能亲善。此外,這些前端在底層往往是通過API與 Ollama 服務(wù)端交互的逗柴,我們也可以直接調(diào)用蛹头,并傳入系統(tǒng)提示詞選項:
curl http://localhost:11434/api/chat -d '{
"model": "llama2-chinese:13b",
"messages": [
{
"role": "system",
"content": "以海盜的口吻簡單作答。"
},
{
"role": "user",
"content": "天空為什么是藍(lán)色的戏溺?"
}
],
"stream": false
}'
其中role為system的消息即為系統(tǒng)提示詞渣蜗,跟Modelfile里面的SYSTEM下面的定義差不多一個意思。
輸出如下:
{
"model":"llama2-chinese:13b",
"created_at":"2024-04-29T01:32:08.448614864Z",
"message":{
"role":"assistant",
"content":"好了旷祸,這個問題太簡單了耕拷。藍(lán)色是由于我們的視覺系統(tǒng)處理光線而有所改變造成的。在水平方向看到的天空大多為天際輻射托享,
其中包括大量的紫外線和可見光線骚烧。這些光線會被散射控淡,而且被大氣層上的大量分子所吸收,進(jìn)而變成藍(lán)色或其他相似的顏色止潘。\n"
},
"done":true,
"total_duration":31927183897,
"load_duration":522246,
"prompt_eval_duration":224460000,
"eval_count":149,
"eval_duration":31700862000
}
部分內(nèi)容摘自該文章https://blog.csdn.net/sinat_29950703/article/details/136194337,感謝辫诅!