TrustedSec LLM 库 是一个用于与支持工具调用的本地大语言模型进行交互的 Python 库。通过利用本地 LLM 端点与 MCP(模型上下文协议)集成,它可以运行通常只有前沿模型才能实现的大规模工作流。
# 克隆或复制仓库
git clone https://github.com/trustedsec/ts_llmlib.git
cd ts_llmlib
# 使用 pip 安装
pip install -e .
将 ts_llmlib/ 目录复制到你的项目中:
cp -rf ts_llmlib /path/to/your/project/
from ts_llmlib import ChatSession
# 使用默认设置初始化(连接到 http://localhost:1234/v1/chat/completions)
chat = ChatSession()
# 运行提示词
response = chat.run_prompt("当前目录下有哪些文件?")
print(response['content'])
from ts_llmlib import ChatSession
# 使用自定义设置配置
chat = ChatSession(
system_prompt="你是一个乐于助人的助手,善于使用文件工具。",
tool_list=[], # 空列表 = 使用默认文件工具
mcp_servers={
"default": "http://localhost:3000/mcp"
},
llm_endpoint_url="http://localhost:1234/v1/chat/completions",
model_name="qwen3-coder-next",
timeout=60,
max_runtime=300
)
response = chat.run_prompt("将 'hello' 写入 /tmp/greeting.txt")
print(response['content'])
from ts_llmlib import ChatSession
chat = ChatSession()
history = [
{"role": "user", "content": "2+2 等于几?"},
{"role": "assistant", "content": "答案是 4。"}
]
response = chat.run_prompt("你能把它写入文件吗?", history=history)
| 工具 |
|---|
MCP(模型上下文协议)支持与外部工具和服务集成。配置了 MCP 服务器后,ts_llmlib 将:
from ts_llmlib import ChatSession
chat = ChatSession(
mcp_servers={
"ghidraSvr": "http://localhost:8081/sse"
},
llm_endpoint_url="http://localhost:1234/v1/chat/completions",
model_name="qwen3-coder-next"
)
# 聊天会话将自动获取并集成 Ghidra 工具
# 例如 list_methods, decompile_function, get_xrefs_to 等
ts_llmlib 支持 RPC 风格和 SSE(服务器推送事件)端点:
http://localhost:3000/mcphttp://localhost:3000/sse(RPC 调用时自动转换为 /mcp)ChatSession(
system_prompt: str | None = None,
tool_list: list | None = None,
mcp_servers: dict[str, str] | None = None,
llm_endpoint_url: str = "http://localhost:1234/v1/chat/completions",
model_name: str = "default",
timeout: int = 60,
max_runtime: int = 300
)
参数说明:
system_prompt (str | None):自定义系统提示词。默认为一个最小化助手提示词。tool_list (list | None):自定义工具定义列表。空列表使用内置工具。mcp_servers (dict[str, str] | None):服务器名称到 URL 的字典。llm_endpoint_url (str):LLM API 端点的 URL。model_name (str):LLM 端点的模型标识符。timeout (int):HTTP 请求超时时间(秒)。max_runtime (int):单次提示词的最大执行时间(秒)。response = chat.run_prompt(
user_prompt: str,
conversation_history: list[dict] | None = None,
disable_tools: list[str] | None = None,
max_runtime: int | None = None
) -> dict
参数说明:
user_prompt (str):用户的消息或问题。conversation_history (list[dict] | None):可选的对话历史,格式为角色/内容对列表。disable_tools (list[str] | None):本次调用中禁用的工具名称列表。max_runtime (int | None):为本次调用覆盖默认的最大运行时间。返回值:
{
"content": str, # LLM 响应文本
"tool_calls": list, # 工具调用列表(如果有)
"usage": dict | None, # 令牌用量(如果 LLM 提供)
"error": str | None # 错误信息(如果失败)
}
ToolRegistry 管理聊天会话可用的所有工具:
tool_list 参数定义的用户工具要覆盖默认路径,可以设置以下变量,这些变量会在 ChatSession 中被检查。
TS_LLM_MODEL=qwen3-coder-next TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions
# 使用示例
export TS_LLM_MODEL=qwen3-coder-next
export TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions
ts_llmlib-redclippy
# 或者
TS_LLM_MODEL=qwen3-coder-next TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions ts_llmlib-redclippy
分析 C/C++ 源文件中的安全漏洞:
ts_llmlib-cpp-analyze <source_folder> <output_folder>
集成 Ghidra 的逆向工程二进制分析:
# 基本分析
ts_llmlib-ghidra-analyze <output_folder>
# 仅重命名模式(第一遍)
ts_llmlib-ghidra-analyze --rename_only <output_folder>
# 仅处理先前未命名的函数
ts_llmlib-ghidra-analyze --process_unnamed_only <output_folder>
# 分组分析(用于调用关系分组)
ts_llmlib-ghidra-analyze --grouped <output_folder>
如果不使用 --rename_only 运行完毕后,可以通过以下命令清理结构:
ts_llmlib-ghidra-cleanup <input_folder> <output_folder>
从 JSON 审查文件生成格式化漏洞报告:
ts_llmlib-ghidra-report <review_folder>
运行基于 Qt 的 RedClippy 聊天界面(注意此示例需要 pyside6):
ts_llmlib-redclippy
ts_llmlib 连接到任何兼容 OpenAI 的 API 端点。常见的本地 LLM 服务器:
| 服务器 | 默认 URL |
|---|---|
| Ollama | http://localhost:11434/v1/chat/completions |
| LM Studio | http://localhost:1234/v1/chat/completions |
| vLLM | http://localhost:8000/v1/chat/completions |
两个超时设置控制执行:
timeout):单次 API 请求的最大时间max_runtime):处理提示词(包括工具调用)允许的总墙钟时间如果超过任一限制,响应中将包含错误信息。
所有错误都在响应字典中返回:
response = chat.run_prompt("某个提示词")
if response.get('error'):
print(f"错误:{response['error']}")
else:
print(response['content'])
max_runtime 限制ts_llmlib/
├── __init__.py # 包初始化,导出 ChatSession
├── client.py # 用于向 LLM 端点发起 HTTP 请求的 LLMClient
├── chat.py # ChatSession 类(主要 API)
├── mcp.py # 用于模型上下文协议集成的 MCPClient
├── tools.py # 用于工具管理的 ToolRegistry
├── HOW_TO_TS_LLMLIB.md # 原始文档
└── examples/ # 示例脚本
├── c_cpp_analyze.py # C/C++ 漏洞分析脚本
├── redclippy.py # 基于 Qt 的 GUI 聊天应用
├── ghidra_analyze.py # 集成 MCP 的 Ghidra 二进制分析
├── ghidra_vuln_report.py # 漏洞报告生成器
├── ghidra_cleanup.py # 输出文件重组工具
└── example_ts_llmlib.py # 演示库用法的示例脚本
pyproject.toml # 现代 Python 包配置(脚本在此定义)
LICENSE.txt # BSD-3-Clause 许可证
README.md # 本文件
BSD-3-Clause 许可证 - 详情请见 LICENSE.txt 文件。
欢迎贡献!请随时提交拉取请求。
git checkout -b feature/AmazingFeature)git commit -m '添加一些 AmazingFeature')git push origin feature/AmazingFeature)| 参数 |
|---|
| 描述 |
|---|
read_local_file | path: str | 读取本地文件内容 |
write_local_file | path: str, content: str | 将内容写入本地文件 |
list_directory | path: str | 列出路径下的文件和目录 |