File tree 3 files changed +177
-186
lines changed
packages/fern-docs/bundle
src/app/[host]/[domain]/api/fern-docs/search/v2/reindex/turbopuffer
3 files changed +177
-186
lines changed Original file line number Diff line number Diff line change 93
93
"@vercel/kv" : " ^2.0.0" ,
94
94
"@vercel/otel" : " ^1.10.1" ,
95
95
"@workos-inc/node" : " ^7.31.0" ,
96
- "ai" : " ^4.2.10 " ,
96
+ "ai" : " ^4.3.13 " ,
97
97
"algoliasearch" : " ^5.20.3" ,
98
98
"bezier-easing" : " ^2.1.0" ,
99
99
"braintrust" : " ^0.0.184" ,
Original file line number Diff line number Diff line change 1
1
import { NextRequest , NextResponse } from "next/server" ;
2
2
3
3
import { createOpenAI } from "@ai-sdk/openai" ;
4
- import { embedMany } from "ai" ;
4
+ import { Embedding , embedMany } from "ai" ;
5
5
6
6
import { getAuthEdgeConfig , getEdgeFlags } from "@fern-docs/edge-config" ;
7
7
import { turbopufferUpsertTask } from "@fern-docs/search-server/turbopuffer" ;
@@ -79,11 +79,24 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
79
79
...edgeFlags ,
80
80
} ,
81
81
vectorizer : async ( chunks ) => {
82
- const embeddings = await embedMany ( {
83
- model : embeddingModel ,
84
- values : chunks ,
85
- } ) ;
86
- return embeddings . embeddings ;
82
+ // max 300k tokens per request, handle this manually
83
+ let payload = [ ] ;
84
+ let payloadLength = 0 ;
85
+ let embeddings : Embedding [ ] = [ ] ;
86
+ for ( const chunk of chunks ) {
87
+ payloadLength += chunk . length ;
88
+ payload . push ( chunk ) ;
89
+ if ( payloadLength >= 100000 ) {
90
+ const embeddingOutput = await embedMany ( {
91
+ model : embeddingModel ,
92
+ values : payload ,
93
+ } ) ;
94
+ embeddings = embeddings . concat ( embeddingOutput . embeddings ) ;
95
+ payload = [ ] ;
96
+ payloadLength = 0 ;
97
+ }
98
+ }
99
+ return embeddings ;
87
100
} ,
88
101
authed : ( node ) => {
89
102
if ( authEdgeConfig == null ) {
You can’t perform that action at this time.
0 commit comments