A Python client for Tool Server Protocol (TSP)
Project description
pyTSPClient
轻量级 Python 客户端,用于连接 TSP 工具服务器。
安装
pip install pytspclient
国内网络环境
如遇网络问题,可使用镜像源:
# pip 使用清华源
pip install pytspclient -i https://pypi.tuna.tsinghua.edu.cn/simple
构建 gtsp 时需设置 Go 代理:
# Go module 代理
export GOPROXY=https://goproxy.cn,direct
# Go 下载镜像(如需)
# macOS: https://golang.google.cn/dl/
# Linux: https://golang.google.cn/dl/
使用方式
一、Normal API — 普通场景使用
自己构建参数来调用工具。完整示例见 demo_basic.py。
from pytspclient import TSPClient, ToolCall
# 启动并初始化
tsp = await TSPClient.from_stdio("./gtsp").start()
# 获取 TSP 信息
print(tsp.tools) # 工具 Schema 列表
print(tsp.workdir) # TSP 工作目录
# 调用工具
call = ToolCall(name="read_file", input={"file_path": "hello.txt"})
result = await tsp.call_tool(call)
print(result.output)
# 关闭
await tsp.shutdown()
二、Adapter API — 直接对接 LLM 使用
使用 LLM 返回的对象,直接对接 TSP。完整示例见 demo_agent.py。
from pytspclient import TSPClient
from openai import OpenAI
tsp = await TSPClient.from_stdio("./gtsp").start()
adapter = tsp.for_openai()
llm = OpenAI()
messages = [{"role": "user", "content": "读取 hello.txt"}]
while True:
resp = llm.chat.completions.create(model="gpt-4o", messages=messages, tools=adapter.tools)
messages.append(resp.choices[0].message)
if adapter.parse_tool_calls(resp):
results = await adapter.execute_tool_calls(resp)
messages.extend(adapter.to_tool_messages(results))
else:
print(resp.choices[0].message.content)
break
API 参考
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pytspclient-0.2.6.tar.gz
(12.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytspclient-0.2.6.tar.gz.
File metadata
- Download URL: pytspclient-0.2.6.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a845c22b7a8d01d24f7fa9380bddf8d12a89586cde874a333b850df487f5b16
|
|
| MD5 |
51f53913aab344a88552d4449987d6ff
|
|
| BLAKE2b-256 |
21a141484f04f7423496b86482b71510249e88598816fab1fd32a8bb07786a79
|
File details
Details for the file pytspclient-0.2.6-py3-none-any.whl.
File metadata
- Download URL: pytspclient-0.2.6-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
192bae66aa5729961892342ce2dda275fa107389e7e303726f113b23aa43051d
|
|
| MD5 |
243e4856c3b8b71c126768b1cdfca7cc
|
|
| BLAKE2b-256 |
4577695bb8e4073fda41acdc93f0adf589632b56d16e81b47f25e6d437bc1586
|