feat: add cloudflare workers ai config and global credentials

This commit is contained in:
Ben User
2026-02-01 11:22:52 +00:00
parent 70f160b396
commit b9e7cccaaf
35 changed files with 1806 additions and 10 deletions

View File

@@ -0,0 +1,38 @@
#!/bin/bash
# Workers AI 调用示例 - 需要 ACCOUNT_ID
API_TOKEN="trV3uWTLDCORnGQy8hXTI7r8wj2AT2nFBIomFNVf"
ACCOUNT_ID="你的_ACCOUNT_ID" # 需要从 dashboard 获取
echo "===== Workers AI 调用示例 ====="
echo ""
echo "[1] 文本生成调用:"
cat << 'EOF'
curl -X POST \
"https://api.cloudflare.com/client/v4/accounts/YOUR_ID/ai/run/@cf/meta/llama-3.1-8b-instruct" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "你好,介绍一下自己"}
]
}'
EOF
echo ""
echo ""
echo "[2] 可用的 AI 模型列表:"
echo " - @cf/meta/llama-3.1-8b-instruct (文本聊天)"
echo " - @cf/meta/llama-2-7b-chat-int8 (轻量聊天)"
echo " - @cf/stabilityai/stable-diffusion-xl-base-1.0 (图像生成)"
echo " - @cf/openai/whisper (语音识别)"
echo " - @cf/baai/bge-base-en-v1.5 (文本嵌入/向量)"
echo ""
echo "[3] 各种用途:"
echo " ✓ 聊天机器人 ✓ 代码生成 ✓ 内容总结"
echo " ✓ 智能搜索 ✓ 文生图 ✓ 翻译"
echo ""
echo "===== 从 Cloudflare Dashboard 获取 Account ID ====="
echo "1. 登录 https://dash.cloudflare.com"
echo "2. 右侧边栏找到 'Account ID'"
echo "3. 复制填入上面脚本"

33
cloudflare/main.tf Normal file
View File

@@ -0,0 +1,33 @@
# Cloudflare Workers AI Configuration
# API Token: trV3uWTLDCORnGQy8hXTI7r8wj2AT2nFBIomFNVf
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 3.32"
}
}
}
# Configure Cloudflare Provider with API Token
provider "cloudflare" {
api_token = "trV3uWTLDCORnGQy8hXTI7r8wj2AT2nFBIomFNVf"
}
# Get current account information
data "cloudflare_accounts" "this" {}
# Example: Cloudflare Workers AI Model Usage
# Note: Workers AI can be invoked via Cloudflare's REST API using this token
# Output account information
output "account_id" {
description = "Cloudflare Account ID"
value = length(data.cloudflare_accounts.this.accounts) > 0 ? data.cloudflare_accounts.this.accounts[0].id : null
}
output "account_name" {
description = "Cloudflare Account Name"
value = length(data.cloudflare_accounts.this.accounts) > 0 ? data.cloudflare_accounts.this.accounts[0].name : null
}

53
cloudflare/test_workers_ai.sh Executable file
View File

@@ -0,0 +1,53 @@
#!/bin/bash
# Cloudflare Workers AI 测试脚本
# 使用 REST API 直接调用
API_TOKEN="trV3uWTLDCORnGQy8hXTI7r8wj2AT2nFBIomFNVf"
ACCOUNT_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" | jq -r '.result[0].id')
echo "===== Cloudflare Workers AI 测试 ====="
echo "Account ID: $ACCOUNT_ID"
echo ""
# 测试1: 文本生成 (Llama 模型)
echo "[测试1] AI 文本生成 - 问:什么是 Cloudflare Workers AI"
curl -s -X POST \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/ai/run/@cf/meta/llama-3.1-8b-instruct" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "system", "content": "你是一个简洁的AI助手"},
{"role": "user", "content": "用一句话介绍 Cloudflare Workers AI 是什么"}
]
}' | jq -r '.result.response'
echo ""
echo "[测试2] AI 代码辅助 - 写个 Python 函数"
curl -s -X POST \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/ai/run/@cf/meta/llama-3.1-8b-instruct" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "写一个Python函数判断字符串是否为回文"}
]
}' | jq -r '.result.response'
echo ""
echo "[测试3] 文本翻译 - 中译英"
curl -s -X POST \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/ai/run/@cf/meta/llama-3.1-8b-instruct" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "把这句话翻译成英文:云计算让技术变得更简单"}
]
}' | jq -r '.result.response'
echo ""
echo "===== 测试完成 ====="