tamar-model-client 0.1.19__tar.gz → 0.1.20__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/PKG-INFO +217 -61
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/README.md +216 -60
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/setup.py +1 -1
- tamar_model_client-0.1.20/tamar_model_client/async_client.py +644 -0
- tamar_model_client-0.1.20/tamar_model_client/core/__init__.py +34 -0
- tamar_model_client-0.1.20/tamar_model_client/core/base_client.py +168 -0
- tamar_model_client-0.1.20/tamar_model_client/core/logging_setup.py +84 -0
- tamar_model_client-0.1.20/tamar_model_client/core/request_builder.py +221 -0
- tamar_model_client-0.1.20/tamar_model_client/core/response_handler.py +136 -0
- tamar_model_client-0.1.20/tamar_model_client/core/utils.py +171 -0
- tamar_model_client-0.1.20/tamar_model_client/error_handler.py +283 -0
- tamar_model_client-0.1.20/tamar_model_client/exceptions.py +375 -0
- tamar_model_client-0.1.20/tamar_model_client/json_formatter.py +61 -0
- tamar_model_client-0.1.20/tamar_model_client/sync_client.py +680 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client.egg-info/PKG-INFO +217 -61
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client.egg-info/SOURCES.txt +12 -1
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client.egg-info/top_level.txt +1 -0
- tamar_model_client-0.1.20/tests/__init__.py +1 -0
- tamar_model_client-0.1.20/tests/stream_hanging_analysis.py +357 -0
- tamar_model_client-0.1.20/tests/test_google_azure_final.py +448 -0
- tamar_model_client-0.1.20/tests/test_simple.py +235 -0
- tamar_model_client-0.1.19/tamar_model_client/async_client.py +0 -753
- tamar_model_client-0.1.19/tamar_model_client/exceptions.py +0 -11
- tamar_model_client-0.1.19/tamar_model_client/json_formatter.py +0 -26
- tamar_model_client-0.1.19/tamar_model_client/sync_client.py +0 -692
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/setup.cfg +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/__init__.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/auth.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/enums/__init__.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/enums/channel.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/enums/invoke.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/enums/providers.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/generated/__init__.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/generated/model_service_pb2.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/generated/model_service_pb2_grpc.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/logging_icons.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/schemas/__init__.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/schemas/inputs.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/schemas/outputs.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client/utils.py +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client.egg-info/dependency_links.txt +0 -0
- {tamar_model_client-0.1.19 → tamar_model_client-0.1.20}/tamar_model_client.egg-info/requires.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: tamar-model-client
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.20
|
4
4
|
Summary: A Python SDK for interacting with the Model Manager gRPC service
|
5
5
|
Home-page: http://gitlab.tamaredge.top/project-tap/AgentOS/model-manager-client
|
6
6
|
Author: Oscar Ou
|
@@ -30,16 +30,52 @@ Dynamic: summary
|
|
30
30
|
|
31
31
|
# Tamar Model Client
|
32
32
|
|
33
|
-
**Tamar Model Client** 是一款高性能的 Python SDK
|
34
|
-
模型服务商(如OpenAI、Google、Azure OpenAI)。
|
33
|
+
**Tamar Model Client** 是一款高性能的 Python SDK,通过 gRPC 协议连接 Model Manager 服务,为多个 AI 模型服务商提供统一的调用接口。无论您使用 OpenAI、Google、Azure 还是其他 AI 服务,都可以通过一套 API 轻松切换和管理。
|
35
34
|
|
36
|
-
##
|
35
|
+
## 🎯 为什么选择 Tamar Model Client?
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
### 您遇到过这些问题吗?
|
38
|
+
|
39
|
+
❌ 需要同时集成 OpenAI、Google、Azure 等多个 AI 服务,每个都有不同的 API?
|
40
|
+
❌ 难以统计和控制不同服务商的使用成本?
|
41
|
+
❌ 在不同 AI 提供商之间切换需要修改大量代码?
|
42
|
+
❌ 每个服务商都有自己的错误处理和重试逻辑?
|
43
|
+
|
44
|
+
### ✅ Tamar Model Client 一站式解决!
|
45
|
+
|
46
|
+
🎉 **一个 SDK,访问所有 AI 服务**
|
47
|
+
📊 **统一的使用量和成本统计**
|
48
|
+
🔄 **无缝切换,一行代码搞定**
|
49
|
+
🛡️ **生产级的错误处理和重试机制**
|
50
|
+
|
51
|
+
## ✨ 核心特性
|
52
|
+
|
53
|
+
### 🔌 多服务商支持
|
54
|
+
- **OpenAI** (GPT-3.5/4, DALL-E)
|
55
|
+
- **Google** (Gemini - AI Studio & Vertex AI)
|
56
|
+
- **Azure OpenAI** (企业级部署)
|
57
|
+
- **Anthropic** (Claude)
|
58
|
+
- **DeepSeek** (深度求索)
|
59
|
+
- **Perplexity** (搜索增强生成)
|
60
|
+
|
61
|
+
### ⚡ 灵活的调用方式
|
62
|
+
- 🧩 **同步/异步** 双模式客户端
|
63
|
+
- 📡 **流式/非流式** 响应支持
|
64
|
+
- 📦 **批量请求** 并行处理
|
65
|
+
- 🔄 **自动重试** 指数退避策略
|
66
|
+
|
67
|
+
### 🛡️ 生产级特性
|
68
|
+
- 🔐 **JWT 认证** 安全可靠
|
69
|
+
- 📊 **使用量追踪** Token 统计与成本计算
|
70
|
+
- 🆔 **请求追踪** 唯一 request_id
|
71
|
+
- ⚠️ **完善错误处理** 详细错误信息
|
72
|
+
- ✅ **类型安全** Pydantic v2 验证
|
73
|
+
|
74
|
+
### 🚀 高性能设计
|
75
|
+
- 🔗 **gRPC 通信** HTTP/2 长连接
|
76
|
+
- ♻️ **连接复用** 减少握手开销
|
77
|
+
- 🎯 **智能路由** 自动选择最优通道
|
78
|
+
- 📈 **性能监控** 延迟与吞吐量指标
|
43
79
|
|
44
80
|
## 📋 安装
|
45
81
|
|
@@ -47,41 +83,44 @@ Dynamic: summary
|
|
47
83
|
pip install tamar-model-client
|
48
84
|
```
|
49
85
|
|
50
|
-
|
86
|
+
### 系统要求
|
51
87
|
|
52
88
|
- Python ≥ 3.8
|
53
|
-
- Windows / Linux / macOS
|
89
|
+
- 支持 Windows / Linux / macOS
|
90
|
+
- 依赖项会自动安装(grpcio, pydantic, python-dotenv 等)
|
54
91
|
|
55
|
-
## 🏗️
|
92
|
+
## 🏗️ 项目架构
|
56
93
|
|
57
94
|
```
|
58
95
|
tamar_model_client/
|
59
|
-
├── generated/
|
60
|
-
│ ├── model_service.proto
|
61
|
-
│
|
62
|
-
|
63
|
-
├──
|
64
|
-
│
|
65
|
-
|
66
|
-
├──
|
67
|
-
│ ├──
|
68
|
-
│
|
69
|
-
|
70
|
-
├── async_client.py
|
71
|
-
├──
|
72
|
-
├──
|
73
|
-
|
74
|
-
└── __init__.py # 包初始化
|
96
|
+
├── 📁 generated/ # gRPC 生成的代码
|
97
|
+
│ ├── model_service.proto # Protocol Buffer 定义
|
98
|
+
│ └── *_pb2*.py # 生成的 Python 代码
|
99
|
+
├── 📁 schemas/ # Pydantic 数据模型
|
100
|
+
│ ├── inputs.py # 请求模型(ModelRequest, UserContext)
|
101
|
+
│ └── outputs.py # 响应模型(ModelResponse, Usage)
|
102
|
+
├── 📁 enums/ # 枚举定义
|
103
|
+
│ ├── providers.py # AI 服务商(OpenAI, Google, Azure...)
|
104
|
+
│ ├── invoke.py # 调用类型(generation, images...)
|
105
|
+
│ └── channel.py # 服务通道(openai, vertexai...)
|
106
|
+
├── 📄 sync_client.py # 同步客户端 TamarModelClient
|
107
|
+
├── 📄 async_client.py # 异步客户端 AsyncTamarModelClient
|
108
|
+
├── 📄 exceptions.py # 异常层级定义
|
109
|
+
├── 📄 auth.py # JWT 认证管理
|
110
|
+
└── 📄 utils.py # 工具函数
|
75
111
|
```
|
76
112
|
|
77
113
|
## 🚀 快速开始
|
78
114
|
|
79
|
-
### 客户端初始化
|
115
|
+
### 1️⃣ 客户端初始化
|
80
116
|
|
81
117
|
```python
|
82
118
|
from tamar_model_client import TamarModelClient, AsyncTamarModelClient
|
83
119
|
|
84
|
-
#
|
120
|
+
# 方式一:使用环境变量(推荐)
|
121
|
+
client = TamarModelClient() # 自动读取环境变量配置
|
122
|
+
|
123
|
+
# 方式二:代码配置
|
85
124
|
client = TamarModelClient(
|
86
125
|
server_address="localhost:50051",
|
87
126
|
jwt_token="your-jwt-token"
|
@@ -90,16 +129,43 @@ client = TamarModelClient(
|
|
90
129
|
# 异步客户端
|
91
130
|
async_client = AsyncTamarModelClient(
|
92
131
|
server_address="localhost:50051",
|
93
|
-
jwt_secret_key="your-jwt-secret-key" #
|
132
|
+
jwt_secret_key="your-jwt-secret-key" # 使用密钥自动生成 JWT
|
94
133
|
)
|
95
134
|
```
|
96
135
|
|
97
|
-
|
136
|
+
### 2️⃣ 基础示例 - 与 AI 对话
|
137
|
+
|
138
|
+
```python
|
139
|
+
from tamar_model_client import TamarModelClient
|
140
|
+
from tamar_model_client.schemas import ModelRequest, UserContext
|
141
|
+
from tamar_model_client.enums import ProviderType
|
142
|
+
|
143
|
+
# 创建客户端
|
144
|
+
client = TamarModelClient()
|
145
|
+
|
146
|
+
# 构建请求
|
147
|
+
request = ModelRequest(
|
148
|
+
provider=ProviderType.OPENAI,
|
149
|
+
model="gpt-3.5-turbo",
|
150
|
+
messages=[
|
151
|
+
{"role": "user", "content": "你好,请介绍一下你自己。"}
|
152
|
+
],
|
153
|
+
user_context=UserContext(
|
154
|
+
user_id="test_user",
|
155
|
+
org_id="test_org",
|
156
|
+
client_type="python-sdk"
|
157
|
+
)
|
158
|
+
)
|
159
|
+
|
160
|
+
# 发送请求
|
161
|
+
response = client.invoke(request)
|
162
|
+
print(f"AI 回复: {response.content}")
|
163
|
+
```
|
98
164
|
|
99
165
|
|
100
|
-
##
|
166
|
+
## 📚 详细使用示例
|
101
167
|
|
102
|
-
|
168
|
+
### OpenAI 调用示例
|
103
169
|
|
104
170
|
```python
|
105
171
|
from tamar_model_client import TamarModelClient
|
@@ -138,7 +204,7 @@ else:
|
|
138
204
|
print(f"Token 使用情况: {response.usage}")
|
139
205
|
```
|
140
206
|
|
141
|
-
|
207
|
+
### Google 调用示例 (AI Studio / Vertex AI)
|
142
208
|
|
143
209
|
```python
|
144
210
|
from tamar_model_client import TamarModelClient
|
@@ -201,7 +267,7 @@ else:
|
|
201
267
|
print(f"Token 使用情况: {vertex_response.usage}")
|
202
268
|
```
|
203
269
|
|
204
|
-
|
270
|
+
### Azure OpenAI 调用示例
|
205
271
|
|
206
272
|
```python
|
207
273
|
from tamar_model_client import TamarModelClient
|
@@ -430,19 +496,57 @@ response = client.invoke(
|
|
430
496
|
)
|
431
497
|
```
|
432
498
|
|
499
|
+
## 🛠️ 高级功能
|
500
|
+
|
501
|
+
### 🔥 使用场景和最佳实践
|
502
|
+
|
503
|
+
#### 使用场景
|
504
|
+
1. **多模型比较**:同时调用多个服务商的模型,比较输出质量
|
505
|
+
2. **成本优化**:根据任务类型自动选择性价比最高的模型
|
506
|
+
3. **高可用架构**:主备模型自动切换,确保服务稳定
|
507
|
+
4. **统一监控**:集中管理所有 AI 服务的使用量和成本
|
508
|
+
|
509
|
+
#### 最佳实践
|
510
|
+
1. **客户端管理**
|
511
|
+
```python
|
512
|
+
# ✅ 推荐:单例模式使用
|
513
|
+
client = TamarModelClient()
|
514
|
+
# 整个应用生命周期使用同一个客户端
|
515
|
+
|
516
|
+
# ❌ 避免:频繁创建客户端
|
517
|
+
for i in range(100):
|
518
|
+
client = TamarModelClient() # 不推荐!
|
519
|
+
```
|
520
|
+
|
521
|
+
2. **错误处理**
|
522
|
+
```python
|
523
|
+
try:
|
524
|
+
response = client.invoke(request)
|
525
|
+
except TamarModelException as e:
|
526
|
+
logger.error(f"Model error: {e.message}, request_id: {e.request_id}")
|
527
|
+
# 实施降级策略或重试
|
528
|
+
```
|
529
|
+
|
530
|
+
3. **性能优化**
|
531
|
+
- 使用批量 API 处理大量请求
|
532
|
+
- 启用流式响应减少首字延迟
|
533
|
+
- 合理设置 max_tokens 避免浪费
|
534
|
+
|
433
535
|
### ⚠️ 注意事项
|
434
536
|
|
435
|
-
|
537
|
+
1. **参数说明**
|
538
|
+
- **必填参数**:`provider`, `model`, `user_context`
|
539
|
+
- **可选参数**:`channel`, `invoke_type`(系统可自动推断)
|
540
|
+
- **流式控制**:通过 `stream=True/False` 参数控制
|
541
|
+
|
542
|
+
2. **连接管理**
|
543
|
+
- gRPC 使用 HTTP/2 长连接,客户端应作为单例使用
|
544
|
+
- 如需多实例,务必调用 `client.close()` 释放资源
|
436
545
|
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
- **客户端连接管理**
|
442
|
-
- gRPC 使用 HTTP/2 长连接,**建议将客户端实例作为单例使用**
|
443
|
-
- 若需创建多个实例,**请务必调用** `client.close()` **方法手动关闭连接**,以防止连接堆积或资源泄露
|
444
|
-
- **异常处理**:
|
445
|
-
- 所有接口均提供详细的**错误信息** 以及 **请求ID(request_id)**,业务调用时建议纳入对应日志便于后期排错
|
546
|
+
3. **错误处理**
|
547
|
+
- 所有错误包含 `request_id` 用于问题追踪
|
548
|
+
- 网络错误会自动重试(指数退避)
|
549
|
+
- 提供商错误保留原始错误信息
|
446
550
|
|
447
551
|
## ⚙️ 环境变量配置(推荐)
|
448
552
|
|
@@ -501,12 +605,17 @@ from tamar_model_client import TamarModelClient
|
|
501
605
|
client = TamarModelClient() # 将使用环境变量中的配置
|
502
606
|
```
|
503
607
|
|
504
|
-
##
|
608
|
+
## 🔧 开发指南
|
505
609
|
|
506
610
|
### 环境设置
|
507
611
|
|
508
|
-
1.
|
612
|
+
1. **克隆仓库**
|
613
|
+
```bash
|
614
|
+
git clone https://github.com/your-org/tamar-model-client.git
|
615
|
+
cd tamar-model-client
|
616
|
+
```
|
509
617
|
|
618
|
+
2. **创建虚拟环境**
|
510
619
|
```bash
|
511
620
|
python -m venv .venv
|
512
621
|
source .venv/bin/activate # Linux/macOS
|
@@ -514,35 +623,82 @@ source .venv/bin/activate # Linux/macOS
|
|
514
623
|
.venv\Scripts\activate # Windows
|
515
624
|
```
|
516
625
|
|
517
|
-
|
518
|
-
|
626
|
+
3. **安装开发依赖**
|
519
627
|
```bash
|
520
628
|
pip install -e .
|
629
|
+
pip install -r requirements-dev.txt # 如果有开发依赖
|
521
630
|
```
|
522
631
|
|
523
|
-
###
|
524
|
-
|
525
|
-
运行以下命令生成 gRPC 相关代码:
|
632
|
+
### 代码生成
|
526
633
|
|
634
|
+
如果需要更新 gRPC 定义:
|
527
635
|
```bash
|
636
|
+
# 生成 gRPC 代码
|
528
637
|
python make_grpc.py
|
638
|
+
|
639
|
+
# 验证生成的代码
|
640
|
+
python -m pytest tests/
|
529
641
|
```
|
530
642
|
|
531
|
-
###
|
643
|
+
### 发布流程
|
644
|
+
|
532
645
|
```bash
|
646
|
+
# 1. 更新版本号 (setup.py)
|
647
|
+
# 2. 构建包
|
533
648
|
python setup.py sdist bdist_wheel
|
534
|
-
twine upload dist/*
|
535
649
|
|
650
|
+
# 3. 检查构建
|
651
|
+
twine check dist/*
|
652
|
+
|
653
|
+
# 4. 上传到 PyPI
|
654
|
+
twine upload dist/*
|
536
655
|
```
|
537
656
|
|
538
|
-
|
657
|
+
### 贡献指南
|
658
|
+
|
659
|
+
1. Fork 项目
|
660
|
+
2. 创建功能分支 (`git checkout -b feature/amazing-feature`)
|
661
|
+
3. 提交更改 (`git commit -m 'Add amazing feature'`)
|
662
|
+
4. 推送到分支 (`git push origin feature/amazing-feature`)
|
663
|
+
5. 创建 Pull Request
|
664
|
+
|
665
|
+
## 📊 性能指标
|
666
|
+
|
667
|
+
- **响应延迟**: 平均 < 10ms(gRPC 开销)
|
668
|
+
- **并发支持**: 1000+ 并发请求
|
669
|
+
- **连接复用**: HTTP/2 多路复用
|
670
|
+
- **自动重试**: 指数退避,最多 5 次
|
671
|
+
|
672
|
+
## 🤝 支持与贡献
|
673
|
+
|
674
|
+
### 获取帮助
|
675
|
+
|
676
|
+
- 📖 [API 文档](https://docs.tamar-model-client.com)
|
677
|
+
- 🐛 [提交 Issue](https://github.com/your-org/tamar-model-client/issues)
|
678
|
+
- 💬 [讨论区](https://github.com/your-org/tamar-model-client/discussions)
|
679
|
+
- 📝 [更新日志](CHANGELOG.md)
|
680
|
+
|
681
|
+
### 相关项目
|
682
|
+
|
683
|
+
- [Model Manager Server](https://github.com/your-org/model-manager) - 后端 gRPC 服务
|
684
|
+
- [Model Manager Dashboard](https://github.com/your-org/model-manager-dashboard) - 管理控制台
|
685
|
+
|
686
|
+
## 📜 许可证
|
539
687
|
|
540
|
-
MIT
|
688
|
+
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件
|
541
689
|
|
542
|
-
##
|
690
|
+
## 👥 团队
|
543
691
|
|
544
|
-
- Oscar Ou (oscar.ou@tamaredge.ai)
|
692
|
+
- **Oscar Ou** - 项目负责人 - [oscar.ou@tamaredge.ai](mailto:oscar.ou@tamaredge.ai)
|
693
|
+
- [贡献者列表](https://github.com/your-org/tamar-model-client/graphs/contributors)
|
545
694
|
|
546
|
-
|
695
|
+
---
|
547
696
|
|
548
|
-
|
697
|
+
<div align="center">
|
698
|
+
<p>
|
699
|
+
<b>⭐ 如果这个项目对您有帮助,请给个 Star 支持我们!⭐</b>
|
700
|
+
</p>
|
701
|
+
<p>
|
702
|
+
Made with ❤️ by Tamar Edge Team
|
703
|
+
</p>
|
704
|
+
</div>
|