my-llm-py 0.1.3__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 (84) hide show
  1. my_llm_py-0.1.3/.gitignore +11 -0
  2. my_llm_py-0.1.3/LICENSE +201 -0
  3. my_llm_py-0.1.3/PKG-INFO +121 -0
  4. my_llm_py-0.1.3/README.md +100 -0
  5. my_llm_py-0.1.3/docs/guides/building.md +67 -0
  6. my_llm_py-0.1.3/docs/reference/api.md +353 -0
  7. my_llm_py-0.1.3/docs/reference/configuration.md +337 -0
  8. my_llm_py-0.1.3/docs/reference/design-coverage.md +36 -0
  9. my_llm_py-0.1.3/examples/local_demo.py +130 -0
  10. my_llm_py-0.1.3/pyproject.toml +77 -0
  11. my_llm_py-0.1.3/src/my_llm/__init__.py +47 -0
  12. my_llm_py-0.1.3/src/my_llm/_version.py +3 -0
  13. my_llm_py-0.1.3/src/my_llm/codec/__init__.py +8 -0
  14. my_llm_py-0.1.3/src/my_llm/codec/anthropic/__init__.py +6 -0
  15. my_llm_py-0.1.3/src/my_llm/codec/anthropic/codec.py +34 -0
  16. my_llm_py-0.1.3/src/my_llm/codec/anthropic/decoding/__init__.py +1 -0
  17. my_llm_py-0.1.3/src/my_llm/codec/anthropic/decoding/stream.py +162 -0
  18. my_llm_py-0.1.3/src/my_llm/codec/anthropic/encoding/__init__.py +1 -0
  19. my_llm_py-0.1.3/src/my_llm/codec/anthropic/encoding/content.py +49 -0
  20. my_llm_py-0.1.3/src/my_llm/codec/anthropic/encoding/messages.py +83 -0
  21. my_llm_py-0.1.3/src/my_llm/codec/anthropic/encoding/parameters.py +50 -0
  22. my_llm_py-0.1.3/src/my_llm/codec/anthropic/encoding/tools.py +33 -0
  23. my_llm_py-0.1.3/src/my_llm/codec/gemini/__init__.py +6 -0
  24. my_llm_py-0.1.3/src/my_llm/codec/gemini/codec.py +37 -0
  25. my_llm_py-0.1.3/src/my_llm/codec/gemini/decoding/__init__.py +1 -0
  26. my_llm_py-0.1.3/src/my_llm/codec/gemini/decoding/stream.py +104 -0
  27. my_llm_py-0.1.3/src/my_llm/codec/gemini/encoding/__init__.py +1 -0
  28. my_llm_py-0.1.3/src/my_llm/codec/gemini/encoding/content.py +33 -0
  29. my_llm_py-0.1.3/src/my_llm/codec/gemini/encoding/messages.py +88 -0
  30. my_llm_py-0.1.3/src/my_llm/codec/gemini/encoding/parameters.py +52 -0
  31. my_llm_py-0.1.3/src/my_llm/codec/gemini/encoding/tools.py +38 -0
  32. my_llm_py-0.1.3/src/my_llm/codec/ollama/__init__.py +6 -0
  33. my_llm_py-0.1.3/src/my_llm/codec/ollama/codec.py +31 -0
  34. my_llm_py-0.1.3/src/my_llm/codec/ollama/decoding/__init__.py +1 -0
  35. my_llm_py-0.1.3/src/my_llm/codec/ollama/decoding/stream.py +68 -0
  36. my_llm_py-0.1.3/src/my_llm/codec/ollama/encoding/__init__.py +1 -0
  37. my_llm_py-0.1.3/src/my_llm/codec/ollama/encoding/content.py +27 -0
  38. my_llm_py-0.1.3/src/my_llm/codec/ollama/encoding/messages.py +38 -0
  39. my_llm_py-0.1.3/src/my_llm/codec/ollama/encoding/parameters.py +31 -0
  40. my_llm_py-0.1.3/src/my_llm/codec/ollama/encoding/tools.py +25 -0
  41. my_llm_py-0.1.3/src/my_llm/codec/openai/__init__.py +7 -0
  42. my_llm_py-0.1.3/src/my_llm/codec/openai/codec.py +94 -0
  43. my_llm_py-0.1.3/src/my_llm/codec/openai/decoding/__init__.py +1 -0
  44. my_llm_py-0.1.3/src/my_llm/codec/openai/decoding/chat.py +99 -0
  45. my_llm_py-0.1.3/src/my_llm/codec/openai/decoding/responses.py +143 -0
  46. my_llm_py-0.1.3/src/my_llm/codec/openai/encoding/__init__.py +1 -0
  47. my_llm_py-0.1.3/src/my_llm/codec/openai/encoding/content.py +99 -0
  48. my_llm_py-0.1.3/src/my_llm/codec/openai/encoding/messages.py +198 -0
  49. my_llm_py-0.1.3/src/my_llm/codec/openai/encoding/parameters.py +109 -0
  50. my_llm_py-0.1.3/src/my_llm/codec/openai/encoding/tools.py +40 -0
  51. my_llm_py-0.1.3/src/my_llm/codec/utils/__init__.py +1 -0
  52. my_llm_py-0.1.3/src/my_llm/codec/utils/common.py +64 -0
  53. my_llm_py-0.1.3/src/my_llm/codec/utils/input.py +138 -0
  54. my_llm_py-0.1.3/src/my_llm/codec/utils/native.py +213 -0
  55. my_llm_py-0.1.3/src/my_llm/codec/utils/tools.py +32 -0
  56. my_llm_py-0.1.3/src/my_llm/codec/utils/validation.py +137 -0
  57. my_llm_py-0.1.3/src/my_llm/invoke.py +418 -0
  58. my_llm_py-0.1.3/src/my_llm/profile/__init__.py +7 -0
  59. my_llm_py-0.1.3/src/my_llm/profile/model_profile.py +65 -0
  60. my_llm_py-0.1.3/src/my_llm/profile/provider_profile.py +118 -0
  61. my_llm_py-0.1.3/src/my_llm/profile/utils/__init__.py +1 -0
  62. my_llm_py-0.1.3/src/my_llm/profile/utils/common.py +53 -0
  63. my_llm_py-0.1.3/src/my_llm/profile/utils/resolution.py +156 -0
  64. my_llm_py-0.1.3/src/my_llm/py.typed +0 -0
  65. my_llm_py-0.1.3/src/my_llm/types/__init__.py +21 -0
  66. my_llm_py-0.1.3/src/my_llm/types/exceptions.py +54 -0
  67. my_llm_py-0.1.3/src/my_llm/types/messages.py +175 -0
  68. my_llm_py-0.1.3/tests/conftest.py +152 -0
  69. my_llm_py-0.1.3/tests/contract/test_decoding.py +266 -0
  70. my_llm_py-0.1.3/tests/integration/test_documentation.py +144 -0
  71. my_llm_py-0.1.3/tests/integration/test_message_inputs.py +133 -0
  72. my_llm_py-0.1.3/tests/integration/test_native_sdk_transport.py +439 -0
  73. my_llm_py-0.1.3/tests/integration/test_sdk_transport.py +355 -0
  74. my_llm_py-0.1.3/tests/packaging/test_distribution.py +90 -0
  75. my_llm_py-0.1.3/tests/unit/test_edge_cases.py +223 -0
  76. my_llm_py-0.1.3/tests/unit/test_encoding.py +346 -0
  77. my_llm_py-0.1.3/tests/unit/test_invoke.py +137 -0
  78. my_llm_py-0.1.3/tests/unit/test_message_conversion.py +456 -0
  79. my_llm_py-0.1.3/tests/unit/test_messages.py +86 -0
  80. my_llm_py-0.1.3/tests/unit/test_model_catalogue.py +150 -0
  81. my_llm_py-0.1.3/tests/unit/test_native_codecs.py +586 -0
  82. my_llm_py-0.1.3/tests/unit/test_ownership.py +193 -0
  83. my_llm_py-0.1.3/tests/unit/test_profiles.py +129 -0
  84. my_llm_py-0.1.3/tests/unit/test_public_api.py +51 -0
