Conda环境运行本地大模型
·
Conda环境运行本地模型
conda安装:https://www.anaconda.com/docs/getting-started/miniconda/main
> conda --version
> conda create -n ai-dev python=3.11 -y
> conda activate ai-dev
modelscope安装及模型下载
> pip install modelscope
> modelscope download --model Qwen/Qwen3-0.6B --local_dir /home/llm/models/qwen0.6
模型下载
> modelscope download --model Qwen/Qwen3-0.6B --local_dir /home/llm/models/qwen0.6
> modelscope download --model Qwen/Qwen3-TTS-12Hz-0.6B-CustomVoice --local_dir /home/llm/models/qwen3-tts-0.6b
测试:test.py
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
class ChatHandler:
def __init__(self, model_path="/home/llm/models/qwen0.6"):
"""
初始化聊天模型处理器
Args:
model_path: 本地模型路径
"""
self.model_path = model_path
self.tokenizer = None
self.model = None
self._load_model()
def _load_model(self):
"""
加载模型和分词器
"""
print(f"加载模型: {self.model_path}")
# 加载tokenizer
self.tokenizer = AutoTokenizer.from_pretrained(
self.model_path,
trust_remote_code=True,
padding_side="right"
)
# 加载模型
self.model = AutoModelForCausalLM.from_pretrained(
self.model_path,
dtype=torch.float16,
device_map="auto",
trust_remote_code=True
)
print("模型加载完成")
def generate_response(self, messages, max_new_tokens=1024):
"""
生成模型回复
Args:
messages: 对话消息列表,格式为[{"role": "user", "content": "消息内容"}]
max_new_tokens: 最大生成token数,默认值增大以避免回复被截断
Returns:
str: 回复内容
"""
# 确保生成的回复不会被截断
if max_new_tokens < 512:
max_new_tokens = 512
# 构造对话
text = self.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True, enable_thinking=False
)
inputs = self.tokenizer(text, return_tensors="pt").to(self.model.device)
# 生成回复
outputs = self.model.generate(**inputs, max_new_tokens=max_new_tokens)
# 解码回复
response = self.tokenizer.decode(
outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True
)
return response
环境依赖导出
> conda env export --no-builds > environment.yml
environment.yml
name: ai-dev
channels:
- defaults
dependencies:
- _libgcc_mutex=0.1
- _openmp_mutex=5.1
- bzip2=1.0.8
- ca-certificates=2025.12.2
- expat=2.7.3
- ld_impl_linux-64=2.44
- libexpat=2.7.3
- libffi=3.4.4
- libgcc=15.2.0
- libgcc-ng=15.2.0
- libgomp=15.2.0
- libnsl=2.0.0
- libstdcxx=15.2.0
- libstdcxx-ng=15.2.0
- libuuid=1.41.5
- libxcb=1.17.0
- libzlib=1.3.1
- ncurses=6.5
- openssl=3.0.18
- packaging=25.0
- pip=25.3
- pthread-stubs=0.3
- python=3.11.14
- readline=8.3
- setuptools=80.10.1
- sqlite=3.51.1
- tk=8.6.15
- tzdata=2025c
- wheel=0.46.3
- xorg-libx11=1.8.12
- xorg-libxau=1.0.12
- xorg-libxdmcp=1.1.5
- xorg-xorgproto=2024.1
- xz=5.6.4
- zlib=1.3.1
- pip:
- accelerate==1.12.0
- aiofiles==24.1.0
- aiohappyeyeballs==2.6.1
- aiohttp==3.13.3
- aiosignal==1.4.0
- annotated-doc==0.0.4
- annotated-types==0.7.0
- anyio==4.12.1
- attrs==25.4.0
- audioread==3.1.0
- brotli==1.2.0
- certifi==2026.1.4
- cffi==2.0.0
- charset-normalizer==3.4.4
- click==8.3.1
- coloredlogs==15.0.1
- cuda-bindings==12.9.4
- cuda-pathfinder==1.3.3
- datasets==4.5.0
- decorator==5.2.1
- dill==0.4.0
- einops==0.8.2
- fastapi==0.128.0
- ffmpy==1.0.0
- filelock==3.20.3
- flatbuffers==25.12.19
- frozenlist==1.8.0
- fsspec==2025.10.0
- gradio==6.5.1
- gradio-client==2.0.3
- groovy==0.1.2
- h11==0.16.0
- hf-xet==1.2.0
- httpcore==1.0.9
- httpx==0.28.1
- huggingface-hub==0.36.0
- humanfriendly==10.0
- idna==3.11
- jinja2==3.1.6
- joblib==1.5.3
- lazy-loader==0.4
- librosa==0.11.0
- llvmlite==0.46.0
- markdown-it-py==4.0.0
- markupsafe==3.0.3
- mdurl==0.1.2
- modelscope==1.34.0
- mpmath==1.3.0
- msgpack==1.1.2
- multidict==6.7.1
- multiprocess==0.70.18
- networkx==3.6.1
- numba==0.63.1
- numpy==2.3.5
- nvidia-cublas-cu12==12.8.4.1
- nvidia-cuda-cupti-cu12==12.8.90
- nvidia-cuda-nvrtc-cu12==12.8.93
- nvidia-cuda-runtime-cu12==12.8.90
- nvidia-cudnn-cu12==9.10.2.21
- nvidia-cufft-cu12==11.3.3.83
- nvidia-cufile-cu12==1.13.1.3
- nvidia-curand-cu12==10.3.9.90
- nvidia-cusolver-cu12==11.7.3.90
- nvidia-cusparse-cu12==12.5.8.93
- nvidia-cusparselt-cu12==0.7.1
- nvidia-nccl-cu12==2.27.5
- nvidia-nvjitlink-cu12==12.8.93
- nvidia-nvshmem-cu12==3.4.5
- nvidia-nvtx-cu12==12.8.90
- onnxruntime==1.23.2
- orjson==3.11.6
- pandas==3.0.0
- peft==0.18.1
- pillow==12.1.0
- platformdirs==4.5.1
- pooch==1.9.0
- propcache==0.4.1
- protobuf==6.33.5
- psutil==7.2.2
- pyarrow==23.0.0
- pycparser==3.0
- pydantic==2.12.5
- pydantic-core==2.41.5
- pydub==0.25.1
- pygments==2.19.2
- python-dateutil==2.9.0.post0
- python-multipart==0.0.22
- pytz==2025.2
- pyyaml==6.0.3
- qwen-tts==0.0.5
- regex==2026.1.15
- requests==2.32.5
- rich==14.3.1
- safehttpx==0.1.7
- safetensors==0.7.0
- scikit-learn==1.8.0
- scipy==1.17.0
- semantic-version==2.10.0
- shellingham==1.5.4
- six==1.17.0
- soundfile==0.13.1
- sox==1.5.0
- soxr==1.0.0
- starlette==0.50.0
- sympy==1.14.0
- threadpoolctl==3.6.0
- tokenizers==0.22.2
- tomlkit==0.13.3
- torch==2.10.0
- torchaudio==2.10.0
- torchvision==0.25.0
- tqdm==4.67.1
- transformers==4.57.3
- triton==3.6.0
- typer==0.21.1
- typer-slim==0.21.1
- typing-extensions==4.15.0
- typing-inspection==0.4.2
- urllib3==2.6.3
- uvicorn==0.40.0
- xxhash==3.6.0
- yarl==1.22.0
prefix: /root/miniconda3/envs/ai-dev
更多推荐

所有评论(0)