SimpleLLMFunc 0.2.7__tar.gz → 0.2.9__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.
Files changed (26) hide show
  1. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/PKG-INFO +141 -14
  2. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/README.md +140 -13
  3. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/llm_decorator/utils.py +355 -104
  4. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/tool/tool.py +54 -3
  5. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/pyproject.toml +1 -1
  6. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/LICENSE +0 -0
  7. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/.DS_Store +0 -0
  8. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/__init__.py +0 -0
  9. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/config.py +0 -0
  10. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/interface/__init__.py +0 -0
  11. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/interface/key_pool.py +0 -0
  12. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/interface/llm_interface.py +0 -0
  13. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/interface/openai_compatible.py +0 -0
  14. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/interface/token_bucket.py +0 -0
  15. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/llm_decorator/__init__.py +0 -0
  16. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/llm_decorator/llm_chat_decorator.py +0 -0
  17. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/llm_decorator/llm_function_decorator.py +0 -0
  18. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/llm_decorator/multimodal_types.py +0 -0
  19. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/logger/__init__.py +0 -0
  20. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/logger/logger.py +0 -0
  21. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/logger/logger_config.py +0 -0
  22. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/tool/__init__.py +0 -0
  23. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/tool/agent.log +0 -0
  24. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/tool/log_indices/trace_index.json +0 -0
  25. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/type/__init__.py +0 -0
  26. {simplellmfunc-0.2.7 → simplellmfunc-0.2.9}/SimpleLLMFunc/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: SimpleLLMFunc
3
- Version: 0.2.7
3
+ Version: 0.2.9
4
4
  Summary: 一个轻量但完备的LLM/Agent应用开发框架,提供装饰器实现将函数DocString作为Prompt而无需函数体具体实现但能够享受函数定义和类型标注带来效率提升的开发体验。以最Code的方式,用最少的代码将LLM能力集成到任意Python项目中。
5
5
  Author: Ni Jingzhe
6
6
  Author-email: nijingzhe@zju.edu.cn
@@ -22,7 +22,7 @@ Description-Content-Type: text/markdown
22
22
  ![SimpleLLMFunc](https://github.com/NiJingzhe/SimpleLLMFunc/blob/master/img/repocover_new.png?raw=true)
23
23
 
24
24
  <center>
25
- <h2 style="font-size:2em;">Everything is Function, Prompt is Code</h2>
25
+ <h2 style="font-size:2em;">LLM as Function, Prompt as Code</h2>
26
26
  </center>
27
27
 
28
28
  ----
@@ -36,7 +36,7 @@ Description-Content-Type: text/markdown
36
36
  [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/NiJingzhe/SimpleLLMFunc/graphs/commit-activity)
37
37
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/NiJingzhe/SimpleLLMFunc/pulls)
38
38
 
39
- ### 更新说明
39
+ ### 更新说明 (0.2.9 Latest)
40
40
  ### Look here: [Change Log](https://github.com/NiJingzhe/SimpleLLMFunc/blob/master/CHANGELOG.md)
41
41
 
42
42
  ### 文档(ReadtheDoc)
@@ -70,8 +70,11 @@ Prompt会以DocString的形式存在,一方面强制你撰写良好的函数
70
70
  ## 特性
71
71
 
72
72
  - **LLM函数装饰器**:简化LLM调用,支持类型安全的函数定义和返回值处理(但是小模型有很大概率无法输出正确的json格式)
73
+ - **异步支持**:提供 `async_llm_function` 和 `async_llm_chat` 装饰器,支持原生异步调用
74
+ - **多模态支持**:支持文本、图片URL和本地图片路径的多模态输入处理
73
75
  - **通用模型接口**:支持任何符合OpenAI API格式的模型服务,无需针对每个供应商开发专门实现
74
76
  - **API密钥管理**:自动化API密钥负载均衡,优化资源利用
77
+ - **流量控制**:集成令牌桶算法,实现智能流量平滑
75
78
  - **结构化输出**:使用Pydantic模型定义结构化返回类型
