OLLaMA搭建本地大模型

Ollama 是一款開源的本地運(yùn)行大型語言模型(LLM)的框架,它允許用戶在自己的設(shè)備上直接運(yùn)行各種大型語言模型,包括 Llama、Mistral、Qwen 等多種模型,無需依賴網(wǎng)絡(luò)連接。
Ollama 還提供跨平臺(tái)的支持,包括 macOS、Windows、Linux 以及 Docker, 幾乎覆蓋了所有主流操作系統(tǒng)。

1. 配置OLLaMA

OLLaMA可以運(yùn)行本地大語言模型,模型名稱如下:
https://ollama.com/library
每個(gè)模型都有其特點(diǎn)和適用場(chǎng)景:

  1. Llama 2:這是一個(gè)預(yù)訓(xùn)練的大型語言模型,具有7B、13B和70B三種不同規(guī)模的模型。Llama 2增加了預(yù)訓(xùn)練語料,上下文長(zhǎng)度從2048提升到4096,使得模型能夠理解和生成更長(zhǎng)的文本。
  2. OpenHermes:這個(gè)模型專注于代碼生成和編程任務(wù),適合用于軟件開發(fā)和腳本編寫等場(chǎng)景。
  3. Solar:這是一個(gè)基于Llama 2的微調(diào)版本,專為對(duì)話場(chǎng)景優(yōu)化。Solar在安全性和有用性方面進(jìn)行了人工評(píng)估和改進(jìn),旨在成為封閉源模型的有效替代品。
  4. Qwen:7B:這是一個(gè)中文微調(diào)過的模型,特別適合處理中文文本。它需要至少8GB的內(nèi)存進(jìn)行推理,推薦配備16GB以流暢運(yùn)行。
    綜上所述,這些模型各有側(cè)重點(diǎn),用戶可以根據(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

qwen下載.png

qwen使用.png

第一次下載時(shí)間長(zhǎng)點(diǎn),后面再運(yùn)行就不用下載了

2. langchain實(shí)現(xiàn)

2.1.LLMChain調(diào)用

LLMChain是一個(gè)簡(jiǎn)單的鏈,接受一個(gè)提示模板,使用用戶輸入格式化它并從LLM返回響應(yīng)。
其中,prompt_template是一個(gè)非常關(guān)鍵的組件,可以讓你創(chuàng)建一個(gè)非常簡(jiǎn)單的鏈,它將接收用戶輸入,使用它格式化提示,然后將其發(fā)送到LLM。

實(shí)現(xiàn)目標(biāo):創(chuàng)建LLM鏈。假設(shè)我們想要?jiǎng)?chuàng)建一個(gè)公司名字
英文版

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 = "請(qǐng)給制作 {product} 的公司起個(gè)名字,只回答公司名即可"

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("襪子"))    # 加個(gè).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)

運(yùn)行輸出結(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本地代碼方式集成實(shí)現(xiàn),實(shí)現(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)

運(yùn)行后,顯示效果如下:

>>>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的方式來對(duì)大模型進(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.運(yùn)行LLM

ollama run llama-translator

運(yùn)行結(jié)果如下:

>>> 今天心情不錯(cuò)

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
* 不錯(cuò) (bù hǎo) - good, fine, well

So, "今天心情不錯(cuò)" 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 "不錯(cuò)"
 is an adverb that can be translated as "good," "fine," or "well."

>>> 我愛你中國(guó)

Translation: "I love you China."

Introduction to some key concepts or words in the translated content:

* 愛 (ài) - love, loving
* 中國(guó) (zhōng guó) - China, People's Republic of China

So, "我愛你中國(guó)" 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 "中國(guó)" 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)驗(yàn),大家都知道系統(tǒng)提示詞的重要性。好的系統(tǒng)提示詞能有效地將大模型定制成自己需要的狀態(tài)。在 Ollama 中,有多種方法可以自定義系統(tǒng)提示詞。

首先,不少 Ollama 前端已提供系統(tǒng)提示詞的配置入口,推薦直接利用其功能。此外,這些前端在底層往往是通過API與 Ollama 服務(wù)端交互的,我們也可以直接調(diào)用,并傳入系統(tǒng)提示詞選項(xiàng):

curl http://localhost:11434/api/chat -d '{
  "model": "llama2-chinese:13b",
  "messages": [
    {
      "role": "system",
      "content": "以海盜的口吻簡(jiǎn)單作答。"
    },
    {
      "role": "user",
      "content": "天空為什么是藍(lán)色的?"
    }
  ],
  "stream": false
}'

其中rolesystem的消息即為系統(tǒng)提示詞,跟Modelfile里面的SYSTEM下面的定義差不多一個(gè)意思。
輸出如下:

{
"model":"llama2-chinese:13b",
"created_at":"2024-04-29T01:32:08.448614864Z",
"message":{
     "role":"assistant",
     "content":"好了,這個(gè)問題太簡(jiǎn)單了。藍(lán)色是由于我們的視覺系統(tǒng)處理光線而有所改變?cè)斐傻摹T谒椒较蚩吹降奶炜沾蠖酁樘祀H輻射,
          其中包括大量的紫外線和可見光線。這些光線會(huì)被散射,而且被大氣層上的大量分子所吸收,進(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,感謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容