Skip to main content

A Python client for Tool Server Protocol (TSP)

Project description

pyTSPClient

轻量级 Python 客户端,用于连接 TSP 工具服务器。

安装

pip install pytspclient

使用方式

一、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.3.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytspclient-0.2.3-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file pytspclient-0.2.3.tar.gz.

File metadata

  • Download URL: pytspclient-0.2.3.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for pytspclient-0.2.3.tar.gz
Algorithm Hash digest
SHA256 004ff15830d53c2d09888169baf9b5889b430b07d5e6b9dfa0ae572dc4b6daf5
MD5 189f222670ba34317749e01950cd464f
BLAKE2b-256 a3c5dd1b92542de8acbf7cd5754ee21750d2bedc0c82c1fe3d9cd3b1cfb51fa9

See more details on using hashes here.

File details

Details for the file pytspclient-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: pytspclient-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for pytspclient-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7d18701534d03a7287e00a487b22b273f827eee630977cc278ca1414f9bd5f70
MD5 6ae5446d593500511e587d7873308677
BLAKE2b-256 7af6792b7938a95684425a3821e47d64f49713241989d3ea60fbfaf5265af689

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page