76
79
  - **强大的日志系统**:支持trace_id跟踪和搜索,方便调试和监控,即将支持token用量统计
77
80
  - **工具系统**:支持Agent与外部环境交互,易于扩展
@@ -84,19 +87,26 @@ SimpleLLMFunc/
84
87
  │ ├── interface/ # LLM 接口
85
88
  │ │ ├── llm_interface.py # LLM 接口抽象类
86
89
  │ │ ├── key_pool.py # API 密钥管理
87
- │ │ └── openai_compatible.py # OpenAI Compatible 通用接口实现
88
- ├── llm_function/ # LLM函数装饰器
90
+ │ │ ├── openai_compatible.py # OpenAI Compatible 通用接口实现
91
+ │ └── token_bucket.py # 流量控制令牌桶实现
92
+ │ ├── llm_decorator/ # LLM装饰器
89
93
  │ │ ├── llm_chat_decorator.py # 对话函数装饰器实现
90
- │ │ └── llm_function_decorator.py # 无状态函数装饰器实现
94
+ │ │ ├── llm_function_decorator.py # 无状态函数装饰器实现
95
+ │ │ ├── multimodal_types.py # 多模态类型定义
96
+ │ │ └── utils.py # 装饰器工具函数
91
97
  │ ├── logger/ # 日志系统
92
98
  │ │ ├── logger.py # 日志核心实现
93
99
  │ │ └── logger_config.py # 日志配置
94
100
  │ ├── tool/ # 工具系统
95
101
  │ │ └── tool.py # 工具基类和工具函数装饰器定义
96
- └── config.py # 全局配置
102
+ ├── type/ # 类型定义
103
+ │ │ └── __init__.py # 多模态类型导出
104
+ │ ├── config.py # 全局配置
105
+ │ └── utils.py # 通用工具函数
97
106
  └── examples/ # 示例代码
98
107
  ├── llm_function_example.py # LLM函数示例
99
108
  ├── llm_chat_example.py # 对话函数示例
109
+ ├── async_llm_func.py # 异步LLM函数示例
100
110
  └── simple_manus.py # 包含多种工具和对话函数的综合示例
