1. ollama部署
conda环境准备
sh Miniconda3-....sh
# 查看安装是否成功
../miniconda3/bin/conda --version
# 激活base环境
../miniconda3/bin/conda init bash
source ~/.bashrc
安装ollama的python依赖包
pip install ollama
下载ollama的install文件
https://ollama.com/install.sh
下载后修改如下内容
status "Downloading ollama..."
# 注释此行
#curl --fail --show-error --location --progress-bar -o $TEMP_DIR/ollama "https://ollama.com/download/ollama-linux-${ARCH}${VER_PARAM}"
status "Installing ollama to $BINDIR..."
$SUDO install -o0 -g0 -m755 -d $BINDIR
#$SUDO install -o0 -g0 -m755 $TEMP_DIR/ollama $BINDIR/ollama
# 修改安装包位置
$SUDO install -o0 -g0 -m755 ./ollama-linux-amd64 $BINDIR/ollama
下载安装包,与install.sh放在同一目录
https://github.com/ollama/ollama/releases/
执行安装
./install.sh
在宿主机上直接安装时,ollama会默认启动,如果没有启动的话,需要手动启动ollama服务,启动后可以打开网页:http://127.0.0.1:11434
ollama serve
2. 下载模型
graphRAG需要一个大模型和一个向量模型,模型存放在…/.ollama/models位置。
ollama pull mistral #llm
ollama pull nomic-embed-text #embedding
ollama list
由于ollama使用的是GGUF格式的模型文件,在内网中pull无法连接时,可以先在外网安装一个ollama,将模型下载下来,将models文件夹复制到内网使用。
ollama采用如下命令调用模型。
采用api调用ollama的模型
# llm
curl http://localhost:11434/api/chat -d '{
"model": "mistral",
"messages": [
{
"role": "user",
"content": "why is the sky blue?"
}
],
"stream": false
}'
# embedding
curl http://localhost:11434/api/embeddings -d '{
"model": "nomic-embed-text",
"prompt": "The sky is blue because of Rayleigh scattering"
}'
3. 运行graphRAG
安装graphrag依赖包
pip install graphrag
在某一目录下新建input目录,将一个或多个txt文件放入input目录下。
初始化工作区
python -m graphrag.index --init --root ./graphrag
修改.env文件的GRAPHRAG_API_KEY为ollama
修改生成的settings.yaml文件
llm:
model: mistral
# 需要注意的是这是使用v1,尽管前面在测试ollama的api接口时,采用/api/chat,但是在graphrag中采用的是/v1/chat/completions
api_base: http://localhost:11434/v1
embeddings:
llm:
model: nomic-embed-text
api_base: http://localhost:11434/api
修改完毕后,构建图
python -m graphrag.index --root ./graphrag
执行中报错,这是因为graphrag依赖tiktoken,联网环境下tiktoken自动下载cl100k_base编码,在离线环境中需要修改。
Exception type: <class ‘requests.exceptions.ConnectionError’>
Exception value: HTTPSConnectionPool(host=‘openaipublic.blob.core.windows.net’, port=443): Max retries exceeded with url: /encodings/cl100k_base.tiktoken (Caused by NameResolutionError(“<urllib3.connection.HTTPSConnection object at 0x7fdf29eef1f0>: Failed to resolve ‘openaipublic.blob.core.windows.net’ ([Errno -3] Temporary failure in name resolution)”))
首先根据报错信息确认blobpath:https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken
手动下载cl100k_base.tiktoken文件,参考read_file_cached()函数中cache_key的计算方法,将文件重命名为:9b5ad71b2ce5302211f9c61530b329a4922fc6a4
在graphrag调用tiktoken位置,指定cl100k_base.tiktoken的位置:
import os
os.environ["TIKTOKEN_CACHE_DIR"] = "/mnt/temp/graphrag"
上述方法失败了,直接修改read_file()函数,解决,不报错了。
def read_file():
blobpath = "https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken"
cache_key = hashlib.sha1(blobpath.encode()).hexdigest()
cache_dir = "/mnt/temp/graphrag"
cache_path = os.path.join(cache_dir, cache_key)
with open(cache_path, "rb") as f:
data = f.read()
return data