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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytspclient-0.2.4.tar.gz
  • Upload date:
  • Size: 10.7 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.4.tar.gz
Algorithm Hash digest
SHA256 5d4ba0315e4bdc2f94bdb9e559a3af7dfb7b0c406766fcaac23494489b06fcfc
MD5 85c96ba07fa81caa5ff1dd73de1de2c6
BLAKE2b-256 65f9a2bc491031d71ad5fd1c8d4698bb1d25de60929ac57d8aa95a079c892302

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytspclient-0.2.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 33b26208fbb60d8518cfb5fb9e0832a30ad8eff07662fde2affc4d8e132bf0ab
MD5 ec0fea5110928517006026bd1e5edf13
BLAKE2b-256 1b287e80827d0532d175d9d6c5b1a6094b9a7f055c7a38b94da731d378e35352

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