101
111
  ```
102
112
  ## 配置管理
@@ -130,6 +140,10 @@ SimpleLLMFunc的核心理念是 **"Everything is Function, Prompt is Code"**。
130
140
  """
131
141
  使用LLM函数装饰器的示例
132
142
  """
143
+ from typing import List
144
+ from pydantic import BaseModel, Field
145
+ from SimpleLLMFunc import llm_function, OpenAICompatible, app_log
146
+
133
147
  # 定义一个Pydantic模型作为返回类型
134
148
  class ProductReview(BaseModel):
135
149
  rating: int = Field(..., description="产品评分,1-5分")
@@ -225,9 +239,72 @@ Output:
225
239
 
226
240
  这个例子实现了一些工具和一个对话函数,能够实现代码专精的Manus类似物
227
241
 
242
+ - ### 异步装饰器支持
243
+
244
+ SimpleLLMFunc 提供了完整的异步支持,包括 `async_llm_function` 和 `async_llm_chat` 装饰器:
245
+
246
+ ```python
247
+ from SimpleLLMFunc import async_llm_function, async_llm_chat
248
+
249
+ # 异步LLM函数
250
+ @async_llm_function(llm_interface=my_llm_interface)
251
+ async def async_analyze_text(text: str) -> str:
252
+ """异步分析文本内容"""
253
+ pass
254
+
255
+ # 异步对话函数
256
+ @async_llm_chat(llm_interface=my_llm_interface, stream=True)
257
+ async def async_chat(message: str, history: List[Dict[str, str]]) -> AsyncGenerator[Tuple[str, List[Dict[str, str]]], None]:
258
+ """异步对话功能,支持流式响应"""
259
+ pass
260
+
261
+ # 使用示例
262
+ async def main():
263
+ result = await async_analyze_text("需要分析的文本")
264
+
265
+ async for response, updated_history in async_chat("你好", []):
266
+ print(response)
267
+ ```
268
+
269
+ - ### 多模态支持
270
+
271
+ SimpleLLMFunc 支持多模态输入,可以处理文本、图片URL和本地图片:
272
+
273
+ ```python
274
+ from SimpleLLMFunc import llm_function
275
+ from SimpleLLMFunc.type import Text, ImgUrl, ImgPath
276
+
277
+ @llm_function(llm_interface=my_llm_interface)
278
+ def analyze_image(
279
+ description: Text, # 文本描述
280
+ web_image: ImgUrl, # 网络图片URL
281
+ local_image: ImgPath # 本地图片路径
282
+ ) -> str:
283
+ """分析图像并根据描述提供详细说明
284
+
285
+ Args:
286
+ description: 对图像分析的具体要求
287
+ web_image: 要分析的网络图片URL
288
+ local_image: 要对比的本地参考图片路径
289
+
290
+ Returns:
291
+ 详细的图像分析结果
292
+ """
293
+ pass
294
+
295
+ # 使用示例
296
+ result = analyze_image(
297
+ description=Text("请详细描述这两张图片的区别"),
298
+ web_image=ImgUrl("https://example.com/image.jpg"),
299
+ local_image=ImgPath("./reference.jpg")
300
+ )
301
+ ```
302
+
228
303
  ### 装饰器特性
229
304
 
230
305
  - **类型安全**:根据函数签名自动识别参数和返回类型
306
+ - **异步支持**:提供 `async_llm_function` 和 `async_llm_chat` 装饰器,支持原生异步调用
307
+ - **多模态处理**:支持 `Text`、`ImgUrl`、`ImgPath` 类型的多模态输入
231
308
  - **Pydantic集成**:支持Pydantic模型作为返回类型,确保结果符合预定义结构,对于能力较弱的模型有较大概率在自动重试后也无法输出正确的json格式
232
309
  - **提示词自动构建**:基于函数文档和类型标注自动构建提示词
233
310
 
@@ -372,7 +449,47 @@ def get_weather(location: Location, days: int = 1) -> dict:
372
449
  - 直接使用Python原生类型和Pydantic模型进行参数标注
373
450
  - 自动从函数签名和文档字符串提取参数信息
374
451
  - 装饰后的函数仍可直接调用,便于测试
375
- - 当然,任何`llm_function`或者`llm_chat`装饰的函数,也可以接着被`tool`装饰器装饰以变成“智能工具”
452
+ - **支持多模态返回**:工具可以返回 `ImgPath`(本地图片)或 `ImgUrl`(网络图片),实现多模态工具调用
453
+ - 当然,任何`llm_function`或者`llm_chat`装饰的函数,也可以接着被`tool`装饰器装饰以变成"智能工具"
454
+
455
+ ### 多模态工具示例
456
+
457
+ ```python
458
+ from SimpleLLMFunc.tool import tool
459
+ from SimpleLLMFunc.type import ImgPath, ImgUrl
460
+
461
+ @tool(name="generate_chart", description="根据数据生成图表")
462
+ def generate_chart(data: str, chart_type: str = "bar") -> ImgPath:
463
+ """
464
+ 根据提供的数据生成图表
465
+
466
+ Args:
467
+ data: CSV格式的数据
468
+ chart_type: 图表类型,默认为柱状图
469
+
470
+ Returns:
471
+ 生成的图表文件路径
472
+ """
473
+ # 实际实现会生成图表并保存到本地
474
+ chart_path = "./generated_chart.png"
475
+ # ... 图表生成逻辑
476
+ return ImgPath(chart_path)
477
+
478
+ @tool(name="search_web_image", description="搜索网络图片")
479
+ def search_web_image(query: str) -> ImgUrl:
480
+ """
481
+ 搜索网络图片
482
+
483
+ Args:
484
+ query: 搜索关键词
485
+
486
+ Returns:
487
+ 找到的图片URL
488
+ """
489
+ # 实际实现会调用图片搜索API
490
+ image_url = "https://example.com/search_result.jpg"
491
+ return ImgUrl(image_url)
492
+ ```
376
493
 
377
494
  ### 类继承方式(向后兼容)
378
495
 
@@ -408,11 +525,10 @@ class WebSearchTool(Tool):
408
525
  使用装饰器方式定义的工具可以直接传递给LLM函数装饰器:
409
526
 
410
527
  ```python
