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

运行 Meta AI 的 Llama 3.1 8B:实践指南

[复制链接]
链载Ai 显示全部楼层 发表于 2 小时前 |阅读模式 打印 上一主题 下一主题

ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">

ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">介绍

ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">Meta 的 Llama 3.1 是一个强大的大型语言模型(LLM),在文本生成、翻译和问答方面具有令人印象深刻的能力。然而,使用尖端的 LLM 通常需要云资源。本教程使您能够直接在本地计算机上运行 Meta Llama 3.1 的 8B 版本,让您对 AI 交互拥有更多的控制权和隐私。

ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">先决条件:

    ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;" class="list-paddingleft-1">
  • Python 3.x(下载地址:https://www.python.org/downloads/)

  • PyTorch(安装说明:https://pytorch.org/get-started)

  • Transformers 库(pip install transformers

  • Hugging Face 账户(免费创建:https://huggingface.co/)

ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">获取访问令牌

ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">要访问 Hugging Face Hub 上的模型,您需要一个免费的访问令牌。请在 Hugging Face 上创建一个账户,并按照说明生成您的令牌:

    ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;" class="list-paddingleft-1">
  1. 登录 Hugging Face:确保您拥有 Hugging Face 账户并登录到您的个人资料。

  2. 导航到设置:点击右上角的个人资料图片,从下拉菜单中选择“设置”。

  3. 访问令牌选项卡:在左侧边栏中找到并点击“访问令牌”选项卡。

  4. 创建新令牌:点击“新令牌”按钮。

  5. 选择名称和范围:为您的令牌提供一个描述性名称(例如,“Python 脚本访问”),并选择适当的范围。范围定义了授予令牌的权限。对于大多数用例,“读取”访问权限就足够了。

  6. 生成并复制:点击“生成令牌”按钮。令牌将仅显示一次。请安全地复制它,因为您将无法再次查看。

ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">代码解析

ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">提供的 Python 代码便于使用 Llama 3.1 3B 模型进行文本生成。让我们分解关键组件:

导入库:

  • transformers:提供与预训练模型一起使用的工具。

  • torch:Llama 3.1 使用的深度学习框架。

访问令牌和模型 ID:

  • ''替换为您的 Hugging Face 访问令牌。

  • model_id指定 Llama 3.1 的特定版本(meta-llama/Meta-Llama-3.1-8B-Instruct)。

加载模型和分词器:

  • Transformers 中的AutoTokenizerAutoModelForCausalLM函数使用您的访问令牌从 Hugging Face Hub 加载模型和分词器。

GPU 加速(可选):

  • 代码检查 GPU 可用性,并将模型移动到 GPU 以加快处理速度。

对话结束令牌:

  • 这些令牌表示对话或句子的结束。

**generate_text**函数:

  • 此函数接受多个参数:

  • message:用户提示以启动对话。

  • history(可选):包含用户和助手消息的元组列表,以提供上下文。

  • temperature(可选):控制生成的随机性(0 = 确定性,1 = 更随机)。

  • max_new_tokens(可选):响应中生成的最大令牌数。

  • system(可选):提供上下文的可选系统提示。

  • 该函数构建对话历史,将其转换为模型兼容格式,并根据指定参数生成文本。

示例用法:

  • 演示如何使用带有提示和示例历史的generate_text函数。

运行代码

  1. 将代码保存为 Python 脚本(例如,llama_local.py)。

  2. 打开终端或命令提示符,导航到包含脚本的目录。

  3. 使用python llama_local.py运行脚本。

与 Llama 进行实验

运行脚本后,您可以与 Llama 进行交互!提供提示并观察生成的响应。尝试不同的temperaturemax_new_tokens值,以调整响应的随机性和长度。

回复

使用道具 举报

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

本版积分规则

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

  • 微信公众号

  • 商务合作

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