Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions apps/docs/app/api/search/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createLogger } from '@sim/logger'
import { truncateAtCodePoint } from '@sim/utils/string'
import { sql } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { db, docsEmbeddings } from '@/lib/db'
Expand All @@ -11,6 +12,8 @@ export const revalidate = 0

const DEFAULT_SEARCH_LIMIT = 10
const MAX_SEARCH_LIMIT = 20
/** Bounds the paid embedding call per request; real searches are a few words. */
const MAX_QUERY_LENGTH = 256

/** PostgreSQL text-search configuration for the docs' English content. */
const TS_CONFIG = 'english'
Expand All @@ -28,7 +31,11 @@ function getSearchLimit(value: unknown): number {
function getSearchParams(request: NextRequest) {
const searchParams = request.nextUrl.searchParams
return {
query: searchParams.get('query') || searchParams.get('q') || '',
query: truncateAtCodePoint(
searchParams.get('query') || searchParams.get('q') || '',
MAX_QUERY_LENGTH,
''
),
limit: getSearchLimit(searchParams.get('limit')),
}
}
Expand All @@ -38,7 +45,7 @@ export async function GET(request: NextRequest) {
try {
const { query, limit } = getSearchParams(request)

if (!query || query.trim().length === 0) {
if (query.trim().length === 0) {
return NextResponse.json([])
}

Expand Down
3 changes: 3 additions & 0 deletions apps/docs/lib/embeddings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const EMBEDDING_TIMEOUT_MS = 10_000

/**
* Generate embeddings for search queries using OpenAI API
*/
Expand All @@ -19,6 +21,7 @@ export async function generateSearchEmbedding(query: string): Promise<number[]>
model: 'text-embedding-3-small',
encoding_format: 'float',
}),
signal: AbortSignal.timeout(EMBEDDING_TIMEOUT_MS),
Comment thread
waleedlatif1 marked this conversation as resolved.
})

if (!response.ok) {
Expand Down
Loading