Ollama,本地運行大模型最強工具,輕松上手
1 Ollama
Ollama是一個開源平臺,幫助開發(fā)者便捷地在本地計算機上設(shè)置和運行大型語言模型(LLM)。這個平臺簡化了下載、安裝和使用LLM的流程。
想要開始使用Ollama,首先需要下載它。你可以點擊這里進行下載Ollama
下載完成后,打開終端,輸入以下命令來運行模型phi3:
ollama run phi3
或者,這樣,模型phi3的各個層就會被下載到你的電腦上。如果你想先下載模型再運行,可以使用:
ollama pull phi3
ollama run phi3
這樣,模型phi3的各個層就會被下載到你的電腦上。
Ollama還提供了REPL(讀取-求值-打印循環(huán))環(huán)境,這是一個交互式的編程界面,可以在這里輸入代碼,立即看到執(zhí)行結(jié)果,并繼續(xù)輸入新的代碼。
下載模型后,Ollama的REPL就會等待你的指令。
如果想查看所有可用的命令,可以在REPL中輸入/?。
要退出REPL,只需輸入/bye。
/? shortcuts獲取快捷方式列表。
此外,ollama ls命令可以列出你已經(jīng)下載的所有模型。
如果想要刪除某個模型,可以使用ollama rm <modelname>命令。
可以通過這個鏈接(https://ollama.com/library)查看Ollama提供的所有模型,并了解其詳細信息,比如模型的大小和參數(shù)。
運行不同大小的模型需要不同量的內(nèi)存:7B模型至少需要8 GB RAM,13B模型需要16 GB,而33B模型則需要32 GB。Ollama支持GPU加速,如果你沒有GPU,Ollama也可以在CPU上運行,只是速度會慢一些。
模型列表
你還可以在Ollama中設(shè)置新的系統(tǒng)提示。例如,運行ollama run llama3后,使用/set system?命令可以向系統(tǒng)發(fā)出指令,比如讓系統(tǒng)像對小學(xué)生解釋概念一樣來解釋術(shù)語。設(shè)置完成后,使用/save forstudent?保存設(shè)置,然后輸入/bye?退出REPL。之后,你可以通過/ollama run forstudent來運行剛才保存的設(shè)置。
以上就是使用Ollama的基本步驟和一些實用命令。如果你想了解更多,可以訪問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)建一個簡單的對話:
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是一個全能的AI智能體和RAG工具,運行在本地計算機上。
ollama pull llama3:8b-instruct-q8_0
在 AnythingLLM 偏好設(shè)置屏幕中選擇 Ollama。
為工作區(qū)命名
讓我們試試這個模型:
4 打開WebUI
可以通過按照這里安裝說明進行安裝。
