当前位置:   article > 正文

人工智能 | Llama大模型:与AI伙伴合二为一,共创趣味交流体验_registry.ollama.ai地址

registry.ollama.ai地址

Llama 大模型介绍

我们介绍 LLaMA,这是一个基础语言模型的集合,参数范围从 7B 到 65B。我们在数万亿个Token上训练我们的模型,并表明可以专门使用公开可用的数据集来训练最先进的模型,而无需诉诸专有的和无法访问的数据集。特别是,LLaMA-13B 在大多数基准测试中都优于 GPT-3 (175B)。

Llama2 大模型介绍

我们开发并发布了 Llama 2,这是一组经过预训练和微调的大型语言模型 (LLM),其参数规模从 70 亿到 700 亿不等。我们经过微调的大语言模型(称为 Llama 2-Chat)针对对话用例进行了优化。我们的模型在我们测试的大多数基准上都优于开源聊天模型,并且根据我们对有用性和安全性的人工评估,可能是闭源模型的合适替代品。

相关网址

Llama 大语言模型提供的主要模型列表

Code Llama 模型

Code Llama 是一个基于 Llama 2 的大型代码语言模型系列,在开放模型、填充功能、对大输入上下文的支持以及编程任务的零样本指令跟踪能力中提供最先进的性能。我们提供多种风格来覆盖广泛的应用程序:基础模型 (Code Llama)、Python 专业化 (Code Llama - Python) 和指令跟随模型 (Code Llama - Instruct),每个模型都有 7B、13B 和 34B 参数。所有模型均在 16k 个标记序列上进行训练,并在最多 100k 个标记的输入上显示出改进。7B 和 13B Code Llama 和 Code Llama - 指令变体支持基于周围内容的填充。Code Llama 是通过使用更高的代码采样对 Llama2 进行微调而开发的。与 Llama 2 一样,我们对模型的微调版本应用了大量的安全缓解措施。有关模型训练、架构和参数、评估、负责任的人工智能和安全性的详细信息,请参阅我们的研究论文。Llama 材料(包括 Code Llama)的代码生成功能生成的输出可能受第三方许可的约束,包括但不限于开源许可。

Code Llama 提供的主要模型列表

申请模型

申请地址 https://ai.meta.com/resources/models-and-libraries/llama-downloads/

申请通过后,在 hugging face 上如果邮箱一致,会提示已经授权。

使用模型

  • 使用官方的 Api
  • 使用第三方封装 Api llama.cpp-python ollama
  • 使用 Langchain
  • 使用 Hugging face 的 Transformers

Llama

https://github.com/facebookresearch/llama

  1. torchrun--nproc_per_node1example_text_completion.py\
  2. --ckpt_dirllama-2-7b/\
  3. --tokenizer_pathtokenizer.model\
  4. --max_seq_len128--max_batch_size4

NCCL 错误

RuntimeError: Distributed package doesn't have NCCL built in

Windows 和 Mac 上基本跑不起来,因为 Torchrun 依赖 NCCL

https://pytorch.org/docs/stable/distributed.html

Llama.cpp

https://github.com/ggerganov/llama.cpp

Port of Facebook's LLaMA model in C/C++

因为很多同学受限于个人电脑的环境,没法运行完整的 Llama 模型。Llama.cpp 提供了一个非常好的移植版本,可以降低电脑的硬件要求,方便个人电脑运行与测试。

下载

  1. gitclonehttps://github.com/ggerganov/llama.cpp.git
  2. cdllama.cpp
  3. make

模型转换

通过对模型进行转化,可以降低资源消耗。

  1. # obtain the original LLaMA model weights and place them in ./models
  2. ls./models
  3. 65B30B13B7Btokenizer_checklist.chktokenizer.model
  4. # [Optional] for models using BPE tokenizers
  5. ls./models
  6. 65B30B13B7Bvocab.json
  7. # install Python dependencies
  8. python3-mpipinstall-rrequirements.txt
  9. # convert the 7B model to ggml FP16 format
  10. python3convert.pymodels/7B/
  11. # [Optional] for models using BPE tokenizers
  12. pythonconvert.pymodels/7B/--vocabtypebpe
  13. # quantize the model to 4-bits (using q4_0 method)
  14. ./quantize./models/7B/ggml-model-f16.gguf./models/7B/ggml-model-q4_0.ggufq4_0
  15. # update the gguf filetype to current if older version is unsupported by another application
  16. ./quantize./models/7B/ggml-model-q4_0.gguf./models/7B/ggml-model-q4_0-v2.ggufCOPY
  17. # run the inference
  18. ./main-m./models/7B/ggml-model-q4_0.gguf-n128

