FastChat 部署多模型


* [Chatbot Arena](https://chat.lmsys.org/) * [FastChat](https://github.com/lm-sys/FastChat) * [LMSYS BLOG](https://lmsys.org/blog/) * [Use AutoGen for Local LLMs](https://microsoft.github.io/autogen/blog/2023/07/14/Local-LLMs/)

安装

pip

pip install "fschat[model_worker,webui]"

源代码

这种方式安装比较容易调试,适合开发者。

克隆代码

git clone https://github.com/lm-sys/FastChat.git
cd FastChat

创建环境

python -m venv env
source env/bin/activate

安装

LangChain HuggingFaceEmbeddings + FAISS

数据

weather_texts = [
    "😀 今天天气舒适,心情大好。",
    "😀 今天天气晴朗,阳光明媚。",
    "😀 今天天气宜人,适合出门游玩。",
    "😀 今天天气没有下雨,真是太好了。",
    "😀 今天天气比昨天好多了,真是令人欣喜。",
    "😀 今天天气晴空万里,蓝天白云,真是美不胜收。",
    "😀 今天天气温暖如春,空气清新,让人心旷神怡。",
    "😀 今天天气风和日丽,微风徐徐,让人心情舒畅。",
    "😀 今天天气万里无云,阳光灿烂,让人精神振奋。",
    "😀 今天天气秋高气爽,天朗气清,让人心胸开阔。",
    "🥶 今天天气很糟糕。",
    "🥶 今天天气阴沉沉的,让人心情烦躁。",
    "🥶 今天天气下雨了,真是让人沮丧。",
    "🥶 今天天气太热了,出门都觉得热得受不了。",
    "🥶 今天天气太冷了,出门都要穿上厚衣服。",
    "🥶 今天天气乌云密布,风雨欲来,真是让人提心吊胆。",
    "🥶 今天天气寒风刺骨,道路结冰,真是让人寸步难行。",
    "🥶 今天天气闷热潮湿,空气污浊,真是让人喘不过气来。",
    "🥶 今天天气灰蒙蒙的,看不到蓝天白云,真是让人心情沉重。",
    "🥶 今天天气狂风暴雨,树木倒伏,道路封闭,真是让人措手不及。"
]

Private GPT 中文 Embeddings 模型测试

文档

这里使用的文档是:合作方人员出勤及结算管理信息化支撑规则

一、出勤打卡
出勤打卡包括:正常出勤打卡、出差打卡、外出打卡、加班打卡。

1. 正常出勤打卡:指正常的出勤办公打卡。
(1)全天出勤打卡:上班打卡:8点30分之前打卡。下班打卡:17点30分之后打卡。
(2)半天出勤打卡。上午打卡时间段:8点30分之前、12点之后。下午时间段:13点之前,17点30分之后。
(3)打卡(考勤机或企业微信打卡)形式按部门要求为准,最小半天为统计单位。

2. 出差打卡:指出差地出勤办公或在途期间打卡。
(1)固定出差地打卡:打卡时间参照第1条正常出勤上下班打卡;无法定位有效范围的找部门管理员修改工作打卡位置。(具体按照各部门要求执行)
(2)出差在途打卡(使用手机外出打卡)。到车站坐车前打外出打卡一次,到达目的地后打外出打卡一次(往返同理)。下午出差的,上午需打正常出勤卡(上午正常出勤须闭环打卡);上午到达出差地的,下午需打一次外出打卡或上下班打卡。

3. 外出打卡:指外出办事打卡。提外出申请后,可以打外出卡,打外出卡时间需在申请时间内:
(1)半天外出:如外出时间在上午(12点前) 或者下午(12点后),则另外半天需正常出勤打卡。
(2)跨12点外出:如外出跨度期间包含12点,则12点前、12点后分别打外出卡即可记为合格出勤。
// ...

提示词模板 使用以下上下文来回答最后的问题。

将 API 密钥身份验证添加到 FastAPI 应用程序

API_KEY

方法一

from fastapi import Security, HTTPException, status
from fastapi.security.api_key import APIKeyHeader


API_KEY="123456"
API_KEY_NAME = "X-API-KEY"
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=True)

async def get_api_key(api_key: str = Security(api_key_header)):
    if api_key != API_KEY:
        raise HTTPException(
             status_code=status.HTTP_401_UNAUTHORIZED,
             detail="Invalid API Key"
        )


@app.get('/index', dependencies=[Security(get_api_key)])
async def index():
    return {"message": "Hello World"}

APIKeyHeader 的源码

Dockerfile 中 ARG 指令的作用范围

这里主要是了解 Dockerfile 中 ARG 指令的作用范围。

总结

  1. FROM 前定义的参数,只能在 FROM 指令中使用,且能够在多阶段中起作用。
ARG BASE_IMAGE=python:3.10.9
FROM ${BASE_IMAGE} AS builder
...

FROM ${BASE_IMAGE}
...
  1. FROM 后定义的参数,只能作用在本 Stage 阶段。
FROM python:3.10.9 AS builder
ARG APP_HOME=/WALL-E-AI
WORKDIR ${APP_HOME}

FROM python:3.10.9
ARG APP_HOME=/WALL-E-AI
WORKDIR ${APP_HOME}

实验

开源 OCR 引擎基准测试

OCR 引擎

EasyOCR

EasyOCR 支持 80+ 语言。

Abaza = 'abq'
Adyghe = 'ady'
Afrikaans = 'af'
Angika = 'ang'
Arabic = 'ar'
Assamese = 'as'
Avar = 'ava'
Azerbaijani = 'az'
Belarusian = 'be'
Bulgarian = 'bg'
Bihari = 'bh'
Bhojpuri = 'bho'
Bengali = 'bn'
Bosnian = 'bs'
Simplified_Chinese = 'ch_sim'
// ...

安装

pip install torch==2.0.1 torchvision==0.15.2 -i https://download.pytorch.org/whl/cpu
pip install easyocr

代码示例 import easyocr languages = ['ch_sim', 'en'] model = easyocr.

FastAPI 开发 RESTAPI 实践

首页

重定向到 Swagger UI

@app.get("/", include_in_schema=False)
async def index():
    return RedirectResponse('/docs', status_code=303)

使用 route 的 docstring 作为首页内容

@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def index():
    # 需要过滤的路由
    filted_routes = [
        "/openapi.json",
        "/docs",
        "/docs/oauth2-redirect",
        "/redoc",
        "/static",
        "/"
    ]
    
    routes = []
    for route in app.routes:
        if route.path not in filted_routes:
// ...

Markdown to HTML

安装

pip install markdown

使用