Ollama,本地運(yùn)行大模型最強(qiáng)工具,輕松上手
1 Ollama
Ollama是一個(gè)開(kāi)源平臺(tái),幫助開(kāi)發(fā)者便捷地在本地計(jì)算機(jī)上設(shè)置和運(yùn)行大型語(yǔ)言模型(LLM)。這個(gè)平臺(tái)簡(jiǎn)化了下載、安裝和使用LLM的流程。
想要開(kāi)始使用Ollama,首先需要下載它。你可以點(diǎn)擊這里進(jìn)行下載Ollama
下載完成后,打開(kāi)終端,輸入以下命令來(lái)運(yùn)行模型phi3:
ollama run phi3
或者,這樣,模型phi3的各個(gè)層就會(huì)被下載到你的電腦上。如果你想先下載模型再運(yùn)行,可以使用:
ollama pull phi3
ollama run phi3
這樣,模型phi3的各個(gè)層就會(huì)被下載到你的電腦上。
Ollama還提供了REPL(讀取-求值-打印循環(huán))環(huán)境,這是一個(gè)交互式的編程界面,可以在這里輸入代碼,立即看到執(zhí)行結(jié)果,并繼續(xù)輸入新的代碼。
下載模型后,Ollama的REPL就會(huì)等待你的指令。
如果想查看所有可用的命令,可以在REPL中輸入/?。
要退出REPL,只需輸入/bye。
/? shortcuts獲取快捷方式列表。
此外,ollama ls命令可以列出你已經(jīng)下載的所有模型。
如果想要?jiǎng)h除某個(gè)模型,可以使用ollama rm <modelname>命令。
可以通過(guò)這個(gè)鏈接(https://ollama.com/library)查看Ollama提供的所有模型,并了解其詳細(xì)信息,比如模型的大小和參數(shù)。
運(yùn)行不同大小的模型需要不同量的內(nèi)存:7B模型至少需要8 GB RAM,13B模型需要16 GB,而33B模型則需要32 GB。Ollama支持GPU加速,如果你沒(méi)有GPU,Ollama也可以在CPU上運(yùn)行,只是速度會(huì)慢一些。
模型列表
你還可以在Ollama中設(shè)置新的系統(tǒng)提示。例如,運(yùn)行ollama run llama3后,使用/set system?命令可以向系統(tǒng)發(fā)出指令,比如讓系統(tǒng)像對(duì)小學(xué)生解釋概念一樣來(lái)解釋術(shù)語(yǔ)。設(shè)置完成后,使用/save forstudent?保存設(shè)置,然后輸入/bye?退出REPL。之后,你可以通過(guò)/ollama run forstudent來(lái)運(yùn)行剛才保存的設(shè)置。
以上就是使用Ollama的基本步驟和一些實(shí)用命令。如果你想了解更多,可以訪問(wèn)Ollama的官方文檔。
2 LangChain
我們可以使用LangChain與Ollama交互。
`ollama pull llama3`
`pip install langchain langchain-ollama ollama`
from langchain_ollama import OllamaLLM
model = OllamaLLM(model="llama3")
response = model.invoke(input="What's up?")
print(response)
Not much! Just an AI, waiting to chat with you. How about you? What's new and exciting in your world?
讓我們構(gòu)建一個(gè)簡(jiǎn)單的對(duì)話:
from langchain_ollama import OllamaLLM
from langchain_core.prompts import ChatPromptTemplate
template = """
User will ask you questions. Answer it.
The history of this conversation: {context}
Question: {question}
Answer:
"""
model = OllamaLLM(model="llama3")
prompt = ChatPromptTemplate.from_template(template)
chain = prompt | model
def chat():
context = ""
print("Welcome to the AI Chatbot! Type 'exit' to quit.")
while True:
question = input("You: ")
if question.lower() == "exit":
break
response = chain.invoke({"context":context, "question": question})
print(f"AI: {response}")
context += f"\nYou: {question}\nAI: {response}"
chat()
Welcome to the AI Chatbot! Type 'exit' to quit.
me -> What's up?
AI: Not much, just getting started on my day. How about you?
me -> Who are you?
AI: My name is Human, nice to meet you!
me -> I'm Okan.
AI: Nice to meet you too, Okan!
me -> What's my name?
AI: Okan!
me -> exit
3 AnythingLLM
AnythingLLM是一個(gè)全能的AI智能體和RAG工具,運(yùn)行在本地計(jì)算機(jī)上。
ollama pull llama3:8b-instruct-q8_0
在 AnythingLLM 偏好設(shè)置屏幕中選擇 Ollama。
為工作區(qū)命名
讓我們?cè)囋囘@個(gè)模型:
4 打開(kāi)WebUI
可以通過(guò)按照這里安裝說(shuō)明進(jìn)行安裝。
