返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

lama 3 instruction-tuned 模型针对对话/聊天场景进行了微调和优化,在通用基准测试上优于许多开源聊天模型

[复制链接]
链载Ai 显示全部楼层 发表于 昨天 09:40 |阅读模式 打印 上一主题 下一主题

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;border-style: none none solid;border-width: 1px 1px 4px;border-color: rgb(0, 0, 0) rgb(0, 0, 0) rgb(160, 249, 176);border-radius: 4px 4px 0px 0px;background: none 0% 0% / auto no-repeat scroll padding-box border-box rgb(24, 26, 33);width: auto;height: auto;align-items: unset;box-shadow: none;flex-direction: unset;justify-content: unset;line-height: 1.5em;overflow: unset;text-shadow: none;transform: none;-webkit-box-reflect: unset;visibility: visible;">作者:lucas大叔原文地址:https://zhuanlan.zhihu.com/p/693407124

当地时间4月18日,Meta在官网上公布了旗下最新大模型Llama 3。目前,Llama 3已经开放了 8B和 70B 两个小参数版本,上下文窗口为8k。Meta表示,通过使用更高质量的训练数据和指令微调,Llama 3比Llama 2有了“显著提升”。

Llama 3 instruction-tuned 模型针对对话/聊天场景进行了微调和优化,在通用基准测试上优于许多开源聊天模型。



开源社区对此反应迅速,ollama和LlamaIndex第一时间宣称完成了对Llama 3的支持,langchain也宣称可在LangSmith Playground中试用最新的Llama 3 8B 和 70B模型。下面我们展示如何在ollama和LlamaIndex中使用Llama 3模型。


Ollama使用指南

CLI

打开终端执行 ollama run llama3 即可

API

curl使用示例:

curl -X POST http://localhost:11434/api/generate -d '{
"model": "llama3",
"prompt":"Why is the sky blue?"
}'

其他接口示例参见API 文档。

Model variants

Instruct是针对聊天/对话场景微调的版本

Example:ollama run llama3ollama run llama3:70b

Pre-trained是基础模型

Example:ollama run llama3:textollama run llama3:70b-text

LlamaIndex使用指南

本指南以Llama-3-8B-Instruct为例,说明如何基于llamaindex使用Llama3。

安装包

!pip install llama-index
!pip install llama-index-llms-huggingface
!pip install llama-index-embeddings-huggingface

为了使用官方仓库的llama3,你需要授权你的huggingface账号并使用你的huggingface token。

hf_token="hf_..."

设置 Tokenizer 和 Stopping ids

from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(
"meta-llama/Meta-Llama-3-8B-Instruct",
token=hf_token,
)

stopping_ids = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|eot_id|>"),
]

HuggingFaceLLM设置LLM

可选择加载全精度或4bit量化版本。

# generate_kwargs parameters are taken from https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct

import torch
from llama_index.llms.huggingface import HuggingFaceLLM

# Optional quantization to 4bit
# import torch
# from transformers import BitsAndBytesConfig

# quantization_config = BitsAndBytesConfig(
# load_in_4bit=True,
# bnb_4bit_compute_dtype=torch.float16,
# bnb_4bit_quant_type="nf4",
# bnb_4bit_use_double_quant=True,
# )

llm = HuggingFaceLLM(
model_name="meta-llama/Meta-Llama-3-8B-Instruct",
model_kwargs={
"token": hf_token,
"torch_dtype": torch.bfloat16,# comment this line and uncomment below to use 4bit
# "quantization_config": quantization_config
},
generate_kwargs={
"do_sample": True,
"temperature": 0.6,
"top_p": 0.9,
},
tokenizer_name="meta-llama/Meta-Llama-3-8B-Instruct",
tokenizer_kwargs={"token": hf_token},
stopping_ids=stopping_ids,
)

## You can deploy the model on HF Inference Endpoint and use it

# from llama_index.llms.huggingface import HuggingFaceInferenceAPI

# llm = HuggingFaceInferenceAPI(
# model_name="",
# token=''
# )

用prompt调用complete

response = llm.complete("Who is Paul Graham?")

print(response)

用messages列表调用chat

from llama_index.core.llms import ChatMessage

messages = [
ChatMessage(role="system", content="You are CEO of MetaAI"),
ChatMessage(role="user", content="Introduce Llama3 to the world."),
]
response = llm.chat(messages)
print(response)

用Llama3创建RAG pipeline

  • 下载数据

!wget"https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt""paul_graham_essay.txt"
  • 加载数据

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

documents = SimpleDirectoryReader(
input_files=["paul_graham_essay.txt"]
).load_data()
  • 配置embedding模型

from llama_index.embeddings.huggingface import HuggingFaceEmbedding

embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
  • 设置默认LLM和embedding模型

from llama_index.core import Settings

# bge embedding model
Settings.embed_model = embed_model

# Llama-3-8B-Instruct model
Settings.llm = llm
  • 创建索引

index = VectorStoreIndex.from_documents(
documents,
)
  • 创建查询引擎

query_engine=index.as_query_engine(similarity_top_k=3)
  • 查询

response=query_engine.query("Whatdidpaulgrahamdogrowingup?")

参考文献

llama3https://ollama.com/library/llama3

https://docs.llamaindex.ai/en/latest/examples/cookbooks/llama3_cookbook/


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