@@ -0,0 +1,11 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
11
+ 设计文档.md
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [JJJJSTIYYYY]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,121 @@
1
+ Metadata-Version: 2.5
2
+ Name: my-llm-py
3
+ Version: 0.1.3
4
+ Summary: Profile-driven async LLM adapter for OpenAI, Anthropic, Gemini and Ollama.
5
+ Project-URL: Repository, https://github.com/JJJJSTIYYYY/my-llm
6
+ Project-URL: Issues, https://github.com/JJJJSTIYYYY/my-llm/issues
7
+ Author-email: JJJJSTIYYYY <wavejustiy@gmail.com>
8
+ Maintainer-email: JJJJSTIYYYY <wavejustiy@gmail.com>
9
+ License-File: LICENSE
10
+ Keywords: adapter,adaptor,anthropic,gemini,llm,ollama,openai
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Requires-Python: >=3.12
16
+ Requires-Dist: anthropic<1,>=0.76
17
+ Requires-Dist: google-genai<2,>=1.56
18
+ Requires-Dist: ollama<1,>=0.6.3
19
+ Requires-Dist: openai<3,>=2.0
20
+ Description-Content-Type: text/markdown
21
+
22
+ <div align="center">
23
+
24
+ # MyLLM
25
+
26
+ ![Python](https://img.shields.io/badge/Python-3.12+-3776AB?style=flat&logo=python)
27
+ [![License](https://img.shields.io/badge/license-Apache%202.0%20%2B%20MIT-blue.svg)](https://github.com/JJJJSTIYYYY/my-llm/blob/master/LICENSE)
28
+ [![Discord](https://img.shields.io/badge/Discord-Join%20Us-5865F2?style=flat&logo=discord&logoColor=white)](https://discord.gg/tUCQthwK5)
29
+ [![QQ群](https://custom-icon-badges.demolab.com/badge/QQ群-639459172-00BFFF?style=flat&logo=tencent-qq)](https://qun.qq.com/universal-share/share?ac=1&authKey=ommoQrT2zhzHU%2FUxv8pfGCJbNifW%2BJyUAFBkNdzkHTPUxdxCnlgxm5aNgGslTmdE&busi_data=eyJncm91cENvZGUiOiI2Mzk0NTkxNzIiLCJ0b2tlbiI6Im9ZZkdNUWZnSVV1Y2REeUhKNnlTbWEwc05Bb093djRzUXdXNE55dklBVnlBQk9XbGNpS0ZXSDlzK3orSW1sQ3YiLCJ1aW4iOiIzMTI5NDI0NTcyIn0%3D&data=OGTchcr80RAQg8Z8_GZTdvBb7kZDeM9B3hHcNqLaAX2ZK_KYq260C4CubblEBT1bK5fP6zgtnCk2D8fIoph1ZQ&svctype=4&tempid=h5_group_info)
30
+
31
+ **通用的 LLM Provider 协议适配器**
32
+
33
+ </div>
34
+
35
+ 通过配置统一调用 OpenAI(Responses / Chat Completions)、Anthropic Messages、
36
+ Gemini 和 Ollama 原生协议的异步 Python 库,四种协议均使用官方 SDK。
37
+ 兼容服务使用相同的 OpenAI SDK,通过 `ProviderProfile` 和 `ModelProfile` 描述差异,
38
+ 不内置厂商或模型清单。需要 Python 3.12+。
39
+
40
+ ## 使用
41
+
42
+ ```python
43
+ import asyncio
44
+ import os
45
+
46
+ from my_llm import ModelProfile, ProviderProfile, generate
47
+
48
+
49
+ async def main():
50
+ provider = ProviderProfile(
51
+ api_type="responses",
52
+ api_key=os.environ["OPENAI_API_KEY"],
53
+ usage_mode="native",
54
+ )
55
+ model = ModelProfile(model=os.environ["LLM_MODEL"])
56
+ message = await generate(
57
+ provider=provider,
58
+ model=model,
59
+ messages=[{"role": "user", "content": "你好"}],
60
+ reasoning_effort="none",
61
+ stream=False,
62
+ )
63
+ print(message["content"])
64
+
65
+
66
+ asyncio.run(main())
67
+ ```
68
+
69
+ 重复调用时可复用 `MyLLM`,构造时显式传入模型配置:
70
+
71
+ ```python
72
+ from my_llm import ModelProfile, MyLLM, ProviderProfile
73
+
74
+ llm = MyLLM(
75
+ provider=ProviderProfile(api_type="responses"),
76
+ model=ModelProfile(model="your-model-id", max_tokens=2048),
77
+ )
78
+ # 在异步函数中调用:
79
+ # message = await llm.generate(messages=[{"role": "user", "content": "你好"}])
80
+ ```
81
+
82
+ `stream=True` 返回可关闭的异步流,调用时同样需要先 `await`:
83
+
84
+ ```python
85
+ async def print_answer(llm):
86
+ stream = await llm.generate(
87
+ messages=[{"role": "user", "content": "你好"}], stream=True,
88
+ )
89
+ async with stream:
90
+ async for chunk in stream:
91
+ print(chunk.content, end="", flush=True)
92
+ if chunk.tool_calls:
93
+ print(chunk.tool_calls) # 完整工具调用,由调用者执行本地函数
94
+ ```
95
+
96
+ 推理开启方式、强度映射、输出路径、消息名称与用量模式均可配置。Omit 可禁用
97
+ 配置或删除某个字典键。流异常和缺少完成状态会明确报错。
98
+ Tools 可传 Responses 或 Chat Completions 格式,自动识别并按目标 API 转换。
99
+ Messages 同样支持两种格式:自动转换内容块、工具调用及工具结果,并允许混合历史。
100
+ Responses 结果保留原生 output items,支持工具调用后的历史回放。
101
+
102
+ 调用参数、流式消费、工具回合及资源管理见
103
+ [API 使用指南](https://github.com/JJJJSTIYYYY/my-llm/blob/main/docs/reference/api.md);
104
+ Profile 字段、默认值、继承规则及兼容服务适配见
105
+ [配置指南](https://github.com/JJJJSTIYYYY/my-llm/blob/main/docs/reference/configuration.md)。
106
+ 通过 `ProviderProfile(protocol="anthropic" / "gemini" / "ollama")` 选择原生协议。
107
+ Anthropic 必须设置 `ModelProfile.max_tokens` 或调用时的 `max_tokens`。
108
+ 原生协议的参数映射、推理配置和 SDK Client 类型见
109
+
110
+ ## 本地开发与验证
111
+
112
+ ```sh
113
+ uv sync --group dev --locked
114
+ uv run pytest -q
115
+ uv run ruff check src tests examples
116
+ uv run ruff format --check src tests examples
117
+ uv run python examples/local_demo.py
118
+ uv build
119
+ package_version=$(uv run python -c 'from my_llm import __version__; print(__version__)')
120
+ uv run twine check --strict "dist/my_llm-${package_version}-py3-none-any.whl" "dist/my_llm-${package_version}.tar.gz"
121
+ ```
@@ -0,0 +1,100 @@
1
+ <div align="center">
2
+
3
+ # MyLLM
4
+
5
+ ![Python](https://img.shields.io/badge/Python-3.12+-3776AB?style=flat&logo=python)
6
+ [![License](https://img.shields.io/badge/license-Apache%202.0%20%2B%20MIT-blue.svg)](https://github.com/JJJJSTIYYYY/my-llm/blob/master/LICENSE)
7
+ [![Discord](https://img.shields.io/badge/Discord-Join%20Us-5865F2?style=flat&logo=discord&logoColor=white)](https://discord.gg/tUCQthwK5)
8
+ [![QQ群](https://custom-icon-badges.demolab.com/badge/QQ群-639459172-00BFFF?style=flat&logo=tencent-qq)](https://qun.qq.com/universal-share/share?ac=1&authKey=ommoQrT2zhzHU%2FUxv8pfGCJbNifW%2BJyUAFBkNdzkHTPUxdxCnlgxm5aNgGslTmdE&busi_data=eyJncm91cENvZGUiOiI2Mzk0NTkxNzIiLCJ0b2tlbiI6Im9ZZkdNUWZnSVV1Y2REeUhKNnlTbWEwc05Bb093djRzUXdXNE55dklBVnlBQk9XbGNpS0ZXSDlzK3orSW1sQ3YiLCJ1aW4iOiIzMTI5NDI0NTcyIn0%3D&data=OGTchcr80RAQg8Z8_GZTdvBb7kZDeM9B3hHcNqLaAX2ZK_KYq260C4CubblEBT1bK5fP6zgtnCk2D8fIoph1ZQ&svctype=4&tempid=h5_group_info)
9
+
10
+ **通用的 LLM Provider 协议适配器**
11
+
12
+ </div>
13
+
14
+ 通过配置统一调用 OpenAI(Responses / Chat Completions)、Anthropic Messages、
15
+ Gemini 和 Ollama 原生协议的异步 Python 库,四种协议均使用官方 SDK。
16
+ 兼容服务使用相同的 OpenAI SDK,通过 `ProviderProfile` 和 `ModelProfile` 描述差异,
17
+ 不内置厂商或模型清单。需要 Python 3.12+。
18
+
19
+ ## 使用
20
+
21
+ ```python
22
+ import asyncio
23
+ import os
24
+
25
+ from my_llm import ModelProfile, ProviderProfile, generate
26
+
27
+
28
+ async def main():
29
+ provider = ProviderProfile(
30
+ api_type="responses",
31
+ api_key=os.environ["OPENAI_API_KEY"],
32
+ usage_mode="native",
33
+ )
34
+ model = ModelProfile(model=os.environ["LLM_MODEL"])
35
+ message = await generate(
36
+ provider=provider,
37
+ model=model,
38
+ messages=[{"role": "user", "content": "你好"}],
39
+ reasoning_effort="none",
40
+ stream=False,
41
+ )
42
+ print(message["content"])
43
+
44
+
45
+ asyncio.run(main())
46
+ ```
47
+
48
+ 重复调用时可复用 `MyLLM`,构造时显式传入模型配置:
49
+
50
+ ```python
51
+ from my_llm import ModelProfile, MyLLM, ProviderProfile
52
+
53
+ llm = MyLLM(
54
+ provider=ProviderProfile(api_type="responses"),
55
+ model=ModelProfile(model="your-model-id", max_tokens=2048),
56
+ )
57
+ # 在异步函数中调用:
58
+ # message = await llm.generate(messages=[{"role": "user", "content": "你好"}])
59
+ ```
60
+
61
+ `stream=True` 返回可关闭的异步流,调用时同样需要先 `await`:
62
+
63
+ ```python
64
+ async def print_answer(llm):
65
+ stream = await llm.generate(
66
+ messages=[{"role": "user", "content": "你好"}], stream=True,
67
+ )
68
+ async with stream:
69
+ async for chunk in stream:
70
+ print(chunk.content, end="", flush=True)
71
+ if chunk.tool_calls:
72
+ print(chunk.tool_calls) # 完整工具调用,由调用者执行本地函数
73
+ ```
74
+
75
+ 推理开启方式、强度映射、输出路径、消息名称与用量模式均可配置。Omit 可禁用
76
+ 配置或删除某个字典键。流异常和缺少完成状态会明确报错。
77
+ Tools 可传 Responses 或 Chat Completions 格式,自动识别并按目标 API 转换。
78
+ Messages 同样支持两种格式:自动转换内容块、工具调用及工具结果,并允许混合历史。
79
+ Responses 结果保留原生 output items,支持工具调用后的历史回放。
80
+
81
+ 调用参数、流式消费、工具回合及资源管理见
82
+ [API 使用指南](https://github.com/JJJJSTIYYYY/my-llm/blob/main/docs/reference/api.md);
83
+ Profile 字段、默认值、继承规则及兼容服务适配见
84
+ [配置指南](https://github.com/JJJJSTIYYYY/my-llm/blob/main/docs/reference/configuration.md)。
85
+ 通过 `ProviderProfile(protocol="anthropic" / "gemini" / "ollama")` 选择原生协议。
86
+ Anthropic 必须设置 `ModelProfile.max_tokens` 或调用时的 `max_tokens`。
87
+ 原生协议的参数映射、推理配置和 SDK Client 类型见
88
+
89
+ ## 本地开发与验证
90
+
91
+ ```sh
92
+ uv sync --group dev --locked
93
+ uv run pytest -q
94
+ uv run ruff check src tests examples
95
+ uv run ruff format --check src tests examples
96
+ uv run python examples/local_demo.py
97
+ uv build
98
+ package_version=$(uv run python -c 'from my_llm import __version__; print(__version__)')
99
+ uv run twine check --strict "dist/my_llm-${package_version}-py3-none-any.whl" "dist/my_llm-${package_version}.tar.gz"
100
+ ```
@@ -0,0 +1,67 @@
1
+ # 构建与验证
2
+
3
+ 使用接口见 [API 使用指南](../reference/api.md),配置字段见
4
+ [配置指南](../reference/configuration.md)。
5
+
6
+ ## 构建
7
+
8
+ 使用 Python 3.12 或更高版本及 uv,在仓库根目录运行:
9
+
10
+ ```sh
11
+ uv build
12
+ ```
13
+
14
+ Hatchling 从 `src/my_llm/_version.py` 的 `__version__` 读取版本号,根据
15
+ `pyproject.toml` 构建源码包,再从源码包构建 wheel。生成文件如下:
16
+
17
+ - `dist/my_llm-<version>.tar.gz`:源码包。
18
+ - `dist/my_llm-<version>-py3-none-any.whl`:不依赖平台的纯 Python wheel。
19
+
20
+ 升级版本时只修改 `_version.py`,再运行 `uv lock` 更新锁文件。通过
21
+ `my_llm.__version__` 也可读取相同版本;`pyproject.toml` 使用动态版本元数据。
22
+
23
+ 构建依赖仅用于构建环境,不作为用户安装包时的运行依赖。
24
+ wheel 包含 `my_llm` 包和分发元数据,排除目录占位文件。
25
+ 源码包还包含 README、构建配置、使用文档、本地示例和测试。
26
+ `dist/` 已被 Git 忽略。
27
+
28
+ ## 安装验证
29
+
30
+ 在单独的临时虚拟环境中安装本地 wheel,避免覆盖已有开发环境:
31
+
32
+ ```sh
33
+ uv venv /tmp/my-llm-package-check --python 3.12
34
+ package_version=$(uv run python -c 'from my_llm import __version__; print(__version__)')
35
+ uv pip install --python /tmp/my-llm-package-check/bin/python "dist/my_llm-${package_version}-py3-none-any.whl"
36
+ /tmp/my-llm-package-check/bin/python -I -c 'import my_llm; from importlib.metadata import version; assert my_llm.__version__ == version("my-llm"); print(my_llm.__version__); print(my_llm.__file__)'
37
+ ```
38
+
39
+ 输出应为 `_version.py` 中声明的版本,且模块路径位于临时虚拟环境的 `site-packages`。
40
+ `-I` 使用 Python 隔离模式,避免仓库路径或 `PYTHONPATH` 干扰导入验证。
41
+
42
+ 检查 PyPI 分发元数据和 README 渲染格式:
43
+
44
+ ```sh
45
+ uv run twine check --strict "dist/my_llm-${package_version}-py3-none-any.whl" "dist/my_llm-${package_version}.tar.gz"
46
+ ```
47
+
48
+ 修改源文件、文档或版本后重新运行 `uv build`。发布前确定许可证、确认 PyPI
49
+ 名称及版本可用性;本指南只覆盖本地构建与验证。
50
+
51
+ ## 本地测试
52
+
53
+ ```sh
54
+ uv sync --group dev --locked
55
+ uv run pytest -q
56
+ uv run ruff check src tests examples
57
+ uv run ruff format --check src tests examples
58
+ uv run python examples/local_demo.py
59
+ ```
60
+
61
+ 测试使用真实 SDK、固定事件和本地模拟传输,覆盖配置、消息转换、完整工具调用、
62
+ 用量、流失败和连接释放,不访问真实模型服务。
63
+
64
+ 配置依据:
65
+ [PyPA 的 pyproject.toml 指南](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/)和
66
+ [Hatch 构建配置文档](https://hatch.pypa.io/latest/config/build/),版本来源参见
67
+ [Hatch 版本配置](https://hatch.pypa.io/latest/version/)。