Skip to main content

Toolkit for Chat API

Project description

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

安装方法

pip install chattool --upgrade

使用方法

设置密钥和代理链接

通过环境变量设置密钥和代理,比如在 ~/.bashrc 或者 ~/.zshrc 中追加

export OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
export OPENAI_API_BASE="https://api.example.com/v1"
export OPENAI_API_BASE_URL="https://api.example.com" # 可选

注:环境变量 OPENAI_API_BASE 优先于 OPENAI_API_BASE_URL,二者选其一即可。

示例

示例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())

开源协议

使用 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-4.1.1.tar.gz (73.6 kB view details)

Uploaded Source

Built Distribution

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

chattool-4.1.1-py3-none-any.whl (89.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: chattool-4.1.1.tar.gz
  • Upload date:
  • Size: 73.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for chattool-4.1.1.tar.gz
Algorithm Hash digest
SHA256 ec282ddfe3ae327eff6d11992c1c63fb3fe32d37197f391d2c11f6190839e294
MD5 8ed5436915cbe83eb7d0c8b5ad122578
BLAKE2b-256 ccd0212bb23ccf0a8e0eed855e1a4047f72f9c29cb2e8904a2a1bcfe952121a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: chattool-4.1.1-py3-none-any.whl
  • Upload date:
  • Size: 89.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for chattool-4.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 073a34386ba4e83d0da199275631427285d857df459fe093e0d07e5fcb363160
MD5 c9fa2507c085d818462c0a51e99aed55
BLAKE2b-256 38ae1c6c4ca4e6a1dd8cd9a7df2f2cad1f962b4554b24030d380ec29fde8099c

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