Introduces core backend and frontend infrastructure for a PDF translation interface. Adds API endpoints for translation, PDF testing, and AI provider testing; implements PDF text extraction, cost tracking, and pricing logic in the lib directory; adds reusable UI components; and provides comprehensive documentation (SDD, environment setup, Claude instructions). Updates Tailwind and global styles, and includes a sample test PDF and configuration files.
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import { type NextRequest, NextResponse } from "next/server"
|
|
import { createOpenAI } from "@ai-sdk/openai"
|
|
import { openai } from "@ai-sdk/openai"
|
|
import { generateText } from "ai"
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const { provider, apiKey } = await request.json()
|
|
|
|
if (!provider || !apiKey) {
|
|
return NextResponse.json({ error: "缺少必要參數" }, { status: 400 })
|
|
}
|
|
|
|
let model
|
|
let modelName: string
|
|
|
|
if (provider === "openai") {
|
|
// Test OpenAI
|
|
modelName = "gpt-4o-mini"
|
|
model = openai(modelName, { apiKey })
|
|
} else {
|
|
// Test DeepSeek
|
|
modelName = "deepseek-chat"
|
|
const deepseek = createOpenAI({
|
|
apiKey: apiKey,
|
|
baseURL: "https://api.deepseek.com",
|
|
})
|
|
model = deepseek(modelName)
|
|
}
|
|
|
|
// Test with a simple prompt
|
|
const { text } = await generateText({
|
|
model: model,
|
|
prompt: "Hello, this is a test. Please respond with 'API connection successful!'",
|
|
maxTokens: 50,
|
|
temperature: 0,
|
|
})
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: "API 連接成功!",
|
|
provider,
|
|
model: modelName,
|
|
testResponse: text
|
|
})
|
|
|
|
} catch (error: any) {
|
|
console.error("API Test Error:", error)
|
|
|
|
let errorMessage = "API 測試失敗"
|
|
|
|
if (error.message?.includes("API key")) {
|
|
errorMessage = "API 金鑰無效或已過期"
|
|
} else if (error.message?.includes("rate limit")) {
|
|
errorMessage = "API 請求次數超過限制"
|
|
} else if (error.message?.includes("quota")) {
|
|
errorMessage = "API 配額已用完"
|
|
} else if (error.message?.includes("Not Found")) {
|
|
errorMessage = "API 端點錯誤或模型不存在"
|
|
}
|
|
|
|
return NextResponse.json({
|
|
error: errorMessage,
|
|
details: error.message
|
|
}, { status: 400 })
|
|
}
|
|
} |