route.ts
基本信息
- 类型: API 路由
- 路径:
./src/app/api/search/ai/route.ts - 路由:
/api/search/ai
概述
AI 语义搜索接口。使用 OpenAI 嵌入进行向量相似度搜索,返回与查询语义相关的 Prompt 列表。
HTTP 方法
- GET: 执行语义搜索
Query 参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| q | string | 是 | 搜索查询词 |
| limit | number | 否 | 返回结果数量,默认 20 |
响应
成功 (200)
{
"results": [
{
"id": "prompt_id",
"title": "Prompt 标题",
"slug": "prompt-title",
"content": "Prompt 内容...",
"description": "描述",
"author": { ... },
"similarity": 0.89
}
],
"query": "搜索关键词",
"count": 10
}
错误
AI 搜索未启用 (400)
{
"error": "AI Search is not enabled"
}
查询为空 (400)
{
"error": "Query is required"
}
搜索失败 (500)
{
"error": "Search failed"
}
依赖
next/server- Next.js 服务器组件@/lib/ai/embeddings- 嵌入生成和语义搜索
权限
- 公开访问
功能要求
- 需要配置
OPENAIAPIKEY环境变量 - 需要启用 AI 搜索功能(通过配置或环境变量)
工作原理
- 将查询文本转换为向量嵌入
- 使用 pgvector 在数据库中执行相似度搜索
- 返回相似度最高的 Prompt 列表