Skip to content

Commit cb9f0e1

Browse files
committed
feat: add sharpaikit Python SDK with gRPC control plane
1 parent e1143cf commit cb9f0e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+10708
-0
lines changed

python-client/.gitignore

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
23+
# Virtual environments
24+
.venv/
25+
venv/
26+
ENV/
27+
env/
28+
29+
# IDE
30+
.vscode/
31+
.idea/
32+
*.swp
33+
*.swo
34+
*~
35+
36+
# Testing
37+
.pytest_cache/
38+
.coverage
39+
htmlcov/
40+
41+
# Generated gRPC code (optional - can be committed or regenerated)
42+
# sharpaikit/_grpc/agent_pb2.py
43+
# sharpaikit/_grpc/agent_pb2_grpc.py
44+
45+
# OS
46+
.DS_Store
47+
Thumbs.db
48+

python-client/.pypirc.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# PyPI Configuration Example
2+
# Copy this file to ~/.pypirc and fill in your API token
3+
4+
[pypi]
5+
username = __token__
6+
password = pypi-your-api-token-here
7+
8+
[testpypi]
9+
username = __token__
10+
password = pypi-your-test-api-token-here
11+

python-client/GRPC_SETUP.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# gRPC Setup Guide
2+
3+
## Overview
4+
5+
The SharpAIKit Python SDK uses gRPC for communication between Python and the C# runtime. This document explains how to set up and generate the required gRPC code.
6+
7+
## Prerequisites
8+
9+
1. **Protocol Buffers Compiler** (`protoc`)
10+
- Install from: https://grpc.io/docs/protoc-installation/
11+
- Or via package manager:
12+
- macOS: `brew install protobuf`
13+
- Ubuntu: `apt-get install protobuf-compiler`
14+
- Windows: Download from GitHub releases
15+
16+
2. **Python gRPC Tools**
17+
```bash
18+
pip install grpcio-tools
19+
```
20+
21+
## Generating gRPC Code
22+
23+
### For Python
24+
25+
```bash
26+
cd python-client
27+
python generate_grpc.py
28+
```
29+
30+
This will generate:
31+
- `sharpaikit/_grpc/agent_pb2.py` - Protocol buffer message classes
32+
- `sharpaikit/_grpc/agent_pb2_grpc.py` - gRPC service stubs
33+
34+
### Manual Generation
35+
36+
If the script doesn't work, generate manually:
37+
38+
```bash
39+
cd python-client
40+
python -m grpc_tools.protoc \
41+
--proto_path=../../proto \
42+
--python_out=sharpaikit/_grpc \
43+
--grpc_python_out=sharpaikit/_grpc \
44+
../../proto/agent.proto
45+
```
46+
47+
### For C#
48+
49+
C# gRPC code is automatically generated during build via `Grpc.Tools` package.
50+
51+
## Verifying Generation
52+
53+
After generation, you should see:
54+
55+
```bash
56+
ls sharpaikit/_grpc/
57+
# agent_pb2.py
58+
# agent_pb2_grpc.py
59+
# __init__.py
60+
```
61+
62+
## Troubleshooting
63+
64+
### "protoc: command not found"
65+
66+
Install Protocol Buffers compiler (see Prerequisites).
67+
68+
### "ModuleNotFoundError: No module named 'grpc_tools'"
69+
70+
```bash
71+
pip install grpcio-tools
72+
```
73+
74+
### Import Errors
75+
76+
Ensure generated files are in `sharpaikit/_grpc/` and `__init__.py` exists.
77+
78+
## Updating Proto Files
79+
80+
If you modify `proto/agent.proto`:
81+
82+
1. Regenerate Python code: `python generate_grpc.py`
83+
2. Rebuild C# project: `dotnet build src/SharpAIKit.Grpc/`
84+
3. Restart gRPC host
85+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# 完整功能实现计划
2+
3+
## 📋 实现范围
4+
5+
需要实现以下所有功能:
6+
7+
### ⚠️ 部分支持 → 完全支持 (3项)
8+
1. **工具注册** - Python 端直接注册工具
9+
2. **日志** - 完整的 Python 端日志支持
10+
3. **工具定义** - 完整的工具定义和管理
11+
12+
### ❌ 尚未支持 → 完全支持 (28项)
13+
1. **Chain (LCEL)** - 链式调用、管道、并行、分支
14+
2. **Memory 系统** - Buffer, Window, Summary, Vector, Entity
15+
3. **Prompt Templates** - 提示模板创建和格式化
16+
4. **Output Parsers** - JSON, Boolean, List, XML, Regex
17+
5. **Document Loaders** - Text, CSV, JSON, Markdown, Web, Directory
18+
6. **RAG Engine** - 文档索引、检索、问答
19+
7. **Code Interpreter** - C# 代码执行(通过 gRPC)
20+
8. **SharpGraph** - 图编排、节点、边、状态管理
21+
9. **DSPy Optimizer** - 自动提示词优化
22+
10. **MultiModal** - 图像支持
23+
11. **完整的 Observability** - 日志、指标、追踪
24+
25+
## 🎯 实现策略
26+
27+
### 阶段 1: 协议定义 ✅
28+
- [x] 创建完整的 `sharpaikit.proto` 文件
29+
- [x] 定义所有服务的消息类型
30+
31+
### 阶段 2: C# gRPC 服务实现
32+
- [ ] ChainService - 链服务
33+
- [ ] MemoryService - 记忆服务
34+
- [ ] RAGService - RAG 服务
35+
- [ ] GraphService - 图服务
36+
- [ ] PromptService - 提示服务
37+
- [ ] OutputParserService - 输出解析服务
38+
- [ ] DocumentLoaderService - 文档加载服务
39+
- [ ] CodeInterpreterService - 代码解释器服务
40+
- [ ] OptimizerService - 优化器服务
41+
- [ ] ToolService - 工具服务
42+
- [ ] ObservabilityService - 可观测性服务
43+
44+
### 阶段 3: Python SDK 客户端实现
45+
- [ ] Chain 客户端
46+
- [ ] Memory 客户端
47+
- [ ] RAG 客户端
48+
- [ ] Graph 客户端
49+
- [ ] Prompt 客户端
50+
- [ ] OutputParser 客户端
51+
- [ ] DocumentLoader 客户端
52+
- [ ] CodeInterpreter 客户端
53+
- [ ] Optimizer 客户端
54+
- [ ] Tool 客户端
55+
- [ ] Observability 客户端
56+
57+
### 阶段 4: 高级封装
58+
- [ ] Python 端的高级 API(类似 Agent 类)
59+
- [ ] Fluent API 支持
60+
- [ ] 上下文管理器支持
61+
62+
### 阶段 5: 文档和示例
63+
- [ ] 更新所有文档
64+
- [ ] 创建完整示例
65+
- [ ] 更新功能覆盖文档
66+
67+
## 📝 实现顺序(优先级)
68+
69+
### 高优先级(核心功能)
70+
1. **Chain Service** - 最常用的功能
71+
2. **Memory Service** - Agent 必需
72+
3. **RAG Service** - 重要功能
73+
4. **Tool Service** - 工具注册
74+
75+
### 中优先级(增强功能)
76+
5. **Prompt Service** - 提示模板
77+
6. **OutputParser Service** - 输出解析
78+
7. **DocumentLoader Service** - 文档加载
79+
8. **Graph Service** - 图编排
80+
81+
### 低优先级(高级功能)
82+
9. **CodeInterpreter Service** - C# 特有
83+
10. **Optimizer Service** - 高级优化
84+
11. **MultiModal** - 多模态(可集成到现有服务)
85+
12. **Observability Service** - 可观测性
86+
87+
## 🚀 开始实现
88+
89+
由于这是一个大工程,建议:
90+
1. 先实现核心服务(Chain, Memory, RAG)
91+
2. 然后实现增强功能
92+
3. 最后实现高级功能
93+
94+
每个服务都需要:
95+
- C# gRPC 服务实现
96+
- Python SDK 客户端
97+
- 单元测试
98+
- 使用示例
99+