411
- from SimpleLLMFunc.llm_decorator import llm_function
412
- from SimpleLLMFunc.interface import ZhipuAI_glm_4_flash_Interface
528
+ from SimpleLLMFunc import llm_function
413
529
 
414
530
  @llm_function(
415
- llm_interface=ZhipuAI_glm_4_flash_Interface,
531
+ llm_interface=my_llm_interface,
416
532
  toolkit=[get_weather, search_web], # 直接传递被@tool装饰的函数
417
533
  )
418
534
  def answer_with_tools(question: str) -> str:
@@ -432,7 +548,7 @@ def answer_with_tools(question: str) -> str:
432
548
 
433
549
  ```python
434
550
  @llm_function(
435
- llm_interface=ZhipuAI_glm_4_flash_Interface,
551
+ llm_interface=my_llm_interface,
436
552
  toolkit=[get_weather, WebSearchTool()], # 混合使用两种方式定义的工具
437
553
  )
438
554
  def answer_with_mixed_tools(question: str) -> str:
@@ -442,12 +558,23 @@ def answer_with_mixed_tools(question: str) -> str:
442
558
 
443
559
  ## API密钥管理
444
560
 
445
- SimpleLLMFunc使用`APIKeyPool`类通过小根堆管理多个API密钥,实现负载均衡:
561
+ SimpleLLMFunc提供了完善的API密钥和流量管理机制:
562
+
563
+ ### API密钥负载均衡
564
+ 使用`APIKeyPool`类通过小根堆管理多个API密钥,实现负载均衡:
446
565
 
447
566
  - 自动选择最少负载的API密钥
448
567
  - 单例模式确保每个提供商只有一个密钥池,密钥池使用小根堆来进行负载均衡,每次取出load最低的KEY
449
568
  - 自动跟踪每个密钥的使用情况
450
569
 
570
+ ### 流量控制
571
+ 集成了令牌桶算法(TokenBucket)实现智能流量平滑:
572
+
573
+ - 防止API调用频率过高触发限制
574
+ - 支持突发流量的缓冲处理
575
+ - 可在`provider.json`中配置每个模型的流量控制参数
576
+ - 与API密钥池协同工作,提供更稳定的服务
577
+
451
578
  ## 安装和使用
452
579
 
453
580
  ### 1. 源码安装
@@ -481,7 +608,7 @@ pip install SimpleLLMFunc
481
608
  month = {June},
482
609
  title = {{SimpleLLMFunc: A New Approach to Build LLM Applications}},
483
610
  url = {https://github.com/NiJingzhe/SimpleLLMFunc},
484
- version = {0.2.1},
611
+ version = {0.2.9},
485
612
  year = {2025}
486
613
  }
487
614
  ```
@@ -1,7 +1,7 @@
1
1
  ![SimpleLLMFunc](https://github.com/NiJingzhe/SimpleLLMFunc/blob/master/img/repocover_new.png?raw=true)
2
2
 
3
3
  <center>
4
- <h2 style="font-size:2em;">Everything is Function, Prompt is Code</h2>
4
+ <h2 style="font-size:2em;">LLM as Function, Prompt as Code</h2>
5
5
  </center>
6
6
 
7
7
  ----
@@ -15,7 +15,7 @@
15
15
  [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/NiJingzhe/SimpleLLMFunc/graphs/commit-activity)
16
16
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/NiJingzhe/SimpleLLMFunc/pulls)
17
17
 
18
- ### 更新说明
18
+ ### 更新说明 (0.2.9 Latest)
19
19
  ### Look here: [Change Log](https://github.com/NiJingzhe/SimpleLLMFunc/blob/master/CHANGELOG.md)
20
20
 
