Views
No views yet
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3tokenizer = AutoTokenizer.from_pretrained("AI4Bread/XiXi_Qwen_base_14b", trust_remote_code=True)
4# Set `torch_dtype=torch.float16` to load model in float16, otherwise it will be loaded as float32 and cause OOM Error.
5model = AutoModelForCausalLM.from_pretrained("AI4Bread/XiXi_Qwen_base_14b", torch_dtype=torch.float16, trust_remote_code=True).cuda()
6model = model.eval()
7response, history = model.chat(tokenizer, "你好", history=[])
8print(response)
9# Hello! How can I help you today?
10response, history = model.chat(tokenizer, "马铃薯育种有什么注意事项?需要注意什么呢?", history=history)
11print(response)stream_chat:1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_path = "AI4Bread/XiXi_Qwen_base_14b"
5model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16, trust_remote_code=True).cuda()
6tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
7
8model = model.eval()
9length = 0
10for response, history in model.stream_chat(tokenizer, "Hello", history=[]):
11 print(response[length:], flush=True, end="")
12 length = len(response)pip install lmdeploylmdeploy serve api_server internlm/internlm2-chat-7b --model-name internlm2-chat-7b --server-port 23333 1curl http://localhost:23333/v1/chat/completions \
2 -H "Content-Type: application/json" \
3 -d '{
4 "model": "internlm2-chat-7b",
5 "messages": [
6 {"role": "system", "content": "你是一个专业的农业专家"},
7 {"role": "user", "content": "马铃薯种植的时候有哪些注意事项?"}
8 ]
9 }'
vLLM>=0.3.2:pip install vllmpython -m vllm.entrypoints.openai.api_server --model internlm/internlm2-chat-7b --served-model-name internlm2-chat-7b --trust-remote-code1curl http://localhost:8000/v1/chat/completions \
2 -H "Content-Type: application/json" \
3 -d '{
4 "model": "internlm2-chat-7b",
5 "messages": [
6 {"role": "system", "content": "You are a professional agriculture expert."},
7 {"role": "user", "content": "Introduce potato farming to me."}
8 ]
9 }'1# Converting Model to TurboMind (FastTransformer Format)
2lmdeploy convert internlm2-chat-7b /root/autodl-tmp/agri_intern/XiXiLM --tokenizer-path ./GouMang/tokenizer.json
lmdeploy chat turbomind ./workspace1# ApiServer+Turbomind api_server => AsyncEngine => TurboMind
2lmdeploy serve api_server ./workspace \
3 --server-name 0.0.0.0 \
4 --server-port 23333 \
5 --tp 1server_name and server_port indicate the service address and port, respectively. The tp parameter, as mentioned earlier, stands for Tensor Parallelism.Since Gradio requires local access to display the interface, you also need to forward the data to your local machine via SSH. The command is as follows:ssh -CNg -L 6006:127.0.0.1:6006 root@ssh.intern-ai.org.cn -p
1# Gradio+ApiServer. The Server must be started first, and Gradio acts as the Client
2lmdeploy serve gradio http://0.0.0.0:23333 --server-port 60061# Gradio+Turbomind(local)
2lmdeploy serve gradio ./workspacepip install streamlit==1.24.01git clone https://github.com/AI4Bread/GouMang.git
2cd GouMangweb_demo.py with the path where the downloaded parameters of GouMang are storedweb_demo.py file in the directory, and after entering the following command, check this tutorial 5.2 for local port configuration,to map the port to your local machine. Enter http://127.0.0.1:6006 in your local browser.streamlit run web_demo.py --server.address 127.0.0.1 --server.port 6006http://127.0.0.1:6006 page in your browser.
Once the model is loaded, you can start conversing with GouMang like this.
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3tokenizer = AutoTokenizer.from_pretrained("AI4Bread/XiXiLM_14b", trust_remote_code=True)
4# Set `torch_dtype=torch.float16` to load model in float16, otherwise it will be loaded as float32 and cause OOM Error.
5model = AutoModelForCausalLM.from_pretrained("AI4Bread/XiXiLM_14b", torch_dtype=torch.float16, trust_remote_code=True).cuda()
6model = model.eval()
7response, history = model.chat(tokenizer, "你好", history=[])
8print(response)
9# Hello! How can I help you today?
10response, history = model.chat(tokenizer, "马铃薯育种有什么注意事项?需要注意什么呢?", history=history)
11print(response)stream_chat 接口:1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_path = "AI4Bread/XiXi_Qwen_base_14b"
5model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16, trust_remote_code=True).cuda()
6tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
7
8model = model.eval()
9length = 0
10for response, history in model.stream_chat(tokenizer, "马铃薯育种有什么注意事项?需要注意什么呢?", history=[]):
11 print(response[length:], flush=True, end="")
12 length = len(response)pip install lmdeploylmdeploy serve api_server internlm/internlm2-chat-7b --server-port 233331curl http://localhost:23333/v1/chat/completions \
2 -H "Content-Type: application/json" \
3 -d '{
4 "model": "internlm2-chat-7b",
5 "messages": [
6 {"role": "system", "content": "你是一个专业的农业专家"},
7 {"role": "user", "content": "马铃薯种植的时候有哪些注意事项?"}
8 ]
9 }'vLLM>=0.3.2启动兼容 OpenAI API 的服务:pip install vllmpython -m vllm.entrypoints.openai.api_server --model internlm/internlm2-chat-7b --trust-remote-code1curl http://localhost:8000/v1/chat/completions \
2 -H "Content-Type: application/json" \
3 -d '{
4 "model": "internlm2-chat-7b",
5 "messages": [
6 {"role": "system", "content": "你是一个专业的农业专家."},
7 {"role": "user", "content": "请给我介绍一下马铃薯育种."}
8 ]
9 }'1# 将模型转换为 TurboMind (FastTransformer 格式)
2lmdeploy convert internlm2-chat-7b /root/autodl-tmp/agri_intern/XiXiLM --tokenizer-path ./GouMang/tokenizer.json
lmdeploy chat turbomind ./workspace1# ApiServer+Turbomind api_server => AsyncEngine => TurboMind
2lmdeploy serve api_server ./workspace \
3 --server-name 0.0.0.0 \
4 --server-port 23333 \
5 --tp 1api_client 或 triton_client,而是执行 gradio。
请参考LMDeploy部分获取详细信息。由于 Gradio 需要本地访问展示界面,因此也需要通过 ssh 将数据转发到本地。命令如下:ssh -CNg -L 6006:127.0.0.1:6006 root@ssh.intern-ai.org.cn -p <你的 ssh 端口号>
1# Gradio+ApiServer。必须先开启 Server,此时 Gradio 为 Client
2lmdeploy serve gradio http://0.0.0.0:23333 --server-port 60061# Gradio+Turbomind(local)
2lmdeploy serve gradio ./workspace1git clone https://github.com/AI4Bread/GouMang.git
2cd GouMangweb_demo.py 中的模型路径替换为下载的 GouMang 参数存储路径streamlit run /root/personal_assistant/code/InternLM/web_demo.py --server.address 127.0.0.1 --server.port 6006http://127.0.0.1:6006 页面后,模型才会加载。
模型加载完成后,您就可以开始与 西西(句芒) 进行对话了。