前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >lmdeploy 部署 InternLM2.5 7B 多轮对话

lmdeploy 部署 InternLM2.5 7B 多轮对话

原创
作者头像
用户10497140
修改2024-10-16 09:37:31
750
修改2024-10-16 09:37:31

LMDeploy 是一个高效且友好的 LLMs 模型部署工具箱,功能涵盖了量化、推理和服务

首先我们查看一下 LMDeploy 支持的模型 InternLM2.5 7B LLM

LMDeploy 部署 InternLM2.5 _ 7B_chat模型 对话 prompt 信息抽取

启动API服务器

首先让我们进入创建好的conda环境,并通下命令启动API服务器,部署InternLM2.5模型:

代码语言:javascript
复制
conda activate lmdeploy
lmdeploy serve api_server \
    /root/models/internlm2_5-7b-chat \
    --model-format hf \
    --quant-policy 0 \
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

命令解释:

  1. lmdeploy serve api_server:这个命令用于启动API服务器。
  2. /root/models/internlm2_5-7b-chat:这是模型的路径。
  3. --model-format hf:这个参数指定了模型的格式。hf代表“Hugging Face”格式。
  4. --quant-policy 0:这个参数指定了量化策略。
  5. --server-name 0.0.0.0:这个参数指定了服务器的名称。在这里,0.0.0.0是一个特殊的IP地址,它表示所有网络接口。
  6. --server-port 23333:这个参数指定了服务器的端口号。在这里,23333是服务器将监听的端口号。
  7. --tp 1:这个参数表示并行数量(GPU数量)。

稍待片刻,终端显示如下。

保持第一个终端不动,在新建终端中输入exit退出。

输入以下命令,使用Gradio作为前端,启动网页。

代码语言:javascript
复制
lmdeploy serve gradio http://localhost:23333 \
    --server-name 0.0.0.0 \
    --server-port 6006

稍待片刻,等终端如下图所示便保持两个终端不动。

打开浏览器,访问地址http://127.0.0.1:6006,然后就可以与模型尽情对话了。

结构化输出

先启动 InternLM2 模型的 api_server 服务。

代码语言:txt
复制

客户端可以使用 OpenAI 的 python 包进行测试:

代码语言:javascript
复制
touch /root/text2json.py

text2json.py

代码语言:txt
复制
from openai import OpenAI
guide = {
    'type': 'object',
    'properties': {
        'name': {
            'type': 'string'
        },
        'skills': {
            'type': 'array',
            'items': {
                'type': 'string',
                'maxLength': 10
            },
            'minItems': 3
        },
        'work history': {
            'type': 'array',
            'items': {
                'type': 'object',
                'properties': {
                    'company': {
                        'type': 'string'
                    },
                    'duration': {
                        'type': 'string'
                    }
                },
                'required': ['company']
            }
        }
    },
    'required': ['name', 'skills', 'work history']
}
response_format=dict(type='json_schema',  json_schema=dict(name='test',schema=guide))
messages = [{'role': 'user', 'content': 'Make a self-introduction please.'}]
client = OpenAI(api_key='YOUR_API_KEY', base_url='http://0.0.0.0:23333/v1')
model_name = client.models.list().data[0].id
response = client.chat.completions.create(
    model=model_name,
    messages=messages,
    temperature=0.8,
    response_format=response_format,
    top_p=0.8)
print(response)

输出结果是一个 json 格式的回答。

修改后的文件:

代码语言:txt
复制
import json
from openai import OpenAI
def text2json(query):
	guide = {
		'type': 'object',
		'properties': {
			'name': {
				'type': 'string'
			},
			'skills': {
				'type': 'array',
				'items': {
					'type': 'string',
					'maxLength': 10
				},
				'minItems': 3
			},
			'work history': {
				'type': 'array',
				'items': {
					'type': 'object',
					'properties': {
						'company': {
							'type': 'string'
						},
						'duration': {
							'type': 'string'
						}
					},
					'required': ['company']
				}
			}
		},
		'required': ['name', 'skills', 'work history']
	}
response_format=dict(type='json_schema',  json_schema=dict(name='test',schema=guide))
messages = [
{"role": "system", "content": "请你扮演一个角色,名叫Tom,是一位优秀的自然语言理解工程师,精通命名实体识别和关系抽取。同时你也对中医药学有深入研究,尤其是皮肤病相关疾病的诊断与治疗方面,不弱于世界上任何一位医药学家"},
]
messages.append({'role': 'user', 'content': '请找到下面这句话中的实体,实体类别,及实体之间的关系,关系的类别和关系的属性,这句话是:'+query})
client = OpenAI(api_key='internlm', base_url='http://127.0.0.1:23333/v1')
	model_name = client.models.list().data[0].id
