techlab-innov/llmtrace

GitHub: techlab-innov/llmtrace

一款用 Rust 编写的零代码透明代理,为 OpenAI 兼容 API 提供实时安全检测、PII 扫描和成本可观测性。

Stars: 47 | Forks: 1

# LLMTrace [![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/c24334d922172227.svg)](https://github.com/epappas/llmtrace/actions/workflows/ci.yml) [![安全审计](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/5e9ae19b54172229.svg)](https://github.com/epappas/llmtrace/actions/workflows/security.yml) [![crates.io](https://img.shields.io/crates/v/llmtrace.svg)](https://crates.io/crates/llmtrace) [![PyPI](https://img.shields.io/pypi/v/llmtracing.svg)](https://pypi.org/project/llmtracing/) [![许可证: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![Rust](https://img.shields.io/badge/rust-1.75%2B-orange.svg)](https://www.rust-lang.org/) [![GitHub Stars](https://img.shields.io/github/stars/epappas/llmtrace)](https://github.com/epappas/llmtrace/stargazers) **生产环境下的零代码 LLM 可观测性与安全。** LLMTrace 是一个透明的代理,可以实时捕获、分析和保护您的 LLM 交互。将它置于您的应用程序和任何兼容 OpenAI 的 API 之间,即可在不修改任何代码的情况下,即时洞悉提示词注入攻击、PII(个人身份信息)泄露、成本超支以及性能瓶颈。 ## 为什么选择 LLMTrace? 生产环境的 LLM 应用程序面临三个关键的盲区: - **安全漏洞** — 提示词注入、数据泄露、PII 暴露 - **成本失控** — 无法控制的 API 开销、低效的 token 使用 - **性能黑盒** — 对延迟、失败率或用户行为缺乏可见性 LLMTrace 通过透明地部署在您的应用程序和 LLM 提供商之间来解决这些问题,为您提供完整的可观测性和控制力。 ## 核心特性 - **透明代理** — 可直接替换任何兼容 OpenAI 的 API - **ML 集成检测** — 多检测器多数表决(regex、DeBERTa、InjecGuard、PIGuard) - **实时安全** — 提示词注入检测、PII 扫描、数据泄露防护 - **性能监控** — 延迟、token 使用情况、流式指标 (TTFT)、错误追踪 - **成本控制** — 针对每个代理的预算、速率限制、异常检测 - **多租户就绪** — 按 API 密钥或自定义租户标头进行隔离 - **高性能** — 使用 Rust 构建,支持流式响应和熔断器保护 ## 安全性能 | 指标 | 数值 | |-----------|-------| | 准确率 | 87.6% | | 精确率 | 95.5% | | F1 分数 | 86.9% | | 召回率 | 79.7% | 在包含 CyberSecEval2、BIPIA、TensorTrust 和 InjecAgent 在内的 12 种攻击类别的 153 个样本对抗语料库上进行了测试。有关方法和完整结果,请参见 [基准测试/](benchmarks/)。 ## 快速开始 ### 1. 安装 ``` curl -sS https://raw.githubusercontent.com/epappas/llmtrace/main/scripts/install.sh | bash ``` 或使用以下其他方法之一: ``` cargo install llmtrace # from crates.io docker pull ghcr.io/epappas/llmtrace-proxy:latest # Docker ``` ### 2. 运行 ``` export OPENAI_API_KEY="sk-..." llmtrace-proxy --config config.yaml ``` ### 3. 与您的现有代码结合使用 ``` import openai # 之前:直接指向 OpenAI client = openai.OpenAI() # 之后:指向 LLMTrace proxy(就是这样!) client = openai.OpenAI(base_url="http://localhost:8080/v1") # 您的代码保持完全一致 response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Hello!"}] ) ``` ### 4. 查看您的追踪记录 ``` # 查看近期活动 curl http://localhost:8080/api/v1/traces | jq '.[0]' # 检查安全发现 curl http://localhost:8080/api/v1/security/findings | jq # 监控成本 curl http://localhost:8080/api/v1/costs/current | jq ``` **大功告成!** 您现在可以全面了解您的 LLM 交互情况了。 ## 架构 ``` graph LR A[Your Application] -->|HTTP| B[LLMTrace Proxy] B -->|HTTP| C[OpenAI/LLM Provider] B -->|Async| D[Security Engine] B -->|Async| E[Storage Engine] D --> F[SQLite/PostgreSQL] E --> F D --> G[Real-time Alerts] H[Dashboard] -->|REST API| B I[Monitoring] -->|Metrics API| B style B fill:#e1f5fe style D fill:#fff3e0 style E fill:#f3e5f5 ``` **工作原理:** 1. **透明代理** — 您的应用程序将请求发送给 LLMTrace,而不是 OpenAI 2. **请求转发** — LLMTrace 将请求转发给真实的 LLM 提供商 3. **后台分析** — 安全分析和追踪捕获异步进行 4. **零影响** — 即使分析过程出现问题,您的应用程序也无需等待分析完成 ## 集成示例 ### OpenAI Python SDK ``` import openai # 只需更改 base_url client = openai.OpenAI( base_url="http://localhost:8080/v1", api_key="your-openai-key" ) ``` ### OpenAI Node.js SDK ``` import OpenAI from 'openai'; const openai = new OpenAI({ baseURL: 'http://localhost:8080/v1', apiKey: 'your-openai-key' }); ``` ### LangChain ``` from langchain_openai import ChatOpenAI llm = ChatOpenAI( base_url="http://localhost:8080/v1", api_key="your-openai-key" ) ``` ### curl ``` curl http://localhost:8080/v1/chat/completions \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "gpt-4", "messages": [{"role": "user", "content": "Hello!"}]}' ``` **[查看所有集成指南 ->](docs/README.md#guides)** ## 仪表板与监控 LLMTrace 包含一个内置仪表板,用于可视化追踪记录、安全发现和成本: ``` # 访问 dashboard open http://localhost:3000 # 或使用 REST API curl http://localhost:8080/api/v1/traces curl http://localhost:8080/api/v1/security/findings curl http://localhost:8080/api/v1/costs/current ``` **仪表板功能:** - 实时追踪可视化 - 安全事件时间线 - 按模型/代理划分的成本细分 - 性能指标和告警 ## 配置 ### 最小配置 ``` # config.yaml upstream_url: "https://api.openai.com" listen_addr: "0.0.0.0:8080" storage: profile: "lite" # SQLite for simple deployments security: enable_prompt_injection_detection: true enable_pii_detection: true ``` ### 生产环境配置 ``` # config.yaml upstream_url: "https://api.openai.com" listen_addr: "0.0.0.0:8080" storage: profile: "production" postgres_url: "postgresql://user:pass@localhost/llmtrace" clickhouse_url: "http://localhost:8123" redis_url: "redis://localhost:6379" security: enable_prompt_injection_detection: true enable_pii_detection: true enable_streaming_analysis: true cost_control: daily_budget_usd: 1000 per_agent_daily_budget_usd: 100 alerts: slack: webhook_url: "https://hooks.slack.com/..." rate_limiting: requests_per_minute: 1000 burst_capacity: 2000 ``` **[完整配置指南 ->](docs/getting-started/configuration.md)** ## API 参考 | 端点 | 描述 | |----------|-------------| | `GET /api/v1/traces` | 列出最近的追踪记录 | | `GET /api/v1/traces/{id}` | 获取特定追踪记录详情 | | `GET /api/v1/security/findings` | 列出安全事件 | | `GET /api/v1/costs/current` | 成本细分和使用情况 | | `GET /health` | 健康检查和熔断器状态 | | `POST /policies/validate` | 验证自定义安全策略 | **[完整 API 文档 ->](docs/guides/API.md)** ## 安装说明 ### Cargo (Rust Proxy) ``` cargo install llmtrace llmtrace-proxy --config config.yaml ``` ### Pip (Python SDK) ``` pip install llmtracing ``` ``` import llmtrace tracer = llmtrace.configure({"enable_security": True}) span = tracer.start_span("chat_completion", "openai", "gpt-4") span.set_prompt("Hello!") span.set_response("Hi there!") print(span.to_dict()) ``` ### Docker ``` docker pull ghcr.io/epappas/llmtrace-proxy:latest docker run -p 8080:8080 ghcr.io/epappas/llmtrace-proxy:latest ``` ### Docker Compose 及依赖项 ``` curl -o compose.yaml https://raw.githubusercontent.com/epappas/llmtrace/main/compose.yaml docker compose up -d ``` ### 从源码构建 ``` git clone https://github.com/epappas/llmtrace cd llmtrace cargo build --release --features ml ./target/release/llmtrace-proxy --config config.yaml ``` ### Kubernetes ``` helm install llmtrace ./deployments/helm/llmtrace ``` **[包含所有方法的安装指南 ->](docs/getting-started/installation.md)** ## 生产环境部署 ### 高可用性设置 - **负载均衡器** -> 多个 LLMTrace 实例 - **PostgreSQL** 用于持久化追踪存储 - **ClickHouse** 用于高吞吐量分析 - **Redis** 用于缓存和速率限制 ### 安全最佳实践 - API 密钥验证和租户隔离 - 在负载均衡器处进行 TLS 终止 - 组件之间的网络分段 - 定期更新安全策略 ### 监控与告警 - Prometheus 指标导出 - Grafana 仪表板 - PagerDuty/Slack 集成 - OWASP LLM Top 10 合规性报告 **[生产环境部署指南 ->](docs/deployment/kubernetes.md)** ### 开发环境设置 ``` git clone https://github.com/epappas/llmtrace cd llmtrace cargo build --workspace cargo test --workspace ``` ### 项目结构 | Crate | 包 | 用途 | |-------|---------|---------| | `llmtrace-core` | - | 共享类型和特征 | | `llmtrace` | [crates.io](https://crates.io/crates/llmtrace) | HTTP 代理服务器 (`cargo install llmtrace`) | | `llmtrace-security` | - | 安全分析引擎(regex + DeBERTa + InjecGuard + PIGuard 集成) | | `llmtrace-storage` | - | 存储后端(SQLite、PostgreSQL、ClickHouse、Redis) | | `llmtrace-python` | [PyPI](https://pypi.org/project/llmtracing/) | Python SDK(`pip install llmtracing`,导入方式为 `import llmtrace`) | **[开发指南 ->](CONTRIBUTING.md)** ## 许可证 [MIT](LICENSE) - 可免费用于商业和个人用途。 如果 LLMTrace 帮助您保护了您的 LLM 应用程序,请**给本项目点星 (Star)**! **发现了错误?**[提交 Issue](https://github.com/epappas/llmtrace/issues) **有疑问?**[发起讨论](https://github.com/epappas/llmtrace/discussions)
标签:API成本控制, API监控, API网关, CISA项目, DeBERTa, DLL 劫持, LLMOps, MLOps, ML集成检测, OpenAI, Petitpotam, PII扫描, Rust, Token使用分析, 个人身份信息保护, 内存规避, 可观察性, 可视化界面, 大语言模型, 实时安全, 性能监控, 技术教程, 搜索引擎查询, 测试用例, 私有化部署, 网关代理, 网络安全, 网络流量审计, 请求拦截, 逆向工具, 透明代理, 通知系统, 防御规避, 隐私保护, 零代码