llama-agentic-system 是 Llama Stack APIs 的 Agentic 组件
此repo允许您将 Llama 3.1作为能够执行“代理”任务的系统运行,例如:
此外,我们希望将安全评估从 模型级别 转移到整个系统级别。这允许底层模型保持广泛的可操纵性,并适应需要不同级别安全保护的用例。
其中一个安全保护由Llama守卫提供,默认情况下,Llama守卫同时用于输入和输出过滤,但是系统可以配置为修改这个默认设置,例如,在经常观察到拒绝良性提示的情况下,建议使用Llama守卫进行输出过滤,只要满足您的使用案例的安全要求。
注:API仍在发展中,可能会发生变化。请随意构建和试验,但请不要依靠它的稳定性!
使用所需的Python版本创建一个新的conda环境:
ENV=agentic_env with-proxy conda create -n $ENV python=3.10 cd conda activate $ENV 请注意,您也可以使用pip 简单地将其安装为python包。
pip install llama-agentic-system 如果你想运行即时fp8量化,你需要fbgemm-gpu包,它需要 torch>=2.4.0(目前只在 nightly ,但很快就会发布…)。
你可以在llama工具链存储库中找到 fp8_requirements : https://github.com/meta-llama/llama-toolchain/blob/main/fp8_requirements.txt 。
ENV=fp8_env conda create -n $ENV python=3.10 conda activate $ENV pip3 install -r fp8_requirements.txt 使用pip安装包:
pip install -e . 这将根据需要安装所有依赖项。
我们还需要冒泡包装来运行代码执行器作为代理的工具。 安装 bubblewrap 。
通过运行以下命令测试安装:
llama --help 这将打印CLI帮助消息。
usage: llama [-h] {download,inference,model,agentic_system} ... Welcome to the LLama cli options: -h, --help show this help message and exit subcommands: {download,inference,model,agentic_system} 此Llama CLI将帮助您执行以下操作
让我们一步一步地完成设置过程,
使用以下命令下载所需的检查点:
# download the 8B model, this can be run on a single GPU llama download llhf/Meta-Llama-3.1-8B-Instruct # you can also get the 70B model, this will require 8 GPUs however llama download llhf/Meta-Llama-3.1-70B-Instruct # llama-agents have safety enabled by default. For this you will need # safety models -- Llama-Guard and Prompt-Guard llama download llhf/Prompt-Guard-86M --ignore-patterns original llama download llhf/Llama-Guard-3-8B --ignore-patterns original **重要提示:**设置您的环境变量HF_TOKEN或将--hf-token传入命令以验证您的访问权限。
您可以在 https://huggingface.co/settings/tokens 找到您的 token。
提示:
llama download的默认运行方式是--ignore-patterns *.safetensors,因为我们使用original文件夹中的.pth文件。然而,对于 Llama Guard 和 Prompt Guard,我们需要安全传感器。
因此,请确保使用
--ignore-patterns original运行,以便下载安全传感器并忽略.pth文件。
通过运行以下命令配置推理服务器配置:
llama inference configure 按照系统提示填写 checkpoints、model_paralle_size等。
当被要求提供模型的检查点目录时,请提供上一步中的本地模型路径。
这将配置写入 ~/.llama/configs/inference.yaml.
提示: 请注意,当您下载HF checkpoints 时,我们依赖于存储在
original文件夹中的原始.pth文件。因此,如有必要,请确保对检查点目录使用。/original
您应该看到输出如下
YAML configuration has been written to /.llama/configs/inference.yaml 所有配置以及模型都存储在~/.llama
通过运行以下命令运行推理服务器:
llama inference start 这将启动默认运行模型localhost:5000推理服务器。
**提示:**推理配置位于
~/.llama/configs/inference.yaml中。根据需要随意增加max_seq_len或更改检查点目录。
输出形式:
Loading config from : ~/.llama/configs/inference.yaml Yaml config: ------------------------ inference_config: impl_config: impl_type: inline checkpoint_config: checkpoint: checkpoint_type: pytorch checkpoint_dir: /local/checkpoints/Meta-Llama-3.1-8B-Instruct-20240710150000// tokenizer_path: /local/checkpoints/Meta-Llama-3.1-8B-Instruct-20240710150000//tokenizer.model model_parallel_size: 1 quantization_format: bf16 quantization: null torch_seed: null max_seq_len: 2048 max_batch_size: 1 ------------------------ Listening on :::5000 INFO: Started server process [2412753] INFO: Waiting for application startup. > initializing model parallel with size 1 > initializing ddp with size 1 > initializing pipeline with size 1 Loaded in 13.86 seconds NCCL version 2.20.5+cuda12.4 Finished model load YES READY INFO: Application startup complete. INFO: Uvicorn running on http://[::]:5000 (Press CTRL+C to quit) 此服务器在本地运行Llama模型。
提示: 您可能需要使用
-disable-ipv6flag 来禁用ipv6支持
现在已经设置了推理服务器,接下来就是使用 llama-agentic-system API 运行代理应用程序。
我们构建了示例脚本、笔记本和UI聊天界面(使用Mesop!)来帮助您入门。
通过运行以下命令配置代理系统配置:
llama agentic_system configure 按照系统提示操作。当请求模型检查点目录时,提供上一步的本地模型路径。
这会将配置写入~/.llama/configs/agentic_system/inline.yaml。
这个配置看起来像这样
agentic_system_config: impl_config: impl_type: inline inference_config: impl_config: impl_type: remote # the url to the inference server url: http://localhost:5000 # Safety shields safety_config: llama_guard_shield: model_dir: excluded_categories: [] disable_input_check: False disable_output_check: False prompt_guard_shield: model_dir: # Use this config to change the sampling params # when interacting with an agent instance sampling_params: temperature: 0.0 strategy: "top_p" top_p: 0.95 top_k: 0 在repo根目录中,为工具添加API密钥。模型支持的工具需要API密钥–
提示如果您没有API密钥,您仍然可以在没有模型访问工具的情况下运行应用程序。
启动应用程序(内联)并通过运行以下命令与之交互:
mesop app/main.py 这将启动一个mesop应用程序,你可以去localhost:32123玩聊天界面。

类似于这个主应用程序,您也可以尝试其他变体
mesop app/chat_with_custom_tools.py 展示如何集成自定义工具mesop app/chat_moderation_with_llama_guard.py 展示如何修改应用程序以充当聊天版主以确保安全提示保持推理服务器后台运行以加快迭代周期
注意:确保推理服务器仍在运行。
cd conda activate $ENV llama inference start # If not already started python examples/scripts/vacation.py localhost 5000 您应该会看到表单标准输出的输出——
Environment: ipython Tools: brave_search, wolfram_alpha, photogen Cutting Knowledge Date: December 2023 Today Date: 23 July 2024 User> I am planning a trip to Switzerland, what are the top 3 places to visit? Final Llama Guard response shield_type= is_violation=False violation_type=None violation_return_message=None Ran PromptGuardShield and got Scores: Embedded: 0.9999765157699585, Malicious: 1.1110752893728204e-05 StepType.shield_call> No Violation role='user' content='I am planning a trip to Switzerland, what are the top 3 places to visit?' StepType.inference> Switzerland is a beautiful country with a rich history, culture, and natural beauty. Here are three must-visit places to add to your itinerary: .... 提示您可以选择在脚本中执行
--disable-safety操作,以避免一直运行安全防护。
2024-07-24(三)