ailia-llm 1.2.0.0__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,78 @@
1
+ Metadata-Version: 2.1
2
+ Name: ailia_llm
3
+ Version: 1.2.0.0
4
+ Summary: ailia LLM
5
+ Home-page: https://ailia.jp/
6
+ Author: ax Inc.
7
+ Author-email: contact@axinc.jp
8
+ License: https://ailia.ai/en/license/
9
+ Requires-Python: >3.6
10
+ Description-Content-Type: text/markdown
11
+ Requires-Dist: ailia
12
+
13
+ # ailia LLM Python API
14
+
15
+ !! CAUTION !!
16
+ “ailia” IS NOT OPEN SOURCE SOFTWARE (OSS).
17
+ As long as user complies with the conditions stated in [License Document](https://ailia.ai/license/), user may use the Software for free of charge, but the Software is basically paid software.
18
+
19
+ ## About ailia LLM
20
+
21
+ ailia LLM is a library for running LLMs on edge devices. It provides bindings for C++ and Unity.
22
+
23
+ ## Install from pip
24
+
25
+ You can install the ailia LLM free evaluation package with the following command.
26
+
27
+ ```
28
+ pip3 install ailia_llm
29
+ ```
30
+
31
+ ## Install from package
32
+
33
+ You can install the ailia LLM from Package with the following command.
34
+
35
+ ```
36
+ python3 bootstrap.py
37
+ pip3 install ./
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ```python
43
+ import ailia_llm
44
+
45
+ import os
46
+ import urllib.request
47
+
48
+ model_file_path = "gemma-2-2b-it-Q4_K_M.gguf"
49
+ if not os.path.exists(model_file_path):
50
+ urllib.request.urlretrieve(
51
+ "https://storage.googleapis.com/ailia-models/gemma/gemma-2-2b-it-Q4_K_M.gguf",
52
+ model_file_path
53
+ )
54
+
55
+ model = ailia_llm.AiliaLLM()
56
+ model.open(model_file_path)
57
+
58
+ messages = []
59
+ messages.append({"role": "system", "content": "語尾に「わん」をつけてください。"})
60
+ messages.append({"role": "user", "content": "あなたの名前は何ですか?"})
61
+
62
+ stream = model.generate(messages)
63
+
64
+ text = ""
65
+ for delta_text in stream:
66
+ text = text + delta_text
67
+ print(text)
68
+
69
+ if model.context_full():
70
+ raise Exception("Context full")
71
+
72
+ messages.append({"role": "assistant", "content": text})
73
+ ```
74
+
75
+ ## API specification
76
+
77
+ https://github.com/axinc-ai/ailia-sdk
78
+
@@ -0,0 +1,66 @@
1
+ # ailia LLM Python API
2
+
3
+ !! CAUTION !!
4
+ “ailia” IS NOT OPEN SOURCE SOFTWARE (OSS).
5
+ As long as user complies with the conditions stated in [License Document](https://ailia.ai/license/), user may use the Software for free of charge, but the Software is basically paid software.
6
+
7
+ ## About ailia LLM
8
+
9
+ ailia LLM is a library for running LLMs on edge devices. It provides bindings for C++ and Unity.
10
+
11
+ ## Install from pip
12
+
13
+ You can install the ailia LLM free evaluation package with the following command.
14
+
15
+ ```
16
+ pip3 install ailia_llm
17
+ ```
18
+
19
+ ## Install from package
20
+
21
+ You can install the ailia LLM from Package with the following command.
22
+
23
+ ```
24
+ python3 bootstrap.py
25
+ pip3 install ./
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```python
31
+ import ailia_llm
32
+
33
+ import os
34
+ import urllib.request
35
+
36
+ model_file_path = "gemma-2-2b-it-Q4_K_M.gguf"
37
+ if not os.path.exists(model_file_path):
38
+ urllib.request.urlretrieve(
39
+ "https://storage.googleapis.com/ailia-models/gemma/gemma-2-2b-it-Q4_K_M.gguf",
40
+ model_file_path
41
+ )
42
+
43
+ model = ailia_llm.AiliaLLM()
44
+ model.open(model_file_path)
45
+
46
+ messages = []
47
+ messages.append({"role": "system", "content": "語尾に「わん」をつけてください。"})
48
+ messages.append({"role": "user", "content": "あなたの名前は何ですか?"})
49
+
50
+ stream = model.generate(messages)
51
+
52
+ text = ""
53
+ for delta_text in stream:
54
+ text = text + delta_text
55
+ print(text)
56
+
57
+ if model.context_full():
58
+ raise Exception("Context full")
59
+
60
+ messages.append({"role": "assistant", "content": text})
61
+ ```
62
+
63
+ ## API specification
64
+
65
+ https://github.com/axinc-ai/ailia-sdk
66
+
@@ -0,0 +1,201 @@
1
+ import ctypes
2
+ import os
3
+ import sys
4
+
5
+ import numpy
6
+ import ailia
7
+
8
+ import os
9
+ import platform
10
+
11
+ #### dependency check
12
+ if sys.platform == "win32":
13
+ import ctypes
14
+ try:
15
+ for library in ["vcruntime140.dll", "vcruntime140_1.dll", "msvcp140.dll"]:
16
+ ctypes.windll.LoadLibrary(library)
17
+ except:
18
+ print(" WARNING Please install MSVC 2015-2019 runtime from https://docs.microsoft.com/ja-jp/cpp/windows/latest-supported-vc-redist")
19
+
20
+
21
+ #### loading DLL / DYLIB / SO ####
22
+ if sys.platform == "win32":
23
+ dll_platform = "windows/x64"
24
+ dll_name = "ailia_llm.dll"
25
+ dll_name_fallback = "ailia_llm_fallback.dll"
26
+ load_fn = ctypes.WinDLL
27
+ elif sys.platform == "darwin":
28
+ dll_platform = "mac"
29
+ dll_name = "libailia_llm.dylib"
30
+ dll_name_fallback = None
31
+ load_fn = ctypes.CDLL
32
+ else:
33
+ is_arm = "arm" in platform.machine() or platform.machine() == "aarch64"
34
+ if is_arm:
35
+ if platform.architecture()[0] == "32bit":
36
+ dll_platform = "linux/armeabi-v7a"
37
+ else:
38
+ dll_platform = "linux/arm64-v8a"
39
+ else:
40
+ dll_platform = "linux/x64"
41
+ dll_name = "libailia_llm.so"
42
+ dll_name_fallback = None
43
+ load_fn = ctypes.CDLL
44
+
45
+ dll_found = False
46
+ candidate = ["", str(os.path.dirname(os.path.abspath(__file__))) + str(os.sep), str(os.path.dirname(os.path.abspath(__file__))) + str(os.sep) + dll_platform + str(os.sep)]
47
+
48
+ for dir in candidate:
49
+ try:
50
+ dll = load_fn(dir + dll_name) # Load gpu library first
51
+ dll_found = True
52
+ except:
53
+ pass
54
+
55
+ if not dll_found:
56
+ for dir in candidate:
57
+ try:
58
+ dll = load_fn(dir + dll_name_fallback) # Fallback cpu library second
59
+ dll_found = True
60
+ except:
61
+ pass
62
+
63
+ if not dll_found:
64
+ msg = "DLL load failed : \'" + dll_name + "\' is not found"
65
+ raise ImportError(msg)
66
+
67
+ # ==============================================================================
68
+
69
+ from ctypes import *
70
+
71
+ AILIA_LLM_STATUS_SUCCESS = ( 0 )
72
+ AILIA_LLM_STATUS_CONTEXT_FULL = ( -8 )
73
+
74
+ class AILIALLMChatMessage(ctypes.Structure):
75
+ _fields_ = [
76
+ ("role", ctypes.c_char_p),
77
+ ("content", ctypes.c_char_p)
78
+ ]
79
+
80
+ # ==============================================================================
81
+
82
+ dll.ailiaLLMCreate.restype = c_int
83
+ dll.ailiaLLMCreate.argtypes = (POINTER(c_void_p), )
84
+
85
+ dll.ailiaLLMDestroy.restype = None
86
+ dll.ailiaLLMDestroy.argtypes = (c_void_p, )
87
+
88
+ dll.ailiaLLMOpenModelFileA.restype = c_int
89
+ dll.ailiaLLMOpenModelFileA.argtypes = (c_void_p, c_char_p, c_uint32)
90
+
91
+ dll.ailiaLLMOpenModelFileW.restype = c_int
92
+ dll.ailiaLLMOpenModelFileW.argtypes = (c_void_p, c_wchar_p, c_uint32)
93
+
94
+ dll.ailiaLLMGetDeltaTextSize.restype = c_int
95
+ dll.ailiaLLMGetDeltaTextSize.argtypes = (c_void_p, POINTER(c_uint32))
96
+
97
+ dll.ailiaLLMGetDeltaText.restype = c_int
98
+ dll.ailiaLLMGetDeltaText.argtypes = (c_void_p, numpy.ctypeslib.ndpointer(
99
+ dtype=numpy.uint8, flags='CONTIGUOUS'
100
+ ), # text
101
+ ctypes.c_uint)
102
+
103
+ dll.ailiaLLMSetPrompt.restype = c_int
104
+ dll.ailiaLLMSetPrompt.argtypes = (c_void_p, ctypes.POINTER(AILIALLMChatMessage), ctypes.c_uint)
105
+
106
+ dll.ailiaLLMGenerate.restype = c_int
107
+ dll.ailiaLLMGenerate.argtypes = (c_void_p, POINTER(c_uint32))
108
+
109
+ # ==============================================================================
110
+ # base model class
111
+ # ==============================================================================
112
+
113
+ class AiliaLLMError(RuntimeError):
114
+ def __init__(self, message, code):
115
+ super().__init__(f"{message} code:{code}")
116
+ self.code = code
117
+
118
+ class AiliaLLM():
119
+ _instance = None
120
+ _context_full = False
121
+
122
+ def _check(self, status):
123
+ if status != AILIA_LLM_STATUS_SUCCESS:
124
+ raise AiliaLLMError(f"ailia LLM error", status)
125
+
126
+ def _string_buffer_aw(self, path):
127
+ if sys.platform == "win32":
128
+ return ctypes.create_unicode_buffer(path)
129
+ else:
130
+ return ctypes.create_string_buffer(path.encode("utf-8"))
131
+
132
+ def _string_buffer(self, path):
133
+ return ctypes.create_string_buffer(path.encode("utf-8"))
134
+
135
+ def _string_to_c_char_p(self, s):
136
+ return ctypes.c_char_p(s.encode('utf-8'))
137
+
138
+ def __init__(self):
139
+ self._instance = ctypes.c_void_p(None)
140
+ self._check(dll.ailiaLLMCreate(cast(pointer(self._instance), POINTER(c_void_p))))
141
+
142
+ def open(self, model_path, n_ctx = 0):
143
+ if "time_license" in ailia.get_version():
144
+ ailia.check_and_download_license()
145
+ p1 = self._string_buffer_aw(model_path)
146
+
147
+ if sys.platform == "win32":
148
+ self._check(dll.ailiaLLMOpenModelFileW(self._instance, p1, ctypes.c_uint32(n_ctx)))
149
+ else:
150
+ self._check(dll.ailiaLLMOpenModelFileA(self._instance, p1, ctypes.c_uint32(n_ctx)))
151
+
152
+ def generate(self, prompts):
153
+ messages = (AILIALLMChatMessage * len(prompts))()
154
+ for i in range(len(prompts)):
155
+ messages[i].role = self._string_to_c_char_p(prompts[i]["role"])
156
+ messages[i].content = self._string_to_c_char_p(prompts[i]["content"])
157
+
158
+ self._context_full = False
159
+ status = dll.ailiaLLMSetPrompt(self._instance, messages, ctypes.c_uint32(len(prompts)))
160
+ if status == AILIA_LLM_STATUS_CONTEXT_FULL:
161
+ self._context_full = True
162
+ return
163
+ self._check(status)
164
+
165
+ buf = numpy.zeros((0), dtype=numpy.uint8, order='C')
166
+ before_text = ""
167
+
168
+ while True:
169
+ done = ctypes.c_uint(0)
170
+ status = dll.ailiaLLMGenerate(self._instance, ctypes.byref(done))
171
+ self._check(status)
172
+
173
+ if status == AILIA_LLM_STATUS_CONTEXT_FULL:
174
+ self._context_full = True
175
+ break
176
+
177
+ if done.value == 1:
178
+ break
179
+
180
+ count = ctypes.c_uint(0)
181
+ self._check(dll.ailiaLLMGetDeltaTextSize(self._instance, ctypes.byref(count)))
182
+
183
+ new_buf = numpy.zeros((count.value), dtype=numpy.uint8, order='C')
184
+
185
+ self._check(dll.ailiaLLMGetDeltaText(self._instance, new_buf, count))
186
+
187
+ buf = numpy.concatenate([buf[:-1], new_buf])
188
+
189
+ text = bytes(buf[:- 1]).decode("utf-8", errors="ignore")
190
+ delta_text = text[len(before_text):]
191
+
192
+ yield delta_text
193
+ before_text = text
194
+
195
+ def context_full(self):
196
+ return self._context_full
197
+
198
+ def __del__(self):
199
+ if self._instance:
200
+ dll.ailiaLLMDestroy(cast(self._instance, c_void_p))
201
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-2024 The ggml authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,78 @@
1
+ Metadata-Version: 2.1
2
+ Name: ailia_llm
3
+ Version: 1.2.0.0
4
+ Summary: ailia LLM
5
+ Home-page: https://ailia.jp/
6
+ Author: ax Inc.
7
+ Author-email: contact@axinc.jp
8
+ License: https://ailia.ai/en/license/
9
+ Requires-Python: >3.6
10
+ Description-Content-Type: text/markdown
11
+ Requires-Dist: ailia
12
+
13
+ # ailia LLM Python API
14
+
15
+ !! CAUTION !!
16
+ “ailia” IS NOT OPEN SOURCE SOFTWARE (OSS).
17
+ As long as user complies with the conditions stated in [License Document](https://ailia.ai/license/), user may use the Software for free of charge, but the Software is basically paid software.
18
+
19
+ ## About ailia LLM
20
+
21
+ ailia LLM is a library for running LLMs on edge devices. It provides bindings for C++ and Unity.
22
+
23
+ ## Install from pip
24
+
25
+ You can install the ailia LLM free evaluation package with the following command.
26
+
27
+ ```
28
+ pip3 install ailia_llm
29
+ ```
30
+
31
+ ## Install from package
32
+
33
+ You can install the ailia LLM from Package with the following command.
34
+
35
+ ```
36
+ python3 bootstrap.py
37
+ pip3 install ./
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ```python
43
+ import ailia_llm
44
+
45
+ import os
46
+ import urllib.request
47
+
48
+ model_file_path = "gemma-2-2b-it-Q4_K_M.gguf"
49
+ if not os.path.exists(model_file_path):
50
+ urllib.request.urlretrieve(
51
+ "https://storage.googleapis.com/ailia-models/gemma/gemma-2-2b-it-Q4_K_M.gguf",
52
+ model_file_path
53
+ )
54
+
55
+ model = ailia_llm.AiliaLLM()
56
+ model.open(model_file_path)
57
+
58
+ messages = []
59
+ messages.append({"role": "system", "content": "語尾に「わん」をつけてください。"})
60
+ messages.append({"role": "user", "content": "あなたの名前は何ですか?"})
61
+
62
+ stream = model.generate(messages)
63
+
64
+ text = ""
65
+ for delta_text in stream:
66
+ text = text + delta_text
67
+ print(text)
68
+
69
+ if model.context_full():
70
+ raise Exception("Context full")
71
+
72
+ messages.append({"role": "assistant", "content": text})
73
+ ```
74
+
75
+ ## API specification
76
+
77
+ https://github.com/axinc-ai/ailia-sdk
78
+
@@ -0,0 +1,17 @@
1
+ README.md
2
+ setup.py
3
+ ailia_llm/LICENSE_AILIA_EN.pdf
4
+ ailia_llm/LICENSE_AILIA_JA.pdf
5
+ ailia_llm/__init__.py
6
+ ailia_llm.egg-info/PKG-INFO
7
+ ailia_llm.egg-info/SOURCES.txt
8
+ ailia_llm.egg-info/dependency_links.txt
9
+ ailia_llm.egg-info/requires.txt
10
+ ailia_llm.egg-info/top_level.txt
11
+ ailia_llm/linux/x64/libailia_llm.so
12
+ ailia_llm/mac/libailia_llm.dylib
13
+ ailia_llm/oss/LICENSE_LLAMA_CPP.txt
14
+ ailia_llm/windows/x64/ailia_llm.dll
15
+ ailia_llm/windows/x64/ailia_llm_fallback.dll
16
+ ailia_llm/windows/x64/ggml.dll
17
+ ailia_llm/windows/x64/llama.dll
@@ -0,0 +1 @@
1
+ ailia
@@ -0,0 +1 @@
1
+ ailia_llm
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,70 @@
1
+ import os
2
+ import sys
3
+ import platform
4
+ import glob
5
+ import shutil
6
+ import platform
7
+
8
+ from setuptools import setup, Extension
9
+ from setuptools import find_packages
10
+
11
+ with open("README.md", encoding="utf-8") as f:
12
+ long_description = f.read()
13
+
14
+ scripts = []
15
+ for f in glob.glob("ailia_llm/*.py"):
16
+ scripts.append(f)
17
+
18
+ def find_libraries():
19
+ dll_names = []
20
+ platforms = ["win32", "darwin", "linux_armv7l", "linux_aarch64", "linux_x86_64"]
21
+
22
+ for platform in platforms:
23
+ if platform == "win32":
24
+ dll_platform = "windows/x64"
25
+ dll_type = ".dll"
26
+ elif platform == "darwin":
27
+ dll_platform = "mac"
28
+ dll_type = ".dylib"
29
+ else:
30
+ if platform == "linux_armv7l":
31
+ dll_platform = "linux/armeabi-v7a"
32
+ elif platform == "linux_aarch64":
33
+ dll_platform = "linux/arm64-v8a"
34
+ else:
35
+ dll_platform = "linux/x64"
36
+ dll_type = ".so"
37
+
38
+ dll_path = "./ailia_llm/" + dll_platform + "/"
39
+
40
+ for f in glob.glob(dll_path+"*"+dll_type):
41
+ f = f.replace("\\", "/")
42
+ f = f.replace("./ailia_llm/", "./")
43
+ dll_names.append(f)
44
+
45
+ dll_names.append("./LICENSE_AILIA_EN.pdf")
46
+ dll_names.append("./LICENSE_AILIA_JA.pdf")
47
+ dll_names.append("./oss/LICENSE_LLAMA_CPP.txt")
48
+
49
+ return dll_names
50
+
51
+ if __name__ == "__main__":
52
+ setup(
53
+ name="ailia_llm",
54
+ scripts=scripts,
55
+ version="1.2.0.0",
56
+ install_requires=[
57
+ "ailia",
58
+ ],
59
+ description="ailia LLM",
60
+ long_description=long_description,
61
+ long_description_content_type="text/markdown",
62
+ author="ax Inc.",
63
+ author_email="contact@axinc.jp",
64
+ url="https://ailia.jp/",
65
+ license="https://ailia.ai/en/license/",
66
+ packages=find_packages(),
67
+ package_data={"ailia_llm":find_libraries()},
68
+ include_package_data=True,
69
+ python_requires=">3.6",
70
+ )