Skip to main content

Dify SDK

Project description

Dify SDK

一个简单的数学工具库,提供基本的数学运算功能。同时支持Dify API的事件处理功能。

功能特点

  • 提供基本的数学运算:加法、减法、乘法、除法
  • 内置类型提示支持
  • 详细的文档和示例
  • 完整的测试覆盖
  • 健壮的错误处理和日志记录
  • 新增: 支持Dify API的事件处理,包括聊天消息、Agent消息等多种事件类型
  • 新增: 支持停止消息生成功能,可以中断正在进行的AI响应

安装

使用pip安装:

pip install dify_sdk

使用方法

基本用法

from dify_sdk import add, subtract, multiply, divide

# 基本运算
result = add(10, 5)      # 15
result = subtract(10, 5)  # 5
result = multiply(10, 5)  # 50
result = divide(10, 5)    # 2.0

# 支持浮点数
result = add(3.14, 2.71)  # 5.85

高级用法

# 链式操作
result = divide(multiply(add(10, 5), subtract(8, 3)), 2)
# 等同于 ((10 + 5) * (8 - 3)) / 2 = 37.5

事件处理

from dify.app.event_schemas import ConversationEvent, parse_event
from dify.app.schemas import ConversationEventType

# 解析事件
json_data = {
    "event": "message",
    "message_id": "msg_123",
    "conversation_id": "conv_456",
    "answer": "这是一个消息回复",
    "created_at": 1646035200
}

# 自动解析为对应的事件类型
event = parse_event(json_data)

# 根据事件类型处理
if event.event == ConversationEventType.MESSAGE:
    print(f"收到消息: {event.answer}")
elif event.event == ConversationEventType.ERROR:
    print(f"发生错误: {event.message}")

停止消息生成

from dify import DifyClient
from dify.app.schemas import ApiKey

# 创建客户端
client = DifyClient()

# 停止正在生成的消息
async def stop_message_example():
    # 创建API密钥对象
    api_key = ApiKey(
        id="",
        type="api",
        token="your_api_key_here",
        last_used_at=0,
        created_at=0,
    )
    
    # 调用stop_message方法停止消息生成
    result = await client.app.stop_message(
        api_key=api_key,
        task_id="task_id_here",
        user_id="user_id_here"
    )
    
    print(f"停止结果: {result.result}")

更详细的示例请参考 examples/stop_message_example.py

开发

环境设置

  1. 克隆仓库
  2. 创建并激活虚拟环境
  3. 安装开发依赖
git clone https://github.com/yourusername/dify_sdk.git
cd dify_sdk
python -m venv .venv
.venv\Scripts\activate  # Windows
source .venv/bin/activate  # Linux/Mac
pip install -e ".[dev]"

运行测试

pytest

发布到PyPI

本项目使用Hatch作为构建和发布工具。以下是发布到PyPI的步骤:

1. 安装Hatch

pip install hatch
# 或使用uv
uv pip install hatch

2. 配置PyPI凭证

有两种方式配置PyPI凭证:

方式一:使用API令牌(推荐)

  1. PyPI官网注册并登录账号
  2. 在账号设置中创建API令牌
  3. 创建~/.pypirc文件:
[pypi]
username = __token__
password = pypi-AgEIcHlwaS5vcmcCJDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

方式二:使用环境变量

# Windows (PowerShell)
$env:HATCH_INDEX_USER="__token__"
$env:HATCH_INDEX_AUTH="pypi-AgEIcHlwaS5vcmcCJDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Linux/Mac
export HATCH_INDEX_USER=__token__
export HATCH_INDEX_AUTH=pypi-AgEIcHlwaS5vcmcCJDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

3. 构建分发包

hatch build

这将在dist/目录下创建源代码分发包(.tar.gz)和轮子分发包(.whl)。

4. 发布到PyPI

hatch publish

如果您想先在测试环境(TestPyPI)上发布:

hatch publish -r test

5. 验证发布

发布成功后,您可以通过pip安装您的包来验证:

pip install dify_sdk

许可证

MIT

项目结构

dify_sdk/
├── dify/                    # 主库目录
│   ├── __init__.py          # 导出公共API
│   ├── app/                 # 应用相关功能
│   │   ├── __init__.py
│   │   ├── schemas.py       # 数据模型定义
│   │   ├── event_schemas.py # 事件模型定义
├── tests/                   # 测试目录
│   ├── test_core.py         # 核心功能测试
│   └── test_event_schemas.py # 事件模型测试
└── examples/                # 示例目录
    ├── basic_usage.py       # 基本用法示例
    ├── event_example.py     # 事件处理示例
    └── stop_message_example.py # 停止消息生成示例

最近更新

1.2.0 (2023-03-05)

  • 新增:支持停止消息生成功能
    • 添加了stop_message方法,用于中断正在进行的AI响应
    • 提供了详细的示例代码和测试用例
    • 完善了文档说明

1.1.0 (2023-03-04)

  • 新增:支持Dify API的事件处理功能
    • 添加了ConversationEvent联合类型,支持多种事件类型的处理
    • 提供了parse_event函数,用于根据事件类型自动解析事件对象
    • 添加了事件处理示例和测试用例

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

dify_sdk-0.1.10.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

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

dify_sdk-0.1.10-py3-none-any.whl (34.0 kB view details)

Uploaded Python 3

File details

Details for the file dify_sdk-0.1.10.tar.gz.

File metadata

  • Download URL: dify_sdk-0.1.10.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for dify_sdk-0.1.10.tar.gz
Algorithm Hash digest
SHA256 a6c581a2e66638aa1e1b6439f6992697be3292587b33217a42790cd64e6d16a4
MD5 ff8d3064270d010bcc163a5b42d465fa
BLAKE2b-256 dd5133180c31456a2b8bd125451451487c80bb2989c70dee70e7fc73d6c8a87c

See more details on using hashes here.

File details

Details for the file dify_sdk-0.1.10-py3-none-any.whl.

File metadata

  • Download URL: dify_sdk-0.1.10-py3-none-any.whl
  • Upload date:
  • Size: 34.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for dify_sdk-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 17dcd1076c60317fcc2448ba603537d74f30e777a1d3a9e4d97a367480518fbd
MD5 b861fdceb528d04156557347aff12028
BLAKE2b-256 9f0975a09a1d084fd6b9cb13c7f9c9e8deb99147a09b39b7b3e2c7672422aa5e

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