快速开始

5 分钟上手 txcode-sdk

1. 安装

pip install txcode-sdk

依赖:Python >= 3.10, openai >= 1.0.0, tiktoken >= 0.5.0

2. 创建 Client

import asyncio
from txcode_sdk import TxCodeClient

async def main():
    client = TxCodeClient(
        api_key="sk-xxx",
        base_url="https://api.openai.com/v1",
        model="gpt-4",
        agent_type="code",
        max_iterations=50,
        work_dir=".",
    )

    result = await client.chat("Hello, world!")
    print(result.answer)

asyncio.run(main())

3. 发送第一条消息

result = await client.chat("帮我创建一个 hello.py 文件,打印 Hello World")
print(f"成功: {result.success}")
print(f"回答: {result.answer}")
print(f"迭代次数: {result.iterations}")

4. 处理工具调用回调

from txcode_sdk.types import StepInfo

def on_step(step: StepInfo):
    for i, tc in enumerate(step.tool_calls):
        status = "✓" if step.results[i].success else "✗"
        print(f"  [{status}] {tc.name}: {tc.arguments}")

result = await client.chat("列出所有 Python 文件", on_step=on_step)

5. 切换 Agent 类型

result = await client.chat("你好", agent_type="chat")

下一步