【lagent】agent搭建

【lagent】agent搭建

启动webui服务

使用lmdeploy启动一个api_server

1
2
conda activate agent_camp3
lmdeploy serve api_server /share/new_models/Shanghai_AI_Laboratory/internlm2_5-7b-chat --model-name internlm2_5-7b-chat

另开一个终端使用stremlit启动agent_web应用

1
2
3
cd /root/agent_camp3/lagent
conda activate agent_camp3
streamlit run examples/internlm2_agent_web_demo.py

本地powershell建立ssh连接,进行端口映射

Q&A

阅读更多
【评测】opencompass-司南
【Fine-tuning】XTuner微调个人小助手
【RAG】使用Llamaindex框架部署InternLM2-1.8B

【RAG】使用Llamaindex框架部署InternLM2-1.8B

一、前置知识

  • 给模型注入新知识的方式
    • 内部方式:更新模型的权重,但训练代价较大。
    • 外部方式:给模型注入额外的上下文或外部信息,不改变其权重。
  • RAG 工作原理
    • 将问题编码成向量,在向量数据库中找到最相关的文档块(top-k chunks)。
    • 将知识源分割成小块,编码成向量并存储在向量数据库中。
    • 将检索到的文档块与原始问题一起作为提示输入到 LLM 中,生成最终的回答。
  • RAG 效果比对
    • 由于 xtuner 是较新的框架,InternLM2-Chat-1.8B 训练数据库中未收录相关信息,使用 RAG 前问答均未给出准确答案,使用后能获得想要的答案。

二、环境、模型准备

(一)配置基础环境

  • Intern Studio 服务器上部署 LlamaIndex

    • 打开 Intern Studio 界面,点击 创建开发机 配置开发机系统。
    • 填写 开发机名称 后,点击 选择镜像 使用 Cuda11.7-conda 镜像,在资源配置中选择 30% A100 * 1 的选项,立即创建开发机器。
    • 进入开发机后,创建新的 conda 环境,命名为 llamaindex,运行以下命令:
    1
    2
    3
    4
    5
    conda create -n llamaindex python=3.10
    conda env list
    conda activate llamaindex
    conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia
    pip install einops==0.7.0 protobuf==5.26.1
    • 环境激活后,命令行左边会显示当前环境名称。

(二)安装 LlamaIndex

  • 安装 LlamaIndex 和相关的包:

    1
    2
    conda activate llamaindex
    pip install llama-index==0.10.38 llama-index-llms-huggingface==0.2.0 "transformers[torch]==4.41.1" "huggingface_hub[inference]==0.23.1" huggingface_hub==0.23.1 sentence-transformers==2.7.0 sentencepiece==0.2.0

(三)下载 Sentence Transformer 词嵌入模型

  • 新建一个 python 文件,贴入以下代码:

    1
    2
    3
    4
    5
    6
    7
    import os

    # 设置环境变量
    os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'

    # 下载模型
    os.system('huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/model/sentence-transformer')
  • /root/llamaindex_demo 目录下执行该脚本自动开始下载。

(四)下载 NLTK 相关资源

阅读更多
【Prompt Engineering】LangGPT结构化提示词编写

【Prompt Engineering】LangGPT结构化提示词编写

前言

在日常使用大模型时,我发现它经常在数字比对这类基础问题上出错,并且输出结果很不严谨。为了解决这个问题,我尝试使用Prompt Engineering,并在网上找到了一个开源的「结构化提示词框架」-- LangGPT,以下是我的使用过程记录。


实现步骤

step0:前期准备

  1. 创建虚拟环境->激活虚拟环境->安装必要包文件

  2. 创建项目路径->进入项目

  3. 安装必要软件,如tmux

step1:模型部署模型下载->部署模型为OpenAI server->图形化界面调用
‬⁠⁠⁠
step3:langgpt结构化提示词⁠‬编写⁠‍‬⁠‬‬‬‌‌‌‍‌‌
偷懒大法:GPTS有LangGPT提示词专家,用大模型生成即可


tmux使用

tmux可以在终端中创建终端,将进程维持在后台。

阅读更多
8G显存玩转书生大模型Demo

8G显存玩转书生大模型Demo

环境配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建环境
conda create -n demo python=3.10 -y
# 激活环境
conda activate demo
# 安装 torch
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y
# 安装其他依赖
pip install transformers==4.38
pip install sentencepiece==0.1.99
pip install einops==0.8.0
pip install protobuf==5.27.2
pip install accelerate==0.33.0
pip install streamlit==1.37.0

InternLM2-Chat-1.8B 模型部署

一、用Cli Demo 部署

1.创建demo文件夹,用于存放代码。并创建 cli_demo.py文件

1
2
mkdir -p /root/demo
touch /root/demo/cli_demo.py

其中cli_demo.py 的代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


model_name_or_path = "/root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b"

tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True, device_map='cuda:0')
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map='cuda:0')
model = model.eval()

system_prompt = """You are an AI assistant whose name is InternLM (书生·浦语).
- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.
"""

messages = [(system_prompt, '')]

print("=============Welcome to InternLM chatbot, type 'exit' to exit.=============")

while True:
input_text = input("\nUser >>> ")
input_text = input_text.replace(' ', '')
if input_text == "exit":
break

length = 0
for response, _ in model.stream_chat(tokenizer, input_text, messages):
if response is not None:
print(response[length:], flush=True, end="")
length = len(response)
阅读更多