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。
开发
环境设置
- 克隆仓库
- 创建并激活虚拟环境
- 安装开发依赖
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令牌(推荐)
- 在PyPI官网注册并登录账号
- 在账号设置中创建API令牌
- 创建
~/.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
Release history Release notifications | RSS feed
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.6.tar.gz
(29.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
dify_sdk-0.1.6-py3-none-any.whl
(34.3 kB
view details)
File details
Details for the file dify_sdk-0.1.6.tar.gz.
File metadata
- Download URL: dify_sdk-0.1.6.tar.gz
- Upload date:
- Size: 29.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e552bd2dbd45f728bee731279a207b861977ba95dd35373f16777b37824eb9c
|
|
| MD5 |
e562040a82a2a92e83034ab56721c7f4
|
|
| BLAKE2b-256 |
0333bb607104406588a1a7d6a99b32726670c83f8e488c543aa999c146f653f3
|
File details
Details for the file dify_sdk-0.1.6-py3-none-any.whl.
File metadata
- Download URL: dify_sdk-0.1.6-py3-none-any.whl
- Upload date:
- Size: 34.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db0b3da0d897c19b22f44902b655ece0ca5923f9bb6a19a37759f9e714c7dc5c
|
|
| MD5 |
59b8699fca18ff2263c6f0fd0f5d9556
|
|
| BLAKE2b-256 |
84c5693b429b3c63cca404fa340ca8a26429cc023b1ceac1f07ee6bc75b6228e
|