hbllmutils 0.0.1__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.
@@ -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 [yyyy] [name of copyright owner]
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,249 @@
1
+ Metadata-Version: 2.1
2
+ Name: hbllmutils
3
+ Version: 0.0.1
4
+ Summary: A Python utility library for streamlined Large Language Model interactions with unified API and conversation management.
5
+ Home-page: https://github.com/HansBug/hbllmutils
6
+ Author: HansBug
7
+ Author-email: hansbug@buaa.edu.cn
8
+ License: Apache License, Version 2.0
9
+ Project-URL: Homepage, https://github.com/HansBug/hbllmutils
10
+ Project-URL: Documentation, https://hbllmutils.readthedocs.io/en/latest/
11
+ Project-URL: Repository, https://github.com/HansBug/hbllmutils
12
+ Project-URL: Bug Reports, https://github.com/HansBug/hbllmutils/issues
13
+ Project-URL: Source, https://github.com/HansBug/hbllmutils
14
+ Keywords: llm,large language models,openai,api,chatbot,ai,machine learning,nlp,natural language processing,conversation,streaming,async,configuration management,deepseek,gpt,claude,gemini
15
+ Classifier: Development Status :: 4 - Beta
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Intended Audience :: Information Technology
18
+ Classifier: Intended Audience :: Science/Research
19
+ Classifier: Intended Audience :: Education
20
+ Classifier: License :: OSI Approved :: Apache Software License
21
+ Classifier: Operating System :: OS Independent
22
+ Classifier: Operating System :: POSIX
23
+ Classifier: Operating System :: POSIX :: Linux
24
+ Classifier: Operating System :: Microsoft :: Windows
25
+ Classifier: Operating System :: MacOS
26
+ Classifier: Programming Language :: Python
27
+ Classifier: Programming Language :: Python :: 3
28
+ Classifier: Programming Language :: Python :: 3 :: Only
29
+ Classifier: Programming Language :: Python :: 3.7
30
+ Classifier: Programming Language :: Python :: 3.8
31
+ Classifier: Programming Language :: Python :: 3.9
32
+ Classifier: Programming Language :: Python :: 3.10
33
+ Classifier: Programming Language :: Python :: 3.11
34
+ Classifier: Programming Language :: Python :: 3.12
35
+ Classifier: Programming Language :: Python :: 3.13
36
+ Classifier: Programming Language :: Python :: Implementation :: CPython
37
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
38
+ Classifier: Topic :: Software Development
39
+ Classifier: Topic :: Software Development :: Libraries
40
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
41
+ Classifier: Topic :: Scientific/Engineering
42
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
43
+ Classifier: Topic :: Text Processing
44
+ Classifier: Topic :: Text Processing :: Linguistic
45
+ Classifier: Topic :: Communications :: Chat
46
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
47
+ Classifier: Topic :: Utilities
48
+ Classifier: Natural Language :: English
49
+ Classifier: Environment :: Console
50
+ Classifier: Environment :: Web Environment
51
+ Classifier: Typing :: Typed
52
+ Requires-Python: >=3.7
53
+ Description-Content-Type: text/markdown
54
+ License-File: LICENSE
55
+ Requires-Dist: hbutils>=0.14.2
56
+ Requires-Dist: openai
57
+ Requires-Dist: tqdm
58
+ Requires-Dist: pillow
59
+ Requires-Dist: numpy
60
+ Requires-Dist: pyyaml
61
+ Provides-Extra: test
62
+ Requires-Dist: coverage>=5; extra == "test"
63
+ Requires-Dist: mock>=4.0.3; extra == "test"
64
+ Requires-Dist: flake8~=3.5; extra == "test"
65
+ Requires-Dist: pytest~=6.2.5; extra == "test"
66
+ Requires-Dist: pytest-cov~=3.0.0; extra == "test"
67
+ Requires-Dist: pytest-mock~=3.6.1; extra == "test"
68
+ Requires-Dist: pytest-xdist>=1.34.0; extra == "test"
69
+ Requires-Dist: pytest-rerunfailures~=10.2; extra == "test"
70
+ Requires-Dist: pytest-timeout~=2.0.2; extra == "test"
71
+ Requires-Dist: easydict<2,>=1.7; extra == "test"
72
+ Provides-Extra: dev
73
+ Requires-Dist: openai; extra == "dev"
74
+ Requires-Dist: ruff; extra == "dev"
75
+ Requires-Dist: sphinx>=3.2.0; extra == "dev"
76
+ Requires-Dist: natsort; extra == "dev"
77
+ Provides-Extra: doc
78
+ Requires-Dist: Jinja2>=3.0.0; extra == "doc"
79
+ Requires-Dist: sphinx>=3.2.0; extra == "doc"
80
+ Requires-Dist: sphinx_rtd_theme>=0.4.3; extra == "doc"
81
+ Requires-Dist: enum_tools>=0.9.0; extra == "doc"
82
+ Requires-Dist: sphinx-toolbox; extra == "doc"
83
+ Requires-Dist: plantumlcli>=0.0.2; extra == "doc"
84
+ Requires-Dist: packaging; extra == "doc"
85
+ Requires-Dist: sphinx-multiversion>=0.2.4; extra == "doc"
86
+ Requires-Dist: where>=1.0.2; extra == "doc"
87
+ Requires-Dist: easydict<2,>=1.7; extra == "doc"
88
+ Requires-Dist: natsort; extra == "doc"
89
+
90
+ # hbllmutils
91
+
92
+ [![PyPI](https://img.shields.io/pypi/v/hbllmutils)](https://pypi.org/project/hbllmutils/)
93
+ ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/hbllmutils)
94
+ ![PyPI - Implementation](https://img.shields.io/pypi/implementation/hbllmutils)
95
+ ![PyPI - Downloads](https://img.shields.io/pypi/dm/hbllmutils)
96
+
97
+ ![Loc](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/HansBug/f6212c5576d61750212301a636d6c794/raw/loc.json)
98
+ ![Comments](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/HansBug/f6212c5576d61750212301a636d6c794/raw/comments.json)
99
+ [![Maintainability](https://api.codeclimate.com/v1/badges/5b6e14a915b63faeae90/maintainability)](https://codeclimate.com/github/HansBug/hbllmutils/maintainability)
100
+ [![codecov](https://codecov.io/gh/hansbug/hbllmutils/branch/main/graph/badge.svg?token=XJVDP4EFAT)](https://codecov.io/gh/hansbug/hbllmutils)
101
+
102
+ [![Code Test](https://github.com/hansbug/hbllmutils/workflows/Code%20Test/badge.svg)](https://github.com/hansbug/hbllmutils/actions?query=workflow%3A%22Code+Test%22)
103
+ [![Badge Creation](https://github.com/hansbug/hbllmutils/workflows/Badge%20Creation/badge.svg)](https://github.com/hansbug/hbllmutils/actions?query=workflow%3A%22Badge+Creation%22)
104
+ [![Package Release](https://github.com/hansbug/hbllmutils/workflows/Package%20Release/badge.svg)](https://github.com/hansbug/hbllmutils/actions?query=workflow%3A%22Package+Release%22)
105
+
106
+ [![GitHub stars](https://img.shields.io/github/stars/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/stargazers)
107
+ [![GitHub forks](https://img.shields.io/github/forks/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/network)
108
+ ![GitHub commit activity](https://img.shields.io/github/commit-activity/m/hansbug/hbllmutils)
109
+ [![GitHub issues](https://img.shields.io/github/issues/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/issues)
110
+ [![GitHub pulls](https://img.shields.io/github/issues-pr/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/pulls)
111
+ [![Contributors](https://img.shields.io/github/contributors/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/graphs/contributors)
112
+ [![GitHub license](https://img.shields.io/github/license/hansbug/hbllmutils)](https://github.com/HansBug/hbllmutils/blob/master/LICENSE)
113
+
114
+ `hbllmutils` is a Python utility library designed to streamline interactions with Large Language Models (LLMs) by
115
+ providing robust configuration management, a unified API for OpenAI-compatible endpoints, and intuitive conversation
116
+ history handling.
117
+
118
+ ## Features
119
+
120
+ - **Flexible LLM Configuration**: Easily manage multiple LLM API endpoints and models through a simple YAML
121
+ configuration file (`.llmconfig.yaml`), supporting default and fallback settings.
122
+ - **OpenAI-Compatible API**: Interact with various LLM providers that adhere to the OpenAI API specification, offering
123
+ both synchronous and asynchronous request methods.
124
+ - **Streaming Responses**: Efficiently handle streaming responses from LLMs, including optional extraction of reasoning
125
+ content.
126
+ - **Conversation History Management**: Build and maintain complex conversation histories with support for different
127
+ roles (system, user, assistant) and multimodal content (text, images).
128
+ - **Extensible Design**: Built with extensibility in mind, allowing for easy integration of new models or custom
129
+ behaviors.
130
+
131
+ ## Installation
132
+
133
+ You can simply install it with the `pip` command line from the official PyPI site.
134
+
135
+ ```bash
136
+ pip install hbllmutils
137
+ ```
138
+
139
+ For more information about installation, you can refer to
140
+ the [Installation Guide](https://hbllmutils.readthedocs.io/en/latest/tutorials/installation/index.html).
141
+
142
+ ## Configuration: `.llmconfig.yaml`
143
+
144
+ The library uses a `.llmconfig.yaml` file to manage your LLM API credentials and model configurations. This file can be
145
+ placed in your project's root directory or specified explicitly. Below is an example configuration demonstrating how to
146
+ set up multiple API providers and define models, including default and fallback options.
147
+
148
+ ```yaml
149
+ deepseek: &deepseek
150
+ base_url: https://api.deepseek.com/v1
151
+ api_token: sk-457***af74
152
+
153
+ aihubmix: &aihubmix
154
+ base_url: https://aihubmix.com/v1
155
+ api_token: sk-6B9***F0Ad
156
+
157
+ aigcbest: &aigcbest
158
+ base_url: https://api2.aigcbest.top/v1
159
+ api_token: sk-tbK***49kA
160
+
161
+ openroute: &openroute
162
+ base_url: https://openrouter.ai/api/v1
163
+ api_token: sk-or-v1-9bf***a3d4
164
+
165
+ models:
166
+ __default__:
167
+ <<: *deepseek
168
+ model_name: deepseek-chat
169
+
170
+ deepseek-R1:
171
+ <<: *deepseek
172
+ model_name: deepseek-reasoner
173
+
174
+ deepseek-V3:
175
+ <<: *deepseek
176
+ model_name: deepseek-chat
177
+
178
+ __fallback__:
179
+ <<: *aihubmix
180
+ ```
181
+
182
+ **Explanation of the configuration:**
183
+
184
+ - **Anchors (`&` and `*`)**: YAML anchors are used to define reusable blocks. For example, `&deepseek` defines a block
185
+ named `deepseek` which can be referenced later using `*deepseek`.
186
+ - **`models` section**: This is the core of your model definitions.
187
+ - `__default__`: Specifies the default model to use if no `model_name` is explicitly provided to `load_llm_model`.
188
+ - `deepseek-R1`, `deepseek-V3`: Specific model configurations that inherit properties from the defined anchors and
189
+ can override them (e.g., `model_name`).
190
+ - `__fallback__`: Defines a fallback API endpoint. If a requested `model_name` is not found in the `models` section,
191
+ the `__fallback__` configuration will be used, with the requested `model_name` automatically assigned.
192
+
193
+ ## Quick Start Example
194
+
195
+ This example demonstrates how to load a model using the configuration file and interact with it using streaming
196
+ responses and conversation history.
197
+
198
+ First, ensure you have a `.llmconfig.yaml` file set up as described above in your project directory.
199
+
200
+ ```python
201
+ import sys
202
+ from pprint import pprint
203
+
204
+ from hbllmutils.history import LLMHistory
205
+ from hbllmutils.model import load_llm_model
206
+
207
+ # Load the LLM model named 'deepseek-V3' from your .llmconfig.yaml
208
+ # The library will automatically look for .llmconfig.yaml in the current directory
209
+ model = load_llm_model(model_name='deepseek-V3')
210
+ print(f"Loaded Model: {model}")
211
+ pprint(model)
212
+
213
+ # Initialize conversation history with a system prompt
214
+ history = LLMHistory().set_system_prompt(
215
+ 'tell me the appearance of this guy, use json format, like {\'description\': \'xxxxx\', \'name\': \'original name\'}.'
216
+ ).append_user(
217
+ 'donald trump'
218
+ )
219
+
220
+ # Ask the model a question and get a streaming response
221
+ # with_reasoning=True will include any internal reasoning from the model in the stream
222
+ f = model.ask_stream(
223
+ messages=history.to_json(),
224
+ with_reasoning=True,
225
+ )
226
+ print(f"\nStreaming Response (with reasoning):\n")
227
+
228
+ # Iterate through the stream and print chunks as they arrive
229
+ for chunk in f:
230
+ print(chunk, end='')
231
+ sys.stdout.flush()
232
+
233
+ print(f"\n\nAccumulated Reasoning: {f.reasoning_content}")
234
+ print(f"Accumulated Content: {f.content}")
235
+
236
+ # Alternatively, for non-streaming responses, use the ask method:
237
+ # response_content = model.ask(messages=history.to_json())
238
+ # print(f"\nNon-streaming Response: {response_content}")
239
+
240
+ # If you need reasoning content for non-streaming:
241
+ # reasoning, content = model.ask(messages=history.to_json(), with_reasoning=True)
242
+ # print(f"\nNon-streaming Reasoning: {reasoning}")
243
+ # print(f"Non-streaming Content: {content}")
244
+ ```
245
+
246
+ ## Contributing
247
+
248
+ Contributions are welcome! Please feel free to open issues or submit pull requests on
249
+ the [GitHub repository](https://github.com/HansBug/hbllmutils).
@@ -0,0 +1,160 @@
1
+ # hbllmutils
2
+
3
+ [![PyPI](https://img.shields.io/pypi/v/hbllmutils)](https://pypi.org/project/hbllmutils/)
4
+ ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/hbllmutils)
5
+ ![PyPI - Implementation](https://img.shields.io/pypi/implementation/hbllmutils)
6
+ ![PyPI - Downloads](https://img.shields.io/pypi/dm/hbllmutils)
7
+
8
+ ![Loc](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/HansBug/f6212c5576d61750212301a636d6c794/raw/loc.json)
9
+ ![Comments](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/HansBug/f6212c5576d61750212301a636d6c794/raw/comments.json)
10
+ [![Maintainability](https://api.codeclimate.com/v1/badges/5b6e14a915b63faeae90/maintainability)](https://codeclimate.com/github/HansBug/hbllmutils/maintainability)
11
+ [![codecov](https://codecov.io/gh/hansbug/hbllmutils/branch/main/graph/badge.svg?token=XJVDP4EFAT)](https://codecov.io/gh/hansbug/hbllmutils)
12
+
13
+ [![Code Test](https://github.com/hansbug/hbllmutils/workflows/Code%20Test/badge.svg)](https://github.com/hansbug/hbllmutils/actions?query=workflow%3A%22Code+Test%22)
14
+ [![Badge Creation](https://github.com/hansbug/hbllmutils/workflows/Badge%20Creation/badge.svg)](https://github.com/hansbug/hbllmutils/actions?query=workflow%3A%22Badge+Creation%22)
15
+ [![Package Release](https://github.com/hansbug/hbllmutils/workflows/Package%20Release/badge.svg)](https://github.com/hansbug/hbllmutils/actions?query=workflow%3A%22Package+Release%22)
16
+
17
+ [![GitHub stars](https://img.shields.io/github/stars/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/stargazers)
18
+ [![GitHub forks](https://img.shields.io/github/forks/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/network)
19
+ ![GitHub commit activity](https://img.shields.io/github/commit-activity/m/hansbug/hbllmutils)
20
+ [![GitHub issues](https://img.shields.io/github/issues/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/issues)
21
+ [![GitHub pulls](https://img.shields.io/github/issues-pr/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/pulls)
22
+ [![Contributors](https://img.shields.io/github/contributors/hansbug/hbllmutils)](https://github.com/hansbug/hbllmutils/graphs/contributors)
23
+ [![GitHub license](https://img.shields.io/github/license/hansbug/hbllmutils)](https://github.com/HansBug/hbllmutils/blob/master/LICENSE)
24
+
25
+ `hbllmutils` is a Python utility library designed to streamline interactions with Large Language Models (LLMs) by
26
+ providing robust configuration management, a unified API for OpenAI-compatible endpoints, and intuitive conversation
27
+ history handling.
28
+
29
+ ## Features
30
+
31
+ - **Flexible LLM Configuration**: Easily manage multiple LLM API endpoints and models through a simple YAML
32
+ configuration file (`.llmconfig.yaml`), supporting default and fallback settings.
33
+ - **OpenAI-Compatible API**: Interact with various LLM providers that adhere to the OpenAI API specification, offering
34
+ both synchronous and asynchronous request methods.
35
+ - **Streaming Responses**: Efficiently handle streaming responses from LLMs, including optional extraction of reasoning
36
+ content.
37
+ - **Conversation History Management**: Build and maintain complex conversation histories with support for different
38
+ roles (system, user, assistant) and multimodal content (text, images).
39
+ - **Extensible Design**: Built with extensibility in mind, allowing for easy integration of new models or custom
40
+ behaviors.
41
+
42
+ ## Installation
43
+
44
+ You can simply install it with the `pip` command line from the official PyPI site.
45
+
46
+ ```bash
47
+ pip install hbllmutils
48
+ ```
49
+
50
+ For more information about installation, you can refer to
51
+ the [Installation Guide](https://hbllmutils.readthedocs.io/en/latest/tutorials/installation/index.html).
52
+
53
+ ## Configuration: `.llmconfig.yaml`
54
+
55
+ The library uses a `.llmconfig.yaml` file to manage your LLM API credentials and model configurations. This file can be
56
+ placed in your project's root directory or specified explicitly. Below is an example configuration demonstrating how to
57
+ set up multiple API providers and define models, including default and fallback options.
58
+
59
+ ```yaml
60
+ deepseek: &deepseek
61
+ base_url: https://api.deepseek.com/v1
62
+ api_token: sk-457***af74
63
+
64
+ aihubmix: &aihubmix
65
+ base_url: https://aihubmix.com/v1
66
+ api_token: sk-6B9***F0Ad
67
+
68
+ aigcbest: &aigcbest
69
+ base_url: https://api2.aigcbest.top/v1
70
+ api_token: sk-tbK***49kA
71
+
72
+ openroute: &openroute
73
+ base_url: https://openrouter.ai/api/v1
74
+ api_token: sk-or-v1-9bf***a3d4
75
+
76
+ models:
77
+ __default__:
78
+ <<: *deepseek
79
+ model_name: deepseek-chat
80
+
81
+ deepseek-R1:
82
+ <<: *deepseek
83
+ model_name: deepseek-reasoner
84
+
85
+ deepseek-V3:
86
+ <<: *deepseek
87
+ model_name: deepseek-chat
88
+
89
+ __fallback__:
90
+ <<: *aihubmix
91
+ ```
92
+
93
+ **Explanation of the configuration:**
94
+
95
+ - **Anchors (`&` and `*`)**: YAML anchors are used to define reusable blocks. For example, `&deepseek` defines a block
96
+ named `deepseek` which can be referenced later using `*deepseek`.
97
+ - **`models` section**: This is the core of your model definitions.
98
+ - `__default__`: Specifies the default model to use if no `model_name` is explicitly provided to `load_llm_model`.
99
+ - `deepseek-R1`, `deepseek-V3`: Specific model configurations that inherit properties from the defined anchors and
100
+ can override them (e.g., `model_name`).
101
+ - `__fallback__`: Defines a fallback API endpoint. If a requested `model_name` is not found in the `models` section,
102
+ the `__fallback__` configuration will be used, with the requested `model_name` automatically assigned.
103
+
104
+ ## Quick Start Example
105
+
106
+ This example demonstrates how to load a model using the configuration file and interact with it using streaming
107
+ responses and conversation history.
108
+
109
+ First, ensure you have a `.llmconfig.yaml` file set up as described above in your project directory.
110
+
111
+ ```python
112
+ import sys
113
+ from pprint import pprint
114
+
115
+ from hbllmutils.history import LLMHistory
116
+ from hbllmutils.model import load_llm_model
117
+
118
+ # Load the LLM model named 'deepseek-V3' from your .llmconfig.yaml
119
+ # The library will automatically look for .llmconfig.yaml in the current directory
120
+ model = load_llm_model(model_name='deepseek-V3')
121
+ print(f"Loaded Model: {model}")
122
+ pprint(model)
123
+
124
+ # Initialize conversation history with a system prompt
125
+ history = LLMHistory().set_system_prompt(
126
+ 'tell me the appearance of this guy, use json format, like {\'description\': \'xxxxx\', \'name\': \'original name\'}.'
127
+ ).append_user(
128
+ 'donald trump'
129
+ )
130
+
131
+ # Ask the model a question and get a streaming response
132
+ # with_reasoning=True will include any internal reasoning from the model in the stream
133
+ f = model.ask_stream(
134
+ messages=history.to_json(),
135
+ with_reasoning=True,
136
+ )
137
+ print(f"\nStreaming Response (with reasoning):\n")
138
+
139
+ # Iterate through the stream and print chunks as they arrive
140
+ for chunk in f:
141
+ print(chunk, end='')
142
+ sys.stdout.flush()
143
+
144
+ print(f"\n\nAccumulated Reasoning: {f.reasoning_content}")
145
+ print(f"Accumulated Content: {f.content}")
146
+
147
+ # Alternatively, for non-streaming responses, use the ask method:
148
+ # response_content = model.ask(messages=history.to_json())
149
+ # print(f"\nNon-streaming Response: {response_content}")
150
+
151
+ # If you need reasoning content for non-streaming:
152
+ # reasoning, content = model.ask(messages=history.to_json(), with_reasoning=True)
153
+ # print(f"\nNon-streaming Reasoning: {reasoning}")
154
+ # print(f"Non-streaming Content: {content}")
155
+ ```
156
+
157
+ ## Contributing
158
+
159
+ Contributions are welcome! Please feel free to open issues or submit pull requests on
160
+ the [GitHub repository](https://github.com/HansBug/hbllmutils).
File without changes
@@ -0,0 +1,6 @@
1
+ """
2
+ Overview:
3
+ This module provides global configuration management for the hbutils package.
4
+ It includes metadata information such as package name, version, author details,
5
+ and other configuration settings that are used throughout the package.
6
+ """
@@ -0,0 +1,24 @@
1
+ """
2
+ Overview:
3
+ Meta information for hbllmutils package.
4
+
5
+ This module contains metadata constants for the hbllmutils package, including version,
6
+ author information, and project description. These constants are typically used in
7
+ setup configuration and package distribution.
8
+ """
9
+
10
+ #: Title of this project (should be `hbllmutils`).
11
+ __TITLE__ = "hbllmutils"
12
+
13
+ #: Version of this project.
14
+ __VERSION__ = "0.0.1"
15
+
16
+ #: Short description of the project, will be included in ``setup.py``.
17
+ __DESCRIPTION__ = ('A Python utility library for streamlined Large Language Model interactions '
18
+ 'with unified API and conversation management.')
19
+
20
+ #: Author of this project.
21
+ __AUTHOR__ = "HansBug"
22
+
23
+ #: Email of the authors'.
24
+ __AUTHOR_EMAIL__ = "hansbug@buaa.edu.cn"