Upload ms-swift/examples/deploy/client/llm/base/swift_client.py with huggingface_hub
Browse files
ms-swift/examples/deploy/client/llm/base/swift_client.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Alibaba, Inc. and its affiliates.
|
| 2 |
+
import os
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def infer_batch(engine: 'InferEngine', infer_requests: List['InferRequest']):
|
| 9 |
+
request_config = RequestConfig(max_tokens=64, temperature=0)
|
| 10 |
+
|
| 11 |
+
resp_list = engine.infer(infer_requests, request_config)
|
| 12 |
+
|
| 13 |
+
query0 = infer_requests[0].messages[0]['content']
|
| 14 |
+
print(f'query0: {query0}')
|
| 15 |
+
print(f'response0: {resp_list[0].choices[0].message.content}')
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def run_client(host: str = '127.0.0.1', port: int = 8000):
|
| 19 |
+
engine = InferClient(host=host, port=port)
|
| 20 |
+
print(f'models: {engine.models}')
|
| 21 |
+
|
| 22 |
+
infer_requests = [InferRequest(messages=[{'role': 'user', 'content': '浙江 -> 杭州\n安徽 -> 合肥\n四川 ->'}])]
|
| 23 |
+
infer_batch(engine, infer_requests)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
if __name__ == '__main__':
|
| 27 |
+
from swift.llm import InferEngine, InferRequest, InferClient, RequestConfig, run_deploy, DeployArguments
|
| 28 |
+
# NOTE: In a real deployment scenario, please comment out the context of run_deploy.
|
| 29 |
+
with run_deploy(
|
| 30 |
+
DeployArguments(
|
| 31 |
+
model='Qwen/Qwen2.5-1.5B', verbose=False, log_interval=-1, infer_backend='pt',
|
| 32 |
+
use_chat_template=False)) as port:
|
| 33 |
+
run_client(port=port)
|