python-client/INSTALL.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# SharpAIKit Python SDK Installation Guide
2+
3+
## Prerequisites
4+
5+
1. **Python 3.8+**
6+
2. **.NET 8.0 SDK** (for building the gRPC host)
7+
3. **uv** (recommended) or **pip**
8+
9+
## Installation Steps
10+
11+
### Step 1: Build the gRPC Host
12+
13+
```bash
14+
cd ../src/SharpAIKit.Grpc.Host
15+
dotnet build -c Release
16+
```
17+
18+
### Step 2: Generate Python gRPC Code
19+
20+
```bash
21+
cd ../../python-client
22+
23+
# Install grpc-tools if not already installed
24+
uv pip install grpcio-tools
25+
26+
# Generate gRPC code
27+
python generate_grpc.py
28+
```
29+
30+
This will generate:
31+
- `sharpaikit/_grpc/agent_pb2.py`
32+
- `sharpaikit/_grpc/agent_pb2_grpc.py`
33+
34+
### Step 3: Install the Python SDK
35+
36+
#### Using uv (Recommended)
37+
38+
```bash
39+
uv pip install -e .
40+
```
41+
42+
#### Using pip
43+
44+
```bash
45+
pip install -e .
46+
```
47+
48+
## Verification
49+
50+
```python
51+
from sharpaikit import Agent
52+
53+
# This will automatically start the host if needed
54+
agent = Agent(
55+
api_key="your-api-key",
56+
model="gpt-3.5-turbo",
57+
auto_start_host=True
58+
)
59+
60+
# Test connection
61+
result = agent.run("Hello")
62+
print(result.output)
63+
```
64+
65+
## Troubleshooting
66+
67+
### gRPC Code Not Generated
68+
69+
If you see `ImportError: gRPC code not generated`, run:
70+
71+
```bash
72+
python generate_grpc.py
73+
```
74+
75+
### Host Process Fails to Start
76+
77+
1. Ensure .NET 8.0 SDK is installed: `dotnet --version`
78+
2. Build the host: `cd ../src/SharpAIKit.Grpc.Host && dotnet build`
79+
3. Check port 50051 is available
80+
81+
### Connection Errors
82+
83+
1. Verify host is running: `lsof -i :50051`
84+
2. Check firewall settings
85+
3. Verify host address (default: localhost:50051)
86+

python-client/LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
MIT License
2+
3+
Copyright (c) 2025
4+
SharpAIKit Authors and Contributors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the “Software”), to deal
8+
in the Software without restriction, including without limitation the rights to:
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10+
of the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software, along with attribution to
15+
“SharpAIKit Authors and Contributors”.
16+
17+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT.
20+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21+
DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT,
22+
OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
23+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

python-client/MANIFEST.in

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
include README_PYPI.md
2+
include LICENSE
3+
recursive-include sharpaikit *.py
4+
recursive-include sharpaikit/_grpc *.py
5+
global-exclude __pycache__
6+
global-exclude *.pyc
7+
global-exclude *.pyo
8+
global-exclude .DS_Store
9+
global-exclude *.egg-info
10+
global-exclude dist
11+
global-exclude build
12+
global-exclude examples
13+
global-exclude tests
14+
global-exclude *.md
15+
global-exclude *.sh
16+
global-exclude *.toml
17+
global-exclude *.lock
18+
global-exclude Makefile
19+
global-exclude setup.py
20+
global-exclude generate_grpc.py
21+
global-exclude build.sh
22+
global-exclude publish_to_pypi.sh
23+

0 commit comments

Comments
 (0)