当前位置:   article > 正文

用llama-cpp库本地部署llama3-8b模型!_meta-llama-3-8b-instruct-gguf

meta-llama-3-8b-instruct-gguf

下载 Llama 3 8B 模型文件

官网地址:https://huggingface.co/QuantFactory/Meta-Llama-3-8B-Instruct-GGUF/tree/main

国内镜像QuantFactory/Meta-Llama-3-8B-Instruct-GGUF at main

版本号参考:GGML 或GGUF的14种不同量化模式说明-CSDN博客

根据自己需求选择模型版本下载。

创建并激活虚拟环境

  1. conda create -n GGUF python=3.12.2
  2. conda activate GGUF

安装依赖包

  1. pip install llama-cpp-python
  2. pip install openai
  3. pip install uvicorn
  4. pip install starlette
  5. pip install fastapi
  6. pip install sse_starlette
  7. pip install starlette_context
  8. pip install pydantic_settings

安装llama-cpp-python时会报错!

参考我的这篇博客:

pip install llama-cpp-python时报错解决!-CSDN博客

启动大模型

python -m llama_cpp.server --host 0.0.0.0 --model ./Meta-Llama-3-8B-Instruct.Q4_K_M.gguf --n_ctx 2048

启动成功!

编写 Llama 模型对话客户端

  1. from openai import OpenAI
  2. # 注意服务端端口,因为是本地,所以不需要api_key
  3. client = OpenAI(base_url="http://localhost:8000/v1",
  4. api_key="not-needed")
  5. # 对话历史:设定系统角色是一个只能助理,同时提交“自我介绍”问题
  6. history = [
  7. {"role": "system", "content": "你是一个智能助理,你的回答总是正确的、有用的和内容非常精简."},
  8. {"role": "user", "content": "请用中文进行自我介绍,要求不能超过5句话,总字数不超过100个字。"},
  9. ]
  10. print("\033[92;1m")
  11. # 首次自我介绍完毕,接下来是等代码我们的提示
  12. while True:
  13. completion = client.chat.completions.create(
  14. model="local-model",
  15. messages=history,
  16. temperature=0.7,
  17. stream=True,
  18. )
  19. new_message = {"role": "assistant", "content": ""}
  20. for chunk in completion:
  21. if chunk.choices[0].delta.content:
  22. print(chunk.choices[0].delta.content, end="", flush=True)
  23. new_message["content"] += chunk.choices[0].delta.content
  24. history.append(new_message)
  25. print("\033[91;1m")
  26. userinput = input("> ")
  27. if userinput.lower() in ["bye", "quit", "exit"]: # 我们输入bye/quit/exit等均退出客户端
  28. print("\033[0mBYE BYE!")
  29. break
  30. history.append({"role": "user", "content": userinput})
  31. print("\033[92;1m")

开始对话!

参考:https://www.cnblogs.com/obullxl/p/18187815/NTopic2024051101

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/890452
推荐阅读
相关标签
  

闽ICP备14008679号