---
layout: single
title:  "Meta Llama 3"
date:   2024-04-19 08:00:00 +0800
categories: [AI 与大模型, 编程开发]
tags: [Llama3, RUG, LlamaGuard, Ollama, LLM, Llama LLM]
---

## Llama3

- [智能助手 Meta AI](https://www.meta.ai/)
- [Responsible Use Guide](https://ai.meta.com/static-resource/responsible-use-guide/)
- [Trust & Safety](https://llama.meta.com/trust-and-safety/)
- [Meta Blog](https://ai.meta.com/blog/)
- [GitHub: Meta Llama 3](https://github.com/meta-llama/llama3)
- [HuggingChat](https://huggingface.co/chat/)

### 模型
- [Meta-Llama-3-8b](https://huggingface.co/meta-llama/Meta-Llama-3-8B): 8B 基础模型
- [Meta-Llama-3-8b-instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct): 8B 基础模型的指令调优版
- [Meta-Llama-3-70b](https://huggingface.co/meta-llama/Meta-Llama-3-70B): 70B 基础模型
- [Meta-Llama-3-70b-instruct](https://huggingface.co/meta-llama/Meta-Llama-3-70B-instruct): 70B 基础模型的指令调优版
- [Llama Guard 2](): Llama Guard 2 是基于 Llama 3 8B 微调的，为生产环境设计的，能够对大语言模型的输入（即提示）和响应进行分类，以便识别潜在的不安全内容。

#### 下载

- [HuggingFace: Meta Llama 3](https://huggingface.co/collections/meta-llama/meta-llama-3-66214712577ca38149ebb2b6)
- [ModelScope: Meta Llama 3](https://www.modelscope.cn/organization/LLM-Research)
- [Ollama: Meta Llama 3](https://ollama.com/library/llama3)

### 数据集
#### [HuggingFaceH4/no_robots](https://huggingface.co/datasets/HuggingFaceH4/no_robots)

No Robots 是由熟练的人类注释者创建的包含 10,000 条指令和演示的高质量数据集。该数据可用于监督微调（SFT），使语言模型更好地遵循指令。 No Robots 是根据 OpenAI 的 [InstructGPT 论文](https://huggingface.co/papers/2203.02155)中描述的指令数据集进行建模的。

### 介绍
Llama 3 最大的变化是采用了新的 Tokenizer，将词汇表大小扩展至 128,256（前版本为 32,000 Token）。这一更大的词汇库能够更高效地编码文本（无论输入还是输出），并有可能提升模型的多语种处理能力。不过，这也导致嵌入层的输入和输出矩阵尺寸增大，这是小型模型参数增加（从 Llama 2 的 7B 增至 Llama 3 的 8B）的主要原因之一。此外，8B 版本的模型现在采用了分组查询注意力（GQA），这是一种效率更高的表达方式，有助于处理更长的上下文。具有 8000 Token 的上下文长度。

Llama 3 模型在两个拥有 24,000 GPU 的集群上进行了训练，使用的是超过 15 万亿 Token 的新公共在线数据。我们无法得知训练数据具体细节，但可以推测，更大规模且更细致的数据策划是性能提升的重要因素。Llama 3 Instruct 针对对话应用进行了优化，结合了超过 1000 万的人工标注数据，通过监督式微调（SFT）、拒绝采样、邻近策略优化（PPO）和直接策略优化（DPO）进行训练。

### [Benchmarks](https://llama.meta.com/llama3/)

![](/images/2024/Llama3/Meta-Llama-3-Instruct-model-performance.png)

### [信任与安全](https://llama.meta.com/trust-and-safety/)

#### Responsibility at different layers of the development process（开发过程不同层面的责任）
![](/images/2024/Llama3/Responsibility-at-different-layers-of-the-development-process.png)
- [我们对 Meta AI 和 Meta Llama 3 采取负责任的态度](https://ai.meta.com/blog/meta-llama-3-meta-ai-responsibility/)

#### Responsible LLM Product Development Stages
![](/images/2024/Llama3/Responsible-LLM-Product-Development-Stages.jpeg)

#### Meta Llama Cybersec Eval 2
![](/images/2024/Llama3/Cybersec-Eval-2.jpeg)

#### System-level safeguards
![](/images/2024/Llama3/System-level-safeguards.png)

#### Meta Llama Code Shield
![](/images/2024/Llama3/Meta-Llama-Code-Shield.png)

- [负责任使用指南](https://llama.meta.com/responsible-use-guide/)
- [MLCommons AI Safety](https://mlcommons.org/ai-safety/)
    - [AI Safety Benchmarks](https://mlcommons.org/benchmarks/ai-safety/)
    - [Announcing MLCommons AI Safety v0.5 Proof of Concept](https://mlcommons.org/2024/04/mlc-aisafety-v0-5-poc/)

### A system-level approach to responsibility

![](/images/2024/Llama3/A-system-level-approach-to-responsibility.png)

- [Responsible Use Guide](https://ai.meta.com/static-resource/responsible-use-guide/)
- [Introducing Meta Llama 3: The most capable openly available LLM to date](https://ai.meta.com/blog/meta-llama-3/)

### [Meta Llama Cybersec Eval 2](https://ai.meta.com/research/publications/cyberseceval-2-a-wide-ranging-cybersecurity-evaluation-suite-for-large-language-models/)
- Insecure code practice(generation and autocomplete)（不安全的代码实践（生成和自动完成））
- Cyber attacker helpfulness（对网络攻击者的帮助）
- Code interpreter abuse（代码解释器滥用）
- Offensive cybersecurity capabilities（攻击性网络安全能力）
- Susceptibility to prompt injection（对提示注入的易感性）

### 设置提示
基础模型不具备固定的提示格式。如同其他基础模型，它们可以用来延续输入序列，提供合理的续写或进行零样本/少样本推理。这些模型也是您自定义微调的理想基础。指令版本采用以下对话结构：

```
system

{{ system_prompt }}user

{{ user_msg_1 }}assistant

{{ model_answer_1 }}
```

### 使用 transformers
要在 transformers 中使用 Llama 3 模型，请确保安装了最新版本：

```python
pip install -U "transformers==4.40.0" --upgrade
```

### LangChain
```python
from langchain_community.llms import Ollama
from langchain_core.prompts import ChatPromptTemplate

chat = Ollama(model="llama3", temperature=0)

system = "You are a helpful assistant that translates {input_language} to {output_language}."
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])

chain = prompt | chat
response = chain.invoke(
    {
        "input_language": "English",
        "output_language": "Chinese",
        "text": "I love Python",
    }
)

print('🤖 ', response)
```

```
I'd be happy to help you with that!

The translation of "I love Python" from English to Chinese is:

"" (Wǒ ài de Pītōn)

Here's the breakdown:

* "" (wǒ) means "I"
* "" (ài) means "love" or "like"
* "" (de) is a possessive particle indicating that something belongs to someone
* "" (Pītōn) refers to the programming language Python

So, "" (Wǒ ài de Pītōn) literally means "I love my Python", but in this context, it's more like saying "I love Python"!
```

**llama3-8b 翻译成拼音了**


## 参考资料
- [Getting started with Meta Llama](https://llama.meta.com/docs/get-started/)
- [Build the future of AI with Meta Llama 3](https://llama.meta.com/llama3/)
- [欢迎 Llama 3：Meta 的新一代开源大语言模型](https://huggingface.co/blog/zh/llama3)
- [HuggingFace: meta-llama/Meta-Llama-3-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct)
- [百亿美金开源模型Llama3: 小札最新2万字访谈披露一切 · 全纪录+视频](https://www.163.com/dy/article/J040CCFL05534HHB.html)
- [Meet Your New Assistant: Meta AI, Built With Llama 3](https://about.fb.com/news/2024/04/meta-ai-assistant-built-with-llama-3/)
- [llama-recipes](https://github.com/meta-llama/llama-recipes)
- [OpenEQA: From word models to world models](https://ai.meta.com/blog/openeqa-embodied-question-answering-robotics-ar-glasses/)
- [OpenEQA Embodied Question Answering in the Era of Foundation Models](https://github.com/facebookresearch/open-eqa)
- [Command R+](https://docs.cohere.com/docs/command-r-plus)
- [CohereForAI/c4ai-command-r-plus](https://huggingface.co/CohereForAI/c4ai-command-r-plus)
