说明
使用ollama可以很方便的运行本地大模型(包括官方模型和gguf量化模型),使用streamlit快速构建对话界面。
安装ollama
以linux系统为例
在线安装
在线安装直接按官方命令执行:curl -fsSL https://ollama.com/install.sh | sh
,但鉴于国内网络下不动,可以考虑手动安装
手动安装
下载安装包: curl -L https://ollama.com/download/ollama-linux-amd64.tgz -o ollama-linux-amd64.tgz
解压: sudo tar -C /usr -xzf ollama-linux-amd64.tgz
运行服务: ollama serve
下载模型文件
此处使用modelscope下载qwen的gguf量化模型
安装modelscope下载工具:pip install -U modelscope
下载模型文件: modelscope download --model=Qwen/Qwen2.5-Coder-32B-Instruct-GGUF --include "qwen2.5-coder-32b-instruct-q5_k_m*.gguf" --local_dir .
创建ModelFile
ModelFile用于ollama构建本地模型,示例如下:
1
2
3
4
5
6
7
8FROM ./QwQ-32B-Preview-GGUF/qwen2.5-coder-32b-instruct-q5_k_m.gguf
# sets the temperature to 1 [higher is more creative, lower is more coherent]
PARAMETER temperature 1
# sets the context window size to 4096, this controls how many tokens the LLM can use as context to generate the next token
PARAMETER num_ctx 4096
# sets a custom system message to specify the behavior of the chat assistant
SYSTEM You are Mario from super mario bros, acting as an assistant.
其中FROM为本地大模型的路径
构建ollama本地大模型
执行:ollama create mymodel -f ./Modelfile
名字可以随便起,Modelfile为刚才创建的Modelfile文件路径。
创建完成后运行: ollama run mymodel
编写streamlit页面并与ollama对接
依赖
需要安装的依赖如下:
1 | pip install transformers |
如无法运行可能需要安装torch cuda环境,具体安装此处省略。
编写python代码
创建main.py
编写如下代码:
1 |
|
最终运行启动命令: streamlit run main.py
- 本文作者: reiner
- 本文链接: https://reiner.host/posts/8d748f6c.html
- 版权声明: 转载请注明出处,并附上原文链接