🆕 新增AI提取验证码功能
All checks were successful
BuildImage / build-image (push) Successful in 1m3s

This commit is contained in:
李寻欢 2025-03-25 09:37:35 +08:00
parent b18ddc8f5a
commit f2d5882573
6 changed files with 97 additions and 6 deletions

View File

@ -15,4 +15,9 @@ LOG_LEVEL=debug # 控制日志级别: debug, info, warning, error, critical
# 其他高级选项
# SSL_CERT_PATH=/path/to/cert.pem
# SSL_KEY_PATH=/path/to/key.pem
# SSL_KEY_PATH=/path/to/key.pem
# OpenAI相关配置
AI_BASE_URL=https://api.deepseek.com/v1
AI_KEY=sk-xxxx
AI_MODEL=depseek-chat

View File

@ -0,0 +1,53 @@
name: BuildImage
on:
push:
branches:
- main
jobs:
build-image:
runs-on: ubuntu-latest
container:
# 使用这个镜像不然Docker无法打包镜像
image: catthehacker/ubuntu:act-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Gen Tags
id: gen_tags
uses: docker/metadata-action@v5
with:
images: gitee.ltd/lxh/sms-server
flavor: |
latest=true
tags: |
type=raw,value={{date 'YYYYMMDD' tz='Asia/Shanghai'}}
- name: Print Tags
run: |
echo "${{ steps.gen_tags.outputs.tags }}"
echo "----------------- labels -----------------"
echo "${{ steps.meta.outputs.labels }}"
- name: Login to Repository
uses: docker/login-action@v2
with:
registry: gitee.ltd
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
- name: Build image
uses: docker/build-push-action@v6
with:
push: true
no-cache: true
tags: ${{ steps.gen_tags.outputs.tags }}
labels: ${{ steps.gen_tags.outputs.labels }}
- name: Clear images
run: |
docker images | grep "gitee.ltd/lxh/sms-server"
docker rmi -f $(docker images | grep "gitee.ltd/lxh/sms-server" | awk '{print $3}')

View File

@ -2,11 +2,8 @@ FROM python:3.9-slim
WORKDIR /app
# 安装 git 用于拉取最新代码
RUN apt-get update && apt-get install -y git && apt-get clean
# 拉取最新代码请确保仓库为public否则需要处理身份验证
RUN git clone https://github.com/nmhjklnm/sms_server.git .
# 复制代码
COPY . .
# 安装依赖
RUN pip install --no-cache-dir -r requirements.txt

View File

@ -9,6 +9,9 @@ class Settings(BaseSettings):
host: str = os.getenv("HOST", "0.0.0.0")
port: int = int(os.getenv("PORT", "8322"))
log_level: str = os.getenv("LOG_LEVEL", "info").lower()
ai_base_url: str = os.getenv("AI_BASE_URL", "")
ai_api_key: str = os.getenv("AI_KEY", "")
ai_model: str = os.getenv("AI_MODEL", "")
# 数据库配置
db_connect_args: Dict[str, Any] = {"check_same_thread": False}

View File

@ -1,7 +1,21 @@
from typing import Optional
import re
from openai import OpenAI
from backend.config.settings import settings
def extract_code_with_context(sms_content: str) -> Optional[str]:
# 判断AI相关的三个环境变量是否都有值
# 如果有值调用AI接口
# 如果没有值,调用正则表达式
if settings.ai_base_url != "" and settings.ai_api_key != "" and settings.ai_model != "":
return _extract_code_with_ai(sms_content)
else:
return _extract_code_with_reg(sms_content)
def _extract_code_with_reg(sms_content: str) -> Optional[str]:
pattern = r'(?:验证码|auth|code)[^0-9]{0,20}(\d{4,8})'
match = re.search(pattern, sms_content, re.IGNORECASE)
if match:
@ -9,6 +23,24 @@ def extract_code_with_context(sms_content: str) -> Optional[str]:
fallback = re.findall(r'(\d{4,8})', sms_content)
return fallback[0] if fallback else None
def _extract_code_with_ai(sms_content: str) -> Optional[str]:
client = OpenAI(api_key=settings.api_key, base_url=settings.ai_base_url)
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "你是一个验证码提取助手我会告诉你短信内容请将其中的验证码提取给我只需要返回验证码即可如果没有验证码请返回null不需要返回任何其他无关的内容。",
},
{
"role": "user",
"content": sms_content,
}
],
model=settings.ai_model,
)
code = response.choices[0].message.content
return code if code != "null" else None
def extract_phone_from_sim_slot(sim_slot: Optional[str]) -> Optional[str]:
if not sim_slot:
return None

View File

@ -7,3 +7,4 @@ python-multipart>=0.0.5
aiosqlite>=0.17.0
jinja2>=3.1.6
python-dotenv>=0.19.0
openai>=1.68.2