llama.cpp 编译使用
llama.cpp 是一个基于 llama 模型 (https://github.com/ggerganov/llama.cpp) 的 C++ 库,用于在 C++ 程序中运行 LLaMA(Large Language Model Meta AI)模型。
安装必要组件/工具
1apt install cmake
编译安装llama.cpp
1234567891011git clone https://github.com/ggerganov/llama.cpp.gitcd llama.cpp# 常规模式构建 llama.cppcmake -B buildcmake --build build --config Release# 使用 Nvidia GPUapt install nvidia-cuda-toolkit -ycmake -B build -DGGML_CUDA=ONcmake --build build --config Release
安装python组件
123pip install sentencepiece# 在llama.cpp目录pip instal ...
使用ollama部署本地大模型(启用GPU)
上一篇文章使用Ollama部署了本地大模型,不过那时还没有利用本地的GPU资源。如果需要启用GPU,还需要首先安装nvidia-container-toolkit。
12345678curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey \ | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpgcurl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \ | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \ | sudo tee /etc/apt/sources.list.d/nv ...
使用ollama部署本地大模型
Ollama是一个强大的工具,它允许用户在本地环境中运行和定制大型语言模型。而通过Docker技术,可以简化Ollama的安装和部署过程,使得在本地运行如Llama 3这样的开源大型语言模型变得简单快捷。
硬件配置要求由于大模型对硬件配置要求较高,建议使用配置更高的设备,本次测试暂时选用阿里云节点:
CPU:阿里云8vCPU
内存:16GB
显卡:无(使用CPU跑模型)
Docker安装OllamaOllama支持通过Docker安装,这极大地简化了服务器端的部署过程。以下是使用docker compose工具运行Ollama的步骤:
创建 docker-compose.yaml 文件,内容如下:
12345678910version: '3'services: ollama: image: ollama/ollama:0.4.4 container_name: ollama ports: - "11434:11434" volumes: - ./data:/root ...
从Huggingface和魔搭下载大模型
1、从Huggingface下载安装工具包
1pip install huggingface_hub
设置镜像站点
1export HF_ENDPOINT="https://hf-mirror.com"
开始下载
12345huggingface-cli download \ --repo-type model \ --local-dir ./Llama-3.2-3B-Chinese \ spxiong/Llama-3.2-3B-Chinese-Instruct
2、从魔搭下载12pip install modelscopepython3 -c "from modelscope import snapshot_download;snapshot_download('Qwen/Qwen2.5-3B-Instruct', cache_dir='./models/')"
python 小技巧整理
1、(:=)的使用Python 3.8 中引入,正式名称为赋值表达式,是一种将赋值与表达式相结合的新语法。
123456789101112131415161718# 收集用户输入,直到输入空行lines = []while (line := input("Enter something (leave blank to quit): ")) != "": lines.append(line) # 筛选生成随机数numbers = [n for _ in range(10) if (n := random.randint(1, 100)) > 50]print(numbers)# 赋值与if判断合成一行a = [1, 2, 3]if (n := len(a)) > 2: print(f"The list is long enough ({n} elements).") # 过滤并打印列表中的长单词words = ['apple', 'b ...
python 小技巧整理
1、下划线(_)的作用
python解析器最后一个表达式的值
一个可以忽略的中间变量,filename, _ = 'example.txt'.split('.')
格式化一个比较大的数据,amount = 1_000_000
2、zip用法12345678910111213ids = [1, 2, 3]names = ['Alice', 'Bob', 'Cathy']grades = ['A', 'B', 'A+']zipped = zip(ids, names, grades)students = list(zipped) # [(1, 'Alice', 'A'), (2, 'Bob', 'B'), (3, 'Cathy', 'A+')]unzipped = list(zip(*students))keys = ...
python f-string 一些用例
f-string 在 Python 中有多种特殊用法:
插入表达式、调用函数、格式化数字和日期:f-string 让我们可以直接在字符串中进行计算和格式化。
多行字符串、条件格式化:非常适用于格式化多行文本或者根据条件动态改变输出内容。
嵌套表达式、字典访问、调用方法:使得 f-string 可以处理更复杂的字符串插值。
调试时输出变量名和值:通过 = 语法,方便调试时查看变量的值。
下面整理一下部分f-string的用法与示例。
12345678910111213141516171819202122232425262728293031323334353637383940414243444546print(f'{big_number:,}') # 解释器会自动加上千分位标识符,输出 10,000,000,000,000,000print(f"The number is {x:05d}.") # 输出: The number is 00042.name: str = 'Jack' ...
python f-string
1. f-string 主要概念f-string 是 Python 3.6 中引入的一种新的字符串格式化方法,简化了字符串插值的过程,使得代码更简洁、更直观。f-string 的语法非常简单,就是在字符串前加上字母 f 或 F,然后用花括号 {} 包裹变量或表达式。
2. 基本用法f-string 让我们可以在字符串中直接插入 Python 表达式,Python 会自动将表达式的值转换为字符串并插入其中。
基本语法:1f"some text {expression} more text"
示例 1:将变量插入字符串:
1234name = "Alice"age = 30print(f"My name is {name} and I am {age} years old.")# 输出: My name is Alice and I am 30 years old.
示例 2:插入表达式(例如数学计算):
1234a = 5b = 10pr ...
一个完整的FastAPI + Celery + Redis集成案例
一个完整的FastAPI + Celery + Redis集成案例,包含配置、Docker部署和项目结构:
项目结构1234567891011121314151617fastapi-celery-redis/├── app/│ ├── __init__.py│ ├── config/│ │ ├── __init__.py│ │ └── settings.py│ ├── main.py│ ├── routers/│ │ ├── __init__.py│ │ └── tasks.py│ └── tasks/│ ├── __init__.py│ └── celery_app.py├── docker-compose.yml├── Dockerfile├── requirements.txt└── start.sh
1. 配置文件 (app/config/settings.py)1234567891011from pydantic import BaseSettingsclass Settings ...
在FastAPI中集成Celery实现分布式任务处理
一、核心架构解析1.1 Celery组件架构1234graph LR A[FastAPI] -->|AMQP| B[Message Broker] B --> C[Celery Worker] C --> D[Result Backend]
核心组件:
Producer:FastAPI应用提交任务
Broker:消息传输中间件(RabbitMQ/Redis)
Worker:任务执行节点集群
Result Backend:任务状态存储(Redis/PostgreSQL)
1.2 任务生命周期管理123456789101112131415# 典型任务执行流程@app.post("/tasks")async def create_task(data: TaskData): # 序列化参数 -> 生成任务ID -> 发送到Broker task = process_data.delay(data.dict()) return {"task_id": ...