response = client.chat.completions.create(
		model=model_name,
		messages=messages,
		temperature=0.1,
		response_format=response_format,
		top_p=0.8)
	res = response.json()
	print(type(res))
resdict = json.loads(res)
	result = resdict['choices'][0]['message']['content']#{"id":"5","choices":[{"finish_reason":"stop","index":0,"logprobs":null,"message":{"content":
		print(result)
			if __name__=='__main__':
				text2json('患者,女,23岁,初诊日期2003年12月20日。主诉:左胸部起群集水疱,剧疼3天。现病史:3天前,左胸部疼痛,继而加重并逐渐出现红斑,及簇集小水疱,疼痛剧烈,夜不能寐,口干喜冷饮,大便干结。体格检查:左胸部沿肋骨方向可见红斑及簇集的水疱,部分为血疱,基底潮红。脉象:弦数。舌象:舌苔薄黄。西医诊断:带状疱疹。中医辨证:肝胆湿热。立法:清利肝胆湿热。方药:龙胆草12g、银花12g、连翘10g、赤芍10g、黄芩10g、山栀子10g、当归10g、红花10g、瓜蒌10g、泽兰10g、车前子10g,3剂,每日1剂。12月23日上方服3剂后,局部水疱逐渐消退,疼痛减轻,大便已通。又继服3剂,局部泡疹已干燥结痂、脱屑,疼痛基本消失。'输出结果是一个 json 格式的回答。
代码语言:txt
复制
import json
from openai import OpenAI
def text2json(query):
	guide = {
		'type': 'object',
		'properties': {
			'name': {
				'type': 'string'
			},
			'skills': {
				'type': 'array',
				'items': {
					'type': 'string',
					'maxLength': 10
				},
				'minItems': 3
			},
			'work history': {
				'type': 'array',
				'items': {
					'type': 'object',
					'properties': {
						'company': {
							'type': 'string'
						},
						'duration': {
							'type': 'string'
						}
					},
					'required': ['company']
				}
			}
		},
		'required': ['name', 'skills', 'work history']
	}
response_format=dict(type='json_schema',  json_schema=dict(name='test',schema=guide))
messages = [
{"role": "system", "content": "请你扮演一个角色,名叫Tom,是一位优秀的自然语言理解工程师,精通命名实体识别和关系抽取。同时你也对中医药学有深入研究,尤其是皮肤病相关疾病的诊断与治疗方面,不弱于世界上任何一位医药学家"},
]
messages.append({'role': 'user', 'content': '请找到下面这句话中的实体,实体类别,及实体之间的关系,关系的类别和关系的属性,这句话是:'+query})
client = OpenAI(api_key='internlm', base_url='http://127.0.0.1:23333/v1')
	model_name = client.models.list().data[0].id
response = client.chat.completions.create(
		model=model_name,
		messages=messages,
		temperature=0.1,
		response_format=response_format,
		top_p=0.8)
	res = response.json()
	print(type(res))
resdict = json.loads(res)
	result = resdict['choices'][0]['message']['content']#{"id":"5","choices":[{"finish_reason":"stop","index":0,"logprobs":null,"message":{"content":
		print(result)
			if __name__=='__main__':
				text2json('患者,女,23岁,初诊日期2003年12月20日。主诉:左胸部起群集水疱,剧疼3天。现病史:3天前,左胸部疼痛,继而加重并逐渐出现红斑,及簇集小水疱,疼痛剧烈,夜不能寐,口干喜冷饮,大便干结。体格检查:左胸部沿肋骨方向可见红斑及簇集的水疱,部分为血疱,基底潮红。脉象:弦数。舌象:舌苔薄黄。西医诊断:带状疱疹。中医辨证:肝胆湿热。立法:清利肝胆湿热。方药:龙胆草12g、银花12g、连翘10g、赤芍10g、黄芩10g、山栀子10g、当归10g、红花10g、瓜蒌10g、泽兰10g、车前子10g,3剂,每日1剂。12月23日上方服3剂后,局部水疱逐渐消退,疼痛减轻,大便已通。又继服3剂,局部泡疹已干燥结痂、脱屑,疼痛基本消失。')

查看端口 杀死进程

更换模型 重启服务

代码语言:javascript
复制
lmdeploy serve api_server \
    /root/InternLM/XTuner/merged01 \
    --model-format hf \
    --quant-policy 0 \
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

参考:

欢迎来到 LMDeploy 的中文教程! — lmdeploy

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 启动API服务器
  • 结构化输出
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档