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