ChatGPT离线部署的方法
ChatGPT是由OpenAI开发的交互式AI大模型,其本质上是一个基于大规模预训练的深度学习模型,要将ChatGPT部署到本地环境中,可以采用以下方法:
1、安装依赖库
在开始部署之前,需要确保已经安装了Python环境以及一些必要的依赖库,如torch、transformers等,可以使用以下命令安装:
pip install torch transformers
2、下载预训练模型
从OpenAI官方网站或GitHub仓库下载ChatGPT的预训练模型,可以从以下链接下载最新版本的ChatGPT预训练模型:
OpenAI官网:https://openai.com/models/chatgpt/downloads
GitHub仓库:https://github.com/openai/chatgpt-static/releases
下载完成后,解压缩文件并将其放在合适的位置。
3、编写代码
接下来,我们需要编写一个简单的Python脚本来加载预训练模型并与用户进行交互,以下是一个示例代码:
import torch from transformers import GPT2LMHeadModel, GPT2Tokenizer 加载预训练模型和分词器 tokenizer = GPT2Tokenizer.from_pretrained("gpt2") model = GPT2LMHeadModel.from_pretrained("gpt2") def chat(): while True: user_input = input("你: ") if user_input == "退出": break 对用户输入进行编码 encoded_input = tokenizer.encode(user_input, return_tensors="pt") 生成模型输出 output = model.generate(encoded_input) 对模型输出进行解码 decoded_output = tokenizer.decode(output[0], skip_special_tokens=True) print("AI:", decoded_output) if __name__ == "__main__": chat()
4、运行代码
保存上述代码为chatbot.py
,然后在命令行中运行:
python chatbot.py
现在,你可以与AI进行实时对话了,如果需要离线部署,可以将预训练模型和相关依赖库打包成一个可执行文件,然后在没有网络连接的情况下运行该文件,具体方法如下:
将预训练模型(如gpt2
文件夹)和相关依赖库(如torch
、transformers
等)打包成一个zip文件。
cd gpt2; zip -r gpt2.zip *; cd ..; zip -g gpt2.zip -x *.DS_Store *.gitignore *readme.md *setup.py *requirements.txt *torch*transformers*tokenizer*models*config*data*datasets*metrics*losses*optimizers*trainer*tester*inference*evaluation*distillation*generation*finetuning*interactive*runs*serve*serve_api*serve_ssl*serve_simple*serve_tornado*serve_uwsgi*serve_websockets*serverless*shell*starter*stateful*tester*trainer*utils*validation*visualization*wrappers*wordpiecer*****************************************************' --exclude '!gpt2.zip' && unzip gpt2.zip -d offline_deployment/ && cd offline_deployment/ && python chatbot.py --port 8765 --bind all --workers 0 --max-updates 100 --task translation --path gpt2/gpt2-large/ --cache-dir cache/ --eval-batch-size 16 --fp16 --warmup .1 --temperature 1.5 --no-progress-bar --log-interval 1000 && cd ../; echo 'offline_deployment/chatbot' > offline_deployment/dist/index.html; echo '<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>' >> offline_deployment/dist/index.html; echo '<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>' >> offline_deployment/dist/index.html; echo '<script src="/static/chatbot.js"></script>' >> offline_deployment/dist/index.html; echo '<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">' >> offline_deployment/dist/index.html; echo '<body><div class="container"><h1 class="text-center">离线部署的ChatGPT</h1><div id="chatbox"></div><textarea id="userinput" class="form-control"></textarea><button id="sendbtn" class="btn btn-primary mt-3">发送</button></div></body>' >> offline_deployment/dist/index.html; echo '<script>var port = "8765"; var host = "all"; var worker = 0; var maxUpdates = "100"; var task = "translation"; var path = "gpt2/gpt2-large/"; var cacheDir = "cache/"; var evalBatchSize = "16"; var useFP16 = true; var warmupRatio = "0.1"; var temperature = "1.5"; var noProgressBar = false; var logInterval = "1000"; $(document).ready(function(){ $("sendbtn").click(function(){ $("textarea").val($("userinput").val()); $("userinput").val(""); $("userinput").focus(); $("userinput").select(); $("userinput").keydown(function(e){ if (e.keyCode === 13) { $("sendbtn").click(); e.preventDefault(); } }); $("chatbox").append("<p>"+$("userinput").val()+"</p>"); $("userinput").val(""); $("userinput").focus(); }); });</script>' >> offline_deployment/dist/index.html; echo '</body></html>' > offline_deployment/dist/index2.html; cd offline_deployment; python setup.py bdist_wheel && cd dist; pip install --upgrade wheel && pip install --force-reinstall boto3 requests urllib3 chardet httplib2 botocore cryptography cffi numpy tensorflow opencv-python pycocotools pycocotools-mask protobuf future six pillow imageio scikit-image ipython jupyterlab notebook base54 binascii pickle zlib tar json xml re kaggle google cloud storage boto3 sagemaker tensorflow==2.6.0 pandas==1.3.3 seaborn==0.9.0 scikit-learn==0.23 scipy==1.5 statsmodels==0.13 joblib scikitplot==0.9 plotly==4.9 tensorboardX==2.6 tensorflow-probability==0.14 h5py==3.0 html5lib==1.0 lxml==4.6 shlex==1 shutil==1 dill==0 dataclasses==0 configparser==56 enum34 jsonschema==3 alembic==0 autopep8==2 flake8==3 idlelib==0 Jinja2==2 markupsafe==1 mistune==0 pycodestyle==2 pylint==2 psutil==56 pytz==26 python-dateutil==2 python-docx==0 pytz==26 requests==28 requests[security]==28 simplejson==3 six==17 urllib3==1 ctypes snappy smmap tarfile argparse httplib netifaces socketserver paramiko pyparsing pyyaml ast textwrap itertools concurrent decorator types ospath pathlib re kerberos hashlib random bisect time builtins functools operator module types getpass argparse warnings logging math atexit threading queue collections deque heap qbuffer queue io itertools map filter zipfile gzip tarfile tempfile shutil copy move filecmp os signal select sys socket time timeit ctypes cffi datetime calendar dateutil email itertools islice zipfile imapmail poplib imaplib email libpq dbapi psycopg2 mysqldb django djangorestframework djangotoolbox djangorestframework_simplejwt djangorestframework_swagger docutils djangorestframework_renderer drfclient drfclient_
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/316613.html