test_curl_examples.sh 4.57 KB
#!/bin/bash

###############################################################################
# CN-CLIP REST API 快速测试脚本
#
# 用途:
#   测试 REST API 的各种功能
#
# 使用方法:
#   ./examples/test_curl_examples.sh
#
###############################################################################

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

API_URL="http://localhost:51000"
TEST_IMAGE="https://oss.essa.cn/98532128-cf8e-456c-9e30-6f2a5ea0c19f.jpg"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}CN-CLIP REST API 测试${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# 测试 1: 健康检查
echo -e "${BLUE}测试 1: 健康检查${NC}"
echo "curl ${API_URL}/health"
echo ""

# 测试 2: 编码文本
echo -e "${BLUE}测试 2: 编码文本${NC}"
echo "curl -X POST ${API_URL}/encode/text \\"
echo "  -H 'Content-Type: application/json' \\"
echo "  -d '{\"texts\": [\"一只可爱的猫咪\"]}'"
echo ""

response=$(curl -s -X POST "${API_URL}/encode/text" \
    -H "Content-Type: application/json" \
    -d '{"texts": ["一只可爱的猫咪"]}')

echo "$response" | python3 -m json.tool 2>/dev/null | head -20 || echo "$response"

if echo "$response" | grep -q '"shape": \[1, 1024\]'; then
    echo -e "${GREEN}✓ 文本编码成功${NC}"
else
    echo -e "${RED}✗ 文本编码失败${NC}"
fi

echo ""
echo "按 Enter 继续..."
read

# 测试 3: 编码图像
echo -e "${BLUE}测试 3: 编码图像${NC}"
echo "curl -X POST ${API_URL}/encode/image \\"
echo "  -H 'Content-Type: application/json' \\"
echo "  -d '{\"images\": [\"${TEST_IMAGE}\"]}'"
echo ""

response=$(curl -s -X POST "${API_URL}/encode/image" \
    -H "Content-Type: application/json" \
    -d "{\"images\": [\"${TEST_IMAGE}\"]}")

echo "$response" | python3 -m json.tool 2>/dev/null | head -20 || echo "$response"

if echo "$response" | grep -q '"shape": \[1, 1024\]'; then
    echo -e "${GREEN}✓ 图像编码成功${NC}"
else
    echo -e "${RED}✗ 图像编码失败${NC}"
fi

echo ""
echo "按 Enter 继续..."
read

# 测试 4: 批量编码
echo -e "${BLUE}测试 4: 批量编码${NC}"
echo "curl -X POST ${API_URL}/encode/text \\"
echo "  -H 'Content-Type: application/json' \\"
echo "  -d '{\"texts\": [\"文本1\", \"文本2\", \"文本3\"]}'"
echo ""

response=$(curl -s -X POST "${API_URL}/encode/text" \
    -H "Content-Type: application/json" \
    -d '{"texts": ["文本1", "文本2", "文本3"]}')

echo "$response" | python3 -m json.tool 2>/dev/null | head -20 || echo "$response"

if echo "$response" | grep -q '"shape": \[3, 1024\]'; then
    echo -e "${GREEN}✓ 批量编码成功${NC}"
else
    echo -e "${RED}✗ 批量编码失败${NC}"
fi

echo ""
echo "按 Enter 继续..."
read

# 测试 5: 相似度计算
echo -e "${BLUE}测试 5: 相似度计算${NC}"
echo "curl -X POST ${API_URL}/similarity \\"
echo "  -H 'Content-Type: application/json' \\"
echo "  -d '{\"text\": \"可爱的猫咪\", \"texts\": [\"一只可爱的小猫\", \"美丽的高山\"]}'"
echo ""

response=$(curl -s -X POST "${API_URL}/similarity" \
    -H "Content-Type: application/json" \
    -d '{"text": "可爱的猫咪", "texts": ["一只可爱的小猫", "美丽的高山"]}')

echo "$response" | python3 -m json.tool 2>/dev/null || echo "$response"

if echo "$response" | grep -q '"text_similarities"'; then
    echo -e "${GREEN}✓ 相似度计算成功${NC}"
else
    echo -e "${RED}✗ 相似度计算失败${NC}"
fi

echo ""
echo "按 Enter 继续..."
read

# 测试 6: 混合编码
echo -e "${BLUE}测试 6: 混合编码(文本+图像)${NC}"
echo "curl -X POST ${API_URL}/encode/mixed \\"
echo "  -H 'Content-Type: application/json' \\"
echo "  -d '{\"data\": [\"一只可爱的猫咪\", \"${TEST_IMAGE}\"]}'"
echo ""

response=$(curl -s -X POST "${API_URL}/encode/mixed" \
    -H "Content-Type: application/json" \
    -d "{\"data\": [\"一只可爱的猫咪\", \"${TEST_IMAGE}\"]}")

echo "$response" | python3 -m json.tool 2>/dev/null | head -20 || echo "$response"

if echo "$response" | grep -q '"shape": \[2, 1024\]'; then
    echo -e "${GREEN}✓ 混合编码成功${NC}"
else
    echo -e "${RED}✗ 混合编码失败${NC}"
fi

# 总结
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}测试总结${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo -e "${GREEN}✓ 所有测试完成!${NC}"
echo ""
echo -e "下一步:"
echo "  1. 查看使用文档: cat docs/CNCLIP_CURL_GUIDE.md"
echo "  2. 在你的代码中调用 API"
echo "  3. 集成到你的应用"
echo ""