4月19日,F(xiàn)acebook母公司Meta重磅推出了Llama3。即便大家現(xiàn)在對于大廠和巨頭頻繁迭代AI模型的行為已經(jīng)見怪不怪,Meta的Llama3仍舊顯得與眾不同,因為這是迄今最強大的開源AI模型。LLaMA模型通常采用了類似于GPT(由OpenAI開發(fā))的變換器(Transformer)架構(gòu)。這種架構(gòu)特別適合處理大量的自然語言數(shù)據(jù),并能有效地學(xué)習(xí)語言的深層結(jié)構(gòu)和上下文。結(jié)合LM Studio,我們就可以將LLaMA3部署在本地服務(wù)器,廣泛的應(yīng)用客戶服務(wù)、RAG等領(lǐng)域。下面是一個詳細的動手實踐操作供大家參考。
LM Studio的下載和安裝可參考以下鏈接:
用LM Studio:2分鐘在本地免費部署大語言模型,替代ChatGPT-CSDN博客
一、在LM Studio上下載 LLaMA3?
當(dāng)啟動LM Studio的時候,系統(tǒng)會提示升級,升級完畢后。如下圖:
主頁上可以看到最新支持的LLaMA3的模型,點擊下載按鈕直接下載 (注意這里要通過魔法才行下載模型)
?下載后的模型在My Models這里可以看到
?二、啟動Local Server
選擇LLama3 模型,選擇 Start Server 按鈕
?Local Server啟動之后如下圖:
三、客戶端訪問和測試Local Server
?1、Python Code簡單測試訪問
下載Python 3.11 并安裝openai Python 包
pip install -r requirements.txt
requirements.txt 內(nèi)容如下:
?openai==0.28.0
?文章來源地址http://www.zghlxwxcb.cn/news/detail-859953.html
import openai
# Set the base URL and API key for the OpenAI client
openai.api_base = "http://localhost:1234/v1"
openai.api_key = "not-needed"
# Create a chat completion
completion = openai.ChatCompletion.create(
model="local-model", # this field is currently unused
messages=[
{"role": "system", "content": "Provide detailed technical explanations."},
{"role": "user", "content": "Introduce yourself."}
],
temperature=0.7,
)
# Print the chatbot's response
print(completion.choices[0].message.content)
返回結(jié)果如下圖:
?
2、填寫系統(tǒng)提示詞,測試交互式對話
import openai
# Configuration for OpenAI API
openai.api_base = "http://localhost:1234/v1"
openai.api_key = "not-needed"
# Function to create a chat completion with a dynamic user prompt
def create_chat_completion(user_input, system_message):
return openai.ChatCompletion.create(
model="local-model",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": user_input}
],
temperature=0.7,
)
def main():
# 預(yù)定義的系統(tǒng)提示詞
system_message = (
"你是一位資深的小紅書運營人員,你目前負責(zé)的內(nèi)容方向是電子數(shù)碼,你的任務(wù)是生成小紅書的內(nèi)容文案,要求分解長句,減少重復(fù),語氣輕松幽默,具有真題可讀性。請用中文和用戶對話"
)
# Chat loop
while True:
user_input = input("User: ")
if user_input.lower() in ['exit', 'bye', 'end']:
print("Exiting the chat.")
break
completion = create_chat_completion(user_input, system_message)
print("Model Response: ", completion.choices[0].message.content)
if __name__ == "__main__":
main()
?執(zhí)行Python code 的效果如下,雖然Llama3能夠理解中文的輸入,但是輸出還是英文的。大家可以下載專門的針對漢語訓(xùn)練的LLama3的衍生版本試試看。?
Python 執(zhí)行效果:
?后臺Local Server日志:
?3、采用OpenAI的Python Code 供參考
import openai
# OpenAI API 配置
class OpenAIConfig:
def __init__(self):
self.base_url = "http://localhost:1234/v1"
self.api_type = "open_ai"
self.api_key = "not-needed"
# 將系統(tǒng)提示詞存放在文本文件中加載進來
def read_file_content(file_path):
try:
with open(file_path, "r") as file:
return file.read().strip()
except FileNotFoundError:
print(f"File not found: {file_path}")
return None
# Function to initiate conversation with the local-model and establishes roles and where the instructions come from.
def initiate_conversation(input_text, system_message):
response = openai.ChatCompletion.create(
model="local-model",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": input_text}
],
temperature=0.7,
)
return response.choices[0].message.content.strip()
def main():
# Instantiate configuration
config = OpenAIConfig()
openai.api_base = config.base_url
openai.api_key = config.api_key
# Read system message from file
system_message = read_file_content("my_prompt.txt")
if system_message is None:
return
# Conversation loop
while True:
user_input = input("User: ")
if user_input.lower() in ['exit', 'bye', 'end']:
print("Exiting the conversation.")
break
model_response = initiate_conversation(user_input, system_message)
print("Model Response: ", model_response)
if __name__ == "__main__":
main()
以上就是結(jié)合LM Studio + LLaMA3 大模型在本地部署,提供Chat GPT功能的全過程,大家可以嘗試一下。文章來源:http://www.zghlxwxcb.cn/news/detail-859953.html
?
到了這里,關(guān)于基于LM Studio + LLaMA3 建立本地化的ChatGPT的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!