此步可以省略,直接下载别人转换好的量化模型即可。https://huggingface.co/TheBloke/Llama-2-7b-Chat-GGUF

运行

命令行交互模式

./main-m./models/llama-2-7b.Q4_0.gguf-i-n256--color

开启 Server 模式,访问 http://127.0.0.1:8080/

./server-m./models/llama-2-7b.Q4_0.gguf

Llama-cpp-python

https://github.com/abetlen/llama-cpp-python

pipinstallllama-cpp-python

Mac M1 上构建的时候需要加上特殊的参数

CMAKE_ARGS="-DLLAMA_METAL=on -DCMAKE_OSX_ARCHITECTURES=arm64"FORCE_CMAKE=1pipinstall-Ullama-cpp-python--no-cache-dir--force-reinstall

启动 Api 模式

  1. pipinstallllama-cpp-python[server]
  2. python-mllama_cpp.server--modelmodels/llama-2-7b.Q4_0.gguf
  3. python-mllama_cpp.server--modelmodels/llama-2-7b.Q4_0.gguf--n_gpu_layers1

Ollama

  1. (base)hogwarts:~seveniruby$ollamaservecodellama:7b
  2. 2023/10/0802:31:04images.go:987:totalblobs:6
  3. 2023/10/0802:31:04images.go:994:totalunusedblobsremoved:0
  4. 2023/10/0802:31:04routes.go:535:Listeningon127.0.0.1:11434

api 文档 https://github.com/jmorganca/ollama/blob/main/docs/api.md

基于Langchain使用Llama

使用Langchain调用

  1. def test_llama_cpp_local():
  2. """
  3. 使用本地模型
  4. :return:
  5. """
  6. llm = Llama(model_path="/Users/seveniruby/projects/llama.cpp/models/llama-2-7b.Q4_0.gguf")
  7. output = llm("Q: 法国的首都在哪里\n A: ", echo=True, max_tokens=6, temperature=0)
  8. debug(json.dumps(output, indent=2, ensure_ascii=False))

输出

  1. {
  2. "id":"cmpl-6d3e491e-716f-4e6c-b167-4f52e3f9786f",
  3. "object":"text_completion",
  4. "created":1696709780,
  5. "model":"/Users/seveniruby/projects/llama.cpp/models/llama-2-7b.Q4_0.gguf",
  6. "choices":[
  7. {
  8. "text":"Q: 法国的首都在哪里\n A: 巴黎。\n",
  9. "index":0,
  10. "logprobs":null,
  11. "finish_reason":"length"
  12. }
  13. ],
  14. "usage":{
  15. "prompt_tokens":18,
  16. "completion_tokens":6,
  17. "total_tokens":24
  18. }
  19. }

使用Langchain结合Api服务

  1. def test_langchain_llm():
  2. llm = OpenAI(
  3. openai_api_key=None,
  4. openai_api_base='http://127.0.0.1:8000/v1',
  5. stop=["Q:", "\n"]
  6. )
  7. debug(llm)
  8. prompt = "Q: 中国的首都在哪里?A: "
  9. output = llm(prompt)
  10. debug(output)

基于Langchain与Hugging face

  1. def test_pipeline():
  2. pipe = pipeline(
  3. "text-generation",
  4. model="meta-llama/Llama-2-7b-hf",
  5. torch_dtype=torch.float16,
  6. device='mps', # 按需改成你的cuda或者cpu
  7. revision='main',
  8. )
  9. debug(pipe)
  10. debug(pipe('法国的首都在哪里'))

更多Python基础语法趣味学习视频,请点击!

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

闽ICP备14008679号