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.5.tar.gz (10.8 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.5-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytspclient-0.2.5.tar.gz
  • Upload date:
  • Size: 10.8 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.5.tar.gz
Algorithm Hash digest
SHA256 f6a4c73ae06e297bd06da43484c1c78251904b46368dad226aa678a8b7838fbf
MD5 57d986b1e2e65f087db608baa2b4fb74
BLAKE2b-256 5b5233fa01792355eb77d855b54f140ccc070902a1fa3b389e7988afb3a0aa77

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytspclient-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 10.0 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b42285c3bfb138829a85fcc970efe72bf32d5bb59fa3779bdb5fc20675d00ad2
MD5 026167b4dae4b8144afa0bda3e7e977c
BLAKE2b-256 55abb75827b14ba603e6bb0b115b2af73977afd94f81c0ea0d32036fc8a47683

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