debgpt 0.5.0__py3-none-any.whl

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.
debgpt/__init__.py ADDED
@@ -0,0 +1,28 @@
1
+ '''
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Mo Zhou <lumin@debian.org>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ '''
24
+ __version__ = '0.5.0'
25
+ __copyright__ = '2024, Mo Zhou <lumin@debian.org>'
26
+ __license__ = 'MIT'
27
+ # do not load all components.
28
+ # some components like llm, and backend, requires much more dependencies
debgpt/backend.py ADDED
@@ -0,0 +1,107 @@
1
+ '''
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Mo Zhou <lumin@debian.org>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ '''
24
+ from rich.status import Status
25
+ from typing import List, Dict
26
+ import argparse
27
+ import zmq
28
+ from . import llm
29
+ import rich
30
+ import torch as th
31
+ console = rich.get_console()
32
+
33
+
34
+ class AbstractBackend:
35
+ def __init__(self, args):
36
+ self.llm = llm.create_llm(args)
37
+
38
+ def listen(self, args):
39
+ raise NotImplementedError
40
+
41
+ def server(self):
42
+ raise NotImplementedError
43
+
44
+
45
+ def stat_messages(messages: List[Dict], llm):
46
+ context_size = llm.tok.apply_chat_template(messages, tokenize=True,
47
+ return_tensors='pt').size(1)
48
+ ret = f'num_msgs={len(messages)}, ctx_size={context_size}; '
49
+ ret += f'latest={messages[-1]}'
50
+ return ret
51
+
52
+
53
+ class ZMQBackend(AbstractBackend):
54
+ def __init__(self, args):
55
+ super().__init__(args)
56
+ self.socket = zmq.Context().socket(zmq.REP)
57
+ binduri = args.host + ':' + str(args.port)
58
+ self.socket.bind(binduri)
59
+ console.log(f'ZMQBackend> bind URI {binduri}. Ready to serve.')
60
+
61
+ def listen(self):
62
+ while True:
63
+ msg = self.socket.recv_json()
64
+ yield msg
65
+
66
+ def server(self):
67
+ for query in self.listen():
68
+ console.log(
69
+ f'ZMQBackend> recv query: {stat_messages(query, self.llm)}', markup=False)
70
+ with Status('LLM Calculating ...', spinner='line'):
71
+ reply = self.llm(query)
72
+ console.log(
73
+ f'ZMQBackend> send reply: {stat_messages(reply, self.llm)}', markup=False)
74
+ msg_json = zmq.utils.jsonapi.dumps(reply)
75
+ self.socket.send(msg_json)
76
+
77
+
78
+ def create_backend(args):
79
+ if args.backend_impl == 'zmq':
80
+ backend = ZMQBackend(args)
81
+ else:
82
+ raise NotImplementedError(args.backend_impl)
83
+ return backend
84
+
85
+
86
+ if __name__ == '__main__':
87
+ ag = argparse.ArgumentParser()
88
+ ag.add_argument('--port', '-p', type=int, default=11177,
89
+ help='"11177" looks like "LLM"')
90
+ ag.add_argument('--host', type=str, default='tcp://*')
91
+ ag.add_argument('--backend_impl', type=str,
92
+ default='zmq', choices=('zmq',))
93
+ ag.add_argument('--max_new_tokens', type=int, default=512)
94
+ ag.add_argument('--llm', type=str, default='Mistral7B')
95
+ ag.add_argument('--device', type=str,
96
+ default='cuda' if th.cuda.is_available() else 'cpu')
97
+ ag.add_argument('--precision', type=str,
98
+ default='fp16' if th.cuda.is_available() else '4bit')
99
+ ag = ag.parse_args()
100
+ console.log(ag)
101
+
102
+ backend = create_backend(ag)
103
+ try:
104
+ backend.server()
105
+ except KeyboardInterrupt:
106
+ pass
107
+ console.log('Server shut down.')