21
21
  ### 文档(ReadtheDoc)
@@ -49,8 +49,11 @@ Prompt会以DocString的形式存在,一方面强制你撰写良好的函数
49
49
  ## 特性
50
50
 
51
51
  - **LLM函数装饰器**:简化LLM调用,支持类型安全的函数定义和返回值处理(但是小模型有很大概率无法输出正确的json格式)
52
+ - **异步支持**:提供 `async_llm_function` 和 `async_llm_chat` 装饰器,支持原生异步调用
53
+ - **多模态支持**:支持文本、图片URL和本地图片路径的多模态输入处理
52
54
  - **通用模型接口**:支持任何符合OpenAI API格式的模型服务,无需针对每个供应商开发专门实现
53
55
  - **API密钥管理**:自动化API密钥负载均衡,优化资源利用
56
+ - **流量控制**:集成令牌桶算法,实现智能流量平滑
54
57
  - **结构化输出**:使用Pydantic模型定义结构化返回类型
55
58
  - **强大的日志系统**:支持trace_id跟踪和搜索,方便调试和监控,即将支持token用量统计
56
59
  - **工具系统**:支持Agent与外部环境交互,易于扩展
@@ -63,19 +66,26 @@ SimpleLLMFunc/
63
66
  │ ├── interface/ # LLM 接口
64
67
  │ │ ├── llm_interface.py # LLM 接口抽象类
65
68
  │ │ ├── key_pool.py # API 密钥管理
66
- │ │ └── openai_compatible.py # OpenAI Compatible 通用接口实现
67
- ├── llm_function/ # LLM函数装饰器
69
+ │ │ ├── openai_compatible.py # OpenAI Compatible 通用接口实现
70
+ │ └── token_bucket.py # 流量控制令牌桶实现
71
+ │ ├── llm_decorator/ # LLM装饰器
68
72
  │ │ ├── llm_chat_decorator.py # 对话函数装饰器实现
69
- │ │ └── llm_function_decorator.py # 无状态函数装饰器实现
73
+ │ │ ├── llm_function_decorator.py # 无状态函数装饰器实现
74
+ │ │ ├── multimodal_types.py # 多模态类型定义
75
+ │ │ └── utils.py # 装饰器工具函数
70
76
  │ ├── logger/ # 日志系统
71
77
  │ │ ├── logger.py # 日志核心实现
72
78
  │ │ └── logger_config.py # 日志配置
73
79
  │ ├── tool/ # 工具系统
74
80
  │ │ └── tool.py # 工具基类和工具函数装饰器定义
75
- └── config.py # 全局配置
81
+ ├── type/ # 类型定义
82
+ │ │ └── __init__.py # 多模态类型导出
83
+ │ ├── config.py # 全局配置
84
+ │ └── utils.py # 通用工具函数
76
85
  └── examples/ # 示例代码
77
86
  ├── llm_function_example.py # LLM函数示例
78
87
  ├── llm_chat_example.py # 对话函数示例
88
+ ├── async_llm_func.py # 异步LLM函数示例
79
89
  └── simple_manus.py # 包含多种工具和对话函数的综合示例
