LLaMA Stack
LLaMA Stack 是基于 LLaMA 模型的完整解决方案,提供模型服务、管理和部署功能。
支持的模型
- llama-stack-default - 默认 LLaMA 模型
- llama-stack-instruct - 指令调优模型
- llama-stack-chat - 对话优化模型
- llama-stack-code - 代码专用模型
配置
基础配置
在 config.yaml 或 ~/.bytebuddy/config.yaml 中配置:
yaml
models:
- name: "llamastack-default"
provider: "llamastack"
model: "llama-stack-default"
apiKey: "${LLAMASTACK_API_KEY}"
apiBase: "http://localhost:5000"
roles: ["chat"]
defaultCompletionOptions:
temperature: 0.7
maxTokens: 4096指令调优配置
yaml
models:
- name: "llamastack-instruct"
provider: "llamastack"
model: "llama-stack-instruct"
apiKey: "${LLAMASTACK_API_KEY}"
apiBase: "http://localhost:5000"
roles: ["chat", "edit"]
defaultCompletionOptions:
temperature: 0.3
maxTokens: 2048多模型配置
yaml
models:
- name: "llamastack-chat"
provider: "llamastack"
model: "llama-stack-chat"
apiKey: "${LLAMASTACK_API_KEY}"
apiBase: "http://localhost:5000"
roles: ["chat"]
defaultCompletionOptions:
temperature: 0.7
maxTokens: 4096
- name: "llamastack-code"
provider: "llamastack"
model: "llama-stack-code"
apiKey: "${LLAMASTACK_API_KEY}"
apiBase: "http://localhost:5000"
roles: ["edit", "apply"]
defaultCompletionOptions:
temperature: 0.2
maxTokens: 2048配置字段
必需字段
- name: 模型配置的唯一标识符
- provider: 设置为
"llamastack" - model: 模型名称
- apiKey: LLaMA Stack API 密钥
- apiBase: LLaMA Stack 服务器地址
可选字段
- roles: 模型角色 [
chat,edit,apply,autocomplete] - defaultCompletionOptions:
temperature: 控制随机性 (0-1)maxTokens: 最大令牌数topP: 核采样参数topK: 采样候选数量frequencyPenalty: 频率惩罚presencePenalty: 存在惩罚
环境变量
bash
# ~/.bashrc 或 ~/.zshrc
export LLAMASTACK_API_KEY="your-llamastack-api-key"获取 API 密钥
- 访问 LLaMA Stack 服务控制台
- 注册账户并登录
- 导航到 API 密钥页面
- 生成新的 API 密钥
- 将密钥保存到环境变量
使用场景配置
通用对话
yaml
models:
- name: "general-chat"
provider: "llamastack"
model: "llama-stack-chat"
apiKey: "${LLAMASTACK_API_KEY}"
apiBase: "http://localhost:5000"
roles: ["chat"]
defaultCompletionOptions:
temperature: 0.7
maxTokens: 2048代码生成
yaml
models:
- name: "code-gen"
provider: "llamastack"
model: "llama-stack-code"
apiKey: "${LLAMASTACK_API_KEY}"
apiBase: "http://localhost:5000"
roles: ["edit", "apply"]
defaultCompletionOptions:
temperature: 0.2
maxTokens: 4096指令任务
yaml
models:
- name: "instruction"
provider: "llamastack"
model: "llama-stack-instruct"
apiKey: "${LLAMASTACK_API_KEY}"
apiBase: "http://localhost:5000"
roles: ["chat", "edit"]
defaultCompletionOptions:
temperature: 0.3
maxTokens: 2048故障排除
常见错误
- 401 Unauthorized: 检查 API 密钥是否正确
- Connection Refused: 确认服务器是否运行
- 503 Service Unavailable: 服务器过载或维护中
调试步骤
- 验证 API 密钥格式和有效性
- 检查服务器地址和端口
- 确认网络连接正常
- 查看服务器日志
最佳实践
1. 模型选择
- 对话任务使用 chat 模型
- 代码任务使用 code 模型
- 指令任务使用 instruct 模型
2. 性能优化
- 设置合理的超时值
- 根据任务选择合适的模型
- 实现请求缓存
3. 安全性
- 使用环境变量存储 API 密钥
- 定期轮换密钥
- 监控异常使用
4. 成本控制
- 监控 API 使用量
- 设置 maxTokens 限制
- 选择合适的模型