Dify SDK
Project description
Dify SDK
一个简单的数学工具库,提供基本的数学运算功能。同时支持Dify API的事件处理功能。
功能特点
- 提供基本的数学运算:加法、减法、乘法、除法
- 内置类型提示支持
- 详细的文档和示例
- 完整的测试覆盖
- 健壮的错误处理和日志记录
- 新增: 支持Dify API的事件处理,包括聊天消息、Agent消息等多种事件类型
安装
使用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}")
开发
环境设置
- 克隆仓库
- 创建并激活虚拟环境
- 安装开发依赖
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 # 事件处理示例
最近更新
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.0.tar.gz
(15.0 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.0-py3-none-any.whl
(15.6 kB
view details)
File details
Details for the file dify_sdk-0.1.0.tar.gz.
File metadata
- Download URL: dify_sdk-0.1.0.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59684d727d6b832fb1747b604cf557397c0e5c74adbf58735b53538aa2efd8bf
|
|
| MD5 |
d4b55b45af2a74a8ba0f09544955f8f8
|
|
| BLAKE2b-256 |
98a3a71e15160ffc86b7fd14f26f5212045538bf3ab5d1057601a029ca0198c5
|
File details
Details for the file dify_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dify_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ee3489d7414402bf072f80f855a27c04e7069d1b8399802d841a1fea2ad0440
|
|
| MD5 |
fd71410149d3abdd8d9a6d6020b22c96
|
|
| BLAKE2b-256 |
176118cf68867215f689053229588ac6a9115c9574ee8667aad3dfab1e1b2ae7
|