80
90
  ```
81
91
  ## 配置管理
@@ -109,6 +119,10 @@ SimpleLLMFunc的核心理念是 **"Everything is Function, Prompt is Code"**。
109
119
  """
110
120
  使用LLM函数装饰器的示例
111
121
  """
122
+ from typing import List
123
+ from pydantic import BaseModel, Field
124
+ from SimpleLLMFunc import llm_function, OpenAICompatible, app_log
125
+
112
126
  # 定义一个Pydantic模型作为返回类型
113
127
  class ProductReview(BaseModel):
114
128
  rating: int = Field(..., description="产品评分,1-5分")
@@ -204,9 +218,72 @@ Output:
204
218
 
205
219
  这个例子实现了一些工具和一个对话函数,能够实现代码专精的Manus类似物
206
220
 
221
+ - ### 异步装饰器支持
222
+
223
+ SimpleLLMFunc 提供了完整的异步支持,包括 `async_llm_function` 和 `async_llm_chat` 装饰器:
224
+
225
+ ```python
226
+ from SimpleLLMFunc import async_llm_function, async_llm_chat
227
+
228
+ # 异步LLM函数
229
+ @async_llm_function(llm_interface=my_llm_interface)
230
+ async def async_analyze_text(text: str) -> str:
231
+ """异步分析文本内容"""
232
+ pass
233
+
234
+ # 异步对话函数
235
+ @async_llm_chat(llm_interface=my_llm_interface, stream=True)
236
+ async def async_chat(message: str, history: List[Dict[str, str]]) -> AsyncGenerator[Tuple[str, List[Dict[str, str]]], None]:
237
+ """异步对话功能,支持流式响应"""
238
+ pass
239
+
240
+ # 使用示例
241
+ async def main():
242
+ result = await async_analyze_text("需要分析的文本")
243
+
244
+ async for response, updated_history in async_chat("你好", []):
245
+ print(response)
246
+ ```
247
+
248
+ - ### 多模态支持
249
+
250
+ SimpleLLMFunc 支持多模态输入,可以处理文本、图片URL和本地图片:
251
+
252
+ ```python
253
+ from SimpleLLMFunc import llm_function
254
+ from SimpleLLMFunc.type import Text, ImgUrl, ImgPath
255
+
256
+ @llm_function(llm_interface=my_llm_interface)
257
+ def analyze_image(
258
+ description: Text, # 文本描述
259
+ web_image: ImgUrl, # 网络图片URL
260
+ local_image: ImgPath # 本地图片路径
261
+ ) -> str:
262
+ """分析图像并根据描述提供详细说明
263
+
264
+ Args:
265
+ description: 对图像分析的具体要求
266
+ web_image: 要分析的网络图片URL
267
+ local_image: 要对比的本地参考图片路径
268
+
269
+ Returns:
270
+ 详细的图像分析结果
271
+ """
272
+ pass
273
+
274
+ # 使用示例
275
+ result = analyze_image(
276
+ description=Text("请详细描述这两张图片的区别"),
277
+ web_image=ImgUrl("https://example.com/image.jpg"),
278
+ local_image=ImgPath("./reference.jpg")
279
+ )
280
+ ```
281
+
207
282
  ### 装饰器特性
208
283
 
209
284
  - **类型安全**:根据函数签名自动识别参数和返回类型
285
+ - **异步支持**:提供 `async_llm_function` 和 `async_llm_chat` 装饰器,支持原生异步调用
286
+ - **多模态处理**:支持 `Text`、`ImgUrl`、`ImgPath` 类型的多模态输入
210
287
  - **Pydantic集成**:支持Pydantic模型作为返回类型,确保结果符合预定义结构,对于能力较弱的模型有较大概率在自动重试后也无法输出正确的json格式
211
288
  - **提示词自动构建**:基于函数文档和类型标注自动构建提示词
212
289
 
@@ -351,7 +428,47 @@ def get_weather(location: Location, days: int = 1) -> dict:
351
428
  - 直接使用Python原生类型和Pydantic模型进行参数标注
352
429
  - 自动从函数签名和文档字符串提取参数信息
353
430
  - 装饰后的函数仍可直接调用,便于测试
354
- - 当然,任何`llm_function`或者`llm_chat`装饰的函数,也可以接着被`tool`装饰器装饰以变成“智能工具”
431
+ - **支持多模态返回**:工具可以返回 `ImgPath`(本地图片)或 `ImgUrl`(网络图片),实现多模态工具调用
432
+ - 当然,任何`llm_function`或者`llm_chat`装饰的函数,也可以接着被`tool`装饰器装饰以变成"智能工具"
433
+
434
+ ### 多模态工具示例
435
+
436
+ ```python
437
+ from SimpleLLMFunc.tool import tool
438
+ from SimpleLLMFunc.type import ImgPath, ImgUrl
439
+
440
+ @tool(name="generate_chart", description="根据数据生成图表")
441
+ def generate_chart(data: str, chart_type: str = "bar") -> ImgPath:
442
+ """
443
+ 根据提供的数据生成图表
444
+
445
+ Args:
446
+ data: CSV格式的数据
447
+ chart_type: 图表类型,默认为柱状图
448
+
449
+ Returns:
450
+ 生成的图表文件路径
451
+ """
452
+ # 实际实现会生成图表并保存到本地
453
+ chart_path = "./generated_chart.png"
454
+ # ... 图表生成逻辑
455
+ return ImgPath(chart_path)
456
+
457
+ @tool(name="search_web_image", description="搜索网络图片")
458
+ def search_web_image(query: str) -> ImgUrl:
459
+ """
460
+ 搜索网络图片
461
+
462
+ Args:
463
+ query: 搜索关键词
464
+
465
+ Returns:
466
+ 找到的图片URL
467
+ """
468
+ # 实际实现会调用图片搜索API
469
+ image_url = "https://example.com/search_result.jpg"
470
+ return ImgUrl(image_url)
471
+ ```
355
472
 
356
473
  ### 类继承方式(向后兼容)
357
474
 
@@ -387,11 +504,10 @@ class WebSearchTool(Tool):
387
504
  使用装饰器方式定义的工具可以直接传递给LLM函数装饰器:
388
505
 
389
506
  ```python
