Ollama + Web UI + Continue搭建个人本地大模型工具
MichaelJackyang
2024年05月26日 16:07
收录于文集
共1篇
大语言模型 (LLM)

Ollama简介 Ollama是一个开源的大型语言模型服务工具,它帮助用户快速在本地运行大模型。通过简单的安装指令,用户可以执行一条命令就在本地运行开源大型语言模型,如Llama 2。Ollama极大地简化了在Docker容器内部署和管理LLM的过程,使得用户能够快速地在本地运行大型语言模型。结合web ui工具与VScode插件 Continue即可在本地体验 chatgpt + copilot的效果;

ollama安装

Linux/wsl2

官方文档:https://github.com/ollama/ollama/blob/main/docs/linux.md

推荐自动安装命令:

curl -fsSL https://ollama.com/install.sh | sh

此过程耗时较长,建议休息或夜晚期间进行下载;该脚本安装完成后会将ollama服务设置为开机启动(wsl启动拉起)。

执行ollama如下即表示安装成功

Windows

下载地址:https://ollama.com/download/windows

下载完成后管理员权限安装即可;

ollama运行

ollama运行依赖于后台服务 ollama serve,请确保服务正在运行;

windows上打开显示隐藏的图片,存在ollama羊驼标志即表明服务正在运行;

Linux/WSL2中使用ps查看进程状态:ps -ef | grep ollama

ollama run [modelname]

打开模型库(https://ollama.com/library),选择或者搜索需要的模型名,单击所选模型名称

其中1表示模型的版本,2表示运行模型的指令

运行模型,本次演示时模型已完成下载,所以直接进入了运行环节,第一次启动时需要等待模型下载完成后启动;

ollama配置

ollama服务默认绑定的ip为127.0.0.1,端口为11434;

下载的模型文件存在路径:

  • macOS: ~/.ollama/models

  • Linux: /usr/share/ollama/.ollama/models

  • Windows: C:\Users\%username%\.ollama\models

根据个人需要可以通过设置环境变量更改上述配置;

OLLAMA_HOST:修改绑定地址

OLLAMA_ORIGINS:跨域列表,局域网内部访问可设置成*

OLLAMA_MODELS :修改模型存放地址

注意:配置完成后需要重启服务!!!重启服务!!!重启服务!!!

Linux/wsl2

修改监听ip地址

建议将默认绑定地址修改成0.0.0.0,监听所有网络,避免docker服务映射宿主机ip时无法访问本地回环的服务

  1. sudo vim /etc/systemd/system/ollama.service

  2. add a line Environment under section [Service]

  • [Service]

  • Environment="OLLAMA_HOST=0.0.0.0"

  1. 保存并退出

  2. 重新加载systemd服务配置并重启Ollama服务:

sudo systemctl daemon-reload

sudo systemctl restart ollama

修改模型路径

systemd自启服务

修改服务配置 /etc/systemd/system/ollama.service,配置环境变量 OLLAMA_MODELS;

  • ...

  • [Service]

  • ...

  • Environment="OLLAMA_MODELS=YOUR_PATH"

  • ...

  • ​

注意:systemd配置服务启动按照配置文件的用户权限运行,ollama脚本安装默认生成的ollama.service中用户为ollama,对于指定的路径不一定具有读写权限,造成服务无法正常启动。这里需要修改配置中用户名为个人登录WSL/Ubuntu的用户名,保证执行时具有对应路径的读写权限;

  • [Service]

  • User=your_username

  • Group=your_groupname

用户手动启动

  • OLLAMA_MODELS=YOUR_PATH OLLAMA_HOST=0.0.0.0 ollama serve

Windows

直接设置环境变量即可

交互访问

VS Code

插件链接:https://github.com/ollama/ollama?tab=readme-ov-file#extensions--plugins

可以在vscode中安装插件调用本地大模型,实现类似copilot的效果;

continue

安装使用

在vscode插件库中直接搜索,并完成安装。

1.在vscode插件库中搜索continue,并完成安装。

2.添加模型,选择Ollama

3.选择高级配置,配置api

  • 打开高级设置选项

  • 将localhost改成部署ollama的地址即可,wsl中运行需要填写wsl2的虚拟ip地址,否则无法自动检测到;

  • 点击Autodetect即可完成模型自动检测(模型需要提前下载好)

4.选择使用的模型即可正常开启对话以及代码提示相关功能

在文本框中输入文字即可开启对话啦

配置文件

上述过程配置好后会在系统用户路径下生产配置文件config.json;后续启动vscode或者重新加载窗口,continue插件会根据配置文件自动加载可使用模型;在界面中如下所示:

{

 "models": [

  {

     "model": "AUTODETECT",

     "title": "Ollama (1)",

     "completionOptions": {},

     "apiBase": "http://172.20.207.36:11434",

     "provider": "ollama"

  },

  {

     "model": "AUTODETECT",

     "title": "Ollama",

     "completionOptions": {},

     "apiBase": "http://10.106.44.117:11434",

     "provider": "ollama"

  }

],

 "customCommands": [

  {

     "name": "test",

     "prompt": "{{{ input }}}\n\nWrite a comprehensive set of unit tests for the selected code. It should setup, run tests that check for correctness including important edge cases, and teardown. Ensure that the tests are complete and sophisticated. Give the tests just as chat output, don't edit any file.",

     "description": "Write unit tests for highlighted code"

  }

],

 "tabAutocompleteModel": {

   "title": "Starcoder2 3b",

   "provider": "ollama",

   "model": "starcoder2:3b"

},

 "allowAnonymousTelemetry": true

}

我们只需要关心apiBase即可;由于wsl每次开机的虚拟网卡ip是随机的,所以需要开机启动wsl后修改wsl的ip才可继续使用服务;服务器则无需此配置。

Web & Desktop

https://github.com/ollama/ollama?tab=readme-ov-file#web--desktop

Ollama Web UI

安装方法

可直接参考项目README,https://github.com/open-webui/open-webui/blob/main/README.md

docker run -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main

使用

1.本地浏览器 localhost:3000即可打开服务

注册账号,可随便填写,本地无所谓~,但是需要记得密码,免得造成不必要的麻烦;

2.登录后选择一个模型,即可开始

若存在无法选择模型,请检查后台ollama服务是否正在运行,或者监听ip是否已修改为0.0.0.0

其他辅助工具

wsl开机自启,并且能自动更新continue配置文件

Set ws = CreateObject("Wscript.Shell")

' 启动 WSL

ws.run "wsl -d Ubuntu-22.04", vbhide

' 等待一段时间以确保 WSL 完全启动

WScript.Sleep 30000

' 获取 WSL 的 IP 地址

Set output = ws.exec("wsl hostname -I")

counter = 0

Do While output.Status = 0 And counter < 100

   WScript.Sleep 100

   counter = counter + 1

Loop

WSL_IP = Split(output.StdOut.ReadAll(), " ")(0)

' 更新 config.json 文件

Set fso = CreateObject("Scripting.FileSystemObject")

' Administrator 替换为自己用户名

Set file = fso.OpenTextFile("C:\Users\Administrator\.continue\config.json", 1)

content = file.ReadAll()

file.Close()

Set regex = New RegExp

regex.Pattern = "http://[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:11434"

regex.Global = True

content = regex.Replace(content, "http://" & WSL_IP & ":11434")

' Administrator 替换为自己用户名

Set file = fso.OpenTextFile("C:\Users\Administrator\.continue\config.json", 2)

file.Write content

file.Close()

将上述内容拷贝 C:\Users\{Your_username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup下,并保存为 .vbs后缀,即可实现开机自启动wsl并同步修改continue配置。