语言大模型之不忘初心(上) - VSCode+Ollama

英伟达用一份出色的四季度财报给华尔街空头带来了高达30亿美元的账面损失。所有人都在疯狂买进英伟达,空头们在“AI信仰”的压力下,瑟瑟发抖。多头接着奏乐接着舞,AI的泡沫还在越吹越大。
然而,面对着繁花似锦,我却想到八个字:不忘初心、牢记使命。
AI的作用不是让它聊天、唱歌、拍电影;我们是要让他演变成为实实在在的生产力!所以,就从编程开始吧。今天我们看看如何搭建本地的大预言模型来帮助我们编程。

我们的目标: 本地搭建Ollama和Open-WebUI,然后用VSCode调用相关组件来实现自动化编程。
我们假设: Windows系统上使用WSL,安装了Ubuntu或者·Kali这样的子系统,Docker也已经配置好了。

首先是Ollama(羊驼)的安装:
- 访问https://ollama.com
- 点击下载
- 选择linux
- 拷贝linux 安装命令
进入linux命令行

执行
nvidia-smi
查看GPU状况 (如果没有安装nvidia-smi,那你就要先去vnidia官网按照教程安装了)
虽然这里可以看到显卡,但是并不代表ollama可以检测到,后续的安装,可能会有无法检测到GPU的报错,所以,建议你用下面的命令来安装lshw
sudo apt-get install -y lshw
好了,可以开始安装了
curl -fsSL https://ollama.com/install.sh | bash
安装好ollama,让我们来启动它并让它回答几个问题试试

在http://ollama.com 点击 models 获得模型列表,选择你喜欢的模型,比如gemma,然后点击Tags,找到适合你的发布标签,我们以现存需要较小的2b为例
在linux命令行检测ollama的版本并启动服务:
ollama –verison
ollama serve &
一切正常,开始下载模型数据并运行模型:
ollama pull gemma:2b
ollama run gemma:2b
使用的结果是这样的:
>>> write a python function to sort files in the folder
```python
import os
import shutil
def sort_files(folder_path):
# Get the full path to the folder
folder_path = os.path.join(os.getcwd(), folder_path)
# Get all the files in the folder
files = os.listdir(folder_path)
# Sort the files in ascending order by their name
files.sort()
# Delete the old files
for file in files:
if os.path.isfile(os.path.join(folder_path, file)):
shutil.rmtree(os.path.join(folder_path, file))
# Example usage:
sort_files("my_folder")
```
**Explanation:**
* The `os.listdir()` method returns a list of filenames in the specified folder.
* We use the `sort()` method to sort the files in ascending order by their names.
* We use the `os.path.join()` function to construct the full path to each file.
* We use the `shutil.rmtree()` function to delete the old files.
* The `os.getcwd()` function returns the current working directory, which is used to
join the folder path.
**Notes:**
* This function assumes that the folder contains only regular files. It will not
handle symbolic links or other special files.
* The `sort_files()` function will delete the old files in the folder, including any
subfolders and files.
* You can modify the `sort()` method to sort by a different criteria, such as size
or creation date.
* This function will only sort files in the current folder. You can modify the code
to sort files in a different folder by passing the folder path as an argument.
不仅仅有代码,还有解释,不错!
下面来做VSCode的集成:
- 启动VSCode
- 连接到对应的WSL distro
- 点击Extension图标
- 搜索框输入ollama
- 选择twinny - AI Code Completion and Chat
- 点击安装
注:Twinny插件比Ollama Autocoder多了chat功能,下载量和好评量也高。配置的大部分方法也适用于Ollama Autocoder

安装完,我们还需要进行相关的设置
- 点击齿轮图标
- 选择Extension Setting
- 选择User - twinny (你应该自动就是在这里)
- 检查配置项, 如端口、api路径、llama model, 你会发现,需要安装codellama模型
- 再linux的console运行
ollama pull codellama:7b-code
ollama pull codellama:7b-instruct
安装完所需要的模型,(如前文所写,确保你已经通过ollama serve & 启动了ollama并通过11434提供服务),你的集成工作就做好了。
现在可以试试你的本地大模型编程能力如何了!

- 点击twinny图标,打开聊天窗口
- 在聊天窗口中点击小机器人图标,再次确认所使用的ollama model
- 在聊天窗口中输入你的编程问题
- 经过思考,twinny会给出答案不仅包括了代码,还包括了必要的模块安装命令
- 代码块的下方,如果你选择对勾图标,代码会自动插入到你的程序中
- 你也可以选择拷贝图标,手工拷贝代码到想要放置的位置
不想这么麻烦?也没问题:
打开一个新文档,写出注释,ollama会自动建议代码给你,你就简单敲敲TAB,一行行代码就自动产生了。上面的例子中,我写了红色方框里的注释,后面的函数都是它自动生成的。
VSCode和ollama的集成已经告一段落。锦上添花,搭建本地RAG系统的下一步是搭建Open-WebUI。这项工作,我们后续完成。