390
- from SimpleLLMFunc.llm_decorator import llm_function
391
- from SimpleLLMFunc.interface import ZhipuAI_glm_4_flash_Interface
507
+ from SimpleLLMFunc import llm_function
392
508
 
393
509
  @llm_function(
394
- llm_interface=ZhipuAI_glm_4_flash_Interface,
510
+ llm_interface=my_llm_interface,
395
511
  toolkit=[get_weather, search_web], # 直接传递被@tool装饰的函数
396
512
  )
397
513
  def answer_with_tools(question: str) -> str:
@@ -411,7 +527,7 @@ def answer_with_tools(question: str) -> str:
411
527
 
412
528
  ```python
413
529
  @llm_function(
414
- llm_interface=ZhipuAI_glm_4_flash_Interface,
530
+ llm_interface=my_llm_interface,
415
531
  toolkit=[get_weather, WebSearchTool()], # 混合使用两种方式定义的工具
416
532
  )
417
533
  def answer_with_mixed_tools(question: str) -> str:
@@ -421,12 +537,23 @@ def answer_with_mixed_tools(question: str) -> str:
421
537
 
422
538
  ## API密钥管理
423
539
 
424
- SimpleLLMFunc使用`APIKeyPool`类通过小根堆管理多个API密钥,实现负载均衡:
540
+ SimpleLLMFunc提供了完善的API密钥和流量管理机制:
541
+
542
+ ### API密钥负载均衡
543
+ 使用`APIKeyPool`类通过小根堆管理多个API密钥,实现负载均衡:
425
544
 
426
545
  - 自动选择最少负载的API密钥
427
546
  - 单例模式确保每个提供商只有一个密钥池,密钥池使用小根堆来进行负载均衡,每次取出load最低的KEY
428
547
  - 自动跟踪每个密钥的使用情况
429
548
 
549
+ ### 流量控制
550
+ 集成了令牌桶算法(TokenBucket)实现智能流量平滑:
551
+
552
+ - 防止API调用频率过高触发限制
553
+ - 支持突发流量的缓冲处理
554
+ - 可在`provider.json`中配置每个模型的流量控制参数
555
+ - 与API密钥池协同工作,提供更稳定的服务
556
+
430
557
  ## 安装和使用
431
558
 
432
559
  ### 1. 源码安装
@@ -460,7 +587,7 @@ pip install SimpleLLMFunc
460
587
  month = {June},
461
588
  title = {{SimpleLLMFunc: A New Approach to Build LLM Applications}},
462
589
  url = {https://github.com/NiJingzhe/SimpleLLMFunc},
463
- version = {0.2.1},
590
+ version = {0.2.9},
464
591
  year = {2025}
465
592
  }
466
593
  ```