Skip to content

Commit 85b22e9

Browse files
authored
fix: [AI chat] add auto-batching for vectorizing to avoid max token limits (#2693)
1 parent e311a75 commit 85b22e9

File tree

3 files changed

+177
-186
lines changed

3 files changed

+177
-186
lines changed

packages/fern-docs/bundle/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"@vercel/kv": "^2.0.0",
9494
"@vercel/otel": "^1.10.1",
9595
"@workos-inc/node": "^7.31.0",
96-
"ai": "^4.2.10",
96+
"ai": "^4.3.13",
9797
"algoliasearch": "^5.20.3",
9898
"bezier-easing": "^2.1.0",
9999
"braintrust": "^0.0.184",

packages/fern-docs/bundle/src/app/[host]/[domain]/api/fern-docs/search/v2/reindex/turbopuffer/route.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextRequest, NextResponse } from "next/server";
22

33
import { createOpenAI } from "@ai-sdk/openai";
4-
import { embedMany } from "ai";
4+
import { Embedding, embedMany } from "ai";
55

66
import { getAuthEdgeConfig, getEdgeFlags } from "@fern-docs/edge-config";
77
import { turbopufferUpsertTask } from "@fern-docs/search-server/turbopuffer";
@@ -79,11 +79,24 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
7979
...edgeFlags,
8080
},
8181
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;
87100
},
88101
authed: (node) => {
89102
if (authEdgeConfig == null) {

0 commit comments

Comments
 (0)