windows搭建agent环境
首先安装一个uv,用来管理虚拟环境。
1. 安装msys2
我们一般都是在linux上开发程序,linux有很多的命令,例如ls,grep,echo等等,这些程序在windows都是没有的,msys2你可以认为就是一个小型的linux命令软件,它将linux的这些命令在windows上进行了重写,从用户角度看就像是在linux上开发的一样。可以认为msys2是Windows 上的 “轻量级 Linux 开发工具箱”。
https://www.msys2.org/
官网是从github上下载的,速度比较慢,国内可以通过清华镜像下载
https://mirrors.tuna.tsinghua.edu.cn/msys2/distrib/x86_64/
https://mirrors.tuna.tsinghua.edu.cn/msys2/distrib/i686/
我的系统是win10的,安装完成后打开MSYS2 UCRT64的窗口,然后修改pacman的镜像源,或者直接手动编辑文件也行
sed -i "s#https\?://mirror.msys2.org/#https://mirrors.tuna.tsinghua.edu.cn/msys2/#g" /etc/pacman.d/mirrorlist*
pacman是软件包管理的一个命令,与linux上的apt或者yum这些软件包管理命令并列。后续的一些软件,比如gcc,camke,git等都通过pacman进行下载安装,非常的方便。
# 同步包数据库并更新系统现有包(首次安装可省略更新系统,仅同步数据库即可)
pacman -Syu
2. 安装软件
pacman -S automake autoconf libtool m4 make patch pkg-config
pacman -S mingw-w64-ucrt-x86_64-gcc
pacman -S mingw-w64-ucrt-x86_64-cmake
pacman -S git
pacman -S mingw-w64-ucrt-x86_64-rust
3. 安装python环境
pacman也可以安装python,但是三方库总是安装不成功,所以就不折腾了,感兴趣的可以自己试试
# 安装python虚拟环境工具(推荐)
pacman -S mingw-w64-ucrt-x86_64-python-virtualenv
# 安装python包管理工具(pip已随核心包安装,此为补充)
pacman -S mingw-w64-ucrt-x86_64-python-pip
# 安装常用科学计算库(如numpy/pandas,适配UCRT64)
pacman -S mingw-w64-ucrt-x86_64-python-numpy mingw-w64-ucrt-x86_64-python-pandas
# 通过-Ss搜索自己想要安装的三方库,如果存在则可以直接安装,否则得想办法
pacman -Ss mingw-w64-ucrt-x86_64-python-openai
# -R=卸载,-n=删配置,-s=删无用依赖,-c=删依赖于该包的所有包
pacman -Rscn mingw-w64-ucrt-x86_64-python
直接通过应用商店安装的python,不知道为啥安装uv之后无法使用uv,还是直接下载一个吧,优先选择release版本
https://www.python.org/downloads/windows/
安装的时候记得勾选path,将python添加打运行路径中
3.1 修改pip镜像源
在国内使用Python时,默认的pip镜像源可能会导致下载速度较慢。通过修改镜像源,可以显著提高下载速度。在命令提示符中运行以下命令,将pip的下载源永久更改为清华大学开源镜像站:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
会将配置写入到
Writing to C:\Users\用户名\AppData\Roaming\pip\pip.ini
常用国内镜像源
豆瓣: http://pypi.douban.com/simple/
阿里云: http://mirrors.aliyun.com/pypi/simple/
华为云: https://repo.huaweicloud.com/repository/pypi/simple
清华大学: https://pypi.tuna.tsinghua.edu.cn/simple
中科大: https://pypi.mirrors.ustc.edu.cn/simple/
3.2. Python虚拟环境管理
我们通过uv来进行python虚拟环境的管理
pip install uv
安装完成后可以通过以下命令查看
- uv python install: 安装 Python 版本
- uv python list: 查看可用 Python 版本
- uv python find: 查找已安装的 Python 版本
- uv python pin: 将当前项目固定使用特定 Python 版本
- uv python uninstall: 卸载 Python 版本
查看有哪些可用的版本
uv python list
如果有安装则有具体的路径
cpython-3.15.0a3-windows-x86_64-none <download available>
cpython-3.15.0a3+freethreaded-windows-x86_64-none <download available>
cpython-3.14.2-windows-x86_64-none AppData\Local\Programs\Python\Python314\python.exe
cpython-3.14.2-windows-x86_64-none <download available>
cpython-3.14.2+freethreaded-windows-x86_64-none <download available>
cpython-3.13.11-windows-x86_64-none <download available>
cpython-3.13.11+freethreaded-windows-x86_64-none <download available>
cpython-3.12.12-windows-x86_64-none <download available>
cpython-3.11.14-windows-x86_64-none <download available>
cpython-3.10.19-windows-x86_64-none <download available>
cpython-3.9.25-windows-x86_64-none <download available>
cpython-3.8.20-windows-x86_64-none <download available>
pypy-3.11.13-windows-x86_64-none <download available>
pypy-3.10.16-windows-x86_64-none <download available>
pypy-3.9.19-windows-x86_64-none <download available>
pypy-3.8.16-windows-x86_64-none <download available>
graalpy-3.12.0-windows-x86_64-none <download available>
graalpy-3.11.0-windows-x86_64-none <download available>
graalpy-3.10.0-windows-x86_64-none <download available>
安装一个3.12版本
uv python install 3.12.12
3.2.1 创建虚拟环境
mkdir agent && cd agent
uv venv --python 3.12.12
Using CPython 3.12.12
Creating virtual environment at: .venv
Activate with: .venv\Scripts\activate
3.2.2 激活环境
powershell环境
# pwoershell环境临时放开权限
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
.venv\Scripts\activate.ps1
cmd环境
.venv\Scripts\activate
激活成功则如下显示,我们也可以执行python --version查看当前环境下的版本
(agent) D:\agent>
3.2.3 修改uv pip源
进入到C:\Users\你的用户名\AppData\Roaming
Win + E
%APPDATA%
创建一个uv目录,同时在uv目录创建一个uv.toml
# 1. CPython 代理配置(必须放在 [[index]] 段落上方!)
# 作用:切换腾讯官方同步源,加速 `uv python install 3.x` 下载 Python 解释器
python-install-mirror = "https://cnb.cool/astral-sh/python-build-standalone/-/releases/download/"
# 2. PyPI 源配置(第三方包下载,可配置多个)
[[index]]
url = "https://mirrors.cloud.tencent.com/pypi/simple"
default = true
[[index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
[[index]]
url = "https://mirrors.aliyun.com/pypi/simple"
[[index]]
url = "https://repo.huaweicloud.com/repository/pypi/simple"
[[index]]
url = "https://pypi.doubanio.com/simple"
[[index]]
url = "https://pypi.mirrors.ustc.edu.cn/simple"
[[index]]
url = "https://pypi.org/simple"
[[index]]
url = "https://pypi.python.org/simple"
4. 下载模型和依赖
不会魔法,国内使用modelscope镜像
https://www.modelscope.cn/models
安装依赖
uv pip install modelscope
uv pip install transformers
uv pip install torch
uv pip install sentencepiece
下载模型
modelscope download --model Qwen/Qwen3-0.6B --local_dir ./
5. 搭建LLM服务
5.1 编译llama.cpp
下载
git clone https://github.com/ggml-org/llama.cpp.git
# git clone https://gitclone.com/github.com/ggerganov/llama.cpp
编译
cd llama.cpp
cmake -B build -G "MinGW Makefiles" -DLLAMA_CURL=OFF -DGGML_CUDA=OFF
cmake --build build -j32
5.2 hf转成gguf格式
进行转换
cd llama.cpp
python convert_hf_to_gguf.py Qwen\Qwen3-0___6B --outtype f16 --verbose --outfile Qwen\Qwen3_6B_f16.gguf
进行量化
build\bin\llama-quantize.exe Qwen\Qwen3_6B_f16.gguf Qwen\Qwen3_6B_q4_k_m.gguf q4_k_m
启动服务
build\bin\llama-server.exe -m <modelPath> --host 0.0.0.0 --port 8080
build\bin\llama-server.exe -m Qwen3_6B_q4_k_m.gguf --host 0.0.0.0 --port 8080
测试用例
import openai
client = openai.OpenAI(
base_url="http://127.0.0.1:8080/v1",
api_key = "sk-no-key-required"
)
completion = client.chat.completions.create(
model="qwen", # model name can be chosen arbitrarily
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "请你给我讲一下快速排序算法"}
]
)
print(completion.choices[0].message.content)
更多推荐




所有评论(0)