基于LM Studio + LLaMA3 建立本地化的ChatGPT

devtools/2024/9/23 16:05:31/

4月19日,Facebook母公司Meta重磅推出了Llama3。即便大家现在对于大厂和巨头频繁迭代AI模型的行为已经见怪不怪,Meta的Llama3仍旧显得与众不同,因为这是迄今最强大的开源AI模型。LLaMA模型通常采用了类似于GPT(由OpenAI开发)的变换器(Transformer)架构。这种架构特别适合处理大量的自然语言数据,并能有效地学习语言的深层结构和上下文。结合LM Studio,我们就可以将LLaMA3部署在本地服务器,广泛的应用客户服务、RAG等领域。下面是一个详细的动手实践操作供大家参考。

LM Studio的下载和安装可参考以下链接:

用LM Studio:2分钟在本地免费部署大语言模型,替代ChatGPT-CSDN博客

一、在LM Studio上下载 LLaMA3 

当启动LM Studio的时候,系统会提示升级,升级完毕后。如下图:

主页上可以看到最新支持的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 内容如下:

 openai==0.28.0

 

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 unusedmessages=[{"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)

返回结果如下图:

 

2、填写系统提示词,测试交互式对话

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():# 预定义的系统提示词 system_message = ("你是一位资深的小红书运营人员,你目前负责的内容方向是电子数码,你的任务是生成小红书的内容文案,要求分解长句,减少重复,语气轻松幽默,具有真题可读性。请用中文和用户对话"
)# Chat loopwhile True:user_input = input("User: ")if user_input.lower() in ['exit', 'bye', 'end']:print("Exiting the chat.")breakcompletion = create_chat_completion(user_input, system_message)print("Model Response: ", completion.choices[0].message.content)if __name__ == "__main__":main()

 执行Python code 的效果如下,虽然Llama3能够理解中文的输入,但是输出还是英文的。大家可以下载专门的针对汉语训练的LLama3的衍生版本试试看。 

Python 执行效果:

 后台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"# 将系统提示词存放在文本文件中加载进来
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 configurationconfig = OpenAIConfig()openai.api_base = config.base_urlopenai.api_key = config.api_key# Read system message from filesystem_message = read_file_content("my_prompt.txt")if system_message is None:return# Conversation loopwhile True:user_input = input("User: ")if user_input.lower() in ['exit', 'bye', 'end']:print("Exiting the conversation.")breakmodel_response = initiate_conversation(user_input, system_message)print("Model Response: ", model_response)if __name__ == "__main__":main()

以上就是结合LM Studio + LLaMA3 大模型在本地部署,提供Chat GPT功能的全过程,大家可以尝试一下。

 


http://www.ppmy.cn/devtools/25010.html

相关文章

tableau基础学习——添加标靶图、甘特图、瀑布图

标靶图 添加参考线 添加参考分布 甘特图 创建新的字段 如设置延迟天数****计划交货日期-实际交货日期 为正代表提前交货,负则代表延迟交货 步骤:创建——计算新字段 把延迟天数放在颜色、大小里面就可以 瀑布图 两个表按照地区连接 先做个条形图&…

XYCTF 2024

Web 参考博客:https://www.yuque.com/yunzhiyunweiji/wrgkex/rfpnkn0293l7cp09#ezMake ezhttp Via - HTTP | MDN 代理那里难住了 XFF不给用可以用client-ip ezmd5 让我们上传图片并比较,结合题目名可以猜测应该是比较两个图片的md5值是否相同&…

MySQL怎么看死锁记录

这个结果分成三部分: (1) TRANSACTION,是第一个事务的信息; (2) TRANSACTION,是第二个事务的信息; (3)WE ROLL BACK TRANSACTION (1),是最终的处理结果,表示回滚了第一个事务。 第一个事务的信…

python 注释符

4、注释 1、单行注释 单行注释用于解释代码中的一行或一小段代码。 在Python中,单行注释以#开头,后面的内容都是注释。 单行注释可以放在代码的任何位置,但通常放在代码行的上方或旁边。 单行注释不会影响代码的执行,Python解…

精酿啤酒:酿造工艺的自动化与智能化发展

随着科技的不断进步,自动化与智能化已成为啤酒酿造工艺的重要发展方向。Fendi Club啤酒紧跟时代潮流,积极推动酿造工艺的自动化与智能化发展,旨在提高生产效率、确保产品品质和满足市场需求。 Fendi Club啤酒引入自动化生产设备。他们采用自动…

Go语言中,两个比较流行的缓存库

在 Go 中实现带有过期时间的缓存通常需要一个可以自动处理键值过期的缓存系统。虽然标准库中没有直接提供这种功能,但有几个流行的第三方库可以很好地满足这一需求。下面我会介绍两个比较流行的 Go 缓存库:go-cache 和 bigcache。 1. go-cache go-cache…

flask+uwsgi+nginx+cerbot配置

配置步骤 安装flask和uwsgi pip install Flask uwsgi 创建一个简单的flask应用(app.py)或者是自己的flask项目 from flask import Flask app Flask(__name__)app.route(/) def hello_world():return Hello, World! 配置uwsgi,这里我给出…

踏上大数据第一步:flume

一、概述 Flume是一个分布式、可靠、可用的系统,用于高效地收集、聚合和移动大量日志数据。它旨在从不同的数据源收集数据,并将其存储到中央数据存储中,如HDFS、HBase或Solr等。Flume是Apache旗下的一个顶级项目,是Hadoop生态系统中的一个重要组件。 Flume的主要应用场景包括…