(port: int, print_logprobs: bool = False, test_vlm: bool = False)
| 1 | def _test_client(port: int, print_logprobs: bool = False, test_vlm: bool = False): |
| 2 | import aiohttp |
| 3 | import time |
| 4 | from pprint import pprint |
| 5 | |
| 6 | from swift.infer_engine import InferClient, InferRequest, RequestConfig |
| 7 | |
| 8 | infer_client = InferClient(port=port) |
| 9 | |
| 10 | while True: |
| 11 | try: |
| 12 | models = infer_client.models |
| 13 | print(f'models: {models}') |
| 14 | except aiohttp.ClientConnectorError: |
| 15 | time.sleep(5) |
| 16 | continue |
| 17 | break |
| 18 | |
| 19 | if test_vlm: |
| 20 | query = '这是什么' |
| 21 | # http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png |
| 22 | messages = [{ |
| 23 | 'role': |
| 24 | 'user', |
| 25 | 'content': [ |
| 26 | { |
| 27 | 'type': 'text', |
| 28 | 'text': '这是什么' |
| 29 | }, |
| 30 | { |
| 31 | 'type': 'image_url', |
| 32 | 'image_url': { |
| 33 | 'url': 'cat.png' |
| 34 | } |
| 35 | }, |
| 36 | ] |
| 37 | }] |
| 38 | else: |
| 39 | query = '123*234=?' |
| 40 | messages = [{'role': 'user', 'content': query}] |
| 41 | |
| 42 | infer_request = InferRequest(messages=messages) |
| 43 | request_config = RequestConfig(seed=42, max_tokens=256, temperature=0.8, logprobs=True, top_logprobs=5) |
| 44 | |
| 45 | resp = infer_client.infer([infer_request], request_config=request_config)[0] |
| 46 | response = resp.choices[0].message.content |
| 47 | print(f'query: {query}') |
| 48 | print(f'response: {response}') |
| 49 | if print_logprobs: |
| 50 | pprint(resp.choices[0].logprobs) |
| 51 | |
| 52 | request_config = RequestConfig( |
| 53 | stream=True, seed=42, max_tokens=256, temperature=0.8, top_k=20, top_p=0.8, logprobs=True, top_logprobs=5) |
| 54 | gen_list = infer_client.infer([infer_request], request_config=request_config) |
| 55 | print(f'query: {query}') |
| 56 | print('response: ', end='') |
| 57 | for chunk in gen_list[0]: |
| 58 | if chunk is None: |
| 59 | continue |
| 60 | print(chunk.choices[0].delta.content, end='', flush=True) |
no test coverage detected