1、GraphRAG是什么?
GraphRAG(Graph-based Relation-Aware Grouping)是一种基于图的关系感知分组方法,通常用于计算机视觉和机器学习领域。它的核心思想是利用图结构来表示和处理实体之间的关系,从而更有效地进行分组和识别任务。
2、本地部署
在autodl上进行运行,4090单显卡+24GB内存+pytorch框架2.3.0+python3.12
2.1创建虚拟环境
创建两个环境ollama和graphrag,避免环境产生冲突
conda create -n ollama python==3.11
conda create -n graphrag python==3.11
第一次进入环境可能会出现CondaError: Run 'conda init' before 'conda activate',具体解释见vscode虚拟环境-优快云博客
conda init
#关闭终端再重新打开来激活环境
2.2 ollama及工具包的安装
首先进入ollama环境安装ollama
conda activate ollama
pip install --user ollama -i https://pypi.mirrors.ustc.edu.cn/simple/
还要安装一下ollama工具包,官网给出的方法是:
curl -fsSL https://ollama.com/install.sh | sh
开梯子尝试安装显示网络连接异常,安装失败(也可以自行尝试,有几率能够成功),参考进行【AI基础】租用云GPU之autoDL部署大模型ollama+llama3_autodl ollama-优快云博客进行安装。
首先使用autodl自带的学术资源加速AutoDL帮助文档,
source /etc/network_turbo
执行命令
curl -fsSL https://ollama.com/install.sh | sh
可能直接安装成功,也可能会显示Warning,要求安装一些依赖包,按照要求进行安装和更新即可。
#示例
#WARNING: Unable to detect NVIDIA/AMD GPU.Install lspci or lshw to automatically detect and install GPU dependencies,
sudo apt-get update
sudo apt-get install lshw
然后重新执行安装命令即可。
ollama安装成功后默认以服务形式运行,可运行以下命令检查状态:
systemctl status ollama.service
若成功,显示:
ollama.service - Ollama Service
Loaded: loaded (/etc/systemd/system/ollama.service, enabled)
Active: activate (running)
若出现错误“System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down”,说明systemctl 命令异常,需要安装 systemd和systemctl,使用命令:
apt-get install systemd -y
apt-get install systemctl -y
安装成功后,可重新运行systemctl,检查状态。
2.3 安装graphrag
新建一个终端,进入graphrag环境,安装graphrag
conda activate graphrag
pip install --user graphrag -i https://pypi.mirrors.ustc.edu.cn/simple/
然后创建一下工作目录
mkdir -p ./ragtest/input
#ragtest为大文件名,input为放语料的文件名,可自行修改,后面运行时也要注意修改
将自己的语料放在input下面,文件格式为utf-8编码的txt文件。
初始化工作目录,生成setting.yaml、
python -m graphrag.index --init --root ./ragtest
2.4 修改.env内容如下
GRAPHRAG_API_KEY=ollama
GRAPHRAG_CLAIM_EXTRACTION_ENABLED=True
.env文件一般隐藏,可使用vim命令进入编辑,在vim中,按下i键进入INSERT模式,这时可以编辑文件内容,修改完毕后ESC退出插入格式,输入:wq并按回车保存更改并退出vim。
vim .env #进入.env
i #进入编辑模式
esc #修改完成后esc退出insert模式
:wq #保存修改内容并退出
2.5 setting.yaml修改
主要修改为model:qwen2和model: nomic-embed-text,按照自己选择的model和embedding进行修改即可。
encoding_model: cl100k_base
skip_workflows: []
llm:
api_key: ollama
type: openai_chat # or azure_openai_chat
model: qwen2
model_supports_json: true # recommended if this is available for your model.
# max_tokens: 4000
# request_timeout: 180.0
api_base: http://localhost:11434/v1
# api_version: 2024-02-15-preview
# organization: <organization_id>
# deployment_name: <azure_model_deployment_name