Skip to main content

Toolkit for Chat API

Project description

基于 OpenAI API 的 Chat 对象,支持多轮对话以及异步处理数据等。

安装方法

pip install chattool --upgrade

使用方法

环境变量配置

ChatTool 使用集中式配置管理,支持.env文件、环境变量和默认值。

  1. CLI 管理配置(推荐):

    # 交互式初始化(引导设置各项配置)
    chatenv init -i
    
    # 设置配置项
    chatenv set OPENAI_API_KEY=sk-xxx
    
    # 查看配置
    chatenv cat
    
  2. 手动配置: 你可以手动创建 .env 文件或设置环境变量。

    OpenAI 配置

    export OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    export OPENAI_API_BASE="https://api.example.com/v1"
    export OPENAI_API_MODEL="gpt-3.5-turbo"
    

    阿里云 DNS 配置

    export ALIBABA_CLOUD_ACCESS_KEY_ID="your-access-key-id"
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET="your-access-key-secret"
    export ALIBABA_CLOUD_REGION_ID="cn-hangzhou"
    

    腾讯云 DNS 配置

    export TENCENT_SECRET_ID="your-secret-id"
    export TENCENT_SECRET_KEY="your-secret-key"
    export TENCENT_REGION_ID="ap-guangzhou"
    

Chat 对象使用

示例1,多轮对话:

# 初次对话
chat = Chat("Hello!")
resp = chat.get_response()

# 继续对话
chat.user("How are you?")
next_resp = chat.get_response()

# 人为添加返回内容
chat.user("What's your name?")
chat.assistant("My name is GPT-3.5.")

# 保存对话内容
chat.save("chat.json", mode="w") # 默认为 "a"

# 打印对话历史
chat.print_log()

示例2,批量处理数据(串行),并使用缓存文件 chat.jsonl

# 串行处理(按需保存)
msgs = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
results = []
for m in msgs:
    chat = Chat()
    chat.system("你是一个熟练的数字翻译家。")
    resp = chat.user(f"请将该数字翻译为罗马数字:{m}").get_response()
    results.append(resp.content)
    chat.save("chat.jsonl", mode="a")

示例3,异步并发与流式输出:

import asyncio
from chattool import Chat

async def run():
    # 并发问答
    base = Chat().system("你是一个有用的助手")
    tasks = [base.copy().user(f"请解释:主题 {i}").async_get_response() for i in range(2)]
    responses = await asyncio.gather(*tasks)
    for r in responses:
        print(r.content)

    # 流式输出
    print("流式: ", end="")
    async for chunk in Chat().user("写一首关于春天的短诗").async_get_response_stream():
        if chunk.delta_content:
            print(chunk.delta_content, end="", flush=True)
    print()

asyncio.run(run())

DNS 工具箱

ChatTool 提供了统一的 DNS 管理接口,支持阿里云和腾讯云。

from chattool.tools.dns import create_dns_client

# 创建阿里云客户端
aliyun = create_dns_client("aliyun")
aliyun.add_domain_record("example.com", "www", "A", "1.1.1.1")

# 创建腾讯云客户端
tencent = create_dns_client("tencent")
tencent.add_domain_record("example.com", "www", "A", "1.1.1.1")

命令行工具 (CLI)

提供了便捷的 DDNS(动态域名解析)更新工具和 SSL 证书管理:

# 获取 DNS 记录
chattool dns get test.example.com

# 设置 DNS 记录
chattool dns set test.example.com -v 1.2.3.4

# DDNS 动态域名更新 (公网 IP)
chattool dns ddns -d example.com -r home --monitor

# DDNS 动态域名更新 (局域网 IP)
chattool dns ddns -d example.com -r nas --ip-type local --local-ip-cidr 192.168.1.0/24

# SSL 证书自动更新
chattool dns cert-update -d example.com -e admin@example.com --cert-dir ./certs

开源协议

使用 MIT 协议开源。

更新日志

  • 当前版本 4.1.0,统一 Chat API(同步/异步/流式),默认环境变量配置,改进重试与调试工具
  • 历史:2.x-3.x 阶段逐步完善异步处理与批量用法
  • 更早版本沿革请参考仓库提交记录

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

chattool-5.1.1.tar.gz (76.3 kB view details)

Uploaded Source

Built Distribution

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

chattool-5.1.1-py3-none-any.whl (90.5 kB view details)

Uploaded Python 3

File details

Details for the file chattool-5.1.1.tar.gz.

File metadata

  • Download URL: chattool-5.1.1.tar.gz
  • Upload date:
  • Size: 76.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for chattool-5.1.1.tar.gz
Algorithm Hash digest
SHA256 59605f93ee6dc79969a40deb0381b99e8a13d9ddd5aa04449fe67364d1ecef67
MD5 bf41295a42d3d9c9b0c4c0c2ff2efcd0
BLAKE2b-256 4cdc7d60dafed4be832d146e39cecb3b468ee356743f0564063dd674a3f9b6cb

See more details on using hashes here.

File details

Details for the file chattool-5.1.1-py3-none-any.whl.

File metadata

  • Download URL: chattool-5.1.1-py3-none-any.whl
  • Upload date:
  • Size: 90.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for chattool-5.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0837443d76b1e5d83951bd22b5db6fc64b7c5b5c51564c3833256dfa6e9d7b77
MD5 e74f75e01e802a0399aac6057692345a
BLAKE2b-256 e0308e8729019a7e5e24ebd0f0e16645eee525399cd130f7e4d72a3211755c63

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