-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (88 loc) · 3.84 KB
/
main.py
File metadata and controls
106 lines (88 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from time import sleep
import chat_pb2
from lmdeploy import GenerationConfig, TurbomindEngineConfig, pipeline
from dubbo import Dubbo
from dubbo.configs import RegistryConfig, ServiceConfig
from dubbo.proxy.handlers import RpcMethodHandler, RpcServiceHandler
# the path of a model. It could be one of the following options:
# 1. A local directory path of a turbomind model
# 2. The model_id of a lmdeploy-quantized model
# 3. The model_id of a model hosted inside a model repository
model = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"
backend_config = TurbomindEngineConfig(cache_max_entry_count=0.2, max_context_token_num=20544, session_len=20544)
gen_config = GenerationConfig(
top_p=0.95,
temperature=0.6,
max_new_tokens=8192,
stop_token_ids=[151329, 151336, 151338],
do_sample=True, # enable sampling
)
class DeepSeekAiServicer:
def __init__(self, model: str, backend_config: TurbomindEngineConfig, gen_config: GenerationConfig):
self.llm = pipeline(model, backend_config=backend_config)
self.gen_config = gen_config
def chat(self, stream):
# read request from stream
request = stream.read()
print(f"Received request: {request}")
# prepare prompts
prompts = [{"role": request.role, "content": request.content + "<think>\n"}]
is_think = False
# perform streaming inference
for item in self.llm.stream_infer(prompts, gen_config=gen_config):
# update think status
if item.text == "<think>":
is_think = True
continue
elif item.text == "</think>":
is_think = False
continue
# According to the state of thought, decide the content of the reply.
if is_think:
# send thought
stream.write(chat_pb2.ChatReply(think=item.text, answer=""))
else:
# send answer
stream.write(chat_pb2.ChatReply(think="", answer=item.text))
stream.done_writing()
def build_server_handler():
# build a method handler
deepseek_ai_servicer = DeepSeekAiServicer(model, backend_config, gen_config)
method_handler = RpcMethodHandler.server_stream(
deepseek_ai_servicer.chat,
method_name="chat",
request_deserializer=chat_pb2.ChatRequest.FromString,
response_serializer=chat_pb2.ChatReply.SerializeToString,
)
# build a service handler
service_handler = RpcServiceHandler(
service_name="org.apache.dubbo.samples.llm.api.DeepSeekAiService",
method_handlers=[method_handler],
)
return service_handler
if __name__ == "__main__":
# build a service handler
service_handler = build_server_handler()
service_config = ServiceConfig(service_handler=service_handler)
# Configure the Zookeeper registry
registry_config = RegistryConfig.from_url("zookeeper://zookeeper:2181")
bootstrap = Dubbo(registry_config=registry_config)
# Create and start the server
bootstrap.create_server(service_config).start()
# 30days
sleep(30 * 24 